From a088987e7ab7005db1bb1da61dfc0cf420e02d78 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 13 十一月 2024 17:11:28 +0800 Subject: [PATCH] 1 --- src/main/java/com/moon/server/helper/GeoHelper.java | 59 +++++++++++++++++++++++++++++++++-------------------------- 1 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/moon/server/helper/GeoHelper.java b/src/main/java/com/moon/server/helper/GeoHelper.java index 20ac8da..ac1403e 100644 --- a/src/main/java/com/moon/server/helper/GeoHelper.java +++ b/src/main/java/com/moon/server/helper/GeoHelper.java @@ -5,18 +5,23 @@ import org.apache.commons.logging.LogFactory; import org.gdal.osr.SpatialReference; import org.geotools.geometry.DirectPosition2D; +import org.geotools.geometry.jts.JTS; import org.geotools.referencing.CRS; import org.geotools.referencing.GeodeticCalculator; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.io.WKTReader; +import org.opengis.referencing.crs.CRSAuthorityFactory; import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.opengis.referencing.operation.MathTransform; import java.awt.geom.Point2D; -/** - * Geo甯姪绫� - * @author WWW - * @date 2023-09-14 - */ +@SuppressWarnings("ALL") public class GeoHelper { + public static SpatialReference sr4326; + + public static SpatialReference sr4490; + public static SpatialReference sr104903; public final static double MOON_RADIUS = 1738000; @@ -25,11 +30,14 @@ private final static Log log = LogFactory.getLog(GeoHelper.class); - /** - * 鍒濆鍖栧潗鏍囩郴 - */ public static void initSr() { try { + sr4326 = new SpatialReference(); + sr4326.ImportFromEPSG(StaticData.I4326); + + sr4490 = new SpatialReference(); + sr4490.ImportFromEPSG(StaticData.I4490); + sr104903 = new SpatialReference(StaticData.MOON_2000_WKT); crs104903 = CRS.parseWKT(StaticData.MOON_2000_WKT); @@ -38,9 +46,6 @@ } } - /** - * 鑾峰彇璺濈 - */ public static double getDistance(double x1, double y1, double x2, double y2) { GeodeticCalculator geodeticCalculator = new GeodeticCalculator(crs104903); geodeticCalculator.setStartingGeographicPoint(x1, y1); @@ -49,9 +54,6 @@ return geodeticCalculator.getOrthodromicDistance(); } - /** - * 鑾峰彇璺濈2 - */ public static double getDistance2(double lon1, double lat1, double lon2, double lat2) { double radLat1 = Math.toRadians(lat1); double radLat2 = Math.toRadians(lat2); @@ -62,16 +64,10 @@ return s * MOON_RADIUS; } - /** - * 鑾峰彇鏂瑰悜瑙� - */ public static double getBearing(double x1, double y1, double x2, double y2) { return (90 - Math.toDegrees(Math.atan2(x2 - x1, y2 - y1)) + 360) % 360; } - /** - * 鑾峰彇瑙掑害 - */ public static double getAngle(double x1, double y1, double x2, double y2) { DirectPosition2D p1 = new DirectPosition2D(crs104903, x1, y1); DirectPosition2D p2 = new DirectPosition2D(crs104903, x2, y2); @@ -83,9 +79,6 @@ return gc.getAzimuth(); } - /** - * 鑾峰彇瑙掑害2 - */ public static double getAngle2(double x1, double y1, double x2, double y2) { DirectPosition2D p1 = new DirectPosition2D(x1, y1); DirectPosition2D p2 = new DirectPosition2D(x2, y2); @@ -97,9 +90,6 @@ return gc.getAzimuth(); } - /** - * 鏍规嵁璺濈鍜岃搴﹁幏鍙栫洰鏍囩偣 - */ public static Point2D getPointByDistanceAndAngle(double x, double y, double angle, double distance) { DirectPosition2D p1 = new DirectPosition2D(x, y); @@ -109,4 +99,21 @@ return gc.getDestinationGeographicPoint(); } + + public static double[] csTransform(double x, double y, int epsg) throws Exception { + CRSAuthorityFactory factory = CRS.getAuthorityFactory(true); + CoordinateReferenceSystem target = factory.createCoordinateReferenceSystem("EPSG:4326"); + + CoordinateReferenceSystem sourceCrs = CRS.decode("EPSG:" + epsg); + CoordinateReferenceSystem targetCrs = CRS.decode("EPSG:4326"); + MathTransform transform = CRS.findMathTransform(sourceCrs, targetCrs, true); + + WKTReader reader = new WKTReader(); + Geometry geometry = reader.read("POINT (" + x + " " + y + ")"); + geometry.setSRID(epsg); + + Geometry geo = JTS.transform(geometry, transform); + + return new double[]{geo.getCoordinate().x, geo.getCoordinate().y}; + } } -- Gitblit v1.9.3