geojson数据与Excel数据属性匹配处理

GEOJSON数据关联人口字段

读取GEOJSON数据

添加人口字段,双精度double类型

读取Excel文件

按照街道名称关联人口字段

添加到GEO人口字段

保存GEOJSON数据

package sw.yuyan.wisdombasinwuyubackend.util.date;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.stereotype.Component;

import java.io.*;

@Component
public class GeoJsonUtils {

    public JSONObject addFieldFromExcelToGeoJson(String geoJsonFile, String excelFile, String excelSheetName, String geoJsonNewField, String matchField) throws IOException {
        // 读取GEOJSON文件
        JSONObject geoJson = readGeoJsonFile(geoJsonFile);

        // 读取Excel文件
        Workbook workbook = readExcelFile(excelFile);
        Sheet sheet = workbook.getSheet(excelSheetName);

        // 获取GEOJSON中的features数组
        JSONArray features = geoJson.getJSONArray("features");

        // 遍历features数组
        for (int i = 0; i < features.length(); i++) {
            JSONObject feature = features.getJSONObject(i);

            // 从GEOJSON中获取匹配字段的值
            String fieldValue = feature.getJSONObject("properties").optString(matchField);

            // 在Excel中查找匹配数据,并获取对应的新字段值
            Double newFieldValue = getNewFieldValueFromExcel(sheet, fieldValue);
            // 将新字段添加到GEOJSON的properties中
            JSONObject properties = feature.getJSONObject("properties");
            properties.put(geoJsonNewField, newFieldValue);
        }

        return geoJson;
    }
    public static void saveAsGeoJson(JSONObject jsonObject, String filePath) throws IOException {
        FileWriter fileWriter = new FileWriter(filePath);
        fileWriter.write(jsonObject.toString(4)); // 使用缩进格式写入
        fileWriter.flush();
        fileWriter.close();
    }

    // 读取GEOJSON文件
    public static JSONObject readGeoJsonFile(String filePath) throws IOException, JSONException {
        File file = new File(filePath);
        InputStream inputStream = new FileInputStream(file);
        JSONTokener tokener = new JSONTokener(inputStream);
        JSONObject jsonObject = new JSONObject(tokener);
        inputStream.close();
        return jsonObject;
    }

    // 读取Excel文件
    private Workbook readExcelFile(String file) throws IOException {
        InputStream inputStream = new FileInputStream(file);
        return new XSSFWorkbook(inputStream);
    }

    // 从Excel中获取新字段值
    private Double getNewFieldValueFromExcel(Sheet sheet, String fieldValue) {
        // 遍历Excel行
        for (Row row : sheet) {
            Cell cell = row.getCell(1); // 假设匹配字段在第一列(索引为0)

            // 检查单元格内容是否与匹配字段相等
            if (cell != null && cell.getCellType() == CellType.STRING && cell.getStringCellValue().equals(fieldValue)) {
                Cell newFieldCell = row.getCell(2); // 假设新字段在第二列(索引为1)

                // 检查新字段单元格是否存在且为字符串类型
                if (newFieldCell != null && newFieldCell.getCellType() == CellType.NUMERIC) {
//                    return newFieldCell.getStringCellValue();
                    return newFieldCell.getNumericCellValue();
                }else
                {
//                  匹配为空值
                    return 默认值;
                }
            }
        }
//       没有匹配值,默认值
        return 默认值;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值