目录
GeoTools 简介
官网:https://geotools.org/
git:https://github.com/geotools/geotools
GeoTools 是一个开源的Java库,为地理空间数据提供工具。
安装
以 IntelliJ IDEA 为例,
Step 1:新建项目
Step 2:修改pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.geotools</groupId>
<artifactId>tutorial</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tutorial</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>26-SNAPSHOT</geotools.version>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<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>
<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>
</project>
Step 3:自动导入Maven依赖
Step 4:测试App.java
Hello world
Step 1:在 App.java 同级目录下新建 Quickstart.java 并运行~
package org.geotools;
// 导入相应的包
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.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
* 提示用户提供一个shp文件,并在屏幕上的地图窗口中显示其内容
*/
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"));
// 打开文件选择器的弹窗
File file = JFileDataStoreChooser.showOpenFile("shp", null); // JFileChooser
if (file == null) {
return;
}
LOGGER.config("File selected " + file);
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);
}
}