管道基础大数据平台系统开发-【后端】-Server
12
13693261870
2022-11-21 07a68b8d93aa059968a9709f2c81e6625338032c
src/main/java/com/lf/server/helper/GdalHelper.java
@@ -1,6 +1,5 @@
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;
@@ -29,7 +28,7 @@
public class GdalHelper {
    private final static Log log = LogFactory.getLog(GdalHelper.class);
    static {
    public static void init() {
        // 注册所有的驱动
        gdal.AllRegister();
@@ -42,39 +41,6 @@
        // 为了使属性表字段支持中文,请添加下面这句: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;
        }
    }
    /**
@@ -111,29 +77,9 @@
        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) {
@@ -257,6 +203,7 @@
    /**
     * 读取gdb文件
     *
     * @param filePath
     */
    public static void readGdb(String filePath) {
@@ -291,7 +238,7 @@
                } while (true);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage(), ex);
        }
    }
@@ -309,7 +256,7 @@
            return propertyGetter.get(feature, index);
        } catch (Exception ex) {
            // ex.printStackTrace()
            log.error(ex.getStackTrace());
            log.error(ex.getMessage(), ex);
            return null;
        }