From b546994ced2db809d4efe9a7a338892c4df27e75 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期五, 16 六月 2023 17:43:47 +0800 Subject: [PATCH] 1 --- src/main/java/com/yssh/utils/CalculateUtils.java | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 84 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/yssh/utils/CalculateUtils.java b/src/main/java/com/yssh/utils/CalculateUtils.java index 4f494c3..45193a1 100644 --- a/src/main/java/com/yssh/utils/CalculateUtils.java +++ b/src/main/java/com/yssh/utils/CalculateUtils.java @@ -1,12 +1,95 @@ package com.yssh.utils; +import java.text.SimpleDateFormat; import java.util.*; import com.yssh.entity.MonitorPointPosition; +import org.geotools.geometry.DirectPosition2D; +import org.geotools.referencing.CRS; +import org.geotools.referencing.GeodeticCalculator; +import org.geotools.referencing.crs.DefaultGeographicCRS; +import org.opengis.referencing.crs.CoordinateReferenceSystem; public class CalculateUtils { + private final static SimpleDateFormat ymdh = new SimpleDateFormat("yyyyMMddHH"); /** + * 鑾峰彇骞存湀鏃ユ椂 + */ + public static String getYearMonthDayHour(Date date) { + return ymdh.format(date); + } + + /** + * 榛樿鍦扮悆鍗婂緞,璧ら亾鍗婂緞(鍗曚綅m) + */ + private final static double EARTH_RADIUS1 = 6371000; + + /** + * 杞寲涓哄姬搴�(rad) + */ + private static double rad(double d) + { + return d * Math.PI / 180.0; + } + + /** + * 璁$畻璺濈1 + */ + public static double getDistance1(double lon1, double lat1, double lon2, double lat2) { + double radLat1 = rad(lat1); + double radLat2 = rad(lat2); + double a = radLat1 - radLat2; + double b = rad(lon1) - rad(lon2); + double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); + + s = s * EARTH_RADIUS1; + + return Math.round(s * 100) / 100; + } + + /** + * 璁$畻璺濈2 + */ + public static double getDistance2(double x1, double y1, double x2, double y2) { + // 84鍧愭爣绯绘瀯閫燝eodeticCalculator + GeodeticCalculator geodeticCalculator = new GeodeticCalculator(DefaultGeographicCRS.WGS84); + + // 璧风偣缁忕含搴� + geodeticCalculator.setStartingGeographicPoint(x1, y1); + + // 鏈偣缁忕含搴� + geodeticCalculator.setDestinationGeographicPoint(x2, y2); + + // 璁$畻璺濈锛屽崟浣嶏細绫� + double distance = geodeticCalculator.getOrthodromicDistance(); + + return Math.round(distance * 100) / 100; + } + + /** + * 璁$畻瑙掑害 + */ + public static double getAngle(double x1, double y1, double x2, double y2) { + try { + CoordinateReferenceSystem crs = CRS.decode("EPSG:4326"); + DirectPosition2D p1 = new DirectPosition2D(crs, x1, y1); + DirectPosition2D p2 = new DirectPosition2D(crs, x2, y2); + + GeodeticCalculator gc = new GeodeticCalculator(); + gc.setStartingGeographicPoint(p1); + gc.setDestinationGeographicPoint(p2); + + double angle = gc.getAzimuth(); + + return Math.round(angle * 100) / 100; + } catch (Exception ex) { + return 0; + } + } + + /** + * 璁$畻缁忓害 * @param @param x * @param @param y * @param @return 鍙傛暟 @@ -24,6 +107,7 @@ } /** + * 璁$畻缁村害 * @param @param x * @param @param y * @param @return 鍙傛暟 -- Gitblit v1.9.3