使用 diewald_shapeFileReader 完成 ArcGIS shp文件自动转换为geojson
2018-02-23 17:10
405 查看
diewald_shapeFileReader 是 Thomas Diewald 写的一个关于ArcGIS shp文件的读取工具。主要是针对于 shx dbf shp 三个文件。看了很多转换的部分都是在线转换的。
1 读取文件
2 获取属性字段
3 获取图形类型
4 点
5 多点
6 线
7 面
8 使用
使用java -jar 命令执行jar包
第一个参数为shp文件所在路径地址
第二个参数为文件名称
执行完毕后,会在shp文件所在路径下生成一个同样名字的json文件,就可以直接使用了。
jar包下载地址
1 读取文件
// path 为所在文件夹的路径 // name 为不带后缀的文件名 ShapeFile shapeFile = new ShapeFile(path, name).READ();
2 获取属性字段
// 获取属性表字段个数 int fieldCount = shapeFile.getDBF_fieldCount(); // 获取属性表记录条数 int dataCount = shapeFile.getDBF_recordCount(); // 获取属性信息 // 需要注意的:ISO-8859-1 字符是shapefile读取工具设置的编码 // 但是arcgis是以gbk编码字符存储的,不过这个编码也不是固定的,有的时候utf-8不乱码,有时候gbk不乱码,根据实际情况来 fieldName = new String(fieldName.getBytes("ISO-8859-1"), "utf-8"); // 字段名 fieldName = (fieldName != null) ? fieldName.trim() : fieldName; // 剔除空白 String fieldValue = shapeFile.getDBF_record(i, j); // 字段对应的值 fieldValue = new String(fieldValue.getBytes("ISO-8859-1"), "utf-8"); fieldValue = (fieldValue != null) ? fieldValue.trim() : fieldValue; // 剔除空白
3 获取图形类型
// 获取图形类型====(点、线、面) ShpShape.Type shapeType = shapeFile.getSHP_shapeType();
4 点
if (shapeType.isTypeOfPoint()) { // 单点 ArrayList<Object> pointShapes = shapeFile.getSHP_shape(); for (int i = 0; i < pointShapes.size(); i++) { ShpPoint point = (ShpPoint) pointShapes.get(i); GeoFeature feature = new GeoFeature(); GeoGeometryPoint geoPoint = new GeoGeometryPoint(); double[] xyz = point.getPoint(); double[] xy = new double[]{xyz[0], xyz[1]}; // 去掉z坐标,只保留xy geoPoint.setCoordinates(xy); feature.setGeometry(geoPoint); feature.setProperties(properties.get(i)); collection.getFeatures().add(feature); } }
5 多点
if (shapeType.isTypeOfMultiPoint()) { // 多点 ArrayList<Object> pointsShapes = shapeFile.getSHP_shape(); for (int i = 0; i < pointsShapes.size(); i++) { ShpMultiPoint points = (ShpMultiPoint) pointsShapes.get(i); GeoFeature feature = new GeoFeature(); GeoGeometryMultiPoint geoMultiPoint = new GeoGeometryMultiPoint(); double[][] num_xyz = points.getPoints(); List<double[]> num_xy = new ArrayList<>(); // 去掉z坐标,只保留xy for (int n = 0; n < num_xyz.length; n++) { num_xy.add(new double[]{num_xyz [0], num_xyz [1]}); } geoMultiPoint.setCoordinates(num_xy); feature.setGeometry(geoMultiPoint); feature.setProperties(properties.get(i)); collection.getFeatures().add(feature); }
6 线
if (shapeType.isTypeOfPolyLine()) { // 线 ArrayList<Object> lineShapes = shapeFile.getSHP_shape(); for (int i = 0; i < lineShapes.size(); i++) { ShpPolyLine line = (ShpPolyLine) lineShapes.get(i); GeoFeature feature = new GeoFeature(); if (line.getNumberOfParts() == 1) { // 单线 GeoGeometryLineString geoLine = new GeoGeometryLineString(); double[][] num_xyz = line.getPoints(); //[number of points][x,y,z] List<double[]> num_xy = new ArrayList<>(); // 去掉z坐标,只保留xy for (int n = 0; n < num_xyz.length; n++) { num_xy.add(new double[]{num_xyz [0], num_xyz [1]}); } geoLine.setCoordinates(num_xy); feature.setGeometry(geoLine); feature.setProperties(properties.get(i)); collection.getFeatures().add(feature); } else { // 多线 GeoGeometryMultiLineString geoMultiLine = new GeoGeometryMultiLineString(); double[][][] line_num_xyz = line.getPointsAs3DArray(); // [number of polylines][number of points per polyline][x, y, z, m] List<List<double[]>> line_num_xy = new ArrayList<>(); // 去掉z坐标,只保留xy for (int n = 0; n < line_num_xyz.length; n++) { List<double[]> line_points = new ArrayList<>(); for (int k = 0; k < line_num_xyz .length; k++) { line_points.add(new double[]{line_num_xyz [k][0], line_num_xyz [k][1]}); } line_num_xy.add(line_points); } geoMultiLine.setCoordinates(line_num_xy); feature.setGeometry(geoMultiLine); feature.setProperties(properties.get(i)); collection.getFeatures().add(feature); } } }
7 面
if (shapeType.isTypeOfPolygon()) { // 99d3 面 ArrayList<Object> polygonShapes = shapeFile.getSHP_shape(); for (int i = 0; i < polygonShapes.size(); i++) { ShpPolygon polygon = (ShpPolygon) polygonShapes.get(i); GeoFeature feature = new GeoFeature(); if (polygon.getNumberOfParts() == 1) { // 单面 GeoGeometryPolygon geoPolygon = new GeoGeometryPolygon(); double[][] num_xyz = polygon.getPoints(); // [number of points][x,y,z] List<List<double[]>> num_xy = new ArrayList<>(); List<double[]> xy = new ArrayList<>(); for (int k = 0; k < num_xyz.length; k++) { xy.add(new double[]{num_xyz[k][0], num_xyz[k][1]}); } num_xy.add(xy); geoPolygon.setCoordinates(num_xy); feature.setGeometry(geoPolygon); feature.setProperties(properties.get(i)); collection.getFeatures().add(feature); } else { // 多面 GeoGeometryMultiPolygon geoMutliPolygon = new GeoGeometryMultiPolygon(); double[][][] poly_num_xyz = polygon.getPointsAs3DArray(); List<List<List<double[]>>> poly_num_xys = new ArrayList<>(); List<List<double[]>> poly_num_xy = new ArrayList<>(); // 去掉z坐标,只保留xy for (int n = 0; n < poly_num_xyz.length; n++) { List<double[]> poly_points = new ArrayList<>(); for (int k = 0; k < poly_num_xyz .length; k++) { poly_points.add(new double[]{poly_num_xyz [k][0], poly_num_xyz [k][1]}); } poly_num_xy.add(poly_points); } poly_num_xys.add(poly_num_xy); geoMutliPolygon.setCoordinates(poly_num_xys); feature.setGeometry(geoMutliPolygon); feature.setProperties(properties.get(i)); collection.getFeatures().add(feature); } } }
8 使用
使用java -jar 命令执行jar包
第一个参数为shp文件所在路径地址
第二个参数为文件名称
执行完毕后,会在shp文件所在路径下生成一个同样名字的json文件,就可以直接使用了。
jar包下载地址
相关文章推荐
- 使用auto_prepend_file和auto_append_file配置命令自动完成文件插入
- [置顶] android开发——使用java读取.shp(shapefile)矢量文件
- 使用GMap控件读取.shp多边形文件(shapefile)
- 文件编码自动检测转换工具file-convert,解决乱码问题
- Java 使用DataInputStream将数据写入文件,使用FileReader读取演示
- 使用FileReader和FileWriter读取写入文件内容
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- 利用ArcGIS对shp文件进行投影转换
- 使用start命令自动为ArcGIS runtime导入许可文件 无界面
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- 利用FileReader和FileWriter完成一个文件拷贝功能
- shapefile 和 MapGIS 文件格式之间的转换
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- 使用FileSystemWatcher监测大文件时,如何判断文件已经创建完成?
- 使用FileReader和FileWriter读取写入文件内容
- java 使用FileInputStream,FileReader,BufferedStream读写记事本文件的例子
- 初学Java,IO之使用FileInputStream和FileReader读取文件(四十一)
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- MapInfo数据到ARCGIS数据Shapefile的转换
- 使用EditPlus技巧,提高工作效率(自动完成文件、语法文件下载)