GDAL(Geospatial Data Abstraction Library)是一个开源栅格空间数据转换库。它利用抽象数据模型来表达所支持的各种文件格式。它还有一系列命令行工具来进行数据转换和处理。
参考网址:http://www.gisinternals.com/sdk/
1,GDAL-java写入shp文件
WriteShp.java代码:
package Vector; import org.gdal.gdal.gdal; import org.gdal.ogr.*; import org.gdal.osr.SpatialReference; //将空间数据写入shp文件 public class WriteShp { public static void main(String[] args) { //指定文件的名字和路径 String strVectorFile ="E:\\test\\test.shp"; // 注册所有的驱动 ogr.RegisterAll(); //这个可以看到支持哪些格式的驱动,不用的时候可以注释掉 // int count =ogr.GetDriverCount(); // for(int i=0;i<count;i++){ // System.out.println(ogr.GetDriver(i).GetName()); // } //配置GDAL_DATA路径(gdal根目录下的bin\gdal-data) gdal.SetConfigOption("GDAL_DATA","F:\\GDAL学习文件夹\\release-1900-x64-gdal-2-3-2-mapserver-7-2-1\\bin\\gdal-data"); // 为了支持中文路径,请添加下面这句代码 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES"); // 为了使属性表字段支持中文,请添加下面这句 gdal.SetConfigOption("SHAPE_ENCODING","CP936"); //创建数据,这里以创建ESRI的shp文件为例 String strDriverName = "ESRI Shapefile"; //创建一个文件,根据strDriverName扩展名自动判断是创建shp文件或其他文件 Driver oDriver =ogr.GetDriverByName(strDriverName); if (oDriver == null) { System.out.println(strDriverName+ " 驱动不可用!\n"); return; } // 创建数据源 DataSource oDS = oDriver.CreateDataSource(strVectorFile,null); if (oDS == null) { System.out.println("创建矢量文件【"+ strVectorFile +"】失败!\n" ); return; } // 创建图层,创建一个多边形图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定 //如果是mif或者tab,其实是可以多元素混合存放的,shp每个图层只能存放点线面中的一种。 //所以tab创建图层的时候,选择什么都不影响后面的操作和存储结果 SpatialReference spatialReference = new SpatialReference(); spatialReference.ImportFromEPSG(4326); Layer oLayer =oDS.CreateLayer("TestPolygon", spatialReference, ogr.wkbPolygon, null); if (oLayer == null) { System.out.println("图层创建失败!\n"); return; } // 下面创建属性表 // 先创建一个叫FieldID的整型属性 FieldDefn oFieldID = new FieldDefn("FieldID", ogr.OFTInteger); oLayer.CreateField(oFieldID, 1); // 再创建一个叫名称的字符型属性,字符长度为50 FieldDefn oFieldName = new FieldDefn("名称", ogr.OFTString); oFieldName.SetWidth(100); oLayer.CreateField(oFieldName, 1); FeatureDefn oDefn =oLayer.GetLayerDefn(); // 创建三角形要素 //使用wkb方式创建几何对象 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); //写入文件 oLayer.SyncToDisk(); oDS.SyncToDisk(); System.out.println("\n数据集创建完成!\n"); } }
2,GDAL-java读取shp文件信息
ReadShp.java代码:
package Vector; import org.gdal.gdal.gdal; import org.gdal.ogr.*; import org.gdal.osr.SpatialReference; import java.util.HashMap; import java.util.Map; //读取某个磁盘路径下的shp文件信息(范围、属性等) public class ReadShp { public static void main(String[] args) throws Exception { //指定文件的名字和路径 String strVectorFile ="E:\\test\\test.shp"; // 注册所有的驱动 ogr.RegisterAll(); //配置GDAL_DATA路径(gdal根目录下的bin\gdal-data) gdal.SetConfigOption("GDAL_DATA","F:\\GDAL学习文件夹\\release-1900-x64-gdal-2-3-2-mapserver-7-2-1\\bin\\gdal-data"); // 为了支持中文路径,请添加下面这句代码 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES"); // 为了使属性表字段支持中文,请添加下面这句 gdal.SetConfigOption("SHAPE_ENCODING","CP936"); //读取数据,这里以ESRI的shp文件为例 String strDriverName = "ESRI Shapefile"; //创建一个文件,根据strDriverName扩展名自动判断驱动类型 org.gdal.ogr.Driver oDriver =ogr.GetDriverByName(strDriverName); if (oDriver == null) { System.out.println(strDriverName+ " 驱动不可用!\n"); return; } DataSource dataSource = oDriver.Open(strVectorFile); Layer layer = dataSource.GetLayer("test"); String layerName = layer.GetName(); System.out.println("图层名称:"+layerName); SpatialReference spatialReference = layer.GetSpatialRef(); //System.out.println(spatialReference); System.out.println("空间参考坐标系:"+spatialReference.GetAttrValue("AUTHORITY",0)+spatialReference.GetAttrValue("AUTHORITY",1)); double[] layerExtent = layer.GetExtent(); System.out.println("图层范围:minx:"+layerExtent[0]+",maxx:"+layerExtent[1]+",miny:"+layerExtent[2]+",maxy:"+layerExtent[3]); FeatureDefn featureDefn = layer.GetLayerDefn(); int fieldCount = featureDefn.GetFieldCount(); Map<String,Object> fieldMap = new HashMap(); for(int i=0; i<fieldCount; i++){ FieldDefn fieldDefn = featureDefn.GetFieldDefn(i); //得到属性字段类型 int fieldType = fieldDefn.GetFieldType(); String fieldTypeName = fieldDefn.GetFieldTypeName(fieldType); //得到属性字段名称 String fieldName = fieldDefn.GetName(); fieldMap.put(fieldTypeName,fieldName); } long featureCount = layer.GetFeatureCount(); System.out.println("图层要素个数:"+featureCount); for(int i=0; i<featureCount; i++){ Feature feature = layer.GetFeature(i); Object[] arr = fieldMap.values().toArray(); for(int k=0; k<arr.length; k++){ String fvalue = feature.GetFieldAsString(arr[k].toString()); System.out.print(" 属性名称:"+arr[k].toString()+",属性值:"+fvalue); } System.out.println(); } } }
3,GDAL-java读取tiff文件信息
ReadTiff.java代码:
package Raster; import org.gdal.gdal.Band; import org.gdal.gdal.Dataset; import org.gdal.gdal.Driver; import org.gdal.gdal.gdal; import org.gdal.gdalconst.gdalconstConstants; public class ReadTiff { public static void main(String[] args) { gdal.AllRegister(); String rasterFilePath = "E:\\Test\\TestOpenLayers\\data\\GF1_PMS2_E121.7_N39.9_20170609_L1A0002429674_Fusion.tiff"; Dataset dataset = gdal.Open(rasterFilePath, gdalconstConstants.GA_ReadOnly); if (dataset == null) { System.out.println("GDAL read error: " + gdal.GetLastErrorMsg()); } Driver driver = dataset.GetDriver(); System.out.println("driver short name: " + driver.getShortName()); System.out.println("driver long name: " + driver.getLongName()); System.out.println("metadata list: " + driver.GetMetadata_List()); int xsize = dataset.getRasterXSize(); int ysize = dataset.getRasterYSize(); int count = dataset.getRasterCount(); String proj = dataset.GetProjection(); Band band = dataset.GetRasterBand(1); // 左上角点坐标 lon lat: transform[0]、transform[3] // 像素分辨率 x、y方向 : transform[1]、transform[5] // 旋转角度: transform[2]、transform[4]) double[] transform = dataset.GetGeoTransform(); for(int i=0; i<transform.length; i++){ System.out.println("transform: " + transform[i]); } } }
4,shp转换geojson
package gdal; import org.gdal.*; import org.gdal.ogr.DataSource; import org.gdal.ogr.Driver; import org.gdal.ogr.ogr; import org.gdal.gdal.*; public class Test { public static void main(String[] args) { // 注册所有的驱动 ogr.RegisterAll(); // 为了支持中文路径,请添加下面这句代码 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES"); // 为了使属性表字段支持中文,请添加下面这句 gdal.SetConfigOption("SHAPE_ENCODING",""); //shp文件所在的位置 String strVectorFile = "D:\\sichuan\\sichuanPointALL.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("打开驱动成功!" ); //输出geojson的位置及文件名 dv.CopyDataSource(ds, "D:\\data\\sichuan.geojson"); System.out.println("转换成功!" ); } }
【参考】
GDAL项目官方网站:http://www.gdal.org
GDAL在Java中的使用:http://trac.osgeo.org/gdal/wiki/GdalOgrInJava