mid、mif文件操作工具类

微信搜索:“二十同学” 公众号,欢迎关注一条不一样的成长之路

读写mid、mif文件操作工具类

装载mid文件,将mid文件里的数据变为  List<List<String>> 形式的

public List<List<String>> loadMidFile(File file) {
        List<List<String>> midRows= new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
            String line;
            while ((line = reader.readLine()) != null) {
                List<String> cols = Arrays.asList(line.split(",",-1));
                if (cols.size() > 0) {
                    for (int i = 0; i < cols.size(); i++) {
                        String value = cols.get(i).replace("\"", "");
                        cols.set(i, value);
                    }
                    midRows.add(cols);
                }
            }
            reader.close();
        } catch (FileNotFoundException e) {
            logger.error("loadMidFile():", e);
        } catch (IOException e) {
            logger.error("loadMidFile():", e);
        }
        return midRows;
    }

装载mif文件

public List<String> loadMifFile(File file, Boolean isLoadMifData) {
        //colField 为mid文件里各个值对应的字段
        List<String> colField = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.toUpperCase().startsWith("COLUMNS")) {
                    int num = Integer.parseInt(line.substring("Columns ".length()));
                    for (int i = 0; i < num; i++) {
                        line = reader.readLine().trim();
                        List<String> cols = Arrays.asList(line.split(","));
                        if (cols.size() > 0) {
                            colField.add(cols.get(0).split(" ")[0].trim());
                        }
                    }

                    if (isLoadMifData) {
                        //装载mif里的geo数据(点,线,面不同类型)
                        loadMifFileGeoDatas(reader);
                    }

                    break;
                }
            }
            reader.close();

        } catch (Exception e) {
            logger.error("loadMifFile():", e);
        }

        return colField;
    }
//如果geo数据为面
public Map<Integer, List<List<List<Double>>>> loadMifFileGeoDatas(BufferedReader reader) {
        Map<Integer, List<List<List<Double>>>> regions = new HashMap<>();
        Integer rowIndex = -1;
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.toUpperCase().contains("NONE")){
                    regions.put(++rowIndex, new ArrayList<>());
                }
                if (line.toUpperCase().startsWith("REGION")) {
                    int num = Integer.parseInt(line.substring("Region ".length()));
                    List<List<List<Double>>> dataGroups = new ArrayList<>();

                    for (int i = 0; i < num; i++) {
                        line = reader.readLine().trim();
                        int rowCount = Integer.parseInt(line);
                        List<List<Double>> datas = new ArrayList<>();
                        for (int j = 0; j < rowCount; j++) {
                            line = reader.readLine().trim();
                            List<String> xy = Arrays.asList(line.split(" "));
                            List<Double> points = new ArrayList<>();

                            if (xy.size() == 2) {
                                points.add(Double.parseDouble(xy.get(0)));
                                points.add(Double.parseDouble(xy.get(1)));
                                datas.add(points);
                            }
                        }
                        dataGroups.add(datas);
                    }
                    regions.put(++rowIndex, dataGroups);
                }
            }
            reader.close();

        } catch (Exception e) {
            logger.error("loadMifFileGeoDatas():", e);
        }
        return regions;
    }


 public LinkedHashMap<String, Object> getGeometryMap(Map<Integer, List<List<List<Double>>>> regions,Integer rowIdx) {

        if (regions != null) {
            List<List<List<Double>>> coordinates = regions.getOrDefault(rowIdx, null);
            LinkedHashMap<String, Object> geometryMap = new LinkedHashMap<String, Object>() {{
                put("type", "Polygon");
                put("coordinates", coordinates);
            }};

            return geometryMap;
        }

        return null;

    }
//如果geo数据为点
public Map<Integer, List<Double>> loadMifFileGeoDatas(BufferedReader reader) {
        Map<Integer, List<Double>> pointRows = new HashMap<>();
        Integer rowIndex = -1;
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.toUpperCase().contains("NONE")){
                    pointRows.put(++rowIndex, new ArrayList<>());
                }
                if (line.toUpperCase().startsWith("POINT")) {
                    List<String> point = Arrays.asList(line.split(" "));
                    if (point.size() == 3) {
                        List<Double> point1 = new ArrayList<>();
                        point1.add(Double.parseDouble(point.get(1)));
                        point1.add(Double.parseDouble(point.get(2)));
                        pointRows.put(++rowIndex, point1);
                    }
                }
            }
            reader.close();

        } catch (Exception e) {
            logger.error("loadMifFileGeoDatas():", e);
        }
        
        return pointRows;

    }


 public LinkedHashMap<String, Object> getGeometryMap(Map<Integer, List<Double>> pointRows,Integer rowIdx) {
        if (pointRows!= null) {
            List<Double> pointRow = pointRows.getOrDefault(rowIdx, null);
            LinkedHashMap<String, Object> geometryMap = new LinkedHashMap<String, Object>() {{
                put("type", "Point");
                put("coordinates", pointRow);
            }};

            return geometryMap;
        }
        return null;
    }
//如果geo数据为线
public Map<Integer, List<List<Double>>> loadMifFileGeoDatas(BufferedReader reader)  {
        Map<Integer, List<List<Double>>> lineRows = new HashMap<>();
        Integer rowIndex = -1;
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                List<List<Double>> lines = new ArrayList<>();
                line = line.trim();
                if (line.toUpperCase().contains("NONE")){
                    lineRows.put(++rowIndex, lines);
                }
                if (line.toUpperCase().startsWith("LINE")) {
                    List<String> point = Arrays.asList(line.split(" "));
                    if (point.size() == 5) {
                        List<Double> point1 = new ArrayList<>();
                        point1.add(Double.parseDouble(point.get(1)));
                        point1.add(Double.parseDouble(point.get(2)));
                        lines.add(point1);

                        List<Double> point2 = new ArrayList<>();
                        point2.add(Double.parseDouble(point.get(3)));
                        point2.add(Double.parseDouble(point.get(4)));
                        lines.add(point2);
                    }
                    lineRows.put(++rowIndex, lines);
                }
                if (line.startsWith("Pline")) {
                    int num = Integer.parseInt(line.substring("Pline ".length()));
                    for (int i = 0; i < num; i++) {
                        line = reader.readLine().trim();
                        List<String> point = Arrays.asList(line.split(" "));
                        if (point.size() == 2) {
                            List<Double> points = new ArrayList<>();
                            points.add(Double.parseDouble(point.get(0)));
                            points.add(Double.parseDouble(point.get(1)));
                            lines.add(points);
                        }
                    }
                    lineRows.put(++rowIndex, lines);
                }
            }
            reader.close();

        } catch (Exception e) {
            logger.error("loadMifFileGeoDatas():", e);
        }
        return lineRows;
    }

public LinkedHashMap<String, Object> getGeometryMap(Map<Integer, List<List<Double>>> lineRows,Integer rowIdx) {
        if (lineRows!= null) {
            List<List<Double>> line = lineRows.getOrDefault(rowIdx, null);
            LinkedHashMap<String, Object> geometryMap = new LinkedHashMap<String, Object>() {{
                put("type", "LineString");
                put("coordinates", line);
            }};

            return geometryMap;
        }
        return null;
    }

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十同学

谢谢大佬打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值