suerprisePlus
2024-08-28 5b14811aaeb72e828d948068e899c65877aaca9a
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
package com.yb.controller;
 
import com.alibaba.fastjson.JSON;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
import com.yb.config.R;
import com.yb.entity.ReqEntity;
import com.yb.helper.RsaHelper;
import com.yb.util.EntityHttpUtil;
 
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.WKTWriter;
import org.locationtech.jts.operation.buffer.BufferOp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
 
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
 
 
@Tag(name = "新智接口")
@RestController
@RequestMapping("/api/v1")
public class XzController {
    @Autowired
    WebSocketController webSocketController;
    @Autowired
    private EntityHttpUtil entityHttpUtil;
    @Value("${spring.geographical.canview}")
    String canview;
    @Value("${spring.geographical.entityName}")
    String entityName;
    @Value("${spring.geographical.dbid}")
    String dbid;
    @Value("${spring.geographical.layerId}")
    String layerId;
    @Value("${spring.geographical.queryEntity}")
    String queryEntity;
    @Value("${spring.geographical.publickey}")
    String publickey;
    @Value("${spring.geographical.token}")
    String token;
    private JSONObject jsonObject;
    private JSONObject dataObjject;
    private Coordinate coord;
    private Object filObj;
 
    @PostMapping("/poiMap")
    @Operation(summary = "显示地点")
    public R poiMap(@Parameter String place, @Parameter String mid) throws Exception {
        System.out.println("rec message:" + place + "  mid:" + mid);
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "poiMap");
        hash.put("mid", mid);
        ReqEntity reqEntity = new ReqEntity();
        String item = getQueryFiled(place);
        JSONObject obj = JSONObject.parseObject(item);
        reqEntity.setX(obj.getString("lon"));
        reqEntity.setY(obj.getString("lat"));
//        System.out.print(obj);
        reqEntity.setPlace(obj.getString("enti_name"));
        System.out.println("rec message:" +reqEntity);
        hash.put("poi", reqEntity);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
        return R.ok(hash);
    }
 
    @PostMapping("/aroundPoi")
    @Operation(summary = "显示地点")
    public R aroundPoi(@Parameter String place, @Parameter String mid, @Parameter String type, @Parameter int radius) throws Exception {
 
        System.out.println("rec message:" + place + "  mid:" + mid + "  type:" + type + "  radius:" + radius);
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "aroundpoi");
        hash.put("mid", mid);
        String item = getQueryFiled(place);
        JSONObject obj = JSONObject.parseObject(item);
        Double lon = Double.parseDouble(obj.getString("lon"));
        Double lat = Double.parseDouble(obj.getString("lat"));
        String wkt = getWKt(lon,lat,radius);
        String itemList = getQueryAround(type,wkt);
//        System.out.print("ces: "+itemList);
        JSONArray dataArray =JSONArray.parseArray(itemList);
      ArrayList<ReqEntity> arrayList= new ArrayList<>();
        for(int i =0;i< dataArray.size();i++ ){
 
            Object element = dataArray.get(i);
            if (element instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) element;
                ReqEntity reqEntity= new ReqEntity();
                reqEntity.setX(jsonObject.getString("lon"));
                reqEntity.setY(jsonObject.getString("lat"));
                reqEntity.setPlace(jsonObject.getString("enti_name"));
                arrayList.add(reqEntity);
            }
        }
        System.out.println("rec message:" +arrayList);
        hash.put("poi", arrayList);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
        return R.ok(hash);
    }
 
    public String getWKt(double lon, double lat, int radius) {
        // 创建一个坐标点
        Coordinate coord = new Coordinate(lon, lat);
        // 创建GeometryFactory实例,并指定坐标参考系统为WGS84
        GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
        // 使用坐标创建一个点
        Point point = geometryFactory.createPoint(coord);
        // 创建BufferOp实例,用于生成缓冲区
        BufferOp bufferOp = new BufferOp(point);
        int dis = 500;
        if (radius != 0) {
            dis = radius;
        }
        // 设置缓冲区距离为500米
        Geometry bufferedGeometry = bufferOp.getResultGeometry(dis);
        // 创建WKTWriter实例,用于将Geometry转换为WKT格式
//        WKTWriter wktWriter = new WKTWriter();
 
        // 将缓冲区Geometry转换为WKT格式
//        String wkt = wktWriter.write(bufferedGeometry);
        // 输出WKT
//        System.out.println("WKT: " + wkt);
        Envelope envelope = bufferedGeometry.getEnvelopeInternal();
        double [] wkt = new double[4];
        wkt[0]=envelope.getMinX();
        wkt[1]=envelope.getMinY();
        wkt[2]=envelope.getMaxX();
        wkt[3]=envelope.getMaxY();
        String arrayAsString = Arrays.toString(wkt);
        System.out.println("arrayAsString: " + arrayAsString);
        return arrayAsString;
    }
 
    public  String getQueryAround(String type,String wkt) throws Exception {
        String query = "sjnr  like '%" + type + "%'" + "";
        String key = getPublickey();
        String encrypt = RsaHelper.encrypt(key, query);
        HashMap formData = new HashMap<>();
        formData.put("token", token);
        formData.put("start", "1");
        formData.put("count", "0");
        formData.put("containCount", "true");
        formData.put("layerid", layerId);
        formData.put("dbid", dbid);
        formData.put("where", encrypt);
        formData.put("box", wkt);
        String jsonString = entityHttpUtil.getPostMessage(queryEntity, formData);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        JSONObject dataObjject = jsonObject.getJSONObject("data");
        JSONArray dataArray = dataObjject.getJSONArray("items");
        return dataArray.toString();
    }
    //关键字查询
    public String getQueryFiled(String place) throws Exception {
        String query = "enti_name like '%" + place + "%'" + "";
        String key = getPublickey();
        String encrypt = RsaHelper.encrypt(key, query);
        HashMap formData = new HashMap<>();
        formData.put("token", token);
        formData.put("start", "1");
        formData.put("count", "0");
        formData.put("containCount", "true");
        formData.put("layerid", layerId);
        formData.put("dbid", dbid);
        formData.put("where", encrypt);
        String jsonString = entityHttpUtil.getPostMessage(queryEntity, formData);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        JSONObject dataObjject = jsonObject.getJSONObject("data");
        JSONArray dataArray = dataObjject.getJSONArray("items");
        return dataArray.getJSONObject(0).toString();
    }
 
    //获取秘钥接口
    public String getPublickey() {
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token);
        String jsonString = entityHttpUtil.getPostMessage(publickey, map);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        return jsonObject.getString("data");
    }
 
 
    @PostMapping("/aroundPoi2")
    @Operation(summary = "周边查询")
    public R aroundPoi2(@RequestBody ReqEntity reqEntity) {
        System.out.println("rec message:" + reqEntity);
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "aroundPoi");
        hash.put("mid", "mid");
        List<ReqEntity> list = new ArrayList<>();
//        reqEntity.setX("120.9671");
//        reqEntity.setY("13.5748");
        list.add(reqEntity);
        list.add(reqEntity);
        hash.put("poi", list);
 
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
 
        return R.ok(hash);
    }
 
    /*@GetMapping("/queryPath/{start_point}/{end_point}")
    @Operation(summary = "路线查询")
    public R queryPath(@PathVariable("start_point")String start_point,@PathVariable("end_point")String end_point){
        System.out.println("rec start_point: "+start_point+"  end_point: "+end_point);
        HashMap<String,Object> hash = new HashMap<>();
 
        hash.put("func","searchPath");
        List<ReqEntity> list = new ArrayList<>();
 
        hash.put("path",list);
 
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
 
        return R.ok(hash);
    }
*/
    @PostMapping("/queryMeta")
    @Operation(summary = "元信息查询")
    public R queryMeta(@Parameter String mid) {
        System.out.println("queryMeta ");
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "queryMeta");
        hash.put("mid", mid);
        List<ReqEntity> list = new ArrayList<>();
        hash.put("meta", list);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
 
        return R.ok(hash);
    }
}