月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-08-30 4e7ae7e903a07389607cf47ad8f2bbd5e8ccd8d6
src/main/java/com/moon/server/service/data/RasterService.java
@@ -1,13 +1,14 @@
package com.moon.server.service.data;
import com.moon.server.entity.all.StaticData;
import com.moon.server.entity.data.MetaEntity;
import com.moon.server.entity.data.MetaFileEntity;
import com.moon.server.helper.StringHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gdal.gdal.ColorTable;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.ogr.Geometry;
import org.gdal.ogr.ogr;
import org.gdal.osr.SpatialReference;
@@ -48,7 +49,7 @@
    /**
     * 读取栅格信息
     */
    public void readRasterInfo(MetaEntity me, String file) {
    public void readRasterInfo(MetaFileEntity mf, String file) {
        Dataset ds = null;
        try {
            File f = new File(file);
@@ -56,31 +57,44 @@
                return;
            }
            ds = gdal.Open(file, 0);
            if (null == ds || ds.getRasterCount() == 0) {
            ds = gdal.Open(file, gdalconst.GA_ReadOnly);
            if (null == ds || ds.getRasterCount() == 0 || null == ds.GetSpatialRef()) {
                return;
            }
            // EPSG编码
            String epsg = getEpsg(ds);
            SpatialReference sr = ds.GetSpatialRef();
            // 坐标系统
            mf.setCoorSys(sr.GetName());
            if (StaticData.MOON200.equals(mf.getCoorSys())) {
                // EPSG编码
                mf.setEpsg("EPSG:" + StaticData.I104903);
            } else {
                // EPSG编码:PROJCS、GEOGCS、GEOGCS|UNIT 或 NULL
                String code = sr.GetAuthorityCode(null);
                mf.setEpsg(StringHelper.isEmpty(code) ? null : "EPSG:" + code);
            }
            // 行列数
            mf.setGridsize(String.format("%d,%d", ds.getRasterXSize(), ds.getRasterYSize()));
            // 波段数
            String bands = "" + ds.getRasterCount();
            //vd.Meta.band_type = Enum.GetName(typeof(DataType), ds.GetRasterBand(1).DataType); // 数据类型
            ColorTable colorTable = ds.GetRasterBand(1).GetRasterColorTable();
            mf.setBands(String.valueOf(ds.getRasterCount()));
            // 数据类型
            int dataType = ds.GetRasterBand(1).GetRasterDataType();
            mf.setBandType(getDataType(dataType));
            // 数据颜色表
            String ct = null == colorTable ? null : colorTable.toString();
            ColorTable colorTable = ds.GetRasterBand(1).GetRasterColorTable();
            mf.setCt(null == colorTable ? null : colorTable.toString());
            // 高程基准
            String hDatum = null;
            mf.sethDatum(null);
            double[] transform = new double[6];
            ds.GetGeoTransform(transform);
            // 分辨率
            String resolution = String.format("%f,%f", transform[1], transform[5]);
            double[] tr = new double[6];
            ds.GetGeoTransform(tr);
            mf.setResolution(String.format("%f,%f", tr[1], tr[5]));
            if (!StaticData.EPSGS.contains(epsg)) {
            if (tr[StaticData.I0] == 0.0 && tr[StaticData.I1] == 1.0 && tr[StaticData.I2] == 0.0 && tr[StaticData.I3] == 0.0 && tr[StaticData.I4] == 0.0 && tr[StaticData.I5] == 1.0) {
                return;
            }
            Geometry minPoint = getMinPoint(ds);
            Geometry maxPoint = getMaxPoint(ds);
            double xmin = minPoint.GetX(0);
@@ -90,6 +104,7 @@
            // 四至范围
            String geom = String.format("ST_GeomFromText('POLYGON ((%f %f,%f %f,%f %f,%f %f,%f %f))')", xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin, xmin, ymax);
            mf.setGeom(geom);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
@@ -100,19 +115,41 @@
    }
    /**
     * 获取EPSG编码
     * 获取数据类型
     */
    private String getEpsg(Dataset ds) {
        SpatialReference sr = ds.GetSpatialRef();
        if (sr != null) {
            // 坐标系统
            String coorSys = sr.GetName();
            if (StaticData.MOON200.equals(coorSys)) {
                return "ESRI:" + StaticData.I104903;
            } else {
                String code = sr.GetAuthorityCode(null);
                return StringHelper.isEmpty(code) ? null : "EPSG:" + code;
            }
    private String getDataType(int dataType) {
        if (dataType == gdalconst.GDT_Byte) {
            return "GDT_Byte";
        }
        if (dataType == gdalconst.GDT_UInt16) {
            return "GDT_UInt16";
        }
        if (dataType == gdalconst.GDT_Int16) {
            return "GDT_Int16";
        }
        if (dataType == gdalconst.GDT_UInt32) {
            return "GDT_UInt32";
        }
        if (dataType == gdalconst.GDT_Int32) {
            return "GDT_Int32";
        }
        if (dataType == gdalconst.GDT_Float32) {
            return "GDT_Float32";
        }
        if (dataType == gdalconst.GDT_Float64) {
            return "GDT_Float64";
        }
        if (dataType == gdalconst.GDT_CInt16) {
            return "GDT_CInt16";
        }
        if (dataType == gdalconst.GDT_CInt32) {
            return "GDT_CInt32";
        }
        if (dataType == gdalconst.GDT_CFloat32) {
            return "GDT_CFloat32";
        }
        if (dataType == gdalconst.GDT_CFloat64) {
            return "GDT_CFloat64";
        }
        return null;
@@ -125,58 +162,58 @@
        double[] transform = new double[6];
        ds.GetGeoTransform(transform);
        String epsg = ds.GetSpatialRef().GetAuthorityCode(null);
        double xMin = transform[0];
        double yMin = transform[3] - ds.getRasterYSize() * transform[1];
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AddPoint(xMin, yMin, 0);
        return transform(ds, point, epsg);
        return transform(ds, point);
    }
    /**
     * 获取Dataset的最大点
     */
    private Geometry getMaxPoint(Dataset ds) {
        /**
         * transform[0] 左上角x坐标
         * transform[1] 东西方向分辨率
         * transform[2] 旋转角度, 0表示图像 "北方朝上"
         *
         * transform[3] 左上角y坐标
         * transform[4] 旋转角度, 0表示图像 "北方朝上"
         * transform[5] 南北方向分辨率
         */
        double[] transform = new double[6];
        ds.GetGeoTransform(transform);
        String epsg = ds.GetSpatialRef().GetAuthorityCode(null);
        double xMax = transform[0] + (ds.getRasterXSize() * transform[1]);
        double yMax = transform[3];
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AddPoint(xMax, yMax, 0);
        return transform(ds, point, epsg);
        return transform(ds, point);
    }
    /**
     * 坐标转换
     */
    private Geometry transform(Dataset ds, Geometry point, String epsg) {
    private Geometry transform(Dataset ds, Geometry point) {
        this.initSpatialReference();
        if (StringHelper.isEmpty(epsg)) {
            point.AssignSpatialReference(sr104903);
            return point;
        }
        if (String.valueOf(StaticData.I4326).equals(epsg)) {
            point.AssignSpatialReference(sr4326);
            return point;
        }
        if (String.valueOf(StaticData.I4490).equals(epsg)) {
            point.AssignSpatialReference(sr4490);
        point.AssignSpatialReference(ds.GetSpatialRef());
        if (ds.GetSpatialRef().IsGeographic() > 0) {
            return point;
        }
        point.AssignSpatialReference(ds.GetSpatialRef());
        if (ds.GetSpatialRef().GetName().contains(StaticData.CGCS2000)) {
        String srsName = ds.GetSpatialRef().GetName();
        if (srsName.contains(StaticData.CGCS2000)) {
            point.TransformTo(sr4490);
        } else {
            point.TransformTo(sr4326);
        }
        point.SwapXY();
        return point;
    }