package org.springblade.common.utils;
|
|
import org.geotools.geometry.jts.JTS;
|
import org.geotools.referencing.CRS;
|
import org.locationtech.jts.geom.Coordinate;
|
import org.locationtech.jts.geom.Geometry;
|
import org.locationtech.jts.geom.Point;
|
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;
|
|
/**
|
* 空间坐标工具类
|
*/
|
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;
|
}
|
}
|