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

场景

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的依赖。

        <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>

添加位置

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

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

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年12月5日
下一篇 2023年12月5日

相关推荐