^ 关注我,带你一起学GIS ^
上一篇讲了GIS 开发库 GeoTools 介绍,还没有看过的同学可以先去看看。
本节讲解如何搭建 GeoTools 开发环境,欢迎食用。
阅读说明
由于阅读本文需要一些前置知识,如Java语言、GIS概念、后端开发等,所以特此说明。
1. 开发准备
在进行GeoTools开发前,需要先完成一些准备工作。俗话说,工欲善其事,必先利其器,只有将前期工作做好了,才能更好的推进往后的开发工作。
以下是环境搭建的一些软件工具,包括JDK
、Maven
以继IDE
JDK
:当前例子中使用的JDK
版本为11,需要先行下载,并设置好环境变量。Maven
:当前例子中使用的Maven
版本是3.6.3
,需要先行下载,并配置好仓库地址。IDE
:当前例子中使用的IDE
是IDEA 2020.3
,需要先行下载,并进行项目配置。
JDK
下载地址:https://www.oracle.com/java/technologies/downloads/?er=221886#java11
Maven
下载地址:https://maven.apache.org/download.cgi
IDEA
下载地址:https://www.jetbrains.com.cn/idea
2. 软件配置
软件安装完成之后,需要对IDE进行一些配置,如Maven
配置、JDK
配置、自动导入配置等。
Maven
配置:打开设置选型下的构建工具,选择Maven
,将Maven安装路径、配置文件以继仓库地址修改为本地目标路径。

JDK
配置:打开项目结构设置,将Project
、Modules
以及SDKS
选项下的JDK
版本修改为11。

自动导入配置:在进行项目开发前,有必要开启依赖自动导入。打开设置页面,选择编辑器下的通用选项,点击
Auto Import
选项,勾选以下复选框。Add unambiguous imports on the fly
(自动添加明确的导入)Optimize imports on the fly
(自动优化导入,删除未使用的导入)

3. 项目搭建
新建项目:File->New->Project
。
在New Project
选项中点击Maven
,修改项目JDK
版本为11,然后勾选Create from archetype
复选框,再将滚动条往下滑,找到o_rg.apache.maven.archetypes:maven-archetype-quickstart archetype_
项并将其选中。__点击_Next_
,进行下一步操作。
Name
选项为项目名称,如例子中使用的是geotools-demo
,Location
选项选择项目存储位置,展开Artifact Coordinates
选项,GroupId
初始值为org.example
,可根据需求修改,如例子中修改为org.geotools
。修改完成,点击Next
,_进行下一步操作。_
这里的Maven
安装路径、配置文件路径以及仓库路径因为在之前开发准备一节设置选项中已经配制完成,所以这里可以不用修改,直接点击Finish
即可。
运行项目:经过上面的步骤之后,运行App
中的main
函数,如果不出意外的话将会在控制输出一段文字"Hello World"
。
4. 依赖配置
在将项目成功运行起来之后,就可以进行依赖配置了。打开根目录下的pom.xml
文件,在该文件中对项目依赖包数据进行管理。
举个例子说明一下,Maven
工具作为Java
项目包管理器,就像npm
作为前端项目中的包管理器一样,它们的角色是一样的;而pom.xml
文件作为依赖管理文件和前端项目中地package.json
的作用也是相似的。

首先在<properties>
选项中将编译器版本修改为JDK
版本11,然后添加下方红色的两行,标明当前使用的GeoTools
版本为34-SNAPSHOT
。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<geotools.version>34-SNAPSHOT</geotools.version>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
然后在<dependencies>
选项中添加gt-shapefile
以及gt-swing
依赖项。
<dependencies>
<!-- GeoTools依赖 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
添加GeoTools
仓库地址。
注:GeoTools依赖模块可在以下仓库中进行查找,只是【Maven中央仓库】的最新版本稍微延迟一点。
Maven中央仓库地址:https://mvnrepository.com/search?q=GeoTools&repo=geotools-releases
osgeo仓库地址:https://repo.osgeo.org/repository/snapshot
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
GeoTools需要Java 11,你需要告诉Maven使用11源代码级别。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.11.1</version>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.3</version>
<configuration>
<source>11</source>
<!-- Specifies whether or not the version text is included.-->
<version>false</version>
<!-- Omit qualifying package name before class names in output.-->
<noqualifier>all</noqualifier>
<!-- Shuts off non-error and non-warning messages.-->
<quiet>true</quiet>
<!-- The maximum Java heap size to be used to run javadoc. -->
<maxmemory>${javadoc.maxHeapSize}</maxmemory>
<!-- Specifies the encoding name of the source files.-->
<encoding>UTF-8</encoding>
<!-- Set an additional parameter for the command line. -->
<additionalOptions>-keywords</additionalOptions>
<breakiterator>true</breakiterator>
<excludePackageNames>org.geotools.resources:org.geotools.maven:com:net.opengis:org.w3:javax:it.geosolutions</excludePackageNames>
<!-- Enables the Javadoc tool to interpret a simple, one-argument -->
<!-- custom block tag tagname in doc comments. Note: <placement/> -->
<!-- should be a combination of the letters Xaoptcmf. -->
<tags>
<tag>
<name>todo</name>
<placement>a</placement>
<head>TODO:</head>
</tag>
<tag>
<name>todo:</name>
<placement>a</placement>
<head>TODO:</head>
</tag>
<tag>
<name>TODO</name>
<placement>a</placement>
<head>TODO:</head>
</tag>
<tag>
<name>task</name>
<placement>tfmc</placement>
<head>TODO:</head>
</tag>
<tag>
<name>revisit</name>
<placement>tfmc</placement>
<head>TODO:</head>
</tag>
<tag>
<name>generated</name>
<placement>Xt</placement>
<head>Generated</head>
</tag>
<tag>
<name>UML</name>
<placement>a</placement>
<head>UML:</head>
</tag>
<tag>
<name>uml.property</name>
<placement>a</placement>
<head>UML property:</head>
</tag>
<tag>
<name>generated</name>
<placement>X</placement>
</tag>
</tags>
<!-- Creates links to existing javadoc-generated -->
<!-- documentation of external referenced classes. -->
<links>
<link>https://docs.oracle.com/javase/11/docs/api/</link>
<link>https://javaee.github.io/javaee-spec/javadocs/</link>
<link>https://locationtech.github.io/jts/javadoc/</link>
</links>
</configuration>
<!-- only works with package javadoc:aggregate for use of META-INF/MANIFEST.MF Automatic-Module-Name -->
<reportSets>
<reportSet>
<id>aggregate</id>
<reports>
<report>aggregate</report>
</reports>
<inherited>false</inherited>
</reportSet>
<reportSet>
<id>default</id>
<reports>
<report>javadoc</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
这是官方推荐的pom.xml
文件完整配置例子:https://docs.geotools.org/latest/userguide/_downloads/ae1db6d0f2cea56845a2d6816b10bb9a/pom.xml
下载依赖:如果依赖没有只用下载的话需要进行手动下载。
方式一:点击右侧Maven选项,然后点击下图刷新按钮。

方式二:与上图步骤一致,右键点击 Dependencies
,选择Download Sources
选项。

方式三:在根目录下的 pom.xml
文件上右键,选择Maven
选项,点击Download Sources
选项。

5. 运行项目
在org.geotools
新建一个包tutorial.quickstart
,然后添加一个类QuickStart
。添加项目模块:在org.geotools
右键->New
->Java Class
,输入模块名称。
在QuickStart
类中写入以下代码,用于实现选择本地shp
文件,并在地图上显示。
package org.geotools.tutorial.quickstart;
import org.geotools.api.data.FileDataStore;
import org.geotools.api.data.FileDataStoreFinder;
import org.geotools.api.data.SimpleFeatureSource;
import org.geotools.api.style.Style;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
import java.io.File;
import java.util.logging.Logger;
public class QuickStart {
private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger(QuickStart.class);
/**
* GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
* contents on the screen in a map frame
* @param args Ignored no args required.
* @throws Exception If there are errors finding the shapefile.
*/
public static void main(String[] args) throws Exception{
// display a data store file chooser dialog for shapefiles
LOGGER.info("QuickStart");
LOGGER.config("Welcome Developers");
LOGGER.info("java.util.logging.config.file="+System.getProperty("java.util.logging.config.file"));
// 选择shp文件对话框
File file = JFileDataStoreChooser.showOpenFile("shp",null);
if(file == null){
return;
}
LOGGER.config("File Selected:"+file);
// 文件存储器
FileDataStore dataStore = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = dataStore.getFeatureSource();
// 创建地图
MapContent map = new MapContent();
map.setTitle("QuickStart");
// 地图样式
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource,style);
map.addLayer(layer);
// 显示地图
JMapFrame.showMap(map);
}
}
显示效果如下。
在运行项目时报错:不支持发行版,15或者17多刷新一下Maven
,先清除一下缓存。
点击clean
按钮,清除之前的编译结果并重新编译。
❝
OpenLayers示例数据下载,请在公众号后台回复:ol数据
全国信息化工程师-GIS 应用水平考试资料,请在公众号后台回复:GIS考试
❝
GIS之路公众号已经接入了智能助手,欢迎大家前来提问。
欢迎访问我的博客网站-长谈GIS:
http://shanhaitalk.com
都看到这了,不要忘记点赞、收藏+关注 哦!
本号不定时更新有关 GIS开发 相关内容,欢迎关注