图层数据加载到实体

需求:把西安地铁图层文件加载到数据库,我们首先把他加载到实体

1、图层导出

图层文件数据
一、下载QGIS应用程序,然后下载Bulk Vector Export插件
插件
二、选择layer -> Data Source Manager -> Vector -> Directory -> Vector DataBase(选中你要加载的图层文件夹,然后点击add添加图层数据)
添加图层
三、先选择土层厚,然后选择我们刚才下载的插件选择导出目录,导出后会生成一个压缩包,解压,此时.geojson结尾的文件就是我们需要的文件
导出图层
图层

2、图层解析

一、接下来我们只需要用java代码解析图层即可

如下为JAVA代码

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

    public static void main(String[] args) {
        extractAndTransform();
    }

   /**
     * 解析文件
     */
    public static void extractAndTransform() {

        String filePath = "D:\\测试导入图层数据\\f77c89c2-fdb1-41dd-959c-b328e4ff5a29.geojson";
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            // 读取文件内容到JsonNode
            JsonNode rootNode = objectMapper.readTree(new File(filePath));
            JsonNode featuresNode = rootNode.path("features");

            for (Iterator<JsonNode> it = featuresNode.elements(); it.hasNext(); ) {
                JsonNode feature = it.next();
                JsonNode geometry = feature.path("geometry");
                String lineName = feature.path("properties").path("w").toString();
                System.out.println("lineName:" + lineName);
                List<String> wgsList = formatCoordinates(geometry);

                System.out.println("wgsList: " + wgsList);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 转换经纬度
     *
     * @param geometry
     * @return
     */
    public static List<String> formatCoordinates(JsonNode geometry) {
        List<String> wgsList = new ArrayList<>();

        if ("MultiPolygon".equals(geometry.path("type").asText())) {
            JsonNode coordinates = geometry.path("coordinates");
            if (coordinates.size() > 1) {
                System.out.println("--------------------------");
            }
            for (JsonNode jsonNode : coordinates) {
                if (jsonNode.isArray() && coordinates.size() == 1) {
                    String coordinatesString = jsonNode.toString().trim();
                    ;
                    String s = coordinatesString.replaceAll("\\[\\[\\[", "(((").replaceAll("]]]", ")))").replaceAll(",", " ").replaceAll("] \\[", ",");
                    wgsList.add(String.format("MULTIPOLYGON%s", s));
                } else {
                    for (int i = 0; i < jsonNode.size(); i++) {
                        JsonNode jsonNode1 = jsonNode.get(i);
                        String coordinatesString = jsonNode1.toString().trim();
                        String s = coordinatesString.replaceAll("\\[\\[", "(((").replaceAll("]]", ")))").replaceAll(",", " ").replaceAll("] \\[", ",");
                        wgsList.add(String.format("MULTIPOLYGON%s", s));
                    }

                }
            }

        } else if ("Polygon".equals(geometry.path("type").asText())) {
            JsonNode coordinates = geometry.path("coordinates");
            if (coordinates.isArray() && coordinates.size() == 1) {

                String coordinatesString = coordinates.toString().trim();
                String s = coordinatesString.replaceAll("\\[\\[\\[", "(((").replaceAll("]]]", ")))").replaceAll(",", " ").replaceAll("] \\[", ",");
                wgsList.add(String.format("MULTIPOLYGON%s", s));
            } else {
                for (int i = 0; i < coordinates.size(); i++) {
                    String coordinatesString = coordinates.get(0).toString();
                    String s = coordinatesString.replaceAll("\\[\\[", "(((").replaceAll("]]", ")))").replaceAll(",", " ").replaceAll("] \\[", ",");
                    wgsList.add(String.format("MULTIPOLYGON%s", s));
                }
            }

        } else if ("LineString".equals(geometry.path("type").asText())) {
            JsonNode coordinates = geometry.path("coordinates");
            if (coordinates.isArray() && coordinates.size() == 1) {

                String coordinatesString = coordinates.toString().trim();
                String s = coordinatesString.replaceAll("\\[\\[", "(((").replaceAll("]]", ")))").replaceAll(",", " ").replaceAll("] \\[", ",");
                wgsList.add(String.format("MULTIPOLYGON%s", s));
            } else {
                String coordinatesString = coordinates.toString();
                String s = coordinatesString.replaceAll("\\[\\[", "(((").replaceAll("]]", ")))").replaceAll(",", " ")
                        .replaceAll("] \\[", ",");
                wgsList.add(String.format("MULTIPOLYGON%s", s));
            }

        }
        return wgsList;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值