| | |
| | | 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坐标系构造GeodeticCalculator |
| | | 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 参数 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计算维度 |
| | | * @param @param x |
| | | * @param @param y |
| | | * @param @return 参数 |