【geotool】wkt、geojson、geometry互相转换

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));
    }

}


  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值