Commit 8aa8463f6da1379cf88154365d386b6f6b3f1474

Authored by 潘钊
1 parent 168352fc

更换多边形站内外算法..

src/main/java/com/bsth/data/gpsdata/arrival/GeoCacheData.java
@@ -44,6 +44,8 @@ public class GeoCacheData { @@ -44,6 +44,8 @@ public class GeoCacheData {
44 44
45 //停车场 45 //停车场
46 public static Map<String, Polygon> tccMap; 46 public static Map<String, Polygon> tccMap;
  47 + //停车场
  48 + public static Map<String, com.bsth.util.Geo.Polygon> tccMap2;
47 49
48 //线路限速信息 50 //线路限速信息
49 private static Map<String, Double> speedLimitMap; 51 private static Map<String, Double> speedLimitMap;
@@ -185,8 +187,31 @@ public class GeoCacheData { @@ -185,8 +187,31 @@ public class GeoCacheData {
185 logger.error("停车场:" + tMap.get("PARK_CODE"), e); 187 logger.error("停车场:" + tMap.get("PARK_CODE"), e);
186 } 188 }
187 } 189 }
188 - if (tccTempMap.size() > 0) 190 + if (tccTempMap.size() > 0){
189 tccMap = tccTempMap; 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 private void loadStationRoutesData(){ 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,7 +3,10 @@ package com.bsth.data.gpsdata.arrival.utils;
3 import com.bsth.data.gpsdata.GpsEntity; 3 import com.bsth.data.gpsdata.GpsEntity;
4 import com.bsth.data.gpsdata.arrival.GeoCacheData; 4 import com.bsth.data.gpsdata.arrival.GeoCacheData;
5 import com.bsth.data.gpsdata.arrival.entity.StationRoute; 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 import java.util.List; 11 import java.util.List;
9 import java.util.Map; 12 import java.util.Map;
@@ -26,7 +29,7 @@ public class GeoUtils { @@ -26,7 +29,7 @@ public class GeoUtils {
26 */ 29 */
27 public static StationRoute gpsInStation(GpsEntity gps, List<StationRoute> srs) { 30 public static StationRoute gpsInStation(GpsEntity gps, List<StationRoute> srs) {
28 Point point = geometryFactory.createPoint(new Coordinate(gps.getLat(), gps.getLon())); 31 Point point = geometryFactory.createPoint(new Coordinate(gps.getLat(), gps.getLon()));
29 - double min = -1, distance, distance2; 32 + double min = -1, distance;
30 StationRoute stationRoute = null; 33 StationRoute stationRoute = null;
31 34
32 for (StationRoute sr : srs) { 35 for (StationRoute sr : srs) {
@@ -43,7 +46,13 @@ public class GeoUtils { @@ -43,7 +46,13 @@ public class GeoUtils {
43 } 46 }
44 } else { 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 stationRoute = sr; 56 stationRoute = sr;
48 break; 57 break;
49 } 58 }
@@ -125,7 +134,6 @@ public class GeoUtils { @@ -125,7 +134,6 @@ public class GeoUtils {
125 * gps 是否在某个停车场内 134 * gps 是否在某个停车场内
126 * @param gps 135 * @param gps
127 * @return 136 * @return
128 - */  
129 public static String gpsInCarpark(GpsEntity gps){ 137 public static String gpsInCarpark(GpsEntity gps){
130 Point point = geometryFactory.createPoint(new Coordinate(gps.getLat(), gps.getLon())); 138 Point point = geometryFactory.createPoint(new Coordinate(gps.getLat(), gps.getLon()));
131 139
@@ -139,6 +147,25 @@ public class GeoUtils { @@ -139,6 +147,25 @@ public class GeoUtils {
139 } 147 }
140 } 148 }
141 return null; 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 /**