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

该博客展示了如何在Java中利用GeoTools和JTS库进行GeoJSON、WKT之间的几何数据转换。代码示例包括从GeoJSON字符串创建Geometry对象,将Geometry对象转换回GeoJSON,以及进行WKT到Geometry和Geometry到WKT的转换。这些转换在地理信息系统(GIS)开发中非常常见。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}


### 使用OpenLayers将WKT转换GeoJSON 为了实现从WKTGeoJSON转换,在OpenLayers环境中可以利用`ol/format/WKT`类来读取WKT数据并将其转化为几何对象,之后再通过`ol/format/GeoJSON`类把该几何对象转成GeoJSON格式[^1]。 下面是一个具体的代码实例展示这一过程: ```javascript import 'ol/ol.css'; import {Map, View} from 'ol'; import TileLayer from 'ol/layer/Tile'; import OSM from 'ol/source.OSM'; import WKT from 'ol/format/WKT'; import GeoJSON from 'ol/format/GeoJSON'; // 创建地图实例 const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: [0, 0], zoom: 2 }) }); // 定义要转化的WKT字符串 const wktString = "POINT (1 2)"; // 实例化WKT解析器 const wktFormat = new WKT(); // 解析WKT字符串得到几何对象 const geometry = wktFormat.readGeometry(wktString); // 如果需要获取完整的Feature而非单独的geometry,则可如下操作: const feature = wktFormat.readFeature(wktString); feature.setGeometryName('geom'); // 将几何对象或要素写入GeoJSON格式 const geojsonFormat = new GeoJSON(); const geojsonObject = geojsonFormat.writeFeature(feature); console.log(geojsonObject); ``` 此段代码展示了如何创建一个简单的OpenLayers应用,并定义了一个代表地理坐标的WKT字符串。接着使用`readGeometry()`方法将这个WKT表示法的数据转变为OpenLayers内部使用的几何结构。最后一步则是调用`writeFeature()`函数完成向GeoJSON格式的转变[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值