geotool案例1,快速显示shapefile


geotool案例1,快速显示shapefile

1、初始化maven项目,pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.0modelVersion>

    <groupId>cn.selfreegroupId>
    <artifactId>testartifactId>
    <version>1.0version>

    <name>demoname>
    <description>Demo project for spring bootdescription>

    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <geotools.version>22-SNAPSHOTgeotools.version>
    properties>
    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.3.5.RELEASEversion>
        <relativePath/>
    parent>

    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.geotoolsgroupId>
            <artifactId>gt-shapefileartifactId>
            <version>${geotools.version}version>
        dependency>
        <dependency>
            <groupId>org.geotoolsgroupId>
            <artifactId>gt-swingartifactId>
            <version>${geotools.version}version>
        dependency>
    dependencies>

    <repositories>
        <repository>
            <id>maven2-repository.dev.java.netid>
            <name>Java.net repositoryname>
            <url>http://download.java.net/maven/2url>
        repository>
        <repository>
            <id>osgeoid>
            <name>Open Source Geospatial Foundation Repositoryname>
            <url>http://download.osgeo.org/webdav/geotools/url>
        repository>
        <repository>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
            <id>boundlessid>
            <name>Boundless Maven Repositoryname>
            <url>http://repo.boundlessgeo.com/mainurl>
        repository>
    repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

2、源码,直接放在main函数中运行

// 调用文件对话框,获取shp文件
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
    return;
}

// 存储文件变量为简单要素源变量
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = store.getFeatureSource();

// 创建地图变量,设置标题、样式、图层
MapContent map = new MapContent();
map.setTitle("Quickstart");

Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);![在这里插入图片描述](https://img-blog.csdnimg.cn/20200119172116293.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21jMGNzZG4wMjAxNQ==,size_16,color_FFFFFF,t_70)
map.addLayer(layer);

// 窗体显示地图
JMapFrame.showMap(map);

在这里插入图片描述

源文档( https://docs.geotools.org/stable/userguide/tutorial/quickstart/maven.html )