SectionServiceImpl.java
10.5 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
package com.bsth.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.bsth.common.ResponseCode;
import com.bsth.entity.*;
import com.bsth.repository.*;
import com.bsth.service.SectionService;
import com.bsth.util.CoordinateConverter;
import com.bsth.util.CoordinateConverter.Location;
import com.bsth.util.CustomBeanUtils;
import com.bsth.util.Geo.Point;
import com.bsth.util.GeoConverter;
import com.bsth.util.RoadCutDoubleName;
import org.geolatte.geom.LineString;
import org.geolatte.geom.codec.Wkt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
*
* @ClassName: SectionServiceImpl(路段service业务层实现类)
*
* @Extends : BaseService
*
* @Description: TODO(路段service业务层)
*
* @Author bsth@lq
*
* @Date 2016年05月03日 上午9:21:17
*
* @Version 公交调度系统BS版 0.1
*
*/
@Service
public class SectionServiceImpl extends BaseServiceImpl<Section, Integer> implements SectionService{
@Autowired
SectionRepository sectionRepository;
@Autowired
LineRepository lineRepository;
@Autowired
LineVersionsRepository lineVersionsRepository;
@Autowired
SectionRouteRepository sectionRouteRepository;
@Autowired
LsSectionRouteRepository lsSectionRouteRepository;
@Autowired
StationRouteRepository stationRouteRepository;
@Autowired
LsStationRouteRepository lsStationRouteRepository;
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* @Description :TODO(生成双路名路段)
* @param map <lineId:线路ID; route:路段信息>
* @return
*/
@Override
@Transactional
public Map<String, Object> doubleName(Map<String, Object> map) {
Map<String, Object> resultMap = new HashMap<String, Object>();
try {
// 路段点List
List<Point> bPointsList = new ArrayList<>();
// 截取后的路段
List<Map<String, String>> sectionArrayList = new ArrayList<>();
Integer lineId = Integer.parseInt(map.get("lineId").equals("") ? "" :map.get("lineId").toString());
String lineCode = map.get("lineCode").equals("") ? "" :map.get("lineCode").toString();
Integer directions = Integer.parseInt(map.get("directions").equals("") ? "" :map.get("directions").toString());
String sectionrouteCode = map.get("sectionrouteCode").equals("") ? "" :map.get("sectionrouteCode").toString();
Integer versions = Integer.parseInt(map.get("versions").equals("") ? "" :map.get("versions").toString());
String stationRouteBegin = map.get("stationRouteBegin").equals("") ? "" :map.get("stationRouteBegin").toString();
String stationRouteFinish = map.get("stationRouteFinish").equals("") ? "" :map.get("stationRouteFinish").toString();
String routes = map.get("route").equals("") ? "" :map.get("route").toString();
if(!routes.equals("")) {
String sectionStr = "";
// 转换成JSON数组
JSONArray sectionsArray = JSONArray.parseArray(routes);
// 遍历
for(int s = 0 ;s<sectionsArray.size();s++) {
String pointsLngStr = sectionsArray.getJSONObject(s).get("lng").toString();
String pointsLatStr = sectionsArray.getJSONObject(s).get("lat").toString();
if(s == sectionsArray.size()-1){
sectionStr += pointsLngStr+","+pointsLatStr;
} else {
sectionStr += pointsLngStr+","+pointsLatStr+";";
}
}
Map<String, Object> roads = RoadCutDoubleName.start(sectionStr);
if (!roads.isEmpty()) {
List bSectionList = (List) roads.get("bSections");
List roadName = (List) roads.get("roadName");
// 路段序号
Integer routeCode = 0;
if(!sectionrouteCode.equals("")){
String sectionrouteCodeArray[] = sectionrouteCode.split("_");
routeCode = Integer.valueOf(sectionrouteCodeArray[0])+1;
}else {
routeCode = 1;
}
// 增加路段序号
for(int i = 0; i < bSectionList.size(); i++){
List<Location> bSection = new ArrayList<>();
bSectionList.get(i);
bSection = (List<Location>) bSectionList.get(i);
String sectionsBpoints = "";
// WGS线状图形坐标集合
String sectionsWJPpoints = "";
for (int j = 0; j < bSection.size(); j++) {
bSection.get(j);
Location point = bSection.get(j);
String pointsLngStr = String.valueOf(point.getLng());
String pointsLatStr = String.valueOf(point.getLat());
/** to WGS坐标 */
Location resultPoint = FromBDPointToWGSPoint(pointsLngStr,pointsLatStr);
String WGSLngStr = String.valueOf(resultPoint.getLng());
String WGSLatStr = String.valueOf(resultPoint.getLat());
if(j == 0) {
sectionsBpoints = pointsLngStr + " " + pointsLatStr;
sectionsWJPpoints = WGSLngStr + " " + WGSLatStr;
}else {
sectionsBpoints = sectionsBpoints + "," + pointsLngStr + " " + pointsLatStr;
sectionsWJPpoints = sectionsWJPpoints + "," + WGSLngStr + " " + WGSLatStr;
}
}
// 获取当前最大Id
long findLatestSectionId = sectionRepository.findLatestSectionId() + 1;
// 路段编码
String sectionCode = String.valueOf(findLatestSectionId);
// 路段ID
int sectionId = (int)findLatestSectionId;
String sectionName = roadName.get(i).toString();
// WGS坐标点集合
String gsectionVector = null;
if(!sectionsWJPpoints.equals("")) {
gsectionVector = "LINESTRING(" + sectionsWJPpoints +")";
}
// 原坐标点集合
String bsectionVectorS = null;
if(!sectionsBpoints.equals("")) {
bsectionVectorS = "LINESTRING(" + sectionsBpoints + ")";
}
sectionRepository.systemSave(sectionCode, sectionName, null, "", "", "", gsectionVector, bsectionVectorS, "", null, "", 0, 0, "", 0, "", versions, sectionId);
sectionRouteRepository.sectionUpdSectionRouteCode(lineId, directions,routeCode+i);
SectionRoute route = new SectionRoute();
Line line = lineRepository.findById(lineId).get();
Section section = sectionRepository.findById(sectionId).get();
route.setSectionrouteCode(routeCode+i);
route.setLineCode(lineCode);
route.setSectionCode(sectionCode);
route.setDirections(directions);
route.setVersions(versions);
route.setDestroy(0);
route.setLine(line);
route.setSection(section);
route.setIsRoadeSpeed(0);
sectionRouteRepository.save(route);
}
resultMap.put("status", ResponseCode.SUCCESS);
return resultMap;
} else {
resultMap.put("status", "Failure");
return resultMap;
}
}
resultMap.put("status", ResponseCode.SUCCESS);
} catch (Exception e) {
logger.error("doubleName()", e);
resultMap.put("status", ResponseCode.ERROR);
}
return resultMap;
}
@Override
public void add(Section section) {
centerLine(section);
sectionRepository.save(section);
}
@Override
public void modify(Section section) {
centerLine(section);
Section section1 = sectionRepository.findById(section.getId()).get();
CustomBeanUtils.copyPropertiesIgnoredNull(section, section1);
sectionRepository.save(section1);
}
@Override
public List<String> findDistinctSectionName() {
String query = "SELECT DISTINCT section_name FROM bsth_c_section WHERE id > 1000000";
return jdbcTemplate.queryForList(query, String.class);
}
@Override
public List<Section> findSectionByName(String sectionName) {
String query = "SELECT id,section_code,section_name,croses_road,ST_AsText(bsection_vector) bsection_vector_wkt,ew_direction,sn_direction FROM bsth_c_section WHERE id > 1000000 AND section_name = ?";
return jdbcTemplate.query(query, new Object[]{ sectionName }, BeanPropertyRowMapper.newInstance(Section.class));
}
@Override
public void translateWgs2Bd() {
String query = "SELECT id, ST_AsText(gsection_vector) gsection_vector_wkt FROM bsth_c_section";
List<Section> sections = jdbcTemplate.query(query, BeanPropertyRowMapper.newInstance(Section.class));
List<Section> sections1 = new ArrayList<>();
SectionBatchPreparedStatementSetter preparedStatementSetter = new SectionBatchPreparedStatementSetter();
preparedStatementSetter.setSections(sections1);
for (int i = 0;i < sections.size();i++) {
sections1.add(sections.get(i));
if (i != 0 && i % 2000 == 0) {
jdbcTemplate.batchUpdate("UPDATE bsth_c_section SET bsection_vector = ST_GeomFromText(?) WHERE id = ?", preparedStatementSetter);
sections1.clear();
}
}
if (sections1.size() > 0) {
jdbcTemplate.batchUpdate("UPDATE bsth_c_section SET bsection_vector = ST_GeomFromText(?) WHERE id = ?", preparedStatementSetter);
sections1.clear();
}
}
/** 百度坐标转WGS坐标 */
public Location FromBDPointToWGSPoint(String bLonx,String bLatx) {
double lng = Double.parseDouble(bLonx);
double lat = Double.parseDouble(bLatx);
Location bdLoc = CoordinateConverter.LocationMake(lng, lat);
Location location = CoordinateConverter.bd_decrypt(bdLoc);
Location WGSPoint = CoordinateConverter.transformFromGCJToWGS(location);
return WGSPoint;
}
/**
* 存在wkt 则转换wkt为geo信息
* @param section
*/
public static void centerLine(Section section) {
// 路段坐标信息
String wkt = section.getBsectionVectorWkt();
if (!StringUtils.isEmpty(wkt)) {
LineString baidu = (LineString) Wkt.fromWkt(wkt);
LineString wgs = GeoConverter.lineStringBd2wgs(wkt);
section.setBsectionVector(baidu);
section.setGsectionVector(wgs);
}
}
final static class SectionBatchPreparedStatementSetter implements BatchPreparedStatementSetter {
private List<Section> sections;
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Section section = sections.get(i);
ps.setString(1, GeoConverter.lineStringWgs2bd(section.getGsectionVectorWkt()).toString());
ps.setInt(2, section.getId());
}
@Override
public int getBatchSize() {
return sections.size();
}
public List<Section> getSections() {
return sections;
}
public void setSections(List<Section> sections) {
this.sections = sections;
}
}
}