场景
GeoTools
GeoTools 是一个开源的 Java GIS 工具包,可利用它来开发符合标准的地理信息系统。
GeoTools 提供了 OGC (Open Geospatial Consortium) 规范的一个实现来作为他们的开发。
官网地址:
参考其quick start教程,实现集成到maven项目中并运行示例代码。
https://docs.geotools.org/latest/userguide/tutorial/quickstart/index.html
点击Maven Quickstart
https://docs.geotools.org/latest/userguide/tutorial/quickstart/maven.html
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
实现
1、新建Maven项目并添加Geotools的依赖。
<dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>24-SNAPSHOT</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-swing</artifactId> <version>24-SNAPSHOT</version> </dependency>
注意这里的版本是24,目前官网最新的示例是30,但是30是需要java11的环境,这里的本地环境是java8
所以选择适配Java8的geotools的版本,这里选择24版本。
另外需要注意的是geotools在中央仓库中没有坐标,所以需要添加repository
<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>
添加位置
包括去寻找版本时可以去其仓库地址去查找
https://repo.osgeo.org/#browse/browse:snapshot
2、添加依赖并导入成功之后,参考官方示例代码,新建类
新建类
import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.map.FeatureLayer; import org.geotools.map.Layer; import org.geotools.map.MapContent; import org.geotools.styling.SLD; import org.geotools.styling.Style; import org.geotools.swing.JMapFrame; import org.geotools.swing.data.JFileDataStoreChooser; import java.io.File; import java.io.IOException; public class HelloGeotools { public static void main(String[] args) throws IOException { File file = JFileDataStoreChooser.showOpenFile("shp", null); if (file == null) { return; } FileDataStore store = FileDataStoreFinder.getDataStore(file); SimpleFeatureSource featureSource = store.getFeatureSource(); // Create a map content and add our shapefile to it MapContent map = new MapContent(); map.setTitle("Quickstart"); Style style = SLD.createSimpleStyle(featureSource.getSchema()); Layer layer = new FeatureLayer(featureSource, style); map.addLayer(layer); // Now display the map JMapFrame.showMap(map); } }
运行main方法
选择要预览的shp文件后