About GeoTools
GeoTools is an open source (LGPL) Java code library which provides standards compliant methods for the manipulation of geospatial data, for example to implement Geographic Information Systems (GIS). The GeoTools library implements Open Geospatial Consortium (OGC) specifications as they are developed.
走进 GeoTools
GeoTools
的架构图如下,我们了解
GeoTools
的架构以及各个jar包所构成的库栈,能够帮助我们更加清晰的了解和学习
GeoTools
的各个模块,并且还能够帮助我们在项目里面选择适合我们的库。
../_images/geotools.png
这张架构图遵循了软件工程领域里面架构图设计标准,由下而上,第一层是基础设施,上层建筑依赖于基础设施。
比如:你如果想使用
Referencing
模块里的
gt-opengis
,
gt-referencing
,
gt-metadata
内容,那么当你使用
data
模块的时候,
gt-main
,
jts
,
gt-opengis
,
gt-referencing
,
gt-opengis
,
gt-metadata
,上面
Referencing
的内容也是会引用到的。
GeoTools
常用模块的功能列表:
ModulePurpose
gt-render
Implements of Java2D rendering engine to draw a map
gt-jdbc
Implements for accessing spatial database
gt-main
Implements for accessing spatial data
gt-xml
Implements of common spatial XML formats
gt-cql
Implements of Common Query Language for filters
gt-main
Interfaces for working with spatial information. Implements filter, feature, etc…
Definition and implementation of Geometry
gt-coverage
Implementation for accessing raster information
gt-referencing
Implementation of co-ordinate location and transformation
gt-metadata
Implementation of identification and description
gt-opengis
Definition of interfaces for common spatial concepts
GeoTools
提供插件来支持额外的数据格式、不同的坐标参考系统权限等等。
除此之外,
GeoTools
团队在
GeoTools
的基础上实现了一些扩展,当然了,这些扩展是为了提供一些额外的功能。这些扩展是相互独立的,我们可以直接在项目中使用。
../_images/extension.png
JARExtension
gt-graph
Work with graph and network traversals
gt-validation
Quality assurance for spatial data
gt-wms
Web Map Server client
gt-xsd
Parsing/Encoding for common OGC schemas
gt-brewer
Generation of styles using color brewer
GeoTools
团队
为了支持
GeoTools
中的XML模块,将几个XML模式打包成JAR形式,方便开发者进行调用。
JARSchema
net.opengis.ows
open web services schema
net.opengis.wfs
web feature service
net.opengis.wps
web processing service schema
net.opengis.wcs
web coverage service schema
net.opengis.wfs
web feature service schema
org.w3.xlink
XLink schema
XSD解析器通过一系列XSD插件使用这些工具。这些插件指示如何使用Eclipse XSD库解析和编码额外的内容来解析XML模式文档,并提供“绑定”,显示如何解析和编码Java类,如String、Date、URL和Geometry。
JARBindings
gt-xsd-core
Basic types defined by XML schema
gt-xsd-fes
filter 2.0
gt-xsd-filter
filter (used by OGC CAT and WFS)
gt-xsd-kml
keyhole markup language
gt-xsd-wfs
web feature service
gt-xsd-wps
web processing service
gt-xsd-gml3
geographic markup language 3
gt-xsd-gml2
geographic markup language 2
gt-xsd-ows
open web services
gt-xsd-wcs
web coverage service
gt-xsd-wms
web map service
gt-xsd-sld
style layer descriptor
以下是
GeoTools
不支持的扩展,你也可以使用Maven下载它们来使用。
UnsupportedPurpose
gt-swt
Standard widget toolkit interactive map
gt-swing
Swing interactive map
gt-oracle
retired oracle support
gt-postgis
retired PostGIS support
gt-db2
retired db2 support
gt-wps
Web Processing Service client
gt-process
Job system for spatial data
在POM文件中,首先添加以下内容:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>24-SNAPSHOT</geotools.version>
</properties>
然后在POM文件的
<repositories>
标签中,添加以下依赖,该依赖是
GeoTools
官方的依赖远程仓库位置:
<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>
在POM文件中添加以下依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</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>
在你的Spring Boot工程中新建
QuickStrat
类,并添加以下代码:
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;
在该网站
http://www.naturalearthdata.com/downloads/50m-cultural-vectors/
,根据自己的喜好来下载一个矢量数据文件。
在下图的窗口中勾选你刚才下载、并解压文件后的位置。
http://docs.geotools.org/stable/userguide/_downloads/d4bcf8751cc3f33a9fb673902a960e53/locations.csv
在你的IDE中键入以下代码:
package com.geovis.bin.utils.gis.geotools;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
运行该代码,会弹出对话框,需要你选择下载并解压后的
location.csv
文件
从shp文件读取要素集
运行结果如下: