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

转换需要的类↓

面:

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 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 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 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);

}

}

}

}

5fee28e06d99

文件所在目录结构

5fee28e06d99

需要注意的地方

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值