Commit 8aa8463f6da1379cf88154365d386b6f6b3f1474
1 parent
168352fc
更换多边形站内外算法..
Showing
2 changed files
with
57 additions
and
5 deletions
src/main/java/com/bsth/data/gpsdata/arrival/GeoCacheData.java
| ... | ... | @@ -44,6 +44,8 @@ public class GeoCacheData { |
| 44 | 44 | |
| 45 | 45 | //停车场 |
| 46 | 46 | public static Map<String, Polygon> tccMap; |
| 47 | + //停车场 | |
| 48 | + public static Map<String, com.bsth.util.Geo.Polygon> tccMap2; | |
| 47 | 49 | |
| 48 | 50 | //线路限速信息 |
| 49 | 51 | private static Map<String, Double> speedLimitMap; |
| ... | ... | @@ -185,8 +187,31 @@ public class GeoCacheData { |
| 185 | 187 | logger.error("停车场:" + tMap.get("PARK_CODE"), e); |
| 186 | 188 | } |
| 187 | 189 | } |
| 188 | - if (tccTempMap.size() > 0) | |
| 190 | + if (tccTempMap.size() > 0){ | |
| 189 | 191 | tccMap = tccTempMap; |
| 192 | + tccMap2 = convertPolygonMap(tccMap); | |
| 193 | + } | |
| 194 | + } | |
| 195 | + | |
| 196 | + private Map<String, com.bsth.util.Geo.Polygon> convertPolygonMap(Map<String, Polygon> tccMap) { | |
| 197 | + Map<String, com.bsth.util.Geo.Polygon> rsMap = new HashMap<>(); | |
| 198 | + Set<String> ks = tccMap.keySet(); | |
| 199 | + for(String k : ks){ | |
| 200 | + rsMap.put(k, convertPolygon(tccMap.get(k))); | |
| 201 | + } | |
| 202 | + return rsMap; | |
| 203 | + } | |
| 204 | + | |
| 205 | + public static com.bsth.util.Geo.Polygon convertPolygon(Polygon polygon) { | |
| 206 | + List<com.bsth.util.Geo.Point> ps = new ArrayList<>(); | |
| 207 | + com.bsth.util.Geo.Point p; | |
| 208 | + | |
| 209 | + Coordinate[] cs = polygon.getCoordinates(); | |
| 210 | + for(int i = 0; i < cs.length;i ++){ | |
| 211 | + p = new com.bsth.util.Geo.Point(cs[i].y, cs[i].x); | |
| 212 | + ps.add(p); | |
| 213 | + } | |
| 214 | + return new com.bsth.util.Geo.Polygon(ps); | |
| 190 | 215 | } |
| 191 | 216 | |
| 192 | 217 | private void loadStationRoutesData(){ | ... | ... |
src/main/java/com/bsth/data/gpsdata/arrival/utils/GeoUtils.java
| ... | ... | @@ -3,7 +3,10 @@ package com.bsth.data.gpsdata.arrival.utils; |
| 3 | 3 | import com.bsth.data.gpsdata.GpsEntity; |
| 4 | 4 | import com.bsth.data.gpsdata.arrival.GeoCacheData; |
| 5 | 5 | import com.bsth.data.gpsdata.arrival.entity.StationRoute; |
| 6 | -import com.vividsolutions.jts.geom.*; | |
| 6 | +import com.vividsolutions.jts.geom.Coordinate; | |
| 7 | +import com.vividsolutions.jts.geom.GeometryFactory; | |
| 8 | +import com.vividsolutions.jts.geom.LineString; | |
| 9 | +import com.vividsolutions.jts.geom.Point; | |
| 7 | 10 | |
| 8 | 11 | import java.util.List; |
| 9 | 12 | import java.util.Map; |
| ... | ... | @@ -26,7 +29,7 @@ public class GeoUtils { |
| 26 | 29 | */ |
| 27 | 30 | public static StationRoute gpsInStation(GpsEntity gps, List<StationRoute> srs) { |
| 28 | 31 | Point point = geometryFactory.createPoint(new Coordinate(gps.getLat(), gps.getLon())); |
| 29 | - double min = -1, distance, distance2; | |
| 32 | + double min = -1, distance; | |
| 30 | 33 | StationRoute stationRoute = null; |
| 31 | 34 | |
| 32 | 35 | for (StationRoute sr : srs) { |
| ... | ... | @@ -43,7 +46,13 @@ public class GeoUtils { |
| 43 | 46 | } |
| 44 | 47 | } else { |
| 45 | 48 | //多边形 |
| 46 | - if (sr.getPolygon().contains(point)) { | |
| 49 | + /*if (sr.getPolygon().contains(point)) { | |
| 50 | + stationRoute = sr; | |
| 51 | + break; | |
| 52 | + }*/ | |
| 53 | + com.bsth.util.Geo.Polygon polygon2 = GeoCacheData.convertPolygon(sr.getPolygon()); | |
| 54 | + com.bsth.util.Geo.Point point2 = new com.bsth.util.Geo.Point(gps.getLon(), gps.getLat()); | |
| 55 | + if(com.bsth.util.Geo.GeoUtils.isPointInPolygon(point2, polygon2)){ | |
| 47 | 56 | stationRoute = sr; |
| 48 | 57 | break; |
| 49 | 58 | } |
| ... | ... | @@ -125,7 +134,6 @@ public class GeoUtils { |
| 125 | 134 | * gps 是否在某个停车场内 |
| 126 | 135 | * @param gps |
| 127 | 136 | * @return |
| 128 | - */ | |
| 129 | 137 | public static String gpsInCarpark(GpsEntity gps){ |
| 130 | 138 | Point point = geometryFactory.createPoint(new Coordinate(gps.getLat(), gps.getLon())); |
| 131 | 139 | |
| ... | ... | @@ -139,6 +147,25 @@ public class GeoUtils { |
| 139 | 147 | } |
| 140 | 148 | } |
| 141 | 149 | return null; |
| 150 | + }*/ | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * gps 是否在某个停车场内 | |
| 154 | + * @param gps | |
| 155 | + * @return | |
| 156 | + */ | |
| 157 | + public static String gpsInCarpark(GpsEntity gps){ | |
| 158 | + com.bsth.util.Geo.Point point = new com.bsth.util.Geo.Point(gps.getLon(), gps.getLat()); | |
| 159 | + Map<String, com.bsth.util.Geo.Polygon> carparkMap = GeoCacheData.tccMap2; | |
| 160 | + com.bsth.util.Geo.Polygon polygon; | |
| 161 | + Set<String> codes = carparkMap.keySet(); | |
| 162 | + for(String code : codes){ | |
| 163 | + polygon = carparkMap.get(code); | |
| 164 | + if(com.bsth.util.Geo.GeoUtils.isPointInPolygon(point, polygon)){ | |
| 165 | + return code; | |
| 166 | + } | |
| 167 | + } | |
| 168 | + return null; | |
| 142 | 169 | } |
| 143 | 170 | |
| 144 | 171 | /** | ... | ... |