Browse Source

地名查询任务接口完善

DESKTOP-6LTVLN7\Liumouren 1 month ago
parent
commit
f3a8e456ec

+ 0 - 1
src/main/java/com/skyversation/poiaddr/PoiAddrApplication.java

@@ -9,5 +9,4 @@ public class PoiAddrApplication {
     public static void main(String[] args) {
         SpringApplication.run(PoiAddrApplication.class, args);
     }
-
 }

+ 16 - 12
src/main/java/com/skyversation/poiaddr/addquery/AddressQueryEngine.java

@@ -37,8 +37,8 @@ public class AddressQueryEngine {
     private WDToken wdToken = null;
 
     public static void main(String[] args) {
-        String[] address = new String[]{"上海市青浦区未来城"};
-        AddressResult addressResult = AddressQueryEngine.getInstance().commonSearchByName(address,AddressLevel.values()[1]);
+        String[] address = new String[]{"未来城"};
+        AddressResult addressResult = AddressQueryEngine.getInstance().commonSearchByName(address, AddressLevel.values()[1]);
         System.out.println(addressResult);
     }
 
@@ -59,15 +59,19 @@ public class AddressQueryEngine {
         }
         AddressResult addressResult = new AddressResult();
         List<AddressResult.ContentBean> contentBeans = new ArrayList<>();
-        for (String addr : addrs) {
-            AddressResult.ContentBean contentBean = verificaData(wdjaSearchByName(addr), level, addr);
-            if (contentBean != null) {
-                contentBeans.add(contentBean);
-                addressResult.setData(contentBeans);
-                addressResult.setCode(AddressResultEnum.WDJA_SUCCESS);
-                addressResult.setMessage("成功");
-                return addressResult;
+        try {
+            for (String addr : addrs) {
+                AddressResult.ContentBean contentBean = verificaData(wdjaSearchByName(addr), level, addr);
+                if (contentBean != null) {
+                    contentBeans.add(contentBean);
+                    addressResult.setData(contentBeans);
+                    addressResult.setCode(AddressResultEnum.WDJA_SUCCESS);
+                    addressResult.setMessage("成功");
+                    return addressResult;
+                }
             }
+        } catch (Exception e) {
+            System.err.println("武大吉奥:日常异常" + e);
         }
         if (addressResult.getData() == null || addressResult.getData().size() == 0) {
             for (String addr : addrs) {
@@ -99,7 +103,7 @@ public class AddressQueryEngine {
                 if (contentBean != null) {
                     contentBeans.add(contentBean);
                     addressResult.setData(contentBeans);
-                    addressResult.setCode(AddressResultEnum.GD_SUCCESS);
+                    addressResult.setCode(AddressResultEnum.GDV3_SUCCESS);
                     addressResult.setMessage("成功");
                     return addressResult;
                 }
@@ -118,7 +122,7 @@ public class AddressQueryEngine {
         System.out.println("<<<<<<<<<<----------开始武大吉奥地名地址搜索------------>>>>>>>>>>>>>");
         if (wdToken == null || System.currentTimeMillis() - wdToken.getTime() > 36000000) {
             wdToken = AddressTools.getInstance().getWDToken(Constant.WD_USER_NAME, Constant.WD_USER_PWD);
-            if(wdToken == null){
+            if (wdToken == null) {
                 return null;
             }
             wdToken.setTime(System.currentTimeMillis());

+ 38 - 38
src/main/java/com/skyversation/poiaddr/bean/Village.java

@@ -1,54 +1,54 @@
 package com.skyversation.poiaddr.bean;
-
 import lombok.Data;
+import lombok.NoArgsConstructor;
 
 import java.io.Serializable;
 
+@NoArgsConstructor
 @Data
 public class Village implements Serializable {
 
-    /**
-     * cartodb_id : 367
-     * _ : 5658
-     * objectid : 370
-     * __12 : 18020
-     * __11 : 第三责任网格
-     * __10 : 1810
-     * __9 : null
-     * __8 : 0.8平方公里
-     * __7 : 白鹤镇
-     * __6 : null
-     * __5 : 青浦区
-     * __4 : 赵屯居委
-     * __3 : 181006
-     * __2 : 23年2月修改
-     * y : 2375.26864868131
-     * x : -37029.1538419498
-     * __1 : 东至赵江路、南至梅桥村、西至西大盈港、北至赵屯村
-     * __13 : 473690.980837
-     * shape_leng : 7072.837333
-     * shape_area : 473690.983617
-     */
+//        "cartodb_id": 1,
+//        "_": 1101,
+//        "__1": "5.3平方公里",
+//        "shape__length": 17928.4459939482,
+//        "cjname": "爱国村",
+//        "__3": "",
+//        "__4": "青浦区",
+//        "__5": "180529",
+//        "jz": "金泽镇",
+//        "__7": "18032",
+//        "x": "-44771.99662937",
+//        "y": "-21501.9184600481",
+//        "shape__area": 5359669.72730272,
+//        "__8": "东至新港村交界,南至练塘镇交界,西至钱盛村交界,北至莲湖村交界",
+//        "__9": "1805",
+//        "__10": "第二责任网格",
+//        "objectid": 1,
+//        "__11": 5359669.74085635,
+//        "__12": null,
+//        "__13": null,
+//        "__14": null
 
     private int cartodb_id;
-    private int _5658;
-    private int objectid;
-    private String __12;
-    private String __11;
-    private String __10;
-    private Object __9;
-    private String __8;
-    private String __7;
-    private Object __6;
+    private int __;
+    private String __1;
+    private double shape__length;
+    private String cjname;
     private String __5;
     private String __4;
     private String __3;
-    private String __2;
+    private String jz;
+    private String __7;
     private String y;
     private String x;
-    private String __1;
-    private double __13;
-    private double shape_leng;
-    private double shape_area;
-
+    private double shape__area;
+    private String __10;
+    private String __9;
+    private String __8;
+    private int objectid;
+    private Object __12;
+    private double __11;
+    private Object __14;
+    private Object __13;
 }

+ 91 - 57
src/main/java/com/skyversation/poiaddr/controller/PoiAddressController.java

@@ -8,8 +8,10 @@ import com.skyversation.poiaddr.bean.AddressResult;
 import com.skyversation.poiaddr.entity.FileDataDto;
 import com.skyversation.poiaddr.entity.PoiAddress;
 import com.skyversation.poiaddr.service.PoiAddressService;
+import com.skyversation.poiaddr.util.CsvWriter;
 import com.skyversation.poiaddr.util.ExcelReaderUtils;
 import com.skyversation.poiaddr.util.RequestUtils;
+import com.skyversation.poiaddr.util.SerializationUtils;
 import com.skyversation.poiaddr.util.fileTools.ReadFileData;
 import com.skyversation.poiaddr.util.net.AddressNetTools;
 import com.skyversation.poiaddr.util.status.AddressLevel;
@@ -28,6 +30,10 @@ import javax.annotation.Resource;
 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
@@ -97,7 +103,7 @@ public class PoiAddressController {
                                 @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) String matchingLevel,
+                                @RequestParam(name = "matchingLevel", required = false) Integer matchingLevel,
                                 @RequestParam(name = "regionalJudgment", required = false) MultipartFile regionalJudgment,
                                 @RequestParam(name = "outFileType", required = false) String outFileType,
                                 @RequestParam(name = "outputFileName", required = false) String outputFileName) throws IOException {
@@ -123,8 +129,8 @@ public class PoiAddressController {
                 addr1Key = addrColNames;
             }
 
-            if (matchingLevel == null || matchingLevel.isEmpty()) {
-                return "请传入搜索等级参数,matchingLevel参数不能为空!";
+            if (matchingLevel == null) {
+                matchingLevel = 1;
             }
 //          匹配距离参数合法性判断
             if (matchingDistance != null) {
@@ -132,15 +138,15 @@ public class PoiAddressController {
                     return "当匹配距离参数不为空时,必须传入经纬度参数!";
                 }
             }
-            if(outFileType != null){
-                if(outFileType.contains("csv")){
+            if (outFileType != null) {
+                if (outFileType.contains("csv")) {
                     outFileType = "csv";
                 }
-                if(outFileType.contains("xlsx")){
+                if (outFileType.contains("xlsx")) {
                     outFileType = "xlsx";
                 }
-            }else{
-                outFileType = "csv";
+            } else {
+                outFileType = "xlsx";
             }
 //          经纬度合法性判断
             if (latLonColName != null) {
@@ -160,11 +166,8 @@ public class PoiAddressController {
 //          封装解析文件的参数
 //          TODO 文件数据解析
             List<FileDataDto> fileDataDtoList = ReadFileData.ReadMultipartFile(file);
-            int index = 0;
 //          TODO 补充FileDataDto中的搜索条件参数
             for (FileDataDto fileDataDto : fileDataDtoList) {
-                index++;
-                System.out.println("处理进度:" + index + "/" + fileDataDtoList.size());
 //              搜索等级
                 fileDataDto.setMatchingLevel(matchingLevel);
 //              TODO 数据过滤
@@ -199,57 +202,75 @@ public class PoiAddressController {
                         fileDataDto.setLon(Double.valueOf(lonStr));
                     }
                 }
-//              TODO 地名查询
-                if ((fileDataDto.getAddr1() != null || fileDataDto.getAddr2() != null) && (fileDataDto.getProperties().get("户籍街镇") == null || fileDataDto.getProperties().get("户籍街镇").toString().trim().isEmpty() || "".equals(fileDataDto.getProperties().get("户籍街镇").toString()))) {
-                    System.out.println("开始搜索:" + index + "/" + fileDataDtoList.size());
-                    AddressResult addressResult = AddressQueryEngine.getInstance().commonSearchByName(new String[]{fileDataDto.getAddr1().isEmpty() ? fileDataDto.getAddr2() : fileDataDto.getAddr1()}, AddressLevel.values()[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.getAdname() != null && contentBean.getCjJson() != null) {
-                                        String lng = contentBean.getLat() + "";
-                                        String lat = contentBean.getLon() + "";
-                                        fileDataDto.getProperties().put("户籍街镇", contentBean.getAdname());
-                                        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 {
-                                        fileDataDto.getProperties().put("户籍街镇", null);
-                                        fileDataDto.getProperties().put("户籍居委", null);
-                                        fileDataDto.getProperties().put("纬度", null);
-                                        fileDataDto.getProperties().put("经度", null);
-                                        fileDataDto.getProperties().put("搜索结果地址", null);
-                                    }
-                                }
-                            } catch (Exception e) {
+            }
+//          TODO 地名查询
+            // 创建线程池
+            int threadCount = Runtime.getRuntime().availableProcessors();
+            ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
+            List<Future<?>> futures = new ArrayList<>();
+            // 为每个元素提交任务
+            for (FileDataDto fileDataDto : fileDataDtoList) {
+                futures.add(executorService.submit(() -> {
+                    if (fileDataDto.getAddr1() != null || fileDataDto.getAddr2() != null) {
+                        AddressResult addressResult = AddressQueryEngine.getInstance().commonSearchByName(new String[]{fileDataDto.getAddr1().isEmpty() ? fileDataDto.getAddr2() : fileDataDto.getAddr1()}, 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);
-                                System.err.println(e);
+                            } 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);
                     }
-                } 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坐标系"}
@@ -265,7 +286,7 @@ public class PoiAddressController {
                 inCoordinate = "WGS84";
             }
             List<Map<String, Object>> dataList = new ArrayList<>();
-            // TODO 距离计算和区域判断
+            // 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);
@@ -291,15 +312,28 @@ public class PoiAddressController {
                 }
                 dataList.add(fileDataDto.getProperties());
             }
-//          TODO 转换坐标
 //          TODO 结果输出 result[_匹配等级][_匹配距离][_区域名称][_坐标系].[文件类型]
-            switch (outFileType){
+//            保存实体类到序列化文件
+            SerializationUtils.serialize(fileDataDtoList, "output/output" + outputFileName + ".ser");
+            /*switch (outFileType) {
                 case "xlsx":
-                    ExcelReaderUtils.writeToExcel(dataList, "C:\\Users\\Liumouren\\Desktop\\output" + outputFileName + ".xlsx");
+                    ExcelReaderUtils.writeToExcel(dataList, "output/output" + outputFileName + ".xlsx");
+                    ReadFileData.outputFile("output/output" + outputFileName + ".xlsx", response);
                     break;
                 case "csv":
+                    CsvWriter.writeToCsv(dataList, "output/output" + outputFileName + ".csv");
+                    ReadFileData.outputFile("output/output" + outputFileName + ".csv", response);
                     break;
-            }
+                case "geojson":
+                    break;
+            }*/
+            // 从文件中反序列化读取数据
+            /*List<FileDataDto> deserializedList = SerializationUtils.deserialize("output/output" + outputFileName + ".ser");
+            if (deserializedList != null) {
+                for (FileDataDto person : deserializedList) {
+                    System.out.println(person);
+                }
+            }*/
             System.out.println("处理完成!");
             return "文件解析完成,共" + fileDataDtoList.size() + "条数据!";
         } else {

+ 1 - 1
src/main/java/com/skyversation/poiaddr/entity/FileDataDto.java

@@ -28,7 +28,7 @@ public class FileDataDto implements Serializable {
 //  参考纬度(可能为空)
     private Double lon;
 //  匹配等级
-    private String matchingLevel;
+    private Integer matchingLevel;
 //  匹配距离(可能为空)
     private double matchingDistance;
 //  是否小于匹配距离(可能为空)

+ 11 - 11
src/main/java/com/skyversation/poiaddr/service/AreaService.java

@@ -90,17 +90,17 @@ public class AreaService {
                     GeoJsonBean geoJsonBean = new GeoJsonBean();
                     if(file.getName().contains("cunju")){
                         JSONObject resultProp = new JSONObject();
-                        resultProp.put("网格名称", properties.getString("__11"));
-                        resultProp.put("shape_leng", properties.getString("shape_leng"));
-                        resultProp.put("范围信息", properties.getString("__1"));
-                        resultProp.put("村居代码", properties.getString("__3"));
-                        resultProp.put("街道代码", properties.getString("__10"));
-                        resultProp.put("更新时间", properties.getString("__2"));
-                        resultProp.put("shape_area", properties.getString("shape_area"));
-                        resultProp.put("所属区县", properties.getString("__5"));
-                        resultProp.put("所属居委", properties.getString("__4"));
-                        resultProp.put("所属街道", properties.getString("__7"));
-                        resultProp.put("面积", properties.getString("__8"));
+                        resultProp.put("网格名称", properties.getString("__10"));
+                        resultProp.put("shape_leng", properties.getString("shape__length"));
+                        resultProp.put("范围信息", properties.getString("__8"));
+                        resultProp.put("村居代码", properties.getString("__5"));
+                        resultProp.put("街道代码", properties.getString("__9"));
+//                        resultProp.put("更新时间", properties.getString("__2"));
+                        resultProp.put("shape_area", properties.getString("shape__area"));
+                        resultProp.put("所属区县", properties.getString("__4"));
+                        resultProp.put("所属居委", properties.getString("cjname"));
+                        resultProp.put("所属街道", properties.getString("jz"));
+                        resultProp.put("面积", properties.getString("__1"));
                         resultProp.put("objectid", properties.getString("objectid"));
                         geoJsonBean.setProperties(resultProp);
                     } else if(file.getName().contains("微网格")){

+ 93 - 0
src/main/java/com/skyversation/poiaddr/util/CsvWriter.java

@@ -0,0 +1,93 @@
+package com.skyversation.poiaddr.util;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.*;
+
+public class CsvWriter {
+    /**
+     * 将 List<Map<String, Object>> 类型的数据写入 CSV 文件
+     * @param data 要写入的数据
+     * @param filePath CSV 文件的保存路径
+     * @throws IOException 如果写入文件时发生 I/O 错误
+     */
+    public static void writeToCsv(List<Map<String, Object>> data, String filePath) throws IOException {
+        if (data == null || data.isEmpty()) {
+            return;
+        }
+
+        // 获取所有的列名
+        Set<String> headers = new LinkedHashSet<>();
+        for (Map<String, Object> row : data) {
+            headers.addAll(row.keySet());
+        }
+
+        // 将列名写入 CSV 文件
+        try (FileWriter writer = new FileWriter(filePath)) {
+            writeLine(writer, new ArrayList<>(headers));
+
+            // 遍历数据,将每一行写入 CSV 文件
+            for (Map<String, Object> row : data) {
+                List<String> values = new ArrayList<>();
+                for (String header : headers) {
+                    Object value = row.get(header);
+                    values.add(value != null ? value.toString() : "");
+                }
+                writeLine(writer, values);
+            }
+        }
+    }
+
+    /**
+     * 将一行数据写入 CSV 文件
+     * @param writer 文件写入器
+     * @param values 一行数据的值
+     * @throws IOException 如果写入文件时发生 I/O 错误
+     */
+    private static void writeLine(FileWriter writer, List<String> values) throws IOException {
+        boolean firstValue = true;
+        for (String value : values) {
+            if (!firstValue) {
+                writer.append(",");
+            }
+            writer.append(escapeCsv(value));
+            firstValue = false;
+        }
+        writer.append("\n");
+    }
+
+    /**
+     * 转义 CSV 中的特殊字符
+     * @param value 要转义的值
+     * @return 转义后的字符串
+     */
+    private static String escapeCsv(String value) {
+        if (value == null) {
+            return "";
+        }
+        boolean needsQuotes = value.contains(",") || value.contains("\"") || value.contains("\n");
+        if (needsQuotes) {
+            value = value.replace("\"", "\"\"");
+            return "\"" + value + "\"";
+        }
+        return value;
+    }
+
+    public static void main(String[] args) {
+        List<Map<String, Object>> data = new ArrayList<>();
+        Map<String,Object> item = new HashMap<>();
+        item.put("列名1","test00");
+        item.put("列名3","test02");
+        Map<String,Object> item2 = new HashMap<>();
+        item2.put("列名2","test11");
+        item2.put("列名3","test12");
+        data.add(item);
+        data.add(item2);
+        // 这里可以添加测试数据
+        try {
+            writeToCsv(data, "C:\\Users\\Liumouren\\Desktop\\outputTest.csv");
+            System.out.println("CSV 文件写入成功!");
+        } catch (IOException e) {
+            System.err.println("写入 CSV 文件时发生错误:" + e.getMessage());
+        }
+    }
+}

+ 52 - 0
src/main/java/com/skyversation/poiaddr/util/SerializationUtils.java

@@ -0,0 +1,52 @@
+package com.skyversation.poiaddr.util;
+import com.skyversation.poiaddr.entity.FileDataDto;
+
+import java.io.*;
+import java.util.List;
+
+public class SerializationUtils {
+
+    /**
+     * 将 List<T> 序列化并保存到本地文件
+     * @param list 要序列化的 List
+     * @param filePath 保存文件的路径
+     * @param <T> 泛型类型
+     */
+    public static <T extends Serializable> void serialize(List<T> list, String filePath) {
+        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
+            oos.writeObject(list);
+            System.out.println("数据已成功序列化并保存到 " + filePath);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 从本地文件中反序列化读取 List<T>
+     * @param filePath 读取文件的路径
+     * @param <T> 泛型类型
+     * @return 反序列化后的 List
+     */
+    @SuppressWarnings("unchecked")
+    public static <T extends Serializable> List<T> deserialize(String filePath) {
+        List<T> list = null;
+        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
+            list = (List<T>) ois.readObject();
+            System.out.println("数据已成功从 " + filePath + " 反序列化");
+        } catch (IOException | ClassNotFoundException e) {
+            e.printStackTrace();
+        }
+        return list;
+    }
+
+    public static void main(String[] args) {
+        // 从文件中反序列化读取数据
+        List<FileDataDto> deserializedList = SerializationUtils.deserialize("output/output匹配户籍地址.ser");
+
+        if (deserializedList != null) {
+            for (FileDataDto person : deserializedList) {
+                System.out.println(person);
+            }
+        }
+    }
+}

+ 0 - 2
src/main/java/com/skyversation/poiaddr/util/dms/login.java

@@ -1,7 +1,5 @@
 package com.skyversation.poiaddr.util.dms;
 
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import com.skyversation.poiaddr.util.RequestUtils;
 import org.json.JSONArray;
 import org.json.simple.JSONObject;

+ 49 - 19
src/main/java/com/skyversation/poiaddr/util/fileTools/ReadFileData.java

@@ -10,10 +10,8 @@ import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.util.IOUtils;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileReader;
-import java.io.IOException;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -27,7 +25,7 @@ import java.util.Map;
  * 文件内容解析
  */
 public class ReadFileData {
-//    文件内容解析
+    //    文件内容解析
     public static List<FileDataDto> ReadMultipartFile(MultipartFile multipartFile) throws IOException {
         List<FileDataDto> fileDataDtoList = new ArrayList<>();
 //      首先判断文件类型,走不同的解析方法
@@ -50,8 +48,9 @@ public class ReadFileData {
     public static List<FileDataDto> readGeojson(File file) throws IOException {
         List<FileDataDto> resultList = new ArrayList<>();
         ObjectMapper mapper = new ObjectMapper();
-        List<FeatureDiy> features = mapper.readValue(file, new TypeReference<>() {});
-        for(FeatureDiy featureDiy:features){
+        List<FeatureDiy> features = mapper.readValue(file, new TypeReference<>() {
+        });
+        for (FeatureDiy featureDiy : features) {
             FileDataDto fileDataDto = new FileDataDto();
             fileDataDto.setGeometry(featureDiy.getGeometry());
             fileDataDto.setProperties(featureDiy.getProperties());
@@ -60,8 +59,8 @@ public class ReadFileData {
         return resultList;
     }
 
-//    csv文件内容解析
-    public static List<FileDataDto> readCsv(File file){
+    //    csv文件内容解析
+    public static List<FileDataDto> readCsv(File file) {
         List<FileDataDto> resultList = new ArrayList<>();
         try {
             // 读取CSV文件
@@ -72,16 +71,16 @@ public class ReadFileData {
             int index = 0;
             for (String[] row : csvData) {
                 int rowSize = row.length;
-                if(index == 0){
+                if (index == 0) {
                     headers = new String[rowSize];
                     for (int i = 0; i < rowSize; i++) {
                         headers[i] = row[i];
                     }
-                }else{
-                    FileDataDto fileDataDto= new FileDataDto();
-                    Map<String,Object> colData = new HashMap<>();
+                } else {
+                    FileDataDto fileDataDto = new FileDataDto();
+                    Map<String, Object> colData = new HashMap<>();
                     for (int i = 0; i < rowSize; i++) {
-                        colData.put(headers[i],row[i]);
+                        colData.put(headers[i], row[i]);
                     }
                     fileDataDto.setProperties(colData);
                     resultList.add(fileDataDto);
@@ -115,9 +114,12 @@ public class ReadFileData {
             for (int cellIndex = 0; cellIndex < headerSize; cellIndex++) {
                 Cell headerCell = headerRow.getCell(cellIndex);
                 Cell currentCell = currentRow.getCell(cellIndex);
-                String headerValue = getCellValue(headerCell).toString();
-                Object currentValue = getCellValue(currentCell);
-                rowMap.put(headerValue, currentValue);
+                if(currentCell != null){
+                    currentCell.setCellType(CellType.STRING);
+                    rowMap.put(getCellValue(headerCell).toString(), getCellValue(currentCell).toString());
+                }else{
+                    rowMap.put(getCellValue(headerCell).toString(), null);
+                }
             }
             fileDataDto.setProperties(rowMap);
             resultList.add(fileDataDto);
@@ -128,7 +130,7 @@ public class ReadFileData {
         return resultList;
     }
 
-//    xlsx行内数据解析
+    //    xlsx行内数据解析
     private static Object getCellValue(Cell cell) {
         if (cell == null) {
             return null;
@@ -152,7 +154,7 @@ public class ReadFileData {
         }
     }
 
-//    文件格式转换
+    //    文件格式转换
     public static File convert(MultipartFile multipartFile) throws IOException {
         // 获取原始文件名
         String originalFilename = multipartFile.getOriginalFilename();
@@ -167,4 +169,32 @@ public class ReadFileData {
         return convFile;
     }
 
+    //    根据文件地址返回文件流
+    public static void outputFile(String filePath, HttpServletResponse response) throws IOException {
+        File file = new File(filePath);
+        if (file.exists()) {
+            // 设置响应头
+            response.setContentType("application/octet-stream");
+            response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
+            response.setContentLength((int) file.length());
+
+            // 获取文件输入流
+            try (FileInputStream fis = new FileInputStream(file);
+                 OutputStream os = response.getOutputStream()) {
+                byte[] buffer = new byte[4096];
+                int bytesRead;
+                // 从文件输入流读取数据并写入响应输出流
+                while ((bytesRead = fis.read(buffer)) != -1) {
+                    os.write(buffer, 0, bytesRead);
+                }
+                os.flush();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        } else {
+            // 文件不存在,返回 404 错误
+            response.sendError(HttpServletResponse.SC_NOT_FOUND, "文件未找到");
+        }
+    }
+
 }

+ 4 - 0
src/main/java/com/skyversation/poiaddr/util/geojsonWriter.java

@@ -0,0 +1,4 @@
+package com.skyversation.poiaddr.util;
+
+public class geojsonWriter {
+}

+ 73 - 0
src/main/java/com/skyversation/poiaddr/util/geotools/GeoJsonIntersector.java

@@ -0,0 +1,73 @@
+package com.skyversation.poiaddr.util.geotools;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import org.json.simple.parser.JSONParser;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * 根据街道名称和村名得到青浦匹配的村庄geojson合集
+ */
+public class GeoJsonIntersector {
+    public static void main(String[] args) throws Exception {
+//        run("香花桥街道", "向阳村,爱星村,大联村,东方村,金米村,东斜村,新姚村,新桥村,民惠第三居委会,燕南村,泾阳村,胜利村,天一村,曹泾村,香花桥居委会,朝阳村,金星村,都汇华庭居委会,陈桥村,盈中村,石西村,杨元村,袁家村,七汇村,郏一村,青山居委会,金巷居委会", "博文中学");
+//        run("香花桥街道", "燕南村,泾阳村,东方村,大联村,金米村,新桥村,爱星村,大盈居委会,民惠居委会,民惠第二居委会,民惠第三居委会,新姚村,友爱居委会,胜利村,天一村,向阳村,曹泾村,东斜村", "博文小学");
+//        run("香花桥街道","香花桥居委会,朝阳村,金星村,都汇华庭居委会,盈中村,石西村,杨元村,七汇村,袁家村,郏一村,青山居委会,金巷居委会","香花桥小学");
+//        run("夏阳街道", "南门塔湾,大生桥,李腰泾,王仙村,城南村,塘郁村,枫泾村,太来村,新阳村,金家村,夏阳村,丝绸新村,志诚上和里", "豫才中学");
+//        run("盈浦街道", "南横村", "思源中学");
+        run("香花桥街道",null,"香花桥街道");
+
+    }
+
+    public static void run(String jdNameStr, String czNameStr, String fileName) throws Exception {
+        List<String> jdNameList = new ArrayList<>(Arrays.asList(jdNameStr.split(",")));
+        List<String> nameList = new ArrayList<>();
+        if(czNameStr != null){
+            nameList = new ArrayList<>(Arrays.asList(czNameStr.split(",")));
+            System.out.println("******需要匹配的村名个数:" + nameList.size());
+        }
+
+        JSONObject processingGeoJson = readGeoJson("src/main/resources/geojson/qp_cunju_2024.geojson");
+        JSONArray features = (JSONArray) processingGeoJson.get("features");
+        JSONArray newFeatures = new JSONArray();
+        for (Object feature : features) {
+            JSONObject item = JSONObject.parseObject(feature.toString());
+            String jiezhen = item.getJSONObject("properties").getString("jz");
+            String cunName = item.getJSONObject("properties").getString("cjname");
+            if (jdNameList.contains(jiezhen)) {
+                if(czNameStr == null){
+                    newFeatures.add(feature);
+                }else if(nameList.contains(cunName)){
+                    newFeatures.add(feature);
+                    System.out.println("++++匹配到:" + jiezhen + "." + cunName);
+                    nameList.remove(cunName);
+                }
+            }
+        }
+        if(nameList.size()>0){
+            for (String cName : nameList) {
+                System.out.println("----没有匹配到:" + cName);
+            }
+        }
+        System.out.println("******匹配到的村名个数:" + newFeatures.size());
+        System.out.println("******没有匹配到的村名个数:" + nameList.size());
+        processingGeoJson.put("features", newFeatures);
+        // 5. 保存结果
+        saveGeoJson(processingGeoJson, "output/result_" + fileName + ".geojson");
+        System.out.println("解析完成,已保存结果geojson文件到output/,文件名称:result_" + fileName + ".geojson");
+    }
+    private static JSONObject readGeoJson(String path) throws Exception {
+        String fileData = new JSONParser().parse(new FileReader(path)).toString();
+        return JSONObject.parseObject(fileData);
+    }
+
+    private static void saveGeoJson(JSONObject geoJson, String path) throws Exception {
+        try (FileWriter writer = new FileWriter(path)) {
+            writer.write(geoJson.toJSONString());
+        }
+    }
+}

+ 2 - 1
src/main/java/com/skyversation/poiaddr/util/geotools/GeoJsonPointInRegion.java

@@ -1,4 +1,5 @@
-package com.skyversation.poiaddr.util.geotools;import com.fasterxml.jackson.databind.ObjectMapper;
+package com.skyversation.poiaddr.util.geotools;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.locationtech.jts.geom.Coordinate;
 import org.locationtech.jts.geom.Geometry;
 import org.locationtech.jts.geom.GeometryFactory;

File diff suppressed because it is too large
+ 0 - 0
src/main/resources/geojson/qp_cunju.geojson


Some files were not shown because too many files changed in this diff