Introduction to Geotools and quickstsrt to load the shp file and display it

Scenes

Geotools

Geotools is a java class library, which provides a lot of standard classes and methods to process spatial data. At the same time, this class library is built on the OGC standard and is an implementation of OGC ideas.
OGC is an international standard, so geotools will definitely become the main tool for open source spatial data processing in the future. Most of the current open source software, such as udig, geoserver, etc.,

The processing of spatial data is supported by geotools. Many other web services, command line tools and desktop programs can all be implemented by geotools.

Geotools official website:

https://geotools.org/

The official website has a quick start guide.

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
Follow the public
account Domineering
programmers Get programming-related e-books, tutorial pushes and free downloads.

achieve

Click Quickstart, then select Intellij Quickstart

 

Then you can start quickly by following its official guide.

The way to create a new project here does not use Maven's quick start in the official document. Instead, choose SpingInitializr to quickly create a new project when you choose to create a new project

 

Then add the most basic web dependencies

 

Then open the pom.xml after the newly created project

First add the version attribute of geotools

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>26-SNAPSHOT</geotools.version>
    </properties>

The version used here is 26

Then add geotools related dependencies in denpendencies

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

After adding the dependencies, Maven will prompt that the dependencies cannot be downloaded at this time, and you need to add the repository so that Maven can download the 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>

Complete pom file code

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.badao</groupId>
    <artifactId>geotoolsdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>geotoolsdemo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>26-SNAPSHOT</geotools.version>
    </properties>
    <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>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <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>
    </dependencies>

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

</project>

Then wait for Maven to download related dependencies

After the download is complete, you can see it in the project dependencies

 

Then create a new java class called quickstart under the package

 

The sample code for this class is

package com.badao.geotoolsdemo;
import java.io.File;
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;
public class Quickstart {
    public static void main(String[] args) throws Exception {
        // display a data store file chooser dialog for shapefiles
        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);
    }
}

Then run the main method of this class

 

A selection box for selecting shp files will pop up

Shapefile is a vector graphics format, it can save the location of geometric figures and related attributes. But this format cannot store the topological information of geographic data.

Among them, to form a Shapefile, three files are indispensable, they are ".shp", ".shx" and ".dbf" files

.shp-graphic format, used to save the geometric entities of the element.
.shx—Graphic index format. Geometry position index records the position of each geometry body in the shp file, which can speed up the efficiency of searching a geometry body forward or backward.
.dbf—attribute data format, which stores the attribute data of each geometric shape in dBase IV data table format.

First you need to have a shp file, here is a Chinese provincial administrative division _shp map data file:

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/15785012

Note that the selected shp file should not have Chinese name and Chinese path

 

Then click to open

 

 

Guess you like

Origin blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/114879662