| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- package com.skyversation.poiaddr.controller;
- import com.skyversation.poiaddr.addquery.AddressQueryEngine;
- import com.skyversation.poiaddr.bean.AddressResult;
- import com.skyversation.poiaddr.entity.FileDataDto;
- import com.skyversation.poiaddr.service.impl.TestDataServiceImpl;
- import com.skyversation.poiaddr.util.*;
- import com.skyversation.poiaddr.util.dms.DmsTools;
- import com.skyversation.poiaddr.util.fileTools.ReadFileData;
- import com.skyversation.poiaddr.util.geotools.GeoJsonIntersector;
- import com.skyversation.poiaddr.util.geotools.GeoJsonPointInRegion;
- import com.skyversation.poiaddr.util.status.AddressLevel;
- import com.skyversation.poiaddr.util.zipunit.DataExportUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.json.simple.JSONObject;
- import org.springframework.http.MediaType;
- import org.springframework.util.StringUtils;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.util.*;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- @Slf4j
- @Validated
- @RestController
- @RequestMapping("/poiAddress")
- public class PoiAddressController {
- @Resource
- private TestDataServiceImpl testDataService;
- @Resource
- private AddressQueryEngine addressQueryEngine;
- // 地址查询
- @GetMapping(value = "/selectAddressInfo/{address}")
- public Object selectAddressInfo(@PathVariable("address") String address) {
- return RequestUtils.request(address);
- }
- @RequestMapping("/reverseGeocoding")
- public Object reverseGeocoding(HttpServletRequest request){
- String lonStr = request.getParameter("lon");
- String latStr = request.getParameter("lat");
- double lon = 0;
- double lat = 0;
- try {
- lon = Double.valueOf(lonStr);
- lat = Double.valueOf(latStr);
- } catch (Exception e){
- return MessageManage.getInstance().getResultContent(Constant.PARAM_ERROR, "参数错误", "参数错误");
- }
- if(lon == 0 || lat == 0){
- return MessageManage.getInstance().getResultContent(Constant.PARAM_ERROR, "参数错误", "参数错误");
- }
- AddressResult addressResult = AddressQueryEngine.getInstance().reverseGeocoding(lon, lat);
- if (addressResult == null || addressResult.getData() == null || addressResult.getData().size() < 1 ){
- return MessageManage.getInstance().getResultContent(Constant.NO_DATA,"无数据","未找到");
- }else {
- return MessageManage.getInstance().getAddressResultContent(Constant.SUCCESS,addressResult,"成功");
- }
- }
- @RequestMapping("/selectPoi")
- public Object selectPoi(HttpServletRequest request){
- String address = request.getParameter("address");
- if(!StringUtils.hasText(address)){
- return MessageManage.getInstance().getResultContent(Constant.PARAM_ERROR, "参数错误", "参数错误");
- }
- AddressResult addressResult = addressQueryEngine.commonSearchByName(address,AddressLevel.LEVEL_1);
- if (addressResult == null || addressResult.getData() == null || addressResult.getData().size() < 1 || addressResult.getMessage().equals("失败")){
- return MessageManage.getInstance().getResultContent(Constant.NO_DATA,"无数据","未找到");
- }else {
- return MessageManage.getInstance().getAddressResultContent(Constant.SUCCESS,addressResult,"成功");
- }
- }
- // 根据街镇名称和村居名称返回符合条件的geojson集合文件
- @PostMapping(value = "/selectGeojsonByNames", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
- public Object selectGeojsonByNames(HttpServletResponse response, @RequestParam("jdNameStr") String jdNameStr, @RequestParam(name = "czNameStr", required = false) String czNameStr) throws Exception {
- String fileName = UUID.randomUUID().toString();
- GeoJsonIntersector.run(jdNameStr, czNameStr, fileName);
- ReadFileData.outputFile("output/result_" + fileName + ".geojson", response);
- return "成功";
- }
- /**
- * (开发中)
- * 定时器:每天都全量更新法人库的地名地址数据
- * 每次分页查询每批次处理1W条数据
- * 1、获取数据库连接
- * 2、查询数据
- * 3、得到地名地址字段和值(清洗数据,并地名格式调整)
- * 4、批量多任务处理查询结果并判断网格区划
- * 5、将结果保存为ser文件
- */
- @PostMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
- public Object updateDataBaseData(@RequestParam(name = "pageSize") Integer pageSize, @RequestParam(name = "level") Integer level, @RequestParam(name = "init") Integer init) {
- // 记录程序开始时间
- long startTime = System.currentTimeMillis();
- // 批量处理数据
- if (level == null) {
- level = 1;
- }
- testDataService.iterativeProcessing(pageSize, level, init);// 记录程序结束时间
- long endTime = System.currentTimeMillis();
- return "处理完成!用时" + (endTime - startTime) / 1000 + "秒!";
- }
- /**
- * (测试中)
- * 地名查询任务接口
- * * 参数:
- * ** 必填:
- * *** file: xlsx、csv、geojson、shape
- * *** addrColNames: 地名地址字段名称数组,依次查询
- * ** 非必填:
- * *** inCoordinate: 输入坐标系(默认为wgs84)
- * *** latLonColName: 经纬度字段名称,如果长度为一,经纬度字段分隔符为必填
- * *** latLonSplitStr: 经纬度字段分隔符
- * *** matchingDistance: 匹配距离,如果不为空,经纬度字段名称不能为空
- * *** outCoordinate: 输出坐标系(默认为wgs84)
- * *** matchingLevel: 匹配等级,详情见xmind文件【1、2、3、4、5】
- * *** regionalJudgment: 区域判断,geojson、shape文件[前端可以内置一些常用的区域文件,通过下拉框的方式进行选择]
- * <p>
- * JDK版本要规定为11(已完成)回退为8(已完成)
- * 需要使用request获取参数,参数校验不能太严格。(通过拦截器的方式也实现了参数乱斗并对必传参数的非空判断以及错误提示)
- * 接口调用要使用oauth认证,添加拦截器验证headers里面的token。(已完成,暂时使用的是121的Oauth系统,用户名:sj_test)
- * 任务开始和结束要操作DMS(开始时间-创建时间,结束时间-修改时间,文件名,任务状态,备注)。根据token调用接口就可以(待测试)
- *
- * @return Object
- */
- @RequestMapping(value = "/nameQueryTaskInterface", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
- public Object GeoCoordinate(HttpServletResponse response,
- @RequestHeader(value = "token") String token,
- @RequestParam(name = "file") MultipartFile file,
- @RequestParam(name = "addrColNames") String addrColNames,
- @RequestParam(name = "inCoordinate", required = false) String inCoordinate,
- @RequestParam(name = "latLonColName", required = false) String latLonColName,
- @RequestParam(name = "latLonSplitStr", required = false) String latLonSplitStr,
- @RequestParam(name = "matchingDistance", required = false) Long matchingDistance,
- @RequestParam(name = "outCoordinate", required = false) String outCoordinate,
- @RequestParam(name = "matchingLevel", required = false) Integer matchingLevel,
- @RequestParam(name = "regionalJudgment", required = false) MultipartFile regionalJudgment,
- @RequestParam(name = "outputFileName", required = false) String outputFileName) {
- // 参数合法性判断
- if ((file != null && !file.isEmpty()) && (addrColNames != null && !addrColNames.isEmpty())) {
- // 先更新一下token
- DmsTools.DmsToken = token;
- if (outputFileName == null) {
- outputFileName = "";
- }
- outputFileName += UUID.randomUUID();
- org.json.JSONObject params = new org.json.JSONObject();
- params.put("columnId", "1542");
- params.put("modelId", "1445");
- org.json.JSONObject paramsContent = new org.json.JSONObject();
- paramsContent.put("title", outputFileName);
- paramsContent.put("content", new Date().toString());
- paramsContent.put("c_content", "任务初始化");
- // 0:任务初始化;1:开始处理;2:异常中断;3:任务完成
- paramsContent.put("c_task_status", 0);
- params.put("content", paramsContent);
- JSONObject initDmsData = DmsTools.addContent(params);
- if (initDmsData.get("message") == null || !"200".equals(initDmsData.get("code").toString())) {
- return "任务初始化失败!DMS数据插入异常,请联系管理员!";
- }
- paramsContent.put("id", initDmsData.get("content"));
- System.out.println("任务初始化:" + initDmsData.get("message"));
- try {
- // 搜索地址key
- String addr1Key = "";
- String addr2Key = "";
- // 参考经纬度key
- String latKey = "";
- String lonKey = "";
- String latLonKey = "";
- String SplitStr = "";
- paramsContent.put("c_task_status", 1);
- paramsContent.put("c_content", "开始处理");
- params.put("content", paramsContent);
- DmsTools.updateContent(params);
- // 多地址的形式
- if (addrColNames.contains(",")) {
- String[] addrs = addrColNames.split(",");
- if (addrs.length > 2) {
- return "最多支持两个地址列查询,请检查addrColNames参数!";
- }
- addr1Key = addrs[0];
- addr2Key = addrs[1];
- } else {
- addr1Key = addrColNames;
- }
- if (matchingLevel == null) {
- matchingLevel = 1;
- }
- // 匹配距离参数合法性判断
- if (matchingDistance != null) {
- if (latLonColName == null || latLonColName.isEmpty()) {
- return "当匹配距离参数不为空时,必须传入经纬度参数!";
- }
- }
- // 经纬度合法性判断
- if (latLonColName != null) {
- // 两个key
- if (latLonColName.contains(",")) {
- String[] latLons = latLonColName.split(",");
- latKey = latLons[0];
- lonKey = latLons[1];
- } else if (latLonSplitStr != null && !latLonSplitStr.isEmpty()) {
- // 单个key分割,必须要分割符参数
- latLonKey = latLonColName;
- SplitStr = latLonSplitStr;
- } else {
- return "经纬度列名解析失败,请传入经度和纬度的列名以逗号分割或传入经纬度列名和分隔符参数!";
- }
- }
- // 封装解析文件的参数
- // TODO 文件数据解析
- List<FileDataDto> fileDataDtoList = ReadFileData.ReadMultipartFile(file);
- List<FileDataDto> regionalDataList = new ArrayList<>();
- if (regionalJudgment != null && !regionalJudgment.isEmpty()) {
- // 解析geojson文件得到区域
- regionalDataList = ReadFileData.ReadMultipartFile(regionalJudgment);
- }
- // TODO 补充FileDataDto中的搜索条件参数
- for (FileDataDto fileDataDto : fileDataDtoList) {
- // 搜索等级
- fileDataDto.setMatchingLevel(matchingLevel);
- // TODO 数据过滤
- // set地名地址搜索字段
- Map<String, Object> properties = fileDataDto.getProperties();
- try {
- if (properties != null && addr1Key != null && properties.get(addr1Key) != null && properties.get(addr1Key).toString().length() > 2 && ExcelReaderUtils.isOtherDistrictThanQingpu(properties.get(addr1Key).toString())) {
- String address = "上海市青浦区" + AddressQueryEngine.addressReplaceAll(properties.getOrDefault(addr1Key, "").toString());
- fileDataDto.setAddr1(address);
- }
- if (properties != null && addr2Key != null && properties.get(addr2Key) != null && properties.get(addr2Key).toString().length() > 2 && ExcelReaderUtils.isOtherDistrictThanQingpu(properties.get(addr2Key).toString())) {
- String address = "上海市青浦区" + AddressQueryEngine.addressReplaceAll(properties.getOrDefault(addr2Key, "").toString());
- fileDataDto.setAddr2(address);
- }
- } catch (Exception e) {
- System.err.println(e);
- }
- // 判断是否有参考经纬度字段
- if (properties != null && latKey != null && !latKey.isEmpty() && lonKey != null && !lonKey.isEmpty() && properties.get(latKey) != null && properties.get(lonKey) != null) {
- String latStr = properties.get(latKey).toString();
- String lonStr = properties.get(lonKey).toString();
- fileDataDto.setLat(Double.valueOf(latStr));
- fileDataDto.setLon(Double.valueOf(lonStr));
- } else if (!latLonKey.isEmpty()) {
- String[] latLonKeys = latLonKey.split(SplitStr);
- if (properties != null && properties.get(latLonKeys[0]) != null && properties.get(latLonKeys[1]) != null) {
- String latStr = properties.get(latLonKeys[0]).toString();
- String lonStr = properties.get(latLonKeys[1]).toString();
- fileDataDto.setLat(Double.valueOf(latStr));
- fileDataDto.setLon(Double.valueOf(lonStr));
- }
- }
- }
- // TODO 地名查询
- // 创建线程池
- int threadCount = Runtime.getRuntime().availableProcessors();
- ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
- List<Future<?>> futures = new ArrayList<>();
- // 为每个元素提交任务
- for (FileDataDto fileDataDto : fileDataDtoList) {
- futures.add(executorService.submit(() -> {
- List<String> addrList = new ArrayList<>();
- if (fileDataDto.getAddr1() != null) {
- addrList.add(fileDataDto.getAddr1());
- }
- if (fileDataDto.getAddr2() != null) {
- addrList.add(fileDataDto.getAddr2());
- }
- if (addrList.size() > 0) {
- AddressResult addressResult = addressQueryEngine.commonSearchByName(addrList, AddressLevel.values()[fileDataDto.getMatchingLevel() - 1]);
- if (addressResult != null) {
- if (addressResult.getData() == null || addressResult.getData().size() < 1) {
- fileDataDto.getProperties().put("所属街道", null);
- fileDataDto.getProperties().put("所属居委", null);
- fileDataDto.getProperties().put("纬度", null);
- fileDataDto.getProperties().put("经度", null);
- fileDataDto.getProperties().put("搜索结果地址", null);
- } else {
- try {
- for (AddressResult.ContentBean contentBean : addressResult.getData()) {
- String resultAddrKey = contentBean.getAddress();
- if (resultAddrKey != null && contentBean.getLon() != null && contentBean.getLat() != null && contentBean.getCjJson() != null) {
- String lng = contentBean.getLat() + "";
- String lat = contentBean.getLon() + "";
- fileDataDto.getProperties().put("所属街道", contentBean.getCjJson().getString("所属街道"));
- fileDataDto.getProperties().put("所属居委", contentBean.getCjJson().getString("所属居委"));
- fileDataDto.getProperties().put("纬度", lng);
- fileDataDto.getProperties().put("经度", lat);
- fileDataDto.getProperties().put("搜索结果地址", resultAddrKey);
- System.out.println("成功:" + lat + "," + lng);
- break;
- } else if (resultAddrKey != null && contentBean.getLon() != null && contentBean.getLat() != null) {
- fileDataDto.getProperties().put("所属街道", null);
- fileDataDto.getProperties().put("所属居委", null);
- fileDataDto.getProperties().put("纬度", null);
- fileDataDto.getProperties().put("经度", null);
- fileDataDto.getProperties().put("搜索结果地址", null);
- }
- }
- } catch (Exception e) {
- fileDataDto.getProperties().put("所属街道", null);
- fileDataDto.getProperties().put("所属居委", null);
- fileDataDto.getProperties().put("纬度", null);
- fileDataDto.getProperties().put("经度", null);
- fileDataDto.getProperties().put("搜索结果地址", null);
- System.err.println(e);
- }
- }
- }
- } else {
- fileDataDto.getProperties().put("所属街道", null);
- fileDataDto.getProperties().put("所属居委", null);
- fileDataDto.getProperties().put("纬度", null);
- fileDataDto.getProperties().put("经度", null);
- fileDataDto.getProperties().put("搜索结果地址", null);
- }
- }));
- }
- // 等待所有任务完成
- for (Future<?> future : futures) {
- try {
- future.get();
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- }
- // 关闭线程池
- executorService.shutdown();
- // TODO 转换坐标(根据输入坐标系将参考坐标统一转换成wgs84)
- ArrayList<String> selectOptions = new ArrayList<>();
- // {"WGS84(国际通用)", "GCJ02(高德、QQ地图)", "BD09(百度地图)", "上海2000坐标系"}
- selectOptions.add("WGS84");
- selectOptions.add("GCJ02");
- selectOptions.add("BD09");
- selectOptions.add("SH2000");
- if (inCoordinate != null && !inCoordinate.isEmpty()) {
- if (!selectOptions.contains(inCoordinate)) {
- return "请传入正确的坐标系名称!可选坐标系名称:WGS84,GCJ02,BD09,SH2000";
- }
- } else {
- inCoordinate = "WGS84";
- }
- List<Map<String, Object>> dataList = new ArrayList<>();
- // TODO 距离计算和区域判断和转换坐标
- for (FileDataDto fileDataDto : fileDataDtoList) {
- if (fileDataDto.getLat() != null && fileDataDto.getLon() != null) {
- double[] lonLat = com.skyversation.poiaddr.util.Coordinate.transformationCoordinateByCoordinate(fileDataDto.getLat(), fileDataDto.getLon(), inCoordinate, outCoordinate);
- fileDataDto.setLat(lonLat[1]);
- fileDataDto.setLon(lonLat[0]);
- }
- // TODO 距离计算
- if (matchingDistance != null) {
- if (fileDataDto.getResultLat() != null && fileDataDto.getResultLon() != null && fileDataDto.getLat() != null && fileDataDto.getLon() != null) {
- double distance = com.skyversation.poiaddr.util.Coordinate.calculateDistance(fileDataDto.getResultLat(), fileDataDto.getResultLon(), fileDataDto.getLat(), fileDataDto.getLon());
- fileDataDto.setMatchingDistance(distance);
- double matchingDistanceDb = Double.parseDouble(matchingDistance.toString());
- if (distance < matchingDistanceDb) {
- fileDataDto.setLtMatchingDistance("是");
- } else {
- fileDataDto.setLtMatchingDistance("否");
- }
- }
- }
- // TODO 区域判断(得到经纬度后,要判断得到的点是否在传入的geojson面内)
- if (regionalDataList != null && regionalDataList.size() > 0) {
- // 解析geojson文件得到区域
- boolean isArea = false;
- for (FileDataDto regionlItem : regionalDataList) {
- if (GeoJsonPointInRegion.isPointInGeoJsonRegion(regionlItem.getGeometry().toString(), fileDataDto.getLat(), fileDataDto.getLon())) {
- isArea = true;
- break;
- }
- }
- fileDataDto.getProperties().put("是否在区域内", isArea ? "是" : "否");
- }
- dataList.add(fileDataDto.getProperties());
- }
- // TODO 结果输出 result[_匹配等级][_匹配距离][_区域名称][_坐标系].[文件类型]
- // 保存实体类到序列化文件
- SerializationUtils.serialize(fileDataDtoList, "output/output" + outputFileName + ".ser");
- System.out.println("处理完成!");
- paramsContent.put("c_task_status", 3);
- paramsContent.put("c_content", "处理完成!");
- paramsContent.put("c_file_name", "output/output" + outputFileName + ".ser");
- params.put("content", paramsContent);
- DmsTools.updateContent(params);
- return "文件解析完成,共" + fileDataDtoList.size() + "条数据!";
- } catch (Exception e) {
- paramsContent.put("c_task_status", 2);
- paramsContent.put("c_content", "异常中断:" + e);
- params.put("content", paramsContent);
- DmsTools.updateContent(params);
- return "异常中断:" + e;
- }
- } else {
- return "file或addrColName不能为空!";
- }
- }
- /**
- * (开发中)
- * 根据文件名和文件类型,下载对应文件类型的数据文件
- *
- * @param response
- * @param outFileType: 输出文件类型【xlsx、csv、geojson(暂不支持)、shape(暂不支持)】
- * @param fileName
- * @throws IOException
- */
- @RequestMapping(value = "/getFileByParams", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
- public void getFileByParams(HttpServletResponse response, @RequestParam(name = "outFileType", required = false) String outFileType, @RequestParam(name = "fileName", required = false) String fileName) throws IOException {
- // 从文件中反序列化读取数据
- List<FileDataDto> deserializedList = SerializationUtils.deserialize(fileName);
- List<Map<String, Object>> dataList = new ArrayList<>();
- if (deserializedList != null) {
- for (FileDataDto person : deserializedList) {
- dataList.add(person.getProperties());
- }
- }
- if (outFileType != null) {
- if (outFileType.contains("csv")) {
- outFileType = "csv";
- }
- if (outFileType.contains("xlsx")) {
- outFileType = "xlsx";
- }
- } else {
- outFileType = "csv";
- }
- DataExportUtil.exportDataToZip(dataList, fileName.replace(".ser", ""), response, outFileType);
- }
- }
|