|
|
@@ -3,10 +3,20 @@ package com.wm.gis.service.impl;
|
|
3
|
3
|
import com.wm.gis.entity.GisBaseMap;
|
|
4
|
4
|
import com.wm.gis.entity.IotDevice;
|
|
5
|
5
|
import com.wm.gis.service.GisService;
|
|
|
6
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
7
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
6
|
8
|
import org.locationtech.jts.geom.Coordinate;
|
|
7
|
9
|
import org.locationtech.jts.geom.GeometryFactory;
|
|
8
|
10
|
import org.locationtech.jts.geom.Point;
|
|
|
11
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
12
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
13
|
+import org.springframework.dao.DataAccessException;
|
|
|
14
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
15
|
+import org.springframework.jdbc.core.RowMapper;
|
|
9
|
16
|
import org.springframework.stereotype.Service;
|
|
|
17
|
+import org.springframework.web.client.HttpClientErrorException;
|
|
|
18
|
+import org.springframework.web.client.RestTemplate;
|
|
|
19
|
+import org.springframework.http.*;
|
|
10
|
20
|
|
|
11
|
21
|
import java.time.LocalDateTime;
|
|
12
|
22
|
import java.util.ArrayList;
|
|
|
@@ -14,83 +24,383 @@ import java.util.List;
|
|
14
|
24
|
|
|
15
|
25
|
@Service
|
|
16
|
26
|
public class GisServiceImpl implements GisService {
|
|
17
|
|
-
|
|
|
27
|
+
|
|
|
28
|
+ @Autowired
|
|
|
29
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
30
|
+
|
|
|
31
|
+ @Autowired
|
|
|
32
|
+ private RestTemplate restTemplate;
|
|
|
33
|
+
|
|
|
34
|
+ @Value("${geoserver.url:http://localhost:8080/geoserver}")
|
|
|
35
|
+ private String geoserverUrl;
|
|
|
36
|
+
|
|
|
37
|
+ @Value("${geoserver.workspace:water_management}")
|
|
|
38
|
+ private String geoserverWorkspace;
|
|
|
39
|
+
|
|
|
40
|
+ @Value("${geoserver.username:admin}")
|
|
|
41
|
+ private String geoserverUsername;
|
|
|
42
|
+
|
|
|
43
|
+ @Value("${geoserver.password:geoserver}")
|
|
|
44
|
+ private String geoserverPassword;
|
|
|
45
|
+
|
|
18
|
46
|
private static final GeometryFactory geometryFactory = new GeometryFactory();
|
|
19
|
|
-
|
|
|
47
|
+
|
|
20
|
48
|
@Override
|
|
21
|
49
|
public List<GisBaseMap> getBaseLayers() {
|
|
22
|
50
|
List<GisBaseMap> baseMaps = new ArrayList<>();
|
|
23
|
51
|
|
|
24
|
|
- // 添加精河县基础底图
|
|
25
|
|
- GisBaseMap jingheCounty = new GisBaseMap();
|
|
26
|
|
- jingheCounty.setName("精河县基础底图");
|
|
27
|
|
- jingheCounty.setType("county");
|
|
28
|
|
- jingheCounty.setCreatedAt(LocalDateTime.now());
|
|
29
|
|
- jingheCounty.setUpdatedAt(LocalDateTime.now());
|
|
30
|
|
- baseMaps.add(jingheCounty);
|
|
31
|
|
-
|
|
32
|
|
- // 添加管网图层
|
|
33
|
|
- GisBaseMap pipeNetwork = new GisBaseMap();
|
|
34
|
|
- pipeNetwork.setName("管网数据");
|
|
35
|
|
- pipeNetwork.setType("pipe_network");
|
|
36
|
|
- pipeNetwork.setCreatedAt(LocalDateTime.now());
|
|
37
|
|
- pipeNetwork.setUpdatedAt(LocalDateTime.now());
|
|
38
|
|
- baseMaps.add(pipeNetwork);
|
|
|
52
|
+ try {
|
|
|
53
|
+ // 从数据库获取基础图层
|
|
|
54
|
+ String sql = "SELECT id, name, type, description, created_at, updated_at " +
|
|
|
55
|
+ "FROM gis_base_map WHERE is_active = true ORDER BY order_index ASC";
|
|
|
56
|
+
|
|
|
57
|
+ baseMaps = jdbcTemplate.query(sql, (rs, rowNum) -> {
|
|
|
58
|
+ GisBaseMap baseMap = new GisBaseMap();
|
|
|
59
|
+ baseMap.setId(rs.getLong("id"));
|
|
|
60
|
+ baseMap.setName(rs.getString("name"));
|
|
|
61
|
+ baseMap.setType(rs.getString("type"));
|
|
|
62
|
+ baseMap.setDescription(rs.getString("description"));
|
|
|
63
|
+ baseMap.setCreatedAt(rs.getTimestamp("created_at").toLocalDateTime());
|
|
|
64
|
+ baseMap.setUpdatedAt(rs.getTimestamp("updated_at").toLocalDateTime());
|
|
|
65
|
+ return baseMap;
|
|
|
66
|
+ });
|
|
|
67
|
+
|
|
|
68
|
+ if (baseMaps.isEmpty()) {
|
|
|
69
|
+ // 如果数据库中没有数据,创建默认图层
|
|
|
70
|
+ baseMaps.addAll(createDefaultBaseLayers());
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ // 从GeoServer验证图层状态
|
|
|
74
|
+ validateBaseLayersWithGeoServer(baseMaps);
|
|
|
75
|
+
|
|
|
76
|
+ } catch (DataAccessException e) {
|
|
|
77
|
+ throw new RuntimeException("Failed to retrieve base layers from database", e);
|
|
|
78
|
+ }
|
|
39
|
79
|
|
|
40
|
80
|
return baseMaps;
|
|
41
|
81
|
}
|
|
42
|
|
-
|
|
|
82
|
+
|
|
43
|
83
|
@Override
|
|
44
|
84
|
public GisBaseMap createBaseLayer(GisBaseMap baseMap) {
|
|
45
|
|
- baseMap.setCreatedAt(LocalDateTime.now());
|
|
46
|
|
- baseMap.setUpdatedAt(LocalDateTime.now());
|
|
47
|
|
- return baseMap;
|
|
|
85
|
+ try {
|
|
|
86
|
+ // 保存到数据库
|
|
|
87
|
+ String sql = "INSERT INTO gis_base_map (name, type, description, is_active, order_index, created_at, updated_at) " +
|
|
|
88
|
+ "VALUES (?, ?, ?, true, COALESCE((SELECT MAX(order_index) FROM gis_base_map) + 1, 1), ?, ?)";
|
|
|
89
|
+
|
|
|
90
|
+ jdbcTemplate.update(sql,
|
|
|
91
|
+ baseMap.getName(),
|
|
|
92
|
+ baseMap.getType(),
|
|
|
93
|
+ baseMap.getDescription(),
|
|
|
94
|
+ LocalDateTime.now(),
|
|
|
95
|
+ LocalDateTime.now());
|
|
|
96
|
+
|
|
|
97
|
+ // 发布到GeoServer
|
|
|
98
|
+ publishLayerToGeoServer(baseMap);
|
|
|
99
|
+
|
|
|
100
|
+ // 返回创建的图层(包含ID)
|
|
|
101
|
+ Long id = jdbcTemplate.queryForObject(
|
|
|
102
|
+ "SELECT id FROM gis_base_map WHERE name = ? ORDER BY created_at DESC LIMIT 1",
|
|
|
103
|
+ Long.class, baseMap.getName());
|
|
|
104
|
+ baseMap.setId(id);
|
|
|
105
|
+ baseMap.setCreatedAt(LocalDateTime.now());
|
|
|
106
|
+ baseMap.setUpdatedAt(LocalDateTime.now());
|
|
|
107
|
+
|
|
|
108
|
+ return baseMap;
|
|
|
109
|
+
|
|
|
110
|
+ } catch (DataAccessException e) {
|
|
|
111
|
+ throw new RuntimeException("Failed to create base layer", e);
|
|
|
112
|
+ }
|
|
48
|
113
|
}
|
|
49
|
|
-
|
|
|
114
|
+
|
|
50
|
115
|
@Override
|
|
51
|
116
|
public List<IotDevice> getDevices() {
|
|
52
|
|
- List<IotDevice> devices = new ArrayList<>();
|
|
|
117
|
+ try {
|
|
|
118
|
+ String sql = "SELECT id, device_code, name, device_type, longitude, latitude, gis_layer_name, " +
|
|
|
119
|
+ "location_geom, created_at, updated_at FROM iot_device WHERE is_active = true";
|
|
|
120
|
+
|
|
|
121
|
+ return jdbcTemplate.query(sql, new DeviceRowMapper());
|
|
|
122
|
+
|
|
|
123
|
+ } catch (DataAccessException e) {
|
|
|
124
|
+ throw new RuntimeException("Failed to retrieve devices from database", e);
|
|
|
125
|
+ }
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ @Override
|
|
|
129
|
+ public IotDevice addDevice(IotDevice device) {
|
|
|
130
|
+ try {
|
|
|
131
|
+ // 处理地理空间数据
|
|
|
132
|
+ if (device.getLocationGeom() == null && device.getLongitude() != null && device.getLatitude() != null) {
|
|
|
133
|
+ Coordinate coord = new Coordinate(device.getLongitude(), device.getLatitude());
|
|
|
134
|
+ device.setLocationGeom(geometryFactory.createPoint(coord));
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ // 插入数据库
|
|
|
138
|
+ String sql = "INSERT INTO iot_device (device_code, name, device_type, longitude, latitude, " +
|
|
|
139
|
+ "gis_layer_name, location_geom, created_at, updated_at) " +
|
|
|
140
|
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
141
|
+
|
|
|
142
|
+ jdbcTemplate.update(sql,
|
|
|
143
|
+ device.getDeviceCode(),
|
|
|
144
|
+ device.getName(),
|
|
|
145
|
+ device.getDeviceType(),
|
|
|
146
|
+ device.getLongitude(),
|
|
|
147
|
+ device.getLatitude(),
|
|
|
148
|
+ device.getGisLayerName(),
|
|
|
149
|
+ device.getLocationGeom(),
|
|
|
150
|
+ LocalDateTime.now(),
|
|
|
151
|
+ LocalDateTime.now());
|
|
|
152
|
+
|
|
|
153
|
+ // 发布设备点到GeoServer
|
|
|
154
|
+ publishDevicePointToGeoServer(device);
|
|
|
155
|
+
|
|
|
156
|
+ // 返回创建的设备(包含ID)
|
|
|
157
|
+ Long id = jdbcTemplate.queryForObject(
|
|
|
158
|
+ "SELECT id FROM iot_device WHERE device_code = ? ORDER BY created_at DESC LIMIT 1",
|
|
|
159
|
+ Long.class, device.getDeviceCode());
|
|
|
160
|
+ device.setId(id);
|
|
|
161
|
+ device.setCreatedAt(LocalDateTime.now());
|
|
|
162
|
+ device.setUpdatedAt(LocalDateTime.now());
|
|
|
163
|
+
|
|
|
164
|
+ return device;
|
|
|
165
|
+
|
|
|
166
|
+ } catch (DataAccessException e) {
|
|
|
167
|
+ throw new RuntimeException("Failed to add device", e);
|
|
|
168
|
+ }
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ private List<GisBaseMap> createDefaultBaseLayers() {
|
|
|
172
|
+ List<GisBaseMap> baseMaps = new ArrayList<>();
|
|
53
|
173
|
|
|
54
|
|
- // 模拟一些监测点位
|
|
55
|
|
- IotDevice device1 = new IotDevice();
|
|
56
|
|
- device1.setDeviceCode("SW001");
|
|
57
|
|
- device1.setName("1号水位监测点");
|
|
58
|
|
- device1.setDeviceType("SW");
|
|
59
|
|
- device1.setLongitude(44.0321);
|
|
60
|
|
- device1.setLatitude(82.8973);
|
|
61
|
|
- device1.setGisLayerName("water_level");
|
|
62
|
|
- device1.setCreatedAt(LocalDateTime.now());
|
|
63
|
|
- device1.setUpdatedAt(LocalDateTime.now());
|
|
|
174
|
+ // 精河县基础底图
|
|
|
175
|
+ GisBaseMap countyMap = new GisBaseMap();
|
|
|
176
|
+ countyMap.setName("精河县基础底图");
|
|
|
177
|
+ countyMap.setType("county");
|
|
|
178
|
+ countyMap.setDescription("精河县行政区划底图");
|
|
|
179
|
+ countyMap.setCreatedAt(LocalDateTime.now());
|
|
|
180
|
+ countyMap.setUpdatedAt(LocalDateTime.now());
|
|
64
|
181
|
|
|
65
|
|
- Coordinate coord1 = new Coordinate(44.0321, 82.8973);
|
|
66
|
|
- device1.setLocationGeom(geometryFactory.createPoint(coord1));
|
|
67
|
|
- devices.add(device1);
|
|
|
182
|
+ // 管网数据
|
|
|
183
|
+ GisBaseMap pipeNetwork = new GisBaseMap();
|
|
|
184
|
+ pipeNetwork.setName("管网数据");
|
|
|
185
|
+ pipeNetwork.setType("pipe_network");
|
|
|
186
|
+ pipeNetwork.setDescription("供水管网矢量数据");
|
|
|
187
|
+ pipeNetwork.setCreatedAt(LocalDateTime.now());
|
|
|
188
|
+ pipeNetwork.setUpdatedAt(LocalDateTime.now());
|
|
68
|
189
|
|
|
69
|
|
- IotDevice device2 = new IotDevice();
|
|
70
|
|
- device2.setDeviceCode("YL001");
|
|
71
|
|
- device2.setName("1号压力监测点");
|
|
72
|
|
- device2.setDeviceType("YL");
|
|
73
|
|
- device2.setLongitude(44.0365);
|
|
74
|
|
- device2.setLatitude(82.9058);
|
|
75
|
|
- device2.setGisLayerName("water_pressure");
|
|
76
|
|
- device2.setCreatedAt(LocalDateTime.now());
|
|
77
|
|
- device2.setUpdatedAt(LocalDateTime.now());
|
|
|
190
|
+ baseMaps.add(countyMap);
|
|
|
191
|
+ baseMaps.add(pipeNetwork);
|
|
78
|
192
|
|
|
79
|
|
- Coordinate coord2 = new Coordinate(44.0365, 82.9058);
|
|
80
|
|
- device2.setLocationGeom(geometryFactory.createPoint(coord2));
|
|
81
|
|
- devices.add(device2);
|
|
|
193
|
+ // 保存到数据库
|
|
|
194
|
+ for (GisBaseMap baseMap : baseMaps) {
|
|
|
195
|
+ createBaseLayer(baseMap);
|
|
|
196
|
+ }
|
|
82
|
197
|
|
|
83
|
|
- return devices;
|
|
|
198
|
+ return baseMaps;
|
|
84
|
199
|
}
|
|
85
|
|
-
|
|
86
|
|
- @Override
|
|
87
|
|
- public IotDevice addDevice(IotDevice device) {
|
|
88
|
|
- if (device.getLocationGeom() == null && device.getLongitude() != null && device.getLatitude() != null) {
|
|
89
|
|
- Coordinate coord = new Coordinate(device.getLongitude(), device.getLatitude());
|
|
90
|
|
- device.setLocationGeom(geometryFactory.createPoint(coord));
|
|
|
200
|
+
|
|
|
201
|
+ private void validateBaseLayersWithGeoServer(List<GisBaseMap> baseMaps) {
|
|
|
202
|
+ for (GisBaseMap baseMap : baseMaps) {
|
|
|
203
|
+ try {
|
|
|
204
|
+ String layerUrl = String.format("%s/rest/workspaces/%s/layers/%s.json",
|
|
|
205
|
+ geoserverUrl, geoserverWorkspace, baseMap.getName().toLowerCase().replace(" ", "_"));
|
|
|
206
|
+
|
|
|
207
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
208
|
+ headers.set("Authorization", "Basic " + java.util.Base64.getEncoder()
|
|
|
209
|
+ .encodeToString((geoserverUsername + ":" + geoserverPassword).getBytes()));
|
|
|
210
|
+
|
|
|
211
|
+ HttpEntity<String> entity = new HttpEntity<>(headers);
|
|
|
212
|
+
|
|
|
213
|
+ ResponseEntity<String> response = restTemplate.exchange(
|
|
|
214
|
+ layerUrl, HttpMethod.GET, entity, String.class);
|
|
|
215
|
+
|
|
|
216
|
+ if (response.getStatusCode() != HttpStatus.OK) {
|
|
|
217
|
+ // 如果图层不存在,发布它
|
|
|
218
|
+ publishLayerToGeoServer(baseMap);
|
|
|
219
|
+ }
|
|
|
220
|
+
|
|
|
221
|
+ } catch (HttpClientErrorException e) {
|
|
|
222
|
+ if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
|
|
|
223
|
+ // 图层不存在,发布它
|
|
|
224
|
+ publishLayerToGeoServer(baseMap);
|
|
|
225
|
+ } else {
|
|
|
226
|
+ throw new RuntimeException("Failed to validate layer " + baseMap.getName() + " in GeoServer", e);
|
|
|
227
|
+ }
|
|
|
228
|
+ } catch (Exception e) {
|
|
|
229
|
+ throw new RuntimeException("Failed to connect to GeoServer", e);
|
|
|
230
|
+ }
|
|
|
231
|
+ }
|
|
|
232
|
+ }
|
|
|
233
|
+
|
|
|
234
|
+ private void publishLayerToGeoServer(GisBaseMap baseMap) {
|
|
|
235
|
+ try {
|
|
|
236
|
+ String layerName = baseMap.getName().toLowerCase().replace(" ", "_");
|
|
|
237
|
+ String workspace = geoserverWorkspace;
|
|
|
238
|
+
|
|
|
239
|
+ String publishUrl = String.format("%s/rest/workspaces/%s/datastores/%s/featuretypes.json",
|
|
|
240
|
+ geoserverUrl, workspace, layerName);
|
|
|
241
|
+
|
|
|
242
|
+ // 创建SLD样式
|
|
|
243
|
+ String sldContent = createSLDStyle(baseMap);
|
|
|
244
|
+
|
|
|
245
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
246
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
247
|
+ headers.set("Authorization", "Basic " + java.util.Base64.getEncoder()
|
|
|
248
|
+ .encodeToString((geoserverUsername + ":" + geoserverPassword).getBytes()));
|
|
|
249
|
+
|
|
|
250
|
+ // 发布图层配置
|
|
|
251
|
+ String layerConfig = String.format(
|
|
|
252
|
+ "{ \"featureType\": { \"name\": \"%s\", \"title\": \"%s\", \"sldTitle\": \"%s\" } }",
|
|
|
253
|
+ layerName, baseMap.getName(), baseMap.getName());
|
|
|
254
|
+
|
|
|
255
|
+ HttpEntity<String> entity = new HttpEntity<>(layerConfig, headers);
|
|
|
256
|
+ restTemplate.postForEntity(publishUrl, entity, String.class);
|
|
|
257
|
+
|
|
|
258
|
+ // 应用SLD样式
|
|
|
259
|
+ applySLDStyle(baseMap, sldContent);
|
|
|
260
|
+
|
|
|
261
|
+ } catch (Exception e) {
|
|
|
262
|
+ throw new RuntimeException("Failed to publish layer " + baseMap.getName() + " to GeoServer", e);
|
|
|
263
|
+ }
|
|
|
264
|
+ }
|
|
|
265
|
+
|
|
|
266
|
+ private void publishDevicePointToGeoServer(IotDevice device) {
|
|
|
267
|
+ try {
|
|
|
268
|
+ String layerName = device.getGisLayerName() + "_points";
|
|
|
269
|
+ String workspace = geoserverWorkspace;
|
|
|
270
|
+
|
|
|
271
|
+ String publishUrl = String.format("%s/rest/workspaces/%s/datastores/%s/featuretypes.json",
|
|
|
272
|
+ geoserverUrl, workspace, layerName);
|
|
|
273
|
+
|
|
|
274
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
275
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
276
|
+ headers.set("Authorization", "Basic " + java.util.Base64.getEncoder()
|
|
|
277
|
+ .encodeToString((geoserverUsername + ":" + geoserverPassword).getBytes()));
|
|
|
278
|
+
|
|
|
279
|
+ String layerConfig = String.format(
|
|
|
280
|
+ "{ \"featureType\": { \"name\": \"%s\", \"title\": \"%s\", \"srs\": \"EPSG:4326\" } }",
|
|
|
281
|
+ layerName, device.getName());
|
|
|
282
|
+
|
|
|
283
|
+ HttpEntity<String> entity = new HttpEntity<>(layerConfig, headers);
|
|
|
284
|
+ restTemplate.postForEntity(publishUrl, entity, String.class);
|
|
|
285
|
+
|
|
|
286
|
+ } catch (Exception e) {
|
|
|
287
|
+ throw new RuntimeException("Failed to publish device " + device.getDeviceCode() + " to GeoServer", e);
|
|
|
288
|
+ }
|
|
|
289
|
+ }
|
|
|
290
|
+
|
|
|
291
|
+ private String createSLDStyle(GisBaseMap baseMap) {
|
|
|
292
|
+ switch (baseMap.getType()) {
|
|
|
293
|
+ case "county":
|
|
|
294
|
+ return """
|
|
|
295
|
+ <StyledLayerDescriptor version="1.0.0">
|
|
|
296
|
+ <NamedLayer>
|
|
|
297
|
+ <Name>county_boundary</Name>
|
|
|
298
|
+ <UserStyle>
|
|
|
299
|
+ <Title>精河县边界</Title>
|
|
|
300
|
+ <FeatureTypeStyle>
|
|
|
301
|
+ <Rule>
|
|
|
302
|
+ <PolygonSymbolizer>
|
|
|
303
|
+ <Fill>
|
|
|
304
|
+ <CssParameter name="fill">#FFD700</CssParameter>
|
|
|
305
|
+ </Fill>
|
|
|
306
|
+ <Stroke>
|
|
|
307
|
+ <CssParameter name="stroke">#FF8C00</CssParameter>
|
|
|
308
|
+ <CssParameter name="stroke-width">2</CssParameter>
|
|
|
309
|
+ </Stroke>
|
|
|
310
|
+ </PolygonSymbolizer>
|
|
|
311
|
+ </Rule>
|
|
|
312
|
+ </FeatureTypeStyle>
|
|
|
313
|
+ </UserStyle>
|
|
|
314
|
+ </NamedLayer>
|
|
|
315
|
+ </StyledLayerDescriptor>
|
|
|
316
|
+ """;
|
|
|
317
|
+ case "pipe_network":
|
|
|
318
|
+ return """
|
|
|
319
|
+ <StyledLayerDescriptor version="1.0.0">
|
|
|
320
|
+ <NamedLayer>
|
|
|
321
|
+ <Name>pipe_network</Name>
|
|
|
322
|
+ <UserStyle>
|
|
|
323
|
+ <Title>管网数据</Title>
|
|
|
324
|
+ <FeatureTypeStyle>
|
|
|
325
|
+ <Rule>
|
|
|
326
|
+ <LineSymbolizer>
|
|
|
327
|
+ <Stroke>
|
|
|
328
|
+ <CssParameter name="stroke">#0066CC</CssParameter>
|
|
|
329
|
+ <CssParameter name="stroke-width">3</CssParameter>
|
|
|
330
|
+ </Stroke>
|
|
|
331
|
+ </LineSymbolizer>
|
|
|
332
|
+ </Rule>
|
|
|
333
|
+ </FeatureTypeStyle>
|
|
|
334
|
+ </UserStyle>
|
|
|
335
|
+ </NamedLayer>
|
|
|
336
|
+ </StyledLayerDescriptor>
|
|
|
337
|
+ """;
|
|
|
338
|
+ default:
|
|
|
339
|
+ return """
|
|
|
340
|
+ <StyledLayerDescriptor version="1.0.0">
|
|
|
341
|
+ <NamedLayer>
|
|
|
342
|
+ <Name>default</Name>
|
|
|
343
|
+ <UserStyle>
|
|
|
344
|
+ <FeatureTypeStyle>
|
|
|
345
|
+ <Rule>
|
|
|
346
|
+ <PolygonSymbolizer>
|
|
|
347
|
+ <Fill>
|
|
|
348
|
+ <CssParameter name="fill">#66CC66</CssParameter>
|
|
|
349
|
+ </Fill>
|
|
|
350
|
+ </PolygonSymbolizer>
|
|
|
351
|
+ </Rule>
|
|
|
352
|
+ </FeatureTypeStyle>
|
|
|
353
|
+ </UserStyle>
|
|
|
354
|
+ </NamedLayer>
|
|
|
355
|
+ </StyledLayerDescriptor>
|
|
|
356
|
+ """;
|
|
|
357
|
+ }
|
|
|
358
|
+ }
|
|
|
359
|
+
|
|
|
360
|
+ private void applySLDStyle(GisBaseMap baseMap, String sldContent) {
|
|
|
361
|
+ try {
|
|
|
362
|
+ String layerName = baseMap.getName().toLowerCase().replace(" ", "_");
|
|
|
363
|
+ String workspace = geoserverWorkspace;
|
|
|
364
|
+
|
|
|
365
|
+ String styleUrl = String.format("%s/rest/workspaces/%s/styles/%s.json",
|
|
|
366
|
+ geoserverUrl, workspace, layerName);
|
|
|
367
|
+
|
|
|
368
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
369
|
+ headers.setContentType(MediaType.APPLICATION_XML);
|
|
|
370
|
+ headers.set("Authorization", "Basic " + java.util.Base64.getEncoder()
|
|
|
371
|
+ .encodeToString((geoserverUsername + ":" + geoserverPassword).getBytes()));
|
|
|
372
|
+
|
|
|
373
|
+ HttpEntity<String> entity = new HttpEntity<>(sldContent, headers);
|
|
|
374
|
+ restTemplate.exchange(styleUrl, HttpMethod.PUT, entity, String.class);
|
|
|
375
|
+
|
|
|
376
|
+ // 将样式应用到图层
|
|
|
377
|
+ String applyStyleUrl = String.format("%s/rest/layers/%s:%s/styles.json",
|
|
|
378
|
+ geoserverUrl, workspace, layerName);
|
|
|
379
|
+
|
|
|
380
|
+ String styleConfig = String.format("{\"style\": { \"name\": \"%s\" }}", layerName);
|
|
|
381
|
+ HttpEntity<String> styleEntity = new HttpEntity<>(styleConfig, headers);
|
|
|
382
|
+ restTemplate.exchange(applyStyleUrl, HttpMethod.POST, styleEntity, String.class);
|
|
|
383
|
+
|
|
|
384
|
+ } catch (Exception e) {
|
|
|
385
|
+ throw new RuntimeException("Failed to apply SLD style for layer " + baseMap.getName(), e);
|
|
|
386
|
+ }
|
|
|
387
|
+ }
|
|
|
388
|
+
|
|
|
389
|
+ private static class DeviceRowMapper implements RowMapper<IotDevice> {
|
|
|
390
|
+ @Override
|
|
|
391
|
+ public IotDevice mapRow(java.sql.ResultSet rs, int rowNum) throws java.sql.SQLException {
|
|
|
392
|
+ IotDevice device = new IotDevice();
|
|
|
393
|
+ device.setId(rs.getLong("id"));
|
|
|
394
|
+ device.setDeviceCode(rs.getString("device_code"));
|
|
|
395
|
+ device.setName(rs.getString("name"));
|
|
|
396
|
+ device.setDeviceType(rs.getString("device_type"));
|
|
|
397
|
+ device.setLongitude(rs.getDouble("longitude"));
|
|
|
398
|
+ device.setLatitude(rs.getDouble("latitude"));
|
|
|
399
|
+ device.setGisLayerName(rs.getString("gis_layer_name"));
|
|
|
400
|
+ device.setLocationGeom(rs.getObject("location_geom", org.locationtech.jts.geom.Geometry.class));
|
|
|
401
|
+ device.setCreatedAt(rs.getTimestamp("created_at").toLocalDateTime());
|
|
|
402
|
+ device.setUpdatedAt(rs.getTimestamp("updated_at").toLocalDateTime());
|
|
|
403
|
+ return device;
|
|
91
|
404
|
}
|
|
92
|
|
- device.setCreatedAt(LocalDateTime.now());
|
|
93
|
|
- device.setUpdatedAt(LocalDateTime.now());
|
|
94
|
|
- return device;
|
|
95
|
405
|
}
|
|
96
|
|
-}
|
|
|
406
|
+}
|