| | |
| | | package com.yssh.utils; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.awt.geom.Point2D; |
| | | import java.util.*; |
| | | |
| | | import com.yssh.entity.Coordinate; |
| | |
| | | import org.geotools.referencing.crs.DefaultGeographicCRS; |
| | | import org.opengis.referencing.crs.CoordinateReferenceSystem; |
| | | |
| | | @SuppressWarnings("ALL") |
| | | 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) |
| | | { |
| | | 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); |
| | |
| | | return round2(s); |
| | | } |
| | | |
| | | /** |
| | | * 计算距离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 round2(distance); |
| | | } |
| | | |
| | | public static double round2(double d) { |
| | | return ((long)(d * 100)) / 100D; |
| | | return ((long) (d * 100)) / 100D; |
| | | } |
| | | |
| | | /** |
| | | * 计算角度 |
| | | */ |
| | | public static double round6(double d) { |
| | | return ((long) (d * 1000000)) / 1000000D; |
| | | } |
| | | |
| | | public static double getAngle(double x1, double y1, double x2, double y2) { |
| | | try { |
| | | CoordinateReferenceSystem crs = CRS.decode("EPSG:4326"); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 计算角度2 |
| | | */ |
| | | public static double getAngle2(double x1, double y1, double x2, double y2){ |
| | | public static double getAngle2(double x1, double y1, double x2, double y2) { |
| | | try { |
| | | DirectPosition2D p1 = new DirectPosition2D( x1, y1); |
| | | DirectPosition2D p1 = new DirectPosition2D(x1, y1); |
| | | DirectPosition2D p2 = new DirectPosition2D(x2, y2); |
| | | |
| | | GeodeticCalculator gc = new GeodeticCalculator(); |
| | |
| | | double angle = gc.getAzimuth(); |
| | | |
| | | return round2(angle); |
| | | }catch (Exception ex){ |
| | | } catch (Exception ex) { |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取坐标 |
| | | * @param su |
| | | * @return |
| | | */ |
| | | public static Coordinate getCoordinate(DistanceSuYuan su) { |
| | | String[] sirs = su.getId().split("_"); |
| | | |
| | |
| | | return new Coordinate(lon, lat); |
| | | } |
| | | |
| | | /** |
| | | * 计算经度 |
| | | * @param @param x |
| | | * @param @param y |
| | | * @param @return 参数 |
| | | * @return double 返回类型 |
| | | * @throws |
| | | * @Title: getLon |
| | | * @Description: 计算经度 |
| | | */ |
| | | public static double getLon(int x, int y) { |
| | | double lon = 115.9165227 + 0.000116732 * (x - 0.5) + 0.00000116862 * (y - 0.5); |
| | | if (lon < 115 || lon > 116) { |
| | | System.out.println("lon is invalid"); |
| | | } |
| | | return lon; |
| | | |
| | | return round6(lon); |
| | | } |
| | | |
| | | /** |
| | | * 计算维度 |
| | | * @param @param x |
| | | * @param @param y |
| | | * @param @return 参数 |
| | | * @return double 返回类型 |
| | | * @throws |
| | | * @Title: getLat |
| | | * @Description: 计算维度 |
| | | */ |
| | | public static double getLat(int x, int y) { |
| | | double lat = 39.77250000 + 0.000001000 * (x - 0.5) - 0.00009000000 * (y - 0.5); |
| | | if (lat < 39 || lat > 40) { |
| | | System.out.println("lat is invalid"); |
| | | } |
| | | return lat; |
| | | |
| | | return round6(lat); |
| | | } |
| | | |
| | | /** |
| | | * @param @param checkPoints |
| | | * @param @return 参数 |
| | | * @return List<String> 返回类型 |
| | | * @throws |
| | | * @Title: assembleId |
| | | * @Description: 组装id |
| | | */ |
| | | public static List<String> assembleId(List<MonitorPointPosition> checkPoints) { |
| | | List<String> ids2d = new ArrayList<>(); |
| | | for (MonitorPointPosition point : checkPoints) { |
| | |
| | | return ids2d; |
| | | } |
| | | |
| | | /** |
| | | * @param @param checkPoint |
| | | * @param @param range |
| | | * @param @return 参数 |
| | | * @return List<String> 返回类型 |
| | | * @Title: aloneCrosswiseScope |
| | | * @Description: 单独点位横向范围扩展 |
| | | * @backup 强制将层级改为 0 |
| | | */ |
| | | public static List<String> aloneCrosswiseExtend(MonitorPointPosition checkPoint, int range) { |
| | | List<String> ids = new ArrayList<>(); |
| | | Integer x = checkPoint.getX(); |
| | |
| | | } |
| | | } |
| | | return ids; |
| | | } |
| | | |
| | | public static String getFilterByExtend(MonitorPointPosition point, int range) { |
| | | Integer x = point.getX(); |
| | | Integer y = point.getY(); |
| | | |
| | | return String.format("x between %d and %d and y between %d and %d", x - range / 2, x + range / 2, y - range / 2, y + range / 2); |
| | | } |
| | | |
| | | public static List<String> temporary(MonitorPointPosition point, int range) { |
| | |
| | | return ids3d; |
| | | } |
| | | |
| | | /** |
| | | * 获取方向 |
| | | */ |
| | | public static String getDir(double direction) { |
| | | if (direction < 0) { |
| | | direction = direction + 360; |
| | |
| | | direction = direction - 360; |
| | | } |
| | | |
| | | if (direction > 315 && direction < 45) |
| | | return "北"; |
| | | if (direction >= 45 && direction < 135) |
| | | if (direction >= 22.5 && direction < 67.5) |
| | | return "东北"; |
| | | if (direction >= 67.5 && direction < 112.5) |
| | | return "东"; |
| | | if (direction >= 135 && direction < 225) |
| | | if (direction >= 112.5 && direction < 157.5) |
| | | return "东南"; |
| | | if (direction >= 157.5 && direction < 202.5) |
| | | return "南"; |
| | | if (direction >= 202.5 && direction < 247.5) |
| | | return "西南"; |
| | | if (direction >= 247.5 && direction < 292.5) |
| | | return "西"; |
| | | if (direction >= 292.5 && direction < 337.5) |
| | | return "西北"; |
| | | |
| | | return "西"; |
| | | return "北"; |
| | | } |
| | | |
| | | /** |
| | | * @param @return 参数 |
| | | * @return Double 返回类型 |
| | | * @throws |
| | | * @Title: getWindSpeed |
| | | * @Description: 计算风速 |
| | | */ |
| | | public static List<Coordinate> calcRect(double x, double y) { |
| | | double buffer = 10; |
| | | double dis = round6(Math.sqrt(Math.pow(buffer / 2, 2) * 2)); |
| | | |
| | | List<Coordinate> list = new ArrayList<>(); |
| | | list.add(getPointByDisAndAngle(x, y, 315, dis)); |
| | | list.add(getPointByDisAndAngle(x, y, 45, dis)); |
| | | list.add(getPointByDisAndAngle(x, y, 135, dis)); |
| | | list.add(getPointByDisAndAngle(x, y, 225, dis)); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | private static Coordinate getPointByDisAndAngle(double x, double y, double angle, double dis) { |
| | | try { |
| | | DirectPosition2D p1 = new DirectPosition2D(x, y); |
| | | |
| | | GeodeticCalculator gc = new GeodeticCalculator(); |
| | | gc.setStartingGeographicPoint(p1); |
| | | gc.setDirection(angle, dis); |
| | | |
| | | Point2D dest = gc.getDestinationGeographicPoint(); |
| | | double newX = round6(dest.getX()); |
| | | double newY = round6(dest.getY()); |
| | | |
| | | return new Coordinate(newX, newY); |
| | | } catch (Exception ex) { |
| | | return new Coordinate(); |
| | | } |
| | | } |
| | | |
| | | public static Double getWindSpeed(double v, double u) { |
| | | return Math.sqrt(v * v + u * u); |
| | | return round6(Math.sqrt(v * v + u * u)); |
| | | } |
| | | |
| | | /** |
| | | * @param @param v |
| | | * @param @param u |
| | | * @param @return 参数 |
| | | * @return double 返回类型 |
| | | * @throws |
| | | * @Title: getWindDirection |
| | | * @Description: 计算风向 |
| | | */ |
| | | public static double getWindDirection(double v, double u) { |
| | | double result = Math.atan(u / (v + Math.pow(10, -5))) / Math.PI * 180; |
| | | if (result < 0) { |
| | |
| | | } else if (u < 0 && v > 0) { |
| | | result += 360; |
| | | } |
| | | return result; |
| | | |
| | | return round6(result); |
| | | } |
| | | |
| | | /** |
| | | * @param @param list 排序对象 |
| | | * @param @param property 排序参数 |
| | | * @param @param order 排序顺序 |
| | | * @param @return 参数 |
| | | * @return List<Map < String, Object>> 返回类型 |
| | | * @throws |
| | | * @Title: sort |
| | | * @Description: 排序 |
| | | */ |
| | | public static List<Map<String, Object>> sort(List<Map<String, Object>> list, final String property, final boolean order) { |
| | | if (list == null || property == null) { |
| | | return null; |