| | |
| | | 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; |
| | | |
| | |
| | | |
| | | private final static Log log = LogFactory.getLog(GeoHelper.class); |
| | | |
| | | /** |
| | | * 初始化坐标系 |
| | | */ |
| | | public static void initSr() { |
| | | try { |
| | | sr4326 = new SpatialReference(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取距离 |
| | | */ |
| | | public static double getDistance(double x1, double y1, double x2, double y2) { |
| | | GeodeticCalculator geodeticCalculator = new GeodeticCalculator(crs104903); |
| | | geodeticCalculator.setStartingGeographicPoint(x1, y1); |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | return gc.getAzimuth(); |
| | | } |
| | | |
| | | /** |
| | | * 根据距离和角度获取目标点 |
| | | */ |
| | | public static Point2D getPointByDistanceAndAngle(double x, double y, double angle, double distance) { |
| | | DirectPosition2D p1 = new DirectPosition2D(x, y); |
| | | |
| | |
| | | |
| | | 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}; |
| | | } |
| | | } |