AdaKing88
2023-08-21 23258c585a626b15d770459870a8b24763cf540c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.tairui.app.cim.util;
 
 
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.WKBReader;
import com.vividsolutions.jts.io.WKTReader;
import org.geotools.geojson.geom.GeometryJSON;
import org.springframework.util.StringUtils;
 
import java.io.IOException;
import java.io.StringWriter;
 
public class GeoJsonUtil {
 
 
    public static String getGeoJSON(Point geometry){
 
        if(geometry==null){
            return null;
        }
        StringWriter writer = new StringWriter();
        GeometryJSON g = new GeometryJSON();
        try {
            g.write(geometry, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        String json = writer.toString();
        if("null".equals(json)){
            json = null;
        }
        return json;
    }
 
    public static Geometry getGeometryFromGeometryBinargy(String hexString){
        GeometryFactory geometryFactory = new GeometryFactory();
        WKBReader wktReader = new WKBReader();
        Geometry geometry = null ;
        try {
            geometry =  wktReader.read(WKBReader.hexToBytes(hexString));
        } catch (Exception e) {
            e.printStackTrace();
        }
       return  geometry ;
    }
 
    //geoJSON格式字符串转Point类型
    public static Geometry getGeometryFromGeoJSON(String geoJSON){
 
        if(StringUtils.isEmpty(geoJSON)){
            return null;
        }
 
        GeometryJSON  g = new GeometryJSON();
 
        Geometry geometry =null ;
 
        try {
 
            geometry=(Geometry)g.read(geoJSON);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
 
        return geometry;
    }
 
    //数据库AsText查询出来的wkt格式的字符串转为geoJSON返回到前端
    public static String getGeoJSONFromWKT(String geoWkt){
 
        if(StringUtils.isEmpty(geoWkt)){
            return null;
        }
        WKTReader reader = new WKTReader();
 
        String ret = null;
        try {
            Geometry geometry = reader.read(geoWkt);
            StringWriter writer = new StringWriter();
            GeometryJSON g = new GeometryJSON();
            g.write(geometry, writer);
            ret = writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
 
    }
 
}