小工具知识网

当前位置: 首页 > 行业知识 > Java+GeoTools(开源的Java GIS工具包)快速入门-实现读取shp文件并显示

行业知识

Java+GeoTools(开源的Java GIS工具包)快速入门-实现读取shp文件并显示

2026-12-06 13:08:00 来源:互联网转载

场景

GeoTools

GeoTools 是一个开源的 Java GIS 工具包,可利用它来开发符合标准的地理信息系统。

GeoTools 提供了 OGC (Open Geospatial Consortium) 规范的一个实现来作为他们的开发。

官网地址:

GeoTools The Open Source Java GIS Toolkit — GeoTools

参考其quick start教程,实现集成到maven项目中并运行示例代码。

Quickstart — GeoTools 30-SNAPSHOT User Guide

点击Maven Quickstart

Maven Quickstart — GeoTools 30-SNAPSHOT User Guide

注:

博客:
霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主

实现

1、新建Maven项目并添加Geotools的依赖。

                    org.geotools            gt-shapefile            24-SNAPSHOT                            org.geotools            gt-swing            24-SNAPSHOT        

注意这里的版本是24,目前官网最新的示例是30,但是30是需要java11的环境,这里的本地环境是java8

所以选择适配Java8的geotools的版本,这里选择24版本。

另外需要注意的是geotools在中央仓库中没有坐标,所以需要添加repository

                        osgeo            OSGeo Release Repository            https://repo.osgeo.org/repository/release/            false            true                            osgeo-snapshot            OSGeo Snapshot Repository            https://repo.osgeo.org/repository/snapshot/            true            false            

添加位置

包括去寻找版本时可以去其仓库地址去查找

Nexus Repository Manager

2、添加依赖并导入成功之后,参考官方示例代码,新建类

https://docs.geotools.org/latest/userguide/_downloads/f3a52bd26dc6252b6c7f5e7f9d4a6469/Quickstart.java

新建类

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文件后

上一篇:tcp服务器怎么接收数据?

下一篇:手机的服务器怎么更换地址?