| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.lf.server.entity.ctrl.MarkJsonEntity; |
| | | import com.lf.server.entity.ctrl.ShpRecordEntity; |
| | | import com.lf.server.entity.data.MetaFileEntity; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.gdal.gdal.Band; |
| | |
| | | import org.gdal.osr.SpatialReference; |
| | | |
| | | import java.io.File; |
| | | import java.lang.reflect.Field; |
| | | 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) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取ShapeFile文件获取Mark实体类 |
| | | */ |
| | | public static List<MarkJsonEntity> readShpForMarks(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); |
| | | |
| | | FeatureDefn featureDefn = layer.GetLayerDefn(); |
| | | Map<Integer, Field> fieldMap = new HashMap(5); |
| | | for (int i = 0, count = featureDefn.GetFieldCount(); i < count; i++) { |
| | | try { |
| | | FieldDefn fieldDefn = featureDefn.GetFieldDefn(i); |
| | | Field field = MarkJsonEntity.class.getDeclaredField(fieldDefn.GetName().toLowerCase()); |
| | | field.setAccessible(true); |
| | | fieldMap.put(i, field); |
| | | } catch (Exception e) { |
| | | // |
| | | } |
| | | } |
| | | |
| | | long count = layer.GetFeatureCount(); |
| | | if (count == 0 || fieldMap.size() == 0) { |
| | | return null; |
| | | } |
| | | |
| | | List<MarkJsonEntity> list = new ArrayList<MarkJsonEntity>(); |
| | | for (int i = 0; i < count; i++) { |
| | | Feature f = layer.GetFeature(i); |
| | | MarkJsonEntity me = new MarkJsonEntity(i + 1); |
| | | for (Integer key : fieldMap.keySet()) { |
| | | Field field = fieldMap.get(key); |
| | | switch (field.getType().toString()) { |
| | | case "double": |
| | | field.set(me, f.GetFieldAsDouble(i)); |
| | | break; |
| | | case "long": |
| | | field.set(me, f.GetFieldAsInteger64(i)); |
| | | break; |
| | | case "int": |
| | | field.set(me, f.GetFieldAsInteger(i)); |
| | | break; |
| | | default: |
| | | field.set(me, f.GetFieldAsString(i)); |
| | | break; |
| | | } |
| | | } |
| | | me.setWkt(f.GetGeometryRef().ExportToWkt()); |
| | | |
| | | list.add(me); |
| | | } |
| | | |
| | | layer.delete(); |
| | | driver.delete(); |
| | | |
| | | return list; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建ShapeFile文件 |
| | | * |
| | | * @param list 标绘JSON实体类集合 |
| | | * @param path 路径 |
| | | * @param type 类型:POINT,LINESTRING,POLYGON |
| | | * @return ShapeFile文件名 |
| | | */ |
| | | public static String createShp(List<MarkJsonEntity> list, String path, String type) { |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("ESRI shapefile"); |
| | | if (driver == null) { |
| | | return null; |
| | | } |
| | | |
| | | String filePath = path + File.separator + type.toLowerCase() + ".shp"; |
| | | //DataSource ds = driver.Open(filePath, 0); |
| | | DataSource ds = driver.CreateDataSource(filePath, null); |
| | | |
| | | SpatialReference sr = new SpatialReference(); |
| | | sr.ImportFromEPSG(4326); |
| | | |
| | | int geoType = getGeometryType(type); |
| | | Layer layer = ds.CreateLayer(type.toLowerCase(), sr, geoType); |
| | | |
| | | FieldDefn fdName = new FieldDefn("name", ogr.OFTString); |
| | | fdName.SetWidth(50); |
| | | layer.CreateField(fdName, 0); |
| | | |
| | | FieldDefn fdProps = new FieldDefn("props", ogr.OFTString); |
| | | fdProps.SetWidth(1024); |
| | | // layer.DeleteField(layer.FindFieldIndex("name", 1)); |
| | | layer.CreateField(fdProps, 1); |
| | | |
| | | FeatureDefn featureDefn = layer.GetLayerDefn(); |
| | | for (MarkJsonEntity mje : list) { |
| | | Geometry geo = Geometry.CreateFromWkt(mje.getWkt()); |
| | | |
| | | Feature f = new Feature(featureDefn); |
| | | f.SetField(0, mje.getName()); |
| | | f.SetField(1, mje.getProps()); |
| | | f.SetGeometry(geo); |
| | | |
| | | layer.CreateFeature(f); |
| | | } |
| | | |
| | | layer.delete(); |
| | | ds.delete(); |
| | | driver.delete(); |
| | | |
| | | return filePath; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取图形类型 |
| | | */ |
| | | private static int getGeometryType(String type) { |
| | | switch (type) { |
| | | case "POINT": |
| | | return 1; |
| | | case "LINESTRING": |
| | | return 2; |
| | | case "POLYGON": |
| | | return 3; |
| | | default: |
| | | return -1; |
| | | } |
| | | ogr.RegisterAll(); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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(); |
| | |
| | | } 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; |