StationServiceImpl.java
6.64 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
package com.bsth.service.impl;
import com.bsth.entity.Station;
import com.bsth.entity.search.CustomerSpecs;
import com.bsth.repository.*;
import com.bsth.service.StationService;
import com.bsth.util.CoordinateConverter;
import com.bsth.util.CoordinateConverter.Location;
import com.bsth.util.CustomBeanUtils;
import com.bsth.util.Geo.GeoUtils;
import com.bsth.util.Geo.Point;
import com.bsth.util.GeoConverter;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @ClassName: StationServiceImpl(站点service业务层实现类)
*
* @Extends : BaseService
*
* @Description: TODO(站点service业务层)
*
* @Author bsth@lq
*
* @Date 2016年05月03日 上午9:21:17
*
* @Version 公交调度系统BS版 0.1
*
*/
@Service
public class StationServiceImpl extends BaseServiceImpl<Station, Integer> implements StationService {
@Autowired
private StationRepository stationRepository;
@Autowired
private LsStationRouteRepository lsStationRouteRepository;
@Autowired
private LineRepository lineRepository;
@Autowired
private SectionRepository sectionRepository;
@Autowired
private LsSectionRouteRepository lsSectionRouteRepository;
@Autowired
LineVersionsRepository lineVersionsRepository;
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public long findLatestStationId() {
return stationRepository.findLatestStationId();
}
/**
* 新增站点
* @param station
*/
@Transactional(rollbackFor = Exception.class)
@Override
public void add(Station station) {
centerPoint(station);
stationRepository.save(station);
}
/**
* @Description :TODO(更新站点保存)
*
* @param station
*
* @return Map<String, Object> <SUCCESS ; ERROR>
*/
@Transactional(rollbackFor = Exception.class)
@Override
public void modify(Station station) {
centerPoint(station);
Station station1 = stationRepository.findById(station.getId()).get();
CustomBeanUtils.copyPropertiesIgnoredNull(station, station1);
stationRepository.save(station1);
}
/**
* @Description :TODO(根据坐标点匹配数据库中的站点)
* 将匹配站点的名称赋值到新的站点
*/
@Override
public void matchStation(List<Station> stations) {
Map<String, Object> param = new HashMap<>();
param.put("destroy_eq", 0);
List<Station> stations1 = stationRepository.findAll(new CustomerSpecs<>(param));
for (Station s : stations) {
Location location = CoordinateConverter.LocationMake(s.getCenterPointWkt());
Point point = new Point(location.getLng(), location.getLat());
double matchDistance = 60;
for (Station s1 : stations1) {
Location location1 = CoordinateConverter.LocationMake(s1.getCenterPoint().toString());
Point point1 = new Point(location1.getLng(), location1.getLat());
double distance = GeoUtils.getDistance(point, point1);
if (distance <= matchDistance) {
s.setId(s1.getId());
s.setStationCode(s1.getStationCode());
s.setStationName(s1.getStationName());
break;
}
}
}
}
@Override
public List<Station> findStationByName(String stationName) {
String query = "SELECT id,station_code,station_name,ST_AsText(center_point) center_point_wkt,pass_lines,ew_direction,sn_direction FROM bsth_c_station WHERE station_name LIKE CONCAT(?,'%')";
return jdbcTemplate.query(query, new Object[]{ stationName }, BeanPropertyRowMapper.newInstance(Station.class));
}
@Override
public void translateWgs2Bd() {
String query = "SELECT id, ST_AsText(center_point_wgs) center_point_wgs_wkt FROM bsth_c_station";
List<Station> stations = jdbcTemplate.query(query, BeanPropertyRowMapper.newInstance(Station.class));
List<Station> stations1 = new ArrayList<>();
StationBatchPreparedStatementSetter preparedStatementSetter = new StationBatchPreparedStatementSetter();
preparedStatementSetter.setStations(stations1);
for (int i = 0;i < stations.size();i++) {
stations1.add(stations.get(i));
if (i != 0 && i % 2000 == 0) {
jdbcTemplate.batchUpdate("UPDATE bsth_c_station SET b_jwpoints = ?, center_point = ST_GeomFromText(?) WHERE id = ?", preparedStatementSetter);
stations1.clear();
}
}
if (stations1.size() > 0) {
jdbcTemplate.batchUpdate("UPDATE bsth_c_station SET b_jwpoints = ?, center_point = ST_GeomFromText(?) WHERE id = ?", preparedStatementSetter);
stations1.clear();
}
}
/**
* 更新站点信息
*/
@Transactional
@Override
public void generatePassLine() {
stationRepository.generatePassLine();
}
/**
* 存在wkt 则转换wkt为geo信息
* @param station
*/
private void centerPoint(Station station) {
// 中心点坐标信息
String wkt = station.getCenterPointWkt();
if (!StringUtils.isEmpty(wkt)) {
org.geolatte.geom.Point baidu = (org.geolatte.geom.Point) Wkt.fromWkt(wkt);
org.geolatte.geom.Point wgs = GeoConverter.pointBd2wgs(wkt);
station.setCenterPoint(baidu);
station.setCenterPointWgs(wgs);
}
wkt = station.getCenterPointWgsWkt();
if (!StringUtils.isEmpty(wkt)) {
org.geolatte.geom.Point wgs = (org.geolatte.geom.Point) Wkt.fromWkt(wkt);
org.geolatte.geom.Point baidu = GeoConverter.pointWgs2bd(wkt);
station.setCenterPoint(baidu);
station.setCenterPointWgs(wgs);
}
}
final static class StationBatchPreparedStatementSetter implements BatchPreparedStatementSetter {
private List<Station> stations;
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Station station = stations.get(i);
ps.setString(1, CoordinateConverter.transformFromWGSToBD(CoordinateConverter.LocationMake(station.getCenterPointWgsWkt())).toString());
ps.setString(2, String.format("POINT(%s)", CoordinateConverter.transformFromWGSToBD(CoordinateConverter.LocationMake(station.getCenterPointWgsWkt())).toString()));
ps.setInt(3, station.getId());
}
@Override
public int getBatchSize() {
return stations.size();
}
public List<Station> getStations() {
return stations;
}
public void setStations(List<Station> stations) {
this.stations = stations;
}
}
public static void main(String[] args) {
String coordinates = "43.97675 36.19718";
System.out.println(CoordinateConverter.transformFromWGSToBD(CoordinateConverter.LocationMake(coordinates)).toString());
}
}