Java 使用GDAL 读写 shapefile
读取shp文件,并把它转化为json
import org.gdal.ogr.*; import org.gdal.ogr.Driver; import org.gdal.gdal.*; public class GdalShpTest { public static void main(String[] args) { // 注册所有的驱动 ogr.RegisterAll(); // 为了支持中文路径,请添加下面这句代码 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES"); // 为了使属性表字段支持中文,请添加下面这句 gdal.SetConfigOption("SHAPE_ENCODING",""); String strVectorFile = "D:\\test\\NODE.shp"; //打开文件 DataSource ds = ogr.Open(strVectorFile,0); if (ds == null) { System.out.println("打开文件失败!" ); return; } System.out.println("打开文件成功!" ); Driver dv = ogr.GetDriverByName("GeoJSON"); if (dv == null) { System.out.println("打开驱动失败!" ); return; } System.out.println("打开驱动成功!" ); dv.CopyDataSource(ds, "D:\\test\\node.json"); System.out.println("转换成功!" ); } }
写shp文件
import org.gdal.ogr.*; import org.gdal.gdal.*; class writeShp2 { public static void main(String[] args) { writeShp2 readshpObj = new writeShp2(); readshpObj.WriteVectorFile(); } static void WriteVectorFile() { String strVectorFile = "D:\\test\\test.shp"; ogr.RegisterAll(); gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); gdal.SetConfigOption("SHAPE_ENCODING", "CP936"); String strDriverName = "ESRI Shapefile"; org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName); if (oDriver == null) { System.out.println(strVectorFile + " 驱动不可用!\n"); return; } DataSource oDS = oDriver.CreateDataSource(strVectorFile, null); if (oDS == null) { System.out.println("创建矢量文件【" + strVectorFile + "】失败!\n"); return; } Layer oLayer = oDS.CreateLayer("TestPolygon", null, ogr.wkbPolygon, null); if (oLayer == null) { System.out.println("图层创建失败!\n"); return; } // 下面创建属性表 // 先创建一个叫FieldID的整型属性 FieldDefn oFieldID = new FieldDefn("FieldID", ogr.OFTInteger); oLayer.CreateField(oFieldID); // 再创建一个叫FeatureName的字符型属性,字符长度为50 FieldDefn oFieldName = new FieldDefn("FieldName", ogr.OFTString); oFieldName.SetWidth(100); oLayer.CreateField(oFieldName); FeatureDefn oDefn = oLayer.GetLayerDefn(); // 创建三角形要素 Feature oFeatureTriangle = new Feature(oDefn); oFeatureTriangle.SetField(0, 0); oFeatureTriangle.SetField(1, "三角形"); Geometry geomTriangle = Geometry.CreateFromWkt("POLYGON ((0 0,20 0,10 15,0 0))"); oFeatureTriangle.SetGeometry(geomTriangle); oLayer.CreateFeature(oFeatureTriangle); // 创建矩形要素 Feature oFeatureRectangle = new Feature(oDefn); oFeatureRectangle.SetField(0, 1); oFeatureRectangle.SetField(1, "矩形"); Geometry geomRectangle = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))"); oFeatureRectangle.SetGeometry(geomRectangle); oLayer.CreateFeature(oFeatureRectangle); // 创建五角形要素 Feature oFeaturePentagon = new Feature(oDefn); oFeaturePentagon.SetField(0, 2); oFeaturePentagon.SetField(1, "五角形"); Geometry geomPentagon = Geometry.CreateFromWkt("POLYGON ((70 0,85 0,90 15,80 30,65 15,70 0))"); oFeaturePentagon.SetGeometry(geomPentagon); oLayer.CreateFeature(oFeaturePentagon); oDS.SyncToDisk(); System.out.println("\n数据集创建完成!\n"); } }
得到test.dbf, test.shp, test.shx。
test.dbf如下:
打开形状如下