| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.lf.server.entity.ctrl.ShpRecordEntity; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.gdal.gdal.Band; |
| | |
| | | import org.gdal.ogr.*; |
| | | import org.gdal.osr.SpatialReference; |
| | | |
| | | import java.io.File; |
| | | import java.sql.Time; |
| | | import java.sql.Timestamp; |
| | | import java.time.LocalDate; |
| | |
| | | public class GdalHelper { |
| | | private final static Log log = LogFactory.getLog(GdalHelper.class); |
| | | |
| | | static { |
| | | 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"); |
| | | //System.setProperty("PROJ_LIB", gdalPath + File.separator + "proj7" + File.separator + "share") |
| | | gdal.SetConfigOption("GDAL_DRIVER_PATH", gdalPath + File.separator + "gdalplugins"); |
| | | |
| | | String path = System.getenv("PATH"); |
| | | if (!path.contains(gdalPath)) { |
| | | System.setProperty("PATH", path + ";" + gdalPath); |
| | | } |
| | | } |
| | | |
| | | // 注册所有的驱动 |
| | | gdal.AllRegister(); |
| | | |
| | | // 为了支持中文路径,请添加下面这句代码 |
| | | gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); |
| | | |
| | | // 配置GDAL_DATA路径(gdal根目录下的bin\gdal-data) |
| | | // gdal.SetConfigOption("GDAL_DATA", "E:\\terrait\\TianJin\\Zip\\release-1928-x64-dev\\release-1928-x64\\bin\\gdal-data"); |
| | | // gdal.SetConfigOption("PROJ_LIB", "E:\\terrait\\TianJin\\Zip\\release-1928-x64-dev\\release-1928-x64\\bin\\proj7\\share"); |
| | | |
| | | // 为了使属性表字段支持中文,请添加下面这句:CP936 |
| | | gdal.SetConfigOption("SHAPE_ENCODING", ""); |
| | | } |
| | | |
| | | /** |
| | | * 获取Shp第一条记录的WKT |
| | | */ |
| | | public static ShpRecordEntity readShpFirstRecord(String filePath) { |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("ESRI shapefile"); |
| | | if (driver == null) { |
| | | return null; |
| | | } |
| | | |
| | | DataSource dataSource = driver.Open(filePath); |
| | | Layer layer = dataSource.GetLayer(0); |
| | | if (layer.GetFeatureCount() < 1) { |
| | | return null; |
| | | } |
| | | |
| | | SpatialReference spatialReference = layer.GetSpatialRef(); |
| | | String csid = spatialReference.GetAttrValue("AUTHORITY", 1); |
| | | |
| | | Feature feature = layer.GetFeature(0); |
| | | String wkt = feature.GetGeometryRef().ExportToWkt(); |
| | | |
| | | layer.delete(); |
| | | dataSource.delete(); |
| | | driver.delete(); |
| | | |
| | | return new ShpRecordEntity(wkt, csid); |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | return null; |
| | | } |
| | | ogr.RegisterAll(); |
| | | } |
| | | |
| | | /** |
| | |
| | | gdal.GDALDestroyDriverManager(); |
| | | } |
| | | |
| | | private static void writeShp(String filePath) { |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("ESRI shapefile"); |
| | | if (driver == null) { |
| | | System.out.println(" ESRI shapefile驱动不可用!\n"); |
| | | System.out.println("fail"); |
| | | } |
| | | |
| | | // Open()的第二个参数默认为0,是以只读方式打开文件;1是读写方式打开 |
| | | DataSource dSource = driver.Open(filePath, 0); |
| | | |
| | | Layer layer = dSource.GetLayerByIndex(0); |
| | | FieldDefn fieldDefn = new FieldDefn("name", ogr.OFTString); |
| | | fieldDefn.SetWidth(50); |
| | | layer.CreateField(fieldDefn, 1); |
| | | layer.DeleteField(layer.FindFieldIndex("name", 1)); |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取shp文件 |
| | | * |
| | | * @param filePath |
| | | */ |
| | | public static void readShp(String filePath) { |
| | |
| | | break; |
| | | } |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | |
| | | //layerDto.setCount(count); |
| | | //layerDto.setMap(mapList); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } finally { |
| | | if (dataSource != null) { |
| | | dataSource.delete(); |
| | |
| | | |
| | | /** |
| | | * 读取gdb文件 |
| | | * |
| | | * @param filePath |
| | | */ |
| | | public static void readGdb(String filePath) { |
| | |
| | | } while (true); |
| | | } |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | private static Object getProperty(Feature feature, int index) { |
| | | public static Object getProperty(Feature feature, int index) { |
| | | int type = feature.GetFieldType(index); |
| | | |
| | | PropertyGetter propertyGetter; |