转换需要的类↓
链接: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);
}
}
}
}


网友评论