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 org.stringtemplate.v4.ST; import java.util.*; @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; @PostMapping("/poiMap") @Operation(summary = "显示地点") public R poiMap(@RequestParam Map allParams) throws Exception { String place = allParams.get("place"); String mid = allParams.get("mid"); System.out.println("rec poiMap:" + place + " mid:" + mid); if (place == null || mid == null) { return R.error("数据传递错误"); } HashMap 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")); reqEntity.setPlace(obj.getString("enti_name")); System.out.println("rec poiMap:" + reqEntity); hash.put("poi", reqEntity); String message = JSON.toJSONString(hash); webSocketController.sendAllMessage(message); return R.ok(hash); } @PostMapping("/aroundPoi") @Operation(summary = "周边属性查询") public R aroundPoi(@RequestParam Map allParams) throws Exception { System.out.println("rec aroundPoi:" + allParams); String place = allParams.get("place"); String mid = allParams.get("mid"); String type = allParams.get("type"); String dis = allParams.get("radius"); int radius= 0; if(dis != null){ radius= Integer.parseInt(dis); } // int radius = Integer.parseInt(); System.out.println("rec aroundPoi:" + place + " mid:" + mid + " type:" + type + " radius:" + radius); if (type == null || place == null || mid == null) { return R.error("数据传递错误"); } HashMap hash = new HashMap<>(); hash.put("func", "aroundPoi"); hash.put("mid", mid); System.out.println("rec aroundPoi:" + place + " mid:" + mid + " type:" + type + " radius:" + radius); 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 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 aroundPoi:" + arrayList); hash.put("poi", arrayList); String message = JSON.toJSONString(hash); webSocketController.sendAllMessage(message); return R.ok(hash); } @PostMapping("/queryMeta") @Operation(summary = "元信息查询") public R queryMeta(@RequestParam Map allParams) { String mid = allParams.get("mid"); System.out.println("rec queryMeta:" +" mid:" + mid); if (mid == null) { return R.error("数据传递错误"); } HashMap hash = new HashMap<>(); hash.put("func", "queryMeta"); hash.put("mid", mid); // 统计查询接口拼接 JSONArray jsonArray = new JSONArray(); HashMap staticField = new HashMap<>(); String filed = "sjnr"; staticField.put("type", "count"); staticField.put("field", "sjnr"); staticField.put("outfield", "count_" + filed); jsonArray.add(staticField); HashMap formData = getHashMap(); formData.put("statistics", jsonArray.toString()); formData.put("groupby", filed); String filedString = entityHttpUtil.getPostMessage(queryEntity, formData); System.out.println("rec queryMeta:" + filedString); JSONObject jsonObject = JSON.parseObject(filedString); JSONArray dataArray = jsonObject.getJSONArray("data"); HashMap markMap = new HashMap<>(); JSONArray markArray = new JSONArray(); String markDown = "| 数据类型 | 数据量 |\\n|------|------|\\n"; for (int i = 0; i < dataArray.size(); i++) { JSONObject dataObj = dataArray.getJSONObject(i); System.out.println("rec queryMeta:" + dataObj); String[] filedValue = new String[2]; for (String key : dataObj.keySet()) { if (key == filed) { filedValue[0] = dataObj.getString(key); } else { filedValue[1] = dataObj.getString(key); } } markDown += "| " + filedValue[0] + " | " + filedValue[1] + " |\n"; } markMap.put("markdown", markDown); markArray.add(markMap); hash.put("markdown", markArray); hash.put("content", "地理实体数据库"); System.out.println("rec queryMeta:" + hash); String message = JSON.toJSONString(hash); webSocketController.sendAllMessage(message); return R.ok(hash); } public HashMap getHashMap() { HashMap hashMap = new HashMap<>(); hashMap.put("token", token); hashMap.put("containCount", "true"); hashMap.put("layerid", layerId); hashMap.put("dbid", dbid); return hashMap; } 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 = getHashMap(); formData.put("start", "1"); formData.put("count", "0"); 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 = getHashMap(); formData.put("start", "1"); formData.put("count", "0"); 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 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 hash = new HashMap<>(); hash.put("func", "aroundPoi"); hash.put("mid", "mid"); List 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 hash = new HashMap<>(); hash.put("func","searchPath"); List list = new ArrayList<>(); hash.put("path",list); String message = JSON.toJSONString(hash); webSocketController.sendAllMessage(message); return R.ok(hash); } */ @PostMapping("/queryMeta2") @Operation(summary = "元信息查询") public R queryMeta2(@Parameter String mid) { System.out.println("queryMeta "); HashMap hash = new HashMap<>(); hash.put("func", "queryMeta"); hash.put("mid", mid); List list = new ArrayList<>(); hash.put("meta", list); String message = JSON.toJSONString(hash); webSocketController.sendAllMessage(message); return R.ok(hash); } }