管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-11-01 9b4b8766818ef70c4498436cfd0ce86a1e039570
src/main/java/com/lf/server/helper/GdalHelper.java
@@ -1,6 +1,5 @@
package com.lf.server.helper;
import com.lf.server.entity.ctrl.LayerDto;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gdal.gdal.Band;
@@ -29,20 +28,20 @@
public class GdalHelper {
    private final static Log log = LogFactory.getLog(GdalHelper.class);
    static {
        // 注册所有的驱动
        gdal.AllRegister();
   public static void init() {
       // 注册所有的驱动
       gdal.AllRegister();
        // 为了支持中文路径,请添加下面这句代码
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
       // 为了支持中文路径,请添加下面这句代码
       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");
       // 配置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", "");
    }
       // 为了使属性表字段支持中文,请添加下面这句:CP936
       gdal.SetConfigOption("SHAPE_ENCODING", "");
   }
    /**
     * 读取tif文件
@@ -78,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) {
@@ -154,14 +133,14 @@
        }
    }
    public static LayerDto getLayerDto(String filePath) {
    public static void getLayerDto(String filePath) {
        org.gdal.ogr.Driver driver = ogr.GetDriverByName("OpenFileGDB");
        if (driver == null) {
            return null;
            return;
        }
        List<Map> list = new ArrayList<>();
        List<Map<String, String>> mapList = new ArrayList<>();
        LayerDto layerDto = new LayerDto();
        DataSource dataSource = null;
        try {
            dataSource = driver.Open(filePath, 0);
@@ -208,10 +187,10 @@
                        feature.delete();
                    } while (true);
                }
                layerDto.setLayerName(strlayerName);
                layerDto.setList(list);
                layerDto.setCount(count);
                layerDto.setMap(mapList);
                //layerDto.setLayerName(strlayerName);
                //layerDto.setList(list);
                //layerDto.setCount(count);
                //layerDto.setMap(mapList);
            }
        } catch (Exception e) {
            e.printStackTrace();
@@ -220,11 +199,11 @@
                dataSource.delete();
            }
        }
        return layerDto;
    }
    /**
     * 读取gdb文件
     *
     * @param filePath
     */
    public static void readGdb(String filePath) {
@@ -277,7 +256,7 @@
            return propertyGetter.get(feature, index);
        } catch (Exception ex) {
            // ex.printStackTrace()
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
            log.error(ex.getMessage(), ex);
            return null;
        }