aix
2024-08-05 52d6a8ddb4a1f631edffa7c339a3ba848c0e7a1e
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
96
97
98
99
package com.dji.sample.territory.utils;
 
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Point;
 
 
public class CoordinateSystemUtil {
    public static void main(String[] args) {
        String wtk= "MULTIPOLYGON(((3.94836025714E7 3176072.7897, 3.94835938931E7 3176063.7288, 3.94835894481E7 3176067.1154, 3.94835981264E7 3176075.7938, 3.94836026103E7 3176085.7288, 3.94836089215E7 3176099.7123, 3.94836159065E7 3176121.5139, 3.94836178114E7 3176140.9873, 3.94836181917E7 3176148.7199, 3.94836214775E7 3176146.8663, 3.94836254582E7 3176144.37, 3.94836237382E7 3176137.8123, 3.94836165415E7 3176107.3321, 3.9483608468E7 3176087.3696, 3.94836025714E7 3176072.7897)))";
        String se=wtk.replaceAll(",\\["," ").replaceAll("]","").replaceAll("\\[","");
        System.out.println(se);
        System.out.println(poylonCGCStoWGS(se));
    }
    public static Geometry pointCGCStoWGS(Double lot, Double lat) {
        String wkt = "POINT (" + lot + " " + lat + ")";
        Geometry source = createGeometry(wkt, 4527);
        return coordinateTransform(source, 4326);
    }
 
    public static Geometry pointWGStoCGCS(Double lot, Double lat) {
        String wkt = "POINT (" + lot + " " + lat + ")";
        Geometry source = createGeometry(wkt, 4326);
        return coordinateTransform(source, 4527);
    }
 
    public static String poylonWGStoCGCS(String poylon) {
        Geometry source = createGeometry(poylon, 4326);
        Geometry transformedGeometry = coordinateTransform(source, 4527);
        return transformedGeometry.toText();
    }
 
    public static String poylonCGCStoWGS(String poylon) {
        Geometry source = createGeometry(poylon, 4527);
        Geometry transformedGeometry = coordinateTransform(source, 4326);
        return transformedGeometry.toText();
    }
 
    public static double[] extractCoordinates(Geometry geometry) {
        // 如果是点类型的几何对象,则直接提取坐标
        if (geometry instanceof Point) {
            Coordinate coordinate = geometry.getCoordinate();
            return new double[]{coordinate.getX(), coordinate.getY()};
        } else {
            // 如果不是点类型,则返回 null 或者根据需要处理其他几何对象类型
            return null;
        }
    }
 
    // 格式化坐标值,保留指定小数位数
    public static String formatCoordinate(double coordinate) {
        return String.format("%.15f", coordinate).replaceAll("\\.?0*$", "");
    }
 
    public static Geometry createGeometry(String wkt, int srid) {
        try {
            WKTReader reader = new WKTReader();
            Geometry geometry = reader.read(wkt);
            geometry.setSRID(srid);
            return geometry;
        } catch (ParseException e) {
            System.out.println("WKT解析错误: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
    public static Geometry coordinateTransform(Geometry sourceGeometry, int targetSrid) {
        if (sourceGeometry == null || sourceGeometry.getSRID() == 0 || targetSrid == 0) {
            return null;
        }
        try {
            CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);
            CoordinateReferenceSystem source = factory.createCoordinateReferenceSystem("EPSG:" + sourceGeometry.getSRID());
            CoordinateReferenceSystem target = factory.createCoordinateReferenceSystem("EPSG:" + targetSrid);
            MathTransform transform = CRS.findMathTransform(source, target, true);
            Geometry res = JTS.transform(sourceGeometry, transform);
            if (res != null) {
                res.setSRID(targetSrid);
            }
            return res;
        } catch (FactoryException | TransformException e) {
            e.printStackTrace();
        }
        return null;
    }
}