| | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * GDAL帮助类 |
| | | * @author WWW |
| | | */ |
| | | @SuppressWarnings("ALL") |
| | | public class GdalHelper { |
| | | private final static Log log = LogFactory.getLog(GdalHelper.class); |
| | | |
| | | public static void init(String gdalPath) { |
| | | // 支持中文路径 |
| | | gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); |
| | | // 属性表支持中文:CP936 |
| | | gdal.SetConfigOption("SHAPE_ENCODING", ""); |
| | | gdal.SetConfigOption("PGEO_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s"); |
| | | gdal.SetConfigOption("MDB_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s"); |
| | | |
| | | // 配置环境变量 |
| | | if (!StringHelper.isEmpty(gdalPath)) { |
| | | gdal.SetConfigOption("GDAL_DATA", gdalPath + File.separator + "gdal-data"); |
| | | gdal.SetConfigOption("PROJ_LIB", gdalPath + File.separator + "proj7" + File.separator + "share"); |
| | |
| | | } |
| | | } |
| | | |
| | | // 注册所有的驱动 |
| | | gdal.AllRegister(); |
| | | ogr.RegisterAll(); |
| | | GeoHelper.initSr(); |
| | | } |
| | | |
| | | /** |
| | | * 读取tif文件 |
| | | * |
| | | * @param fileName |
| | | */ |
| | | public static void readTif(String fileName) { |
| | | // 读取影像数据 |
| | | Dataset dataset = gdal.Open(fileName, gdalconstConstants.GA_ReadOnly); |
| | |
| | | return; |
| | | } |
| | | |
| | | // providing various methods for a format specific driver. |
| | | Driver driver = dataset.GetDriver(); |
| | | System.out.println("driver name: " + driver.getLongName()); |
| | | |
| | | // 读取影像信息 |
| | | int xSize = dataset.getRasterXSize(); |
| | | int ySzie = dataset.getRasterYSize(); |
| | | int rasterCount = dataset.getRasterCount(); |
| | | System.out.println("dataset xSize: " + xSize + ", ySzie = " + ySzie + ", rasterCount = " + rasterCount); |
| | | |
| | | Band band = dataset.GetRasterBand(1); |
| | | // the data type of the band. |
| | | int type = band.GetRasterDataType(); |
| | | System.out.println("data type = " + type + ", " + (type == gdalconstConstants.GDT_Byte)); |
| | | |
| | | // Frees the native resource associated to a Dataset object and close the file. |
| | | dataset.delete(); |
| | | |
| | | gdal.GDALDestroyDriverManager(); |
| | | } |
| | | |
| | | /** |
| | | * 读取shp文件 |
| | | * |
| | | * @param filePath |
| | | */ |
| | | public static void readShp(String filePath) { |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("ESRI shapefile"); |
| | |
| | | Map<String, Object> fieldMap = new HashMap(5); |
| | | for (int i = 0, count = featureDefn.GetFieldCount(); i < count; i++) { |
| | | FieldDefn fieldDefn = featureDefn.GetFieldDefn(i); |
| | | //得到属性字段类型 |
| | | int fieldType = fieldDefn.GetFieldType(); |
| | | String fieldTypeName = fieldDefn.GetFieldTypeName(fieldType); |
| | | //得到属性字段名称 |
| | | String fieldName = fieldDefn.GetName(); |
| | | |
| | | fieldMap.put(fieldTypeName, fieldName); |
| | |
| | | dataSource = driver.Open(filePath, 0); |
| | | int num = dataSource.GetLayerCount(); |
| | | for (int i = 0; i < num; i++) { |
| | | // 获取图层 |
| | | Layer layer = dataSource.GetLayer(i); |
| | | // 表名称 |
| | | String strlayerName = layer.GetName(); |
| | | // 获取图层要数个数 |
| | | long count = layer.GetFeatureCount(); |
| | | if (0 != count) { |
| | | do { |
| | | // 获取图层下的要素 |
| | | Feature feature = layer.GetNextFeature(); |
| | | if (null == feature) { |
| | | break; |
| | | } |
| | | // 获取边界坐标 |
| | | |
| | | Geometry geometry = feature.GetGeometryRef(); |
| | | if (geometry != null) { |
| | | String geometryJson = geometry.ExportToJson(); |
| | |
| | | } |
| | | } |
| | | Map map = new HashMap(5); |
| | | //获取属性 |
| | | for (int p = 0; p < feature.GetFieldCount(); p++) { |
| | | map.put(feature.GetFieldDefnRef(p).GetName(), getProperty(feature, p)); |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取gdb文件 |
| | | * |
| | | * @param filePath |
| | | */ |
| | | public static void readGdb(String filePath) { |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("OpenFileGDB"); |
| | |
| | | DataSource dataSource = driver.Open(filePath, 0); |
| | | |
| | | for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) { |
| | | // 获取图层 |
| | | Layer layer = dataSource.GetLayer(i); |
| | | System.out.println(i + "\t" + layer.GetName()); |
| | | do { |
| | | // 获取图层下的要素 |
| | | Feature feature = layer.GetNextFeature(); |
| | | if (null == feature) { |
| | | break; |
| | | } |
| | | // 获取样式,这里是空,待确定 |
| | | |
| | | System.out.println(feature.GetStyleString()); |
| | | // 获取geometry,也可以ExportToWkb() gml等等 |
| | | System.out.println(feature.GetGeometryRef().ExportToWkt()); |
| | | // 获取属性 |
| | | System.out.println("----------------------------------"); |
| | | for (int p = 0; p < feature.GetFieldCount(); p++) { |
| | | System.out.println(feature.GetFieldDefnRef(p).GetName() + "\t" + getProperty(feature, p)); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 属性获取器 |
| | | */ |
| | | @FunctionalInterface |
| | | private interface PropertyGetter { |
| | | /** |
| | | * 获取属性 |
| | | * |
| | | * @param feature |
| | | * @param index |
| | | * @return |
| | | */ |
| | | Object get(Feature feature, int index); |
| | | } |
| | | |
| | | private static final PropertyGetter STRING_PROPERTY_GETTER = (feature, index) -> feature.GetFieldAsString(index); |
| | | |
| | | /** |
| | | * feature.GetFieldType(index)得到一个属性类型的int值,该值对应具体类型 |
| | | */ |
| | | private static final PropertyGetter[] PROPERTY_GETTERS = new PropertyGetter[]{ |
| | | // 0-Integer |
| | | (feature, index) -> feature.GetFieldAsInteger(index), |