GeoCacheData.java
6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.bsth.data.gpsdata.analyse;
import com.bsth.data.gpsdata.GpsEntity;
import com.google.common.collect.ArrayListMultimap;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Polygon;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
/**
* Created by panzhao on 2016/12/23.
*/
@Component
public class GeoCacheData {
static Logger logger = LoggerFactory.getLogger(GeoCacheData.class);
//每辆车缓存最后200条gps
private static final int CACHE_SIZE = 200;
private static Map<String, CircleQueue<GpsEntity>> gpsCacheMap = new HashMap<>();
//线路路段走向
private static ArrayListMultimap<String, LineString> sectionCacheMap;
//线路站点路由
private static ArrayListMultimap<String, StationRoute> stationCacheMap;
//停车场
public static Map<String, Polygon> tccMap;
@Autowired
JdbcTemplate jdbcTemplate;
public static CircleQueue<GpsEntity> getGps(String nbbm) {
return gpsCacheMap.get(nbbm);
}
public static void putGps(GpsEntity gps) {
CircleQueue<GpsEntity> queue = gpsCacheMap.get(gps.getNbbm());
if (queue == null) {
//第一个点从站内开始
if(!gps.isInstation())
return;
queue = new CircleQueue<>(CACHE_SIZE);
gpsCacheMap.put(gps.getNbbm(), queue);
}
queue.add(gps);
}
public static List<StationRoute> getStationRoute(String lineCode, int directions) {
return stationCacheMap.get(lineCode + "_" + directions);
}
public static StationRoute getStation(String lineCode, int directions, String code) {
List<StationRoute> list = getStationRoute(lineCode, directions);
for (StationRoute sr : list) {
if (sr.getCode().equals(code)) {
return sr;
}
}
return null;
}
public static Polygon getTccPolygon(String code){
return tccMap.get(code);
}
public void loadData() {
final GeometryFactory geometryFactory = new GeometryFactory();
//加载站点路由
String sql = "select r.LINE_CODE,r.DIRECTIONS,r.STATION_CODE,r.STATION_MARK,s.SHAPES_TYPE,s.G_LONX,s.G_LATY,ST_AsText(s.G_POLYGON_GRID) as G_POLYGON_GRID,s.RADIUS, r.STATION_ROUTE_CODE from bsth_c_stationroute r left join bsth_c_station s on r.station=s.id where r.destroy=0 order by r.station_route_code";
List<StationRoute> routeList = jdbcTemplate.query(sql, new RowMapper<StationRoute>() {
@Override
public StationRoute mapRow(ResultSet rs, int rowNum) throws SQLException {
StationRoute sRoute = new StationRoute();
sRoute.setCode(rs.getString("STATION_CODE"));
sRoute.setLineCode(rs.getString("LINE_CODE"));
sRoute.setDirections(rs.getInt("DIRECTIONS"));
sRoute.setPoint(geometryFactory.createPoint(new Coordinate(rs.getFloat("G_LATY"), rs.getFloat("G_LONX"))));
sRoute.setRadius(rs.getFloat("RADIUS"));
sRoute.setRouteSort(rs.getInt("STATION_ROUTE_CODE"));
sRoute.setMark(rs.getString("STATION_MARK"));
String shapesType = rs.getString("SHAPES_TYPE");
//多边形电子围栏
if (StringUtils.isNotEmpty(shapesType) && shapesType.equals("d")) {
geometryFactory.createPolygon(parsePolygon(rs.getString("G_POLYGON_GRID")));
}
return sRoute;
}
});
//按线路和走向分组
if (routeList.size() > 0) {
ArrayListMultimap<String, StationRoute> tempMap = ArrayListMultimap.create();
for (StationRoute sr : routeList) {
tempMap.put(sr.getLineCode() + "_" + sr.getDirections(), sr);
}
StationRouteComp srCom = new StationRouteComp();
//连接路由
Set<String> set = tempMap.keySet();
for (String key : set) {
Collections.sort(tempMap.get(key), srCom);
connectStationRoute(tempMap.get(key));
}
stationCacheMap = tempMap;
}
//加载停车场数据
sql = "select PARK_CODE, ST_AsText(G_PARK_POINT) as G_PARK_POINT from bsth_c_car_park where park_code is not null and b_park_point is not null";
List<Map<String, Object>> tccList = jdbcTemplate.queryForList(sql);
Map<String, Polygon> tccTempMap = new HashMap<>();
Polygon polygon;
for (Map<String, Object> tMap : tccList) {
try {
polygon = geometryFactory.createPolygon(parsePolygon(tMap.get("G_PARK_POINT").toString()));
tccTempMap.put(tMap.get("PARK_CODE").toString()
, polygon);
} catch (Exception e) {
logger.error("停车场:" + tMap.get("PARK_CODE") , e);
}
}
if(tccTempMap.size() > 0)
tccMap = tccTempMap;
}
private void connectStationRoute(List<StationRoute> list) {
int size = list.size();
StationRoute sr = null;
for (int i = 0; i < size; i++) {
sr = list.get(i);
//上一个
if (i > 0)
sr.setPrve(list.get(i - 1));
//下一个
if (i < size - 1)
sr.setNext(list.get(i + 1));
}
}
public Coordinate[] parsePolygon(String polygonStr) {
String[] coords = polygonStr.substring(9, polygonStr.length() - 2).split(","), temps;
Coordinate[] cds = new Coordinate[coords.length];
int len = coords.length;
for (int i = 0; i < len; i++) {
temps = coords[i].split(" ");
cds[i] = new Coordinate(Float.parseFloat(temps[1]), Float.parseFloat(temps[0]));
}
return cds;
}
}