POM
<?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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>geotoolTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>geotoolTest</name>
<description>geotoolTest</description>
<properties>
<java.version>1.8</java.version>
<geotools.version>26-SNAPSHOT</geotools.version>
</properties>
<repositories>
<!--geotools -->
<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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>1.18.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
代码
package com.example.geotooltest;
import org.geotools.geojson.geom.GeometryJSON;
import org.junit.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;
import java.io.*;
public class DataTypeConvert {
public Geometry geojson2Geometry(String geojson) throws IOException {
GeometryJSON gjson = new GeometryJSON(7);
return gjson.read(new StringReader(geojson));
}
@Test
public void geojson2GeometryTest() throws IOException {
String json1 = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [ [ 114.318459657701752, 25.451711491442541 ], [ 114.338936430317901, 25.468215158924203 ], [ 114.365220048899801, 25.458129584352076 ], [ 114.377750611246995, 25.405562347188262 ], [ 114.322127139364355, 25.39822738386308 ], [ 114.318459657701752, 25.451711491442541 ] ] ] ] }";
Geometry geo1 = geojson2Geometry(json1);
System.out.println(geo1);
}
public String geometry2Geojson(Geometry geometry) throws IOException {
GeometryJSON gjson = new GeometryJSON(7);
StringWriter writer = new StringWriter();
gjson.write(geometry, writer);
return writer.toString();
}
@Test
public void geometry2GeojsonTest() throws IOException {
Coordinate[] coordinates = new Coordinate[]{
new Coordinate(114.355337061964661, 25.473019331992273),
new Coordinate(114.460986089648614, 25.47481608416377),
new Coordinate(114.47176660267759, 25.380306919943092),
new Coordinate(114.357133814136162, 25.377791466902995),
new Coordinate(114.355337061964661, 25.473019331992273)
};
GeometryFactory gf = new GeometryFactory();
Polygon polygon = gf.createPolygon(coordinates);
String geojson = geometry2Geojson(polygon);
System.out.println(geojson);
}
public Geometry wkt2Geometry(String wkt) throws ParseException {
WKTReader reader = new WKTReader();
return reader.read(wkt);
}
@Test
public void wkt2GeometryTest() throws ParseException {
String wkt = "POLYGON ((114.35533706196466 25.473019331992273, 114.46098608964861 25.47481608416377, 114.47176660267759 25.38030691994309, 114.35713381413616 25.377791466902995, 114.35533706196466 25.473019331992273))\n";
System.out.println(wkt2Geometry(wkt));
}
public String geometry2Wkt(Geometry geometry) throws ParseException {
WKTWriter writer = new WKTWriter();
return writer.write(geometry);
}
@Test
public void geometry2WktTest() throws ParseException {
Coordinate[] coordinates = new Coordinate[]{
new Coordinate(114.355337061964661, 25.473019331992273),
new Coordinate(114.460986089648614, 25.47481608416377),
new Coordinate(114.47176660267759, 25.380306919943092),
new Coordinate(114.357133814136162, 25.377791466902995),
new Coordinate(114.355337061964661, 25.473019331992273)
};
GeometryFactory gf = new GeometryFactory();
Polygon polygon = gf.createPolygon(coordinates);
System.out.println(geometry2Wkt(polygon));
}
}