张洋洋
2025-02-24 9804628abf554c3658345fc8fc9472cfb179fd5f
src/main/java/com/se/simu/config/InitConfig.java
@@ -1,8 +1,16 @@
package com.se.simu.config;
import com.se.simu.helper.CaffeineHelper;
import com.se.simu.helper.GdalHelper;
import com.se.simu.helper.WebHelper;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.Driver;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.ogr.*;
import org.gdal.osr.SpatialReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
@@ -10,21 +18,21 @@
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/**
 * 初始化配置类
 *
 * @author WWW
 * @date 2024-07-16
 */
@Slf4j
@Component
@SuppressWarnings("ALL")
public class InitConfig implements ApplicationRunner {
    @Resource
    Environment env;
    @Value("${server.port}")
    String serverPort;
    @Value("${config.cacheTime}")
    Integer cacheTime;
    @Value("${server.servlet.context-path}")
    String contextPath;
@@ -34,15 +42,143 @@
        // noinspection AlibabaRemoveCommentedCode
        try {
            log.info("***************** 初始化 GDAL *****************" + "\n");
            GdalHelper.init(env.getProperty("sys.path.gdal"));
            GdalHelper.init(env.getProperty("config.gdalPath"));
            CaffeineHelper.init(cacheTime);
            String path = null != contextPath && contextPath.length() > 1 ? contextPath : "";
            log.info("API文档:http://localhost:" + serverPort + path + "/doc.html");
            log.info("API文档:http://{}:{}{}/doc.html", WebHelper.getHostIp(), serverPort, path);
            log.info("***************** 系统启动完毕 *****************" + "\n");
            //log.info("***************** 读取tif文件 *****************" + "\n");
            // 读取tif文件
            //readTif("D:\\soft\\env\\tif\\srtm_12_03.tif");
            //log.info("***************** 读取tif文件完毕 *****************" + "\n");
            //log.info("***************** 读取shp文件 *****************" + "\n");
            // 读取shp文件
            //readShp("D:\\soft\\env\\雨量站点数据\\雨量站点_84\\雨量站点_84.shp");
            //log.info("***************** 读取shp文件完毕 *****************" + "\n");
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
    /**
     * 读取tif文件
     *
     * @param fileName
     */
    public static void readTif(String fileName) {
        // 读取影像数据
        Dataset dataset = gdal.Open(fileName, gdalconstConstants.GA_ReadOnly);
        if (dataset == null) {
            System.out.println("read fail!");
            return;
        }
        // providing various methods for a format specific driver.
        Driver driver = dataset.GetDriver();
        System.out.println("driver name: " + driver.getLongName());
        // 读取影像信息
        int xSize = dataset.getRasterXSize();
        int ySzie = dataset.getRasterYSize();
        int rasterCount = dataset.getRasterCount();
        System.out.println("dataset xSize: " + xSize + ", ySzie = " + ySzie + ", rasterCount = " + rasterCount);
        Band band = dataset.GetRasterBand(1);
        // the data type of the band.
        int type = band.GetRasterDataType();
        System.out.println("data type = " + type + ", " + (type == gdalconstConstants.GDT_Byte));
        // Frees the native resource associated to a Dataset object and close the file.
        dataset.delete();
        gdal.GDALDestroyDriverManager();
    }
    /**
     * 读取 Shapefile 文件并打印属性和几何数据
     *
     * @param shapefilePath SHP 文件路径
     */
    public void readShp(String strVectorFile) {
        // 注册所有的驱动
        ogr.RegisterAll();
        // 为了支持中文路径,请添加下面这句代码
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        // 为了使属性表字段支持中文,请添加下面这句
        gdal.SetConfigOption("SHAPE_ENCODING", "CP936");
        // 读取数据,这里以ESRI的shp文件为例
        String strDriverName = "ESRI Shapefile";
        // 创建一个文件,根据strDriverName扩展名自动判断驱动类型
        org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName);
        if (oDriver == null) {
            System.out.println(strDriverName + " 驱动不可用!\n");
            return;
        }
        DataSource dataSource = oDriver.Open(strVectorFile);
        //Layer layer = dataSource.GetLayer("test");
        Layer layer = dataSource.GetLayer(0);
        for (int i = 0; i < dataSource.GetLayerCount(); i++) {
            Layer layerIdx = dataSource.GetLayer(i);
            System.out.println("图层名称:<==>" + layerIdx.GetName());
        }
        String layerName = layer.GetName();
        System.out.println("图层名称:" + layerName);
        SpatialReference spatialReference = layer.GetSpatialRef();
        //System.out.println(spatialReference);
        System.out.println("空间参考坐标系:" + spatialReference.GetAttrValue("AUTHORITY", 0)
                + spatialReference.GetAttrValue("AUTHORITY", 1));
        double[] layerExtent = layer.GetExtent();
        System.out.println("图层范围:minx:" + layerExtent[0] + ",maxx:" + layerExtent[1] + ",miny:" + layerExtent[2] + ",maxy:" + layerExtent[3]);
        FeatureDefn featureDefn = layer.GetLayerDefn();
        int fieldCount = featureDefn.GetFieldCount();
        Map<String, String> fieldMap = new HashMap<String, String>();
        for (int i = 0; i < fieldCount; i++) {
            FieldDefn fieldDefn = featureDefn.GetFieldDefn(i);
            // 得到属性字段类型
            int fieldType = fieldDefn.GetFieldType();
            String fieldTypeName = fieldDefn.GetFieldTypeName(fieldType);
            // 得到属性字段名称
            String fieldName = fieldDefn.GetName();
            fieldMap.put(fieldTypeName, fieldName);
        }
        System.out.println();
        System.out.println("fileMap:");
        System.out.println(fieldMap);
        System.out.println(layer.GetFeature(1).GetGeometryRef().ExportToJson());
        System.out.println(layer.GetFeature(2).GetGeometryRef().ExportToJson());
        System.out.println(layer.GetFeature(3).GetGeometryRef().ExportToJson());
        for (int i = 0; i < 12; i++) {
            Feature feature = layer.GetFeature(i);
            Object[] arr = fieldMap.values().toArray();
            for (int k = 0; k < arr.length; k++) {
                String fvalue = feature.GetFieldAsString(arr[k].toString());
                System.out.println(" 属性名称:" + arr[k].toString() + ",属性值:" + fvalue);
            }
        }
    }
}