| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.lf.server.entity.ctrl.MarkJsonEntity; |
| | | import com.lf.server.entity.ctrl.ShpRecordEntity; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | |
| | | 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; |
| | |
| | | |
| | | return new ShpRecordEntity(wkt, csid); |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | 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; |
| | | } |
| | | } |
| | | |
| | |
| | | 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) { |
| | |
| | | |
| | | /** |
| | | * 读取gdb文件 |
| | | * |
| | | * @param filePath |
| | | */ |
| | | public static void readGdb(String filePath) { |