Commit 06aa26f80707c0f603cecaff4a668e6b310c4076
1 parent
3e396c1e
m
Showing
4 changed files
with
253 additions
and
17 deletions
trash-garbage/src/main/java/com/trash/garbage/service/impl/GarOrderAssociationServiceAsync.java
| 1 | 1 | package com.trash.garbage.service.impl; |
| 2 | 2 | |
| 3 | +import java.util.ArrayList; | |
| 4 | +import java.util.List; | |
| 5 | + | |
| 6 | +import org.slf4j.Logger; | |
| 7 | +import org.slf4j.LoggerFactory; | |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 9 | +import org.springframework.scheduling.annotation.Async; | |
| 10 | +import org.springframework.scheduling.annotation.EnableAsync; | |
| 11 | +import org.springframework.stereotype.Component; | |
| 12 | + | |
| 3 | 13 | import com.alibaba.fastjson.JSON; |
| 4 | -import com.alibaba.fastjson.JSONArray; | |
| 5 | 14 | import com.alibaba.fastjson.JSONObject; |
| 6 | -import com.trash.common.utils.DateUtils; | |
| 7 | 15 | import com.trash.common.utils.RemoteServerUtils; |
| 8 | -import com.trash.dropPointInfo.domain.DropPointInfo; | |
| 9 | 16 | import com.trash.dropPointInfo.service.IDropPointInfoService; |
| 10 | -import com.trash.dropPointInfo.service.impl.DropPointInfoServiceImpl; | |
| 11 | 17 | import com.trash.garbage.pojo.domain.GarOrder; |
| 12 | 18 | import com.trash.garbage.pojo.domain.GarOrderMatchAsk; |
| 13 | 19 | import com.trash.garbage.pojo.dto.AskTransportDto; |
| 14 | 20 | import com.trash.garbage.pojo.dto.UploadDto; |
| 15 | 21 | import com.trash.garbage.service.GarOrderMatchAskService; |
| 16 | -import com.trash.garbage.utils.HttpUtil; | |
| 17 | -import lombok.extern.slf4j.Slf4j; | |
| 18 | -import org.slf4j.Logger; | |
| 19 | -import org.slf4j.LoggerFactory; | |
| 20 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 21 | -import org.springframework.scheduling.annotation.Async; | |
| 22 | -import org.springframework.scheduling.annotation.EnableAsync; | |
| 23 | -import org.springframework.stereotype.Component; | |
| 24 | - | |
| 25 | -import java.util.ArrayList; | |
| 26 | -import java.util.Date; | |
| 27 | -import java.util.List; | |
| 28 | 22 | |
| 29 | 23 | /** |
| 30 | 24 | * 电子联单服务 |
| ... | ... | @@ -106,7 +100,9 @@ public class GarOrderAssociationServiceAsync { |
| 106 | 100 | garOrderMatchAsk.setUnloadingLonLat(dto.getLongitude()+","+dto.getLatitude()); |
| 107 | 101 | // 获取处置场所的经纬度列表 |
| 108 | 102 | List<double[]> coordinates = parseCoordinates(coord); |
| 109 | - | |
| 103 | + | |
| 104 | + | |
| 105 | + coordinates = com.trash.garbage.utils.DistanceUtil.expansion(coordinates, 200); | |
| 110 | 106 | // 判断是否在多边形内 |
| 111 | 107 | boolean isInside = isPointInPolygon(vehicleLat, vehicleLon, coordinates); |
| 112 | 108 | if (isInside) { | ... | ... |
trash-garbage/src/main/java/com/trash/garbage/utils/DistanceUtil.java
0 → 100644
| 1 | +package com.trash.garbage.utils; | |
| 2 | + | |
| 3 | +import java.util.ArrayList; | |
| 4 | +import java.util.Collections; | |
| 5 | +import java.util.LinkedList; | |
| 6 | +import java.util.List; | |
| 7 | + | |
| 8 | +public class DistanceUtil { | |
| 9 | + /** | |
| 10 | + * 操作多边形扩张/收缩 | |
| 11 | + * | |
| 12 | + * @param points 待扩/收缩张多边形 | |
| 13 | + * @param dist 距离 正数扩张,负数收缩 | |
| 14 | + * @return 扩张/收缩后的多边形 | |
| 15 | + */ | |
| 16 | + public static List<double[]> expansion(List<double[]> pts, int dist) { | |
| 17 | + if (pts.size() <= 3) { | |
| 18 | + return pts; | |
| 19 | + } | |
| 20 | + | |
| 21 | + List<Point> points = new ArrayList<Point>(); | |
| 22 | + | |
| 23 | + for(double[] pt:pts){ | |
| 24 | + Point p = new Point(); | |
| 25 | + p.setLat(pt[0]); | |
| 26 | + p.setLng(pt[1]); | |
| 27 | + } | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + List<Point> npList = new LinkedList<>(); | |
| 32 | + List<Double> lngList = new LinkedList<>(); | |
| 33 | + List<Double> latList = new LinkedList<>(); | |
| 34 | + | |
| 35 | + for (Point point : points) { | |
| 36 | + lngList.add(point.getLng()); | |
| 37 | + latList.add(point.getLat()); | |
| 38 | + } | |
| 39 | + Double minLng = Collections.min(lngList); | |
| 40 | + Double minLat = Collections.min(latList); | |
| 41 | + | |
| 42 | + for (Point point : points) { | |
| 43 | + Point p = new Point(); | |
| 44 | + p.setLng(point.getLng() - minLng); | |
| 45 | + p.setLat(point.getLat() - minLat); | |
| 46 | + npList.add(p); | |
| 47 | + } | |
| 48 | + | |
| 49 | + List<Point> list = enlarge(npList, dist); | |
| 50 | + LinkedList<Point> resultList = new LinkedList<>(); | |
| 51 | + for (Point point : list) { | |
| 52 | + Point np = new Point(); | |
| 53 | + np.setLng(point.getLng() + minLng); | |
| 54 | + np.setLat(point.getLat() + minLat); | |
| 55 | + resultList.add(np); | |
| 56 | + } | |
| 57 | + | |
| 58 | + List<double[]> result = new ArrayList<double[]>(); | |
| 59 | + | |
| 60 | + for(Point np:resultList){ | |
| 61 | + double[] d = {np.getLat(),np.getLng()}; | |
| 62 | + result.add(d); | |
| 63 | + } | |
| 64 | + | |
| 65 | + return result; | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * 多边形外扩/收缩 | |
| 70 | + */ | |
| 71 | + private static List<Point> enlarge(List<Point> points, int dist) { | |
| 72 | + LinkedList<Point> resultList = new LinkedList<>(); | |
| 73 | + int size = points.size(); | |
| 74 | + double angle = dist * dmi(); | |
| 75 | + angle = isConvex(points) ? angle : -angle; | |
| 76 | + | |
| 77 | + for (int i = 0; i < size; i++) { | |
| 78 | + Point pi = points.get(i); | |
| 79 | + Point v1 = VectorUtil.sub(points.get((i - 1 + size) % size), pi); | |
| 80 | + Point v2 = VectorUtil.sub(points.get((i + 1) % size), pi); | |
| 81 | + double norm1 = VectorUtil.norm(v1); | |
| 82 | + double norm2 = VectorUtil.norm(v2); | |
| 83 | + | |
| 84 | + if (norm1 <= 0 || norm2 <= 0) { | |
| 85 | + // 剔除重复点(或距离极近的点) | |
| 86 | + points.remove(i); | |
| 87 | + return enlarge(points, dist); | |
| 88 | + } | |
| 89 | + | |
| 90 | + Point normalizeV1 = VectorUtil.mul(v1, 1 / norm1); | |
| 91 | + Point normalizeV2 = VectorUtil.mul(v2, 1 / norm2); | |
| 92 | + double sinTheta = VectorUtil.cross(normalizeV1, normalizeV2); | |
| 93 | + Point qi = VectorUtil.add(pi, VectorUtil.mul(VectorUtil.add(normalizeV1, normalizeV2), angle / sinTheta)); | |
| 94 | + resultList.add(qi); | |
| 95 | + } | |
| 96 | + | |
| 97 | + return resultList; | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * 计算1米代表的角度 这里只是近似计算,事实上随着纬度的变化其变动范围将受到影响 | |
| 102 | + */ | |
| 103 | + private static double dmi() { | |
| 104 | + | |
| 105 | +// double s = 6378.137 * 1000; | |
| 106 | +// return 1 / (Math.PI / 180 * s); | |
| 107 | + return 0.000009; | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * 判断是否凸多边形 | |
| 112 | + */ | |
| 113 | + private static boolean isConvex(List<Point> points) { | |
| 114 | + double area = 0; | |
| 115 | + Point p1 = VectorUtil.sub(points.get(1), points.get(0)); | |
| 116 | + Point p2; | |
| 117 | + for (int i = 2; i < points.size(); i++) { | |
| 118 | + p2 = VectorUtil.sub(points.get(i), points.get(0)); | |
| 119 | + area += VectorUtil.cross(p1, p2); | |
| 120 | + p1 = p2; | |
| 121 | + } | |
| 122 | + return area > 0; | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * 多边形周长 | |
| 127 | + */ | |
| 128 | + private static double perimeter(List<Point> points) { | |
| 129 | + int size = points.size(); | |
| 130 | + double perimeter = 0.0d; | |
| 131 | + Point point; | |
| 132 | + for (int i = 0; i < size; i++) { | |
| 133 | + point = points.get(i); | |
| 134 | + perimeter += VectorUtil.norm(VectorUtil.sub(points.get((i - 1 + size) % size), point)); | |
| 135 | + } | |
| 136 | + return perimeter; | |
| 137 | + } | |
| 138 | +} | ... | ... |
trash-garbage/src/main/java/com/trash/garbage/utils/Point.java
0 → 100644
| 1 | +package com.trash.garbage.utils; | |
| 2 | + | |
| 3 | +import java.awt.geom.Point2D; | |
| 4 | + | |
| 5 | +public class Point { | |
| 6 | + private double lat; | |
| 7 | + private double lng; | |
| 8 | + | |
| 9 | + | |
| 10 | + public Point() { | |
| 11 | + } | |
| 12 | + | |
| 13 | + | |
| 14 | + public Point(String lng, String lat) { | |
| 15 | + this.lng = Double.parseDouble(lng); | |
| 16 | + this.lat = Double.parseDouble(lat); | |
| 17 | + } | |
| 18 | + | |
| 19 | + public Point(double lng, double lat) { | |
| 20 | + this.lng = lng; | |
| 21 | + this.lat = lat; | |
| 22 | + } | |
| 23 | + | |
| 24 | + @Override | |
| 25 | + public boolean equals(Object obj) { | |
| 26 | + if (obj instanceof Point) { | |
| 27 | + Point bmapPoint = (Point) obj; | |
| 28 | + return bmapPoint.getLng()==lng && bmapPoint.getLat() == lat; | |
| 29 | + } else { | |
| 30 | + return false; | |
| 31 | + } | |
| 32 | + } | |
| 33 | + | |
| 34 | + public double getLat() { | |
| 35 | + return lat; | |
| 36 | + } | |
| 37 | + public void setLat(double lat) { | |
| 38 | + this.lat = lat; | |
| 39 | + } | |
| 40 | + public double getLng() { | |
| 41 | + return lng; | |
| 42 | + } | |
| 43 | + public void setLng(double lng) { | |
| 44 | + this.lng = lng; | |
| 45 | + } | |
| 46 | + | |
| 47 | + public Point2D.Double getPoint2D(){ | |
| 48 | + return new Point2D.Double(getLng(),getLat()); | |
| 49 | + } | |
| 50 | + | |
| 51 | + | |
| 52 | + @Override | |
| 53 | + public String toString() { | |
| 54 | + return getLng()+","+getLat(); | |
| 55 | + } | |
| 56 | +} | ... | ... |
trash-garbage/src/main/java/com/trash/garbage/utils/VectorUtil.java
0 → 100644
| 1 | +package com.trash.garbage.utils; | |
| 2 | +public class VectorUtil { | |
| 3 | + /** | |
| 4 | + * 向量求模数 | |
| 5 | + */ | |
| 6 | + public static double norm(Point point) { | |
| 7 | + return Math.sqrt(point.getLng() * point.getLng() + point.getLat() * point.getLat()); | |
| 8 | + } | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * 向量加法 | |
| 12 | + */ | |
| 13 | + public static Point add(Point point1, Point point2) { | |
| 14 | + Point point = new Point(); | |
| 15 | + point.setLng(point1.getLng() + point2.getLng()); | |
| 16 | + point.setLat(point1.getLat() + point2.getLat()); | |
| 17 | + return point; | |
| 18 | + } | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * 向量减法 | |
| 22 | + */ | |
| 23 | + public static Point sub(Point point1, Point point2) { | |
| 24 | + Point point = new Point(); | |
| 25 | + point.setLng(point1.getLng() - point2.getLng()); | |
| 26 | + point.setLat(point1.getLat() - point2.getLat()); | |
| 27 | + return point; | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * 向量乘法 | |
| 32 | + */ | |
| 33 | + public static Point mul(Point p, double d) { | |
| 34 | + Point point = new Point(); | |
| 35 | + point.setLng(p.getLng() * d); | |
| 36 | + point.setLat(p.getLat() * d); | |
| 37 | + return point; | |
| 38 | + } | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * 向量叉乘 | |
| 42 | + */ | |
| 43 | + public static double cross(Point p1, Point p2) { | |
| 44 | + return p1.getLng() * p2.getLat() - p1.getLat() * p2.getLng(); | |
| 45 | + } | |
| 46 | +} | ... | ... |