TangCheng
2025-03-01 3dada084a7f6d750d2503c12f3883f7556cc2745
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package com.smtaiserver.smtaiserver.control;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
 
import org.springframework.web.servlet.ModelAndView;
 
import com.smtservlet.util.Json;
import com.smtaiserver.smtaiserver.core.SMTAIServerApp;
import com.smtaiserver.smtaiserver.core.SMTAIServerRequest;
import com.smtaiserver.smtaiserver.database.SMTDatabase;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecord;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecords;
import com.smtaiserver.smtaiserver.gismap.SMTGisMapLayerDef;
import com.smtaiserver.smtaiserver.gismap.SMTMapOtypeDef;
import com.smtaiserver.smtaiserver.gismap.SMTMapVPropDef;
import com.smtaiserver.smtaiserver.gismap.tabledef.SMTMapTableDef;
import com.smtaiserver.smtaiserver.gismap.theme.SMTMapThemeDef;
import com.smtaiserver.smtaiserver.javaai.ast.ASTDBMap;
import com.smtservlet.core.SMTRequest;
import com.smtservlet.util.SMTJsonWriter;
import com.smtservlet.util.SMTStatic;
 
public class SMTAIMapControl 
{
    private static class VPROPFilter
    {
        public SMTMapVPropDef        _vpropDef;
        public Json                    _jsonCondtion;
        
        public VPROPFilter(SMTMapVPropDef vpropDef, Json jsonCondtion)
        {
            _vpropDef = vpropDef;
            _jsonCondtion = jsonCondtion;
        }
    }
    
    public ModelAndView getVectorTile(SMTAIServerRequest tranReq) throws Exception 
    {
        int x = tranReq.convParamToInteger("x", true);
        int y = tranReq.convParamToInteger("y", true);
        int z = tranReq.convParamToInteger("z", true);
        String layerId = tranReq.convParamToString("layer_id", true);
        
        //System.out.println(String.format("==========>layer=%s z=%d x=%d y=%d", layerId, z, x, y));
        
        SMTGisMapLayerDef mapLayerDef = SMTAIServerApp.getApp().getMapLayerDef(layerId);
        return mapLayerDef.queryVectorTileToResponse(tranReq, z, x, y);
 
    }
    
    public ModelAndView getMapLayers(SMTRequest tranReq) throws Exception 
    {
        Map<String, SMTGisMapLayerDef> map = SMTAIServerApp.getApp().getMapLayerDefMap();
        
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        
        jsonWr.beginArray("layers");
        for(SMTGisMapLayerDef layerDef : map.values())
        {
            jsonWr.beginMap(null);
            {
                jsonWr.addKeyValue("id", layerDef.getLayerId());
                jsonWr.addKeyValue("title", layerDef.getTitle());
                jsonWr.addKeyValue("group", layerDef.getGroupName());
                jsonWr.addKeyValue("icon", layerDef.getIcon());
                jsonWr.addKeyValue("min_ratio", layerDef.getMinRatio());
                jsonWr.addKeyValue("max_ratio", layerDef.getMaxRatio());
            }
            jsonWr.endMap();
        }
        jsonWr.endArray();
        
        return tranReq.returnJson(jsonWr);
    }
    
    public ModelAndView getMapVProps(SMTRequest tranReq) throws Exception 
    {
        Map<String, SMTMapOtypeDef> mapOTYPE2Def = SMTAIServerApp.getApp().getMapVPropDefMap();
        
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
    
        jsonWr.beginMap("otypes");
        for(SMTMapOtypeDef otypeDef : mapOTYPE2Def.values())
        {
            jsonWr.beginMap(otypeDef.getOTYPE());
            {
                jsonWr.addKeyValue("title", otypeDef.getTitle());
                
                jsonWr.beginMap("vprops");
                for(SMTMapVPropDef vpropDef : otypeDef.getVPropDefMap().values())
                {
                    jsonWr.beginMap(vpropDef.getVPROP());
                    {
                        jsonWr.addKeyValue("title", vpropDef.getTitle());
                        jsonWr.addKeyValue("unit", vpropDef.getUnit());
                    }
                    jsonWr.endMap();
                }
                jsonWr.endMap();
            }
            jsonWr.endMap();
        }
        jsonWr.endMap();
        
        return tranReq.returnJson(jsonWr);
    }
    
    public ModelAndView getMapVPropValues(SMTRequest tranReq) throws Exception 
    {
        String OTYPE = tranReq.convParamToString("otype", true);
        String ONAME = tranReq.convParamToString("oname", true);
        String[] VPROPS = tranReq.convParamToStringArray("vprops", true);
        Date time = tranReq.convParamToDate("time", true);
        
        Map<SMTMapTableDef, List<SMTMapVPropDef>> mapOTYPE2VPROP2Def = new HashMap<>();
        
        // 扫描属性,按照TABLE进行分类
        for(String VPROP : VPROPS)
        {
            SMTMapVPropDef vpropDef = SMTAIServerApp.getApp().getMapVPropDefMap(OTYPE, VPROP);
            SMTMapTableDef tableDef = vpropDef.getTableDef();
            
            List<SMTMapVPropDef> listVPROPDef = mapOTYPE2VPROP2Def.get(tableDef);
            if(listVPROPDef == null)
            {
                listVPROPDef = new ArrayList<>();
                mapOTYPE2VPROP2Def.put(tableDef, listVPROPDef);        
            }
            
            listVPROPDef.add(vpropDef);
        }
        
        // 按照TABLE查询数据
        try(ASTDBMap dbMap = new ASTDBMap())
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            jsonWr.beginMap("values");
            for(Entry<SMTMapTableDef, List<SMTMapVPropDef>> entryTABLEDef : mapOTYPE2VPROP2Def.entrySet())
            {
                SMTMapTableDef tabelDef = entryTABLEDef.getKey();
                
                // 生成查询SQL
                StringBuilder sbSQL = new StringBuilder();
                sbSQL.append("SELECT ONAME\n");
                for(SMTMapVPropDef vpropDef : entryTABLEDef.getValue())
                {
                    sbSQL.append("," + vpropDef.getColumnSql() + " AS " + vpropDef.getVPROP() + "\n");
                }
                sbSQL.append("FROM \n");
                tabelDef.createOnlyTimeSQL(time, sbSQL);
                sbSQL.append("\n");
                sbSQL.append("WHERE OTYPE=? AND ONAME=?");
                
                // 执行查询
                DBRecords recs = dbMap.getDatabase(tabelDef.getDataSourceId())
                        .querySQL(sbSQL.toString(), new Object[] {OTYPE, ONAME});
                if(recs.getRowCount() > 0)
                {
                    DBRecord rec = recs.getRecord(0);
                    for(SMTMapVPropDef vpropDef : entryTABLEDef.getValue())
                    {
                        String VPROP = vpropDef.getVPROP();
                        jsonWr.addKeyValue(VPROP, rec.getString(VPROP));
                    }                    
                }
            }
    
            jsonWr.endMap();
            
            return tranReq.returnJson(jsonWr);
 
        }
        
    }
    
    public ModelAndView getMapDrawStyles(SMTRequest tranReq) throws Exception
    {
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        
        try(SMTDatabase db = SMTAIServerApp.getApp().allocDatabase())
        {
            DBRecords recs = db.querySQL("SELECT * FROM ai_map.map_style_def", null);
            
            jsonWr.beginArray("values");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("id", rec.getString("style_id"));
                    jsonWr.addKeyValue("type", rec.getString("style_type"));
                    
                    String sStyleConfig = rec.getString("style_config");
                    if(!SMTStatic.isNullOrEmpty(sStyleConfig))
                        jsonWr.addKeyRaw("config", sStyleConfig);
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
        }
        
        return tranReq.returnJson(jsonWr);
    }
    
    public ModelAndView getMapVPropViewConfig(SMTRequest tranReq) throws Exception 
    {
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        
        try(SMTDatabase db = SMTAIServerApp.getApp().allocDatabase())
        {
            DBRecords recs = db.querySQL("SELECT otype, vprop_view_json FROM ai_map.map_otype_def WHERE vprop_view_json IS NOT NULL", null);
            
            jsonWr.beginMap("values");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.addKeyRaw(rec.getString("otype"), rec.getString("vprop_view_json"));
            }
            jsonWr.endMap();
        }
        
        return tranReq.returnJson(jsonWr);
    }
    
    public ModelAndView getMapThemeList(SMTRequest tranReq) throws Exception 
    {
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        
        jsonWr.beginArray("values");
        for(SMTMapThemeDef themeDef : SMTAIServerApp.getApp().getMapThemeDefMap().values())
        {
            jsonWr.beginMap(null);
            {
                jsonWr.addKeyValue("id", themeDef.getId());
                jsonWr.addKeyValue("group", themeDef.getGroup());
                jsonWr.addKeyValue("title", themeDef.getTitle());
            }
            jsonWr.endMap();
        }
        jsonWr.endArray();
        
        return tranReq.returnJson(jsonWr);
    }
 
    public ModelAndView getMapLayerGroupList(SMTRequest tranReq) throws Exception 
    {
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        
        try(SMTDatabase db = SMTAIServerApp.getApp().allocDatabase())
        {
            DBRecords recs = db.querySQL("SELECT layer_id, layer_group FROM ai_map.map_layer_def ORDER BY layer_group_order", null);
            
            jsonWr.beginArray("values");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("layer_id", rec.getString("layer_id"));
                    jsonWr.addKeyValue("group", rec.getString("layer_group"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
        }
        
        return tranReq.returnJson(jsonWr);
    }
    
    // "junction_elevation"
    public ModelAndView switchMapTheme(SMTRequest tranReq) throws Exception 
    {
        String[] themeIdList = tranReq.convParamToStringArray("theme_id", true);
        Date curTime = tranReq.convParamToDate("time", true);
        
        // 获取参数
        SMTMapThemeDef.SMTMapThemeStyleMap themeStyleMap = new SMTMapThemeDef.SMTMapThemeStyleMap();
        
        // 生成并合并style
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        try(ASTDBMap dbMap = new ASTDBMap())
        {
            for(String themeId : themeIdList)
            {
                SMTMapThemeDef themeDef = SMTAIServerApp.getApp().getMapThemeDef(themeId);
                themeDef.queryThemeStyles(dbMap, curTime, themeStyleMap);
                themeDef.legendToJson(jsonWr);
            }
        }
        
        // 将结果输出成json
        themeStyleMap.styleToJson(jsonWr);
        
        return tranReq.returnJson(jsonWr);
    }
    
    public ModelAndView searchMapElemet(SMTRequest tranReq) throws Exception 
    {
        String searchText = tranReq.convParamToString("search_text", true);
        int maxCount = tranReq.convParamToInteger("max_count", true);
        Date time = tranReq.convParamToDate("time", true);
        
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        
        int[] count = new int[] {0, maxCount};
        try(SMTDatabase db = SMTAIServerApp.getApp().allocDatabase())
        {
            jsonWr.beginArray("values");
            for(SMTGisMapLayerDef mapLayerDef : SMTAIServerApp.getApp().getMapLayerDefMap().values())
            {
                mapLayerDef.searchElement(db, time, searchText, count, jsonWr);
                if(count[0] >= count[1])
                    break;
            }
            jsonWr.endArray();
        }
        
        return tranReq.returnJson(jsonWr);
    }
    
//    {"name":"extent", "title":"搜索地理范围[minx,miny,maxx,maxy]", "required":false},
//    {"name":"max_count", "title":"最大个数", "required":true},
//    {"name":"time", "title":"要查询的时间", "required":true},
//    {"name":"otype", "title":"要查询的otype", "required":true},
//    {"name":"condtions", "title":"要查询的条件[{vprop:属性名,operator:操作(eq,ne,gt,gte,lt,lte,contains),value:查询值}]", "required":true}
    public ModelAndView advanceSearchMapElemet(SMTRequest tranReq) throws Exception 
    {
        double[] bounds = tranReq.convParamToDoubleArray("extent", false);
        int maxCount = tranReq.convParamToInteger("max_count", true);
        Date time = tranReq.convParamToDate("time", true);
        String otype = tranReq.convParamToString("otype", true);
        Json jsonConditions = tranReq.convParamToJson("condtions", true);
        
        SMTMapOtypeDef otypeDef = SMTAIServerApp.getApp().getMapOTypeDef(otype);
        
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        
        // 将属性按照表定义分组
        Map<SMTMapTableDef, Set<VPROPFilter>> mapTableDef2VpropDef = new HashMap<>();
        for(Json jsonCondition : jsonConditions.asJsonList())
        {
            String vprop = jsonCondition.getJson("vprop").asString();
            SMTMapVPropDef vpropDef = otypeDef.getVPropDef(vprop);
            SMTMapTableDef tableDef = vpropDef.getTableDef();
            
            Set<VPROPFilter> setVpropDef = mapTableDef2VpropDef.get(tableDef);
            if(setVpropDef == null)
            {
                setVpropDef = new HashSet<>();
                mapTableDef2VpropDef.put(tableDef, setVpropDef);
            }
            setVpropDef.add(new VPROPFilter(vpropDef, jsonCondition));
        }
        
        // 根据属性表获取数据
        int curCount = 0;
        jsonWr.beginArray("values");
        try(ASTDBMap dbMap = new ASTDBMap())
        {
            for(Entry<SMTMapTableDef, Set<VPROPFilter>> entryTableDef : mapTableDef2VpropDef.entrySet())
            {
                if(curCount >= maxCount)
                    break;
                
                // 生成SQL字段语句
                SMTMapTableDef tableDef = entryTableDef.getKey();
                Set<VPROPFilter> setVPropDef = entryTableDef.getValue();
                StringBuilder sbSQL = new StringBuilder();
                sbSQL.append("SELECT OTYPE,ONAME,st_asewkt(ST_Transform(SHAPE, 3857)) AS WKT\n");
                for(VPROPFilter vpropFilter : setVPropDef)
                {
                    sbSQL.append("," + vpropFilter._vpropDef.getColumnSql() + " AS " + vpropFilter._vpropDef.getVPROP() + "\n");
                }
                
                // 生成SQL的FROM语句
                sbSQL.append("\nFROM\n");
                tableDef.createOnlyTimeSQL(time, sbSQL);
                
                // 生成SQL的条件语句
                sbSQL.append("\nWHERE 1=1\n");
                
                if(bounds != null)
                {
                    // 边界矩形坐标信息:
                    // x0=0, y0=1, x1=2, y1=3
                    // x0 y0, x0 y1, x1 y1, x1 y0, x0 y0
                    // 0  1   0  3   2  3   2  1   0  1
                    sbSQL.append(String.format(
                        " AND ST_Intersects(ST_GeomFromText('POLYGON ( (%s %s, %s %s, %s %s , %s %s, %s %s))') , ST_Transform(shape, 3857))",
                        SMTStatic.toString(bounds[0]),
                        SMTStatic.toString(bounds[1]),
                        
                        SMTStatic.toString(bounds[0]),
                        SMTStatic.toString(bounds[3]),
                        
                        SMTStatic.toString(bounds[2]),
                        SMTStatic.toString(bounds[3]),
                        
                        SMTStatic.toString(bounds[2]),
                        SMTStatic.toString(bounds[1]),
                        
                        SMTStatic.toString(bounds[0]),
                        SMTStatic.toString(bounds[1])
                        ));
                }
                
                for(VPROPFilter vpropFilter : setVPropDef)
                {
                    sbSQL.append(" AND " + vpropFilter._vpropDef.getColumnSql() + " ");
                    
                    String op = vpropFilter._jsonCondtion.getJson("operate").asString();
                    String value = "'" + vpropFilter._jsonCondtion.getJson("value").asString().replace("'", "''") + "'";
                    if("eq".equals(op))
                    {
                        sbSQL.append("=" + value);
                    }
                    else if("ne".equals(op))
                    {
                        sbSQL.append("<>" + value);
                    }
                    else if("gt".equals(op))
                    {
                        sbSQL.append(">" + value);
                    }
                    else if("gte".equals(op))
                    {
                        sbSQL.append(">=" + value);
                    }
                    else if("lt".equals(op))
                    {
                        sbSQL.append("<" + value);
                    }    
                    else if("lte".equals(op))
                    {
                        sbSQL.append("<=" + value);
                    }
                    else if("contains".equals(op))
                    {
                        sbSQL.append("LIKE" + "'%" + vpropFilter._jsonCondtion.getJson("value").asString().replace("'", "''") + "%'");
                    }
                    else
                    {
                        throw new Exception("unknow op : " + op);
                    }
                }            
                
                sbSQL.append("\nLIMIT " + (maxCount - curCount) + "\n");
                
                // 查询结果集
                SMTDatabase db = dbMap.getDatabase(tableDef.getDataSourceId());
                DBRecords recs = db.querySQL(sbSQL.toString(), null);
                curCount += recs.getRowCount();
                
                // 输出结果集
                for(DBRecord rec : recs.getRecords())
                {
                    jsonWr.beginMap(null);
                    for(Entry<String, Integer> entry : rec.getFieldMap().entrySet())
                    {
                        jsonWr.addKeyValue(entry.getKey(), rec.getString(entry.getValue()));
                    }
                    jsonWr.endMap();
                }
 
            }
        }
        jsonWr.endArray();
        
        return tranReq.returnJson(jsonWr);
    }
    
    
}