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

POM



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.example
    geotoolTest
    0.0.1-SNAPSHOT
    geotoolTest
    geotoolTest
    
        1.8
        26-SNAPSHOT
    
    
        
        
            osgeo
            OSGeo Release Repository
            https://repo.osgeo.org/repository/release/
            
                false
            
            
                true
            
        
        
            osgeo-snapshot
            OSGeo Snapshot Repository
            https://repo.osgeo.org/repository/snapshot/
            
                true
            
            
                false
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.geotools
            gt-geojson
            ${geotools.version}
        
        
            org.locationtech.jts
            jts-core
            1.18.1
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




代码

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

}


你可能感兴趣的:(GIS服务端,geotool)