GeoCacheData.java
16.7 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package com.bsth.data.gpsdata.arrival;
import com.bsth.data.gpsdata.GpsEntity;
import com.bsth.data.gpsdata.arrival.entity.StationRoute;
import com.bsth.data.gpsdata.arrival.entity.TimedEnableStationRoute;
import com.bsth.data.gpsdata.arrival.precondition.entity.PreconditionGeo;
import com.bsth.data.gpsdata.arrival.utils.CircleQueue;
import com.bsth.data.gpsdata.arrival.utils.StationRouteComp;
import com.google.common.base.Splitter;
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.BeanPropertyRowMapper;
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);
//每辆车缓存最后1000条gps
private static final int CACHE_SIZE = 1000;
private static Map<String, CircleQueue<GpsEntity>> gpsCacheMap = new HashMap<>();
//线路路段走向
private static ArrayListMultimap<String, LineString> sectionCacheMap;
//路段编码和名称对照
private static Map<String, String> sectionCode2Name;
//线路站点路由
private static ArrayListMultimap<String, StationRoute> stationCacheMap;
//线路前置进站围栏
public static ArrayListMultimap<String, PreconditionGeo> premiseGeoMap;
//线路_上下行_站点编码 ——> 站点
private static Map<String, StationRoute> routeCodeMap;
//停车场
public static Map<String, Polygon> tccMap;
//停车场
public static Map<String, com.bsth.util.Geo.Polygon> tccMap2;
//线路限速信息
private static Map<String, Double> speedLimitMap;
//需要定时刷新的站点路由
public static Map<String, TimedEnableStationRoute> tesMap = new HashMap<>();
@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) {
queue = new CircleQueue<>(CACHE_SIZE);
gpsCacheMap.put(gps.getNbbm(), queue);
}
queue.add(gps);
}
public static void clear(String nbbm) {
try {
CircleQueue<GpsEntity> queue = gpsCacheMap.get(nbbm);
if (queue != null)
queue.clear();
} catch (Exception e) {
logger.error("", e);
}
}
public static Map<String, String> sectionCode2NameMap(){
return sectionCode2Name;
}
public static StationRoute getRouteCode(GpsEntity gps) {
return routeCodeMap.get(gps.getLineId() + "_" + gps.getUpDown() + "_" + gps.getStopNo());
}
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 Double speedLimit(String lineCode){
return speedLimitMap.get(lineCode);
}
public static List<LineString> getLineStringList(GpsEntity gps){
return sectionCacheMap.get(gps.getLineId() + "_" + gps.getUpDown());
}
public static List<StationRoute> midwayStation(String lineCode, int directions, String sCode, String eCode) {
List<StationRoute> list = getStationRoute(lineCode, directions), rs = new ArrayList<>();
boolean flag = false;
for (StationRoute sr : list) {
if (flag)
rs.add(sr);
if (sr.getCode().equals(sCode))
flag = true;
else if (sr.getCode().equals(eCode))
break;
}
return rs;
}
public static Polygon getTccPolygon(String code) {
return tccMap.get(code);
}
GeometryFactory geometryFactory = new GeometryFactory();
public void loadData() {
loadStationRoutesData();
loadTccMapData();
loadSpeedLimit();
//加载路段信息
loadRoadsData();
//加载前置进站围栏
loadPremiseGeoData();
}
private void loadPremiseGeoData() {
ArrayListMultimap<String, PreconditionGeo> premiseGeoMapCopy = ArrayListMultimap.create();
String sql = "select * from bsth_f_geo_premise";
List<PreconditionGeo> list = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(PreconditionGeo.class));
List<String> coordList;
String[] cs;
com.bsth.util.Geo.Point point;
List<com.bsth.util.Geo.Point> ps;
StationRoute sr;
for(PreconditionGeo p : list){
try{
sr = routeCodeMap.get(p.getLineCode()+"_"+p.getUpDown()+"_"+p.getStationCode());
p.setOrder(sr.getRouteSort());
//polygon
ps = new ArrayList<>();
coordList = Splitter.on(",").trimResults().splitToList(p.getCoords());
for(String c : coordList){
cs = c.split(" ");
point = new com.bsth.util.Geo.Point(Double.parseDouble(cs[0]), Double.parseDouble(cs[1]));
ps.add(point);
}
p.setPolygon(new com.bsth.util.Geo.Polygon(ps));
sr.setPremise(true);
//按线路,走向分组
premiseGeoMapCopy.put(p.getLineCode()+"_"+p.getUpDown(), p);
}catch (Exception e){
logger.error("", e);
}
}
//排序
Set<String> ks = premiseGeoMapCopy.keySet();
PreconditionGeoComp comp = new PreconditionGeoComp();
for(String k : ks){
Collections.sort(premiseGeoMapCopy.get(k), comp);
}
premiseGeoMap = premiseGeoMapCopy;
}
private static class PreconditionGeoComp implements Comparator<PreconditionGeo>{
@Override
public int compare(PreconditionGeo p1, PreconditionGeo p2) {
return p1.getOrder() - p2.getOrder();
}
}
private void loadRoadsData() {
//加载线路下路段空间数据
String sql = "select r.LINE_CODE,r.SECTION_CODE,r.SECTIONROUTE_CODE,s.SECTION_NAME,ST_AsText(s.GSECTION_VECTOR) as GSECTION_VECTOR, r.DIRECTIONS, s.CROSES_ROAD from bsth_c_sectionroute r INNER JOIN bsth_c_section s on r.section=s.id where r.destroy=0 and GSECTION_VECTOR is not null order by line_code,directions,sectionroute_code";
List<Map<String, Object>> secList = jdbcTemplate.queryForList(sql);
String polygonStr, key;
String[] coords;
int i, len;
ArrayListMultimap<String, LineString> sectionCacheTempMap = ArrayListMultimap.create();
Coordinate[] cds;
String[] temps1, temps2;
for (Map<String, Object> tMap : secList) {
//空间数据映射
polygonStr = tMap.get("GSECTION_VECTOR").toString();
key = tMap.get("LINE_CODE") + "_" + tMap.get("DIRECTIONS");
coords = polygonStr.substring(11, polygonStr.length() - 1).split(",");
len = coords.length - 1;
//每2个点连一条线
for(i = 0; i < len; i ++){
temps1 = coords[i].split(" ");
temps2 = coords[i + 1].split(" ");
cds = new Coordinate[2];
cds[0] = new Coordinate(Float.parseFloat(temps1[1]), Float.parseFloat(temps1[0]));
cds[1] = new Coordinate(Float.parseFloat(temps2[1]), Float.parseFloat(temps2[0]));
sectionCacheTempMap.put(key, geometryFactory.createLineString(cds));
}
}
if(sectionCacheTempMap.size() > 0)
sectionCacheMap = sectionCacheTempMap;
Map<String, String> sectionCode2NameTemp = new HashMap<>();
//加载全量路段编码和名称对照
sql = "select SECTION_CODE,SECTION_NAME,CROSES_ROAD from bsth_c_section";
secList = jdbcTemplate.queryForList(sql);
String name = null, code;
for (Map<String, Object> tMap : secList) {
if(tMap.get("CROSES_ROAD") != null && StringUtils.isNotEmpty(tMap.get("CROSES_ROAD").toString()))
name = tMap.get("CROSES_ROAD").toString();
else if(tMap.get("SECTION_NAME") != null && StringUtils.isNotEmpty(tMap.get("SECTION_NAME").toString()))
name = tMap.get("SECTION_NAME").toString();
code = tMap.get("SECTION_CODE").toString();
sectionCode2NameTemp.put(code, name);
}
if(sectionCode2NameTemp.size() > 0)
sectionCode2Name = sectionCode2NameTemp;
}
private void loadTccMapData(){
//加载停车场数据
String 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;
tccMap2 = convertPolygonMap(tccMap);
}
}
private Map<String, com.bsth.util.Geo.Polygon> convertPolygonMap(Map<String, Polygon> tccMap) {
Map<String, com.bsth.util.Geo.Polygon> rsMap = new HashMap<>();
Set<String> ks = tccMap.keySet();
for(String k : ks){
rsMap.put(k, convertPolygon(tccMap.get(k)));
}
return rsMap;
}
public static com.bsth.util.Geo.Polygon convertPolygon(Polygon polygon) {
List<com.bsth.util.Geo.Point> ps = new ArrayList<>();
com.bsth.util.Geo.Point p;
Coordinate[] cs = polygon.getCoordinates();
for(int i = 0; i < cs.length;i ++){
p = new com.bsth.util.Geo.Point(cs[i].y, cs[i].x);
ps.add(p);
}
return new com.bsth.util.Geo.Polygon(ps);
}
private void loadStationRoutesData(){
//加载站点路由
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,s.STATION_NAME 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"));
sRoute.setName(rs.getString("STATION_NAME"));
String shapesType = rs.getString("SHAPES_TYPE");
//多边形电子围栏
if (StringUtils.isNotEmpty(shapesType) && shapesType.equals("d")) {
sRoute.setPolygon(geometryFactory.createPolygon(parsePolygon(rs.getString("G_POLYGON_GRID"))));
}
return sRoute;
}
});
//按线路和走向分组
if (routeList.size() > 0) {
ArrayListMultimap<String, StationRoute> tempMap = ArrayListMultimap.create();
Map<String, StationRoute> codeMap = new HashMap<>(routeList.size());
for (StationRoute sr : routeList) {
tempMap.put(sr.getLineCode() + "_" + sr.getDirections(), sr);
//站点编码 ——> 和路由顺序对照
codeMap.put(sr.getLineCode() + "_" + sr.getDirections() + "_" + sr.getCode(), sr);
}
StationRouteComp srCom = new StationRouteComp();
//连接路由
Set<String> set = tempMap.keySet();
for (String key : set) {
Collections.sort(tempMap.get(key), srCom);
connectStationRoute(tempMap.get(key));
}
//定时启用的站点走向
if(tesMap.size() > 0){
List<String> rems = new ArrayList<>();
long t = System.currentTimeMillis();
for(TimedEnableStationRoute tes : tesMap.values()){
if(tes.getEnableTime() > t){
logger.info("锁住站点路由," + tes.getLineCode());
tempMap.replaceValues(tes.getLineCode() + "_0", stationCacheMap.get(tes.getLineCode() + "_0"));
tempMap.replaceValues(tes.getLineCode() + "_1", stationCacheMap.get(tes.getLineCode() + "_1"));
}
else
rems.add(tes.getLineCode());
}
//remove
if(rems.size() > 0){
for(String lineCode : rems){
logger.info("启用路由," + lineCode);
tesMap.remove(lineCode);
}
}
}
stationCacheMap = tempMap;
routeCodeMap = codeMap;
}
}
private void loadSpeedLimit(){
//加载线路限速信息
String sql = "select l.LINE_CODE,i.SPEEDING from bsth_c_line_information i left join bsth_c_line l on i.line=l.id where i.speed_limit is not null";
List<Map<String, Object>> speedMap = jdbcTemplate.queryForList(sql);
Map<String, Double> speedTempMap = new HashMap<>();
for (Map<String, Object> tMap : speedMap) {
try {
speedTempMap.put(tMap.get("LINE_CODE").toString(), Double.parseDouble(tMap.get("SPEEDING").toString()));
} catch (NumberFormatException e) {
logger.error("speeding is null...");
}
}
speedLimitMap = speedTempMap;
}
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;
}
/**
* 是不是终点站
*
* @param lineId
* @param upDown
* @param stationCode
* @return
*/
public static boolean isEndStation(String lineId, Byte upDown, String stationCode) {
StationRoute station = routeCodeMap.get(lineId + "_" + upDown + "_" + stationCode);
return station != null && station.getMark().equals("E");
}
}