美文网首页
2019-11-27 java shp文件转成json格式

2019-11-27 java shp文件转成json格式

作者: 忆丶往 | 来源:发表于2019-11-28 10:33 被阅读0次
转换需要的类↓

链接:https://pan.baidu.com/s/18bp7zQH8MRov5td6loqZmA
提取码:ecfo
复制这段内容后打开百度网盘手机App,操作更方便哦

面:

   public void shp2Json() throws Exception{
        String filePath = "D:\\扣扣\\84695\\FileRecv\\黄河流域边界";
        String fileName = "边界面";
        ShapeFile shapefile = new ShapeFile(filePath, fileName).READ();
        ShpShape.Type shape_type = shapefile.getSHP_shapeType();

        if(shape_type==ShpShape.Type.Polygon) {
            ShpPolygon shape = shapefile.getSHP_shape(0);
            double[][] doubles = shape.getPoints();
            FeatureCollection featureCollection = new FeatureCollection();
            Feature feature = new Feature();
            LineString line=new LineString();
            Polygon polygon = new Polygon();
            List<LngLatAlt> lngLatAlts = new ArrayList<>();
            for(int i = 0; i < doubles.length ; i++){
                lngLatAlts.add(new LngLatAlt(doubles[i][0],doubles[i][1]));
            }
            polygon.add(lngLatAlts);
            feature.setGeometry(polygon);
            Map<String,Object> col = new HashMap<>();
            feature.setProperties(col);
            featureCollection.add(feature);    
            String json= new ObjectMapper().writeValueAsString(featureCollection);
            FileUtils.write(new File("C:\\Users\\84695\\Desktop\\yrly.json"), json);
            System.out.println(json);
        }
            
    }

线:

public void shp2Json() throws Exception {
        String filePath = "C:\\Users\\84695\\Desktop\\河流";
        String fileName = "支流";
        ShapeFile shapefile = new ShapeFile(filePath, fileName).READ();
        ShpShape.Type shape_type = shapefile.getSHP_shapeType();

        if (shape_type == ShpShape.Type.PolyLine) {
            int size = shapefile.getSHP_shapeCount();
            FeatureCollection featureCollection = new FeatureCollection();

            for (int k = 0; k < size; k++) {
                ShpPolyLine shape = shapefile.getSHP_shape(k);
                double[][] doubles = shape.getPoints();
                int start=0,end=0;
                for (int p = 0; p < shape.getNumberOfParts(); p++) {
                    start=shape.SHP_parts[p];
                    if (p==shape.getNumberOfParts()-1){
                        end=doubles.length;
                    }else{
                        end=shape.SHP_parts[p+1];
                    }
                    Feature feature = new Feature();
                    LineString line = new LineString();
                    for (int i = start; i < end; i++) {
                        line.add(new LngLatAlt(doubles[i][0], doubles[i][1]));
                    }
                    feature.setGeometry(line);
                    Map<String, Object> col = new HashMap<>();
                    feature.setProperties(col);
                    featureCollection.add(feature);

                    String json = new ObjectMapper().writeValueAsString(featureCollection);
                    FileUtils.write(new File("C:\\Users\\84695\\Desktop\\river.json"), json);
                }
            }

        }
    }
文件所在目录结构
需要注意的地方

相关文章

网友评论

      本文标题:2019-11-27 java shp文件转成json格式

      本文链接:https://www.haomeiwen.com/subject/wxtmwctx.html