From 1fd236eff8e31047d30ccfbd94bd329d861f8d4d Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期五, 25 八月 2023 16:29:22 +0800 Subject: [PATCH] 添加读取栅格信息功能 --- src/main/java/com/moon/server/service/data/RasterService.java | 105 ++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 72 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/moon/server/service/data/RasterService.java b/src/main/java/com/moon/server/service/data/RasterService.java index be905d5..b82a7a1 100644 --- a/src/main/java/com/moon/server/service/data/RasterService.java +++ b/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,46 @@ return; } - ds = gdal.Open(file, 0); + ds = gdal.Open(file, gdalconst.GA_ReadOnly); if (null == ds || ds.getRasterCount() == 0) { return; } - // EPSG缂栫爜 - String epsg = getEpsg(ds); - // 娉㈡鏁� - String bands = "" + ds.getRasterCount(); - //vd.Meta.band_type = Enum.GetName(typeof(DataType), ds.GetRasterBand(1).DataType); // 鏁版嵁绫诲瀷 - ColorTable colorTable = ds.GetRasterBand(1).GetRasterColorTable(); - // 鏁版嵁棰滆壊琛� - String ct = null == colorTable ? null : colorTable.toString(); - // 楂樼▼鍩哄噯 - String hDatum = null; - - double[] transform = new double[6]; - ds.GetGeoTransform(transform); - // 鍒嗚鲸鐜� - String resolution = String.format("%f,%f", transform[1], transform[5]); - - if (!StaticData.EPSGS.contains(epsg)) { - return; + SpatialReference sr = ds.GetSpatialRef(); + if (sr != null) { + // 鍧愭爣绯荤粺 + mf.setCoorSys(sr.GetName()); + if (StaticData.MOON200.equals(mf.getCoorSys())) { + // EPSG缂栫爜 + mf.setEpsg("ESRI:" + StaticData.I104903); + } else { + String code = sr.GetAuthorityCode(null); + // EPSG缂栫爜 + mf.setEpsg(StringHelper.isEmpty(code) ? null : "EPSG:" + code); + } } + // 琛屽垪鏁� + mf.setGridsize(String.format("%d,%d", ds.getRasterXSize(), ds.getRasterYSize())); + // 娉㈡鏁� + mf.setBands(String.valueOf(ds.getRasterCount())); + // 鏁版嵁绫诲瀷 + int dataType = ds.GetRasterBand(1).GetRasterDataType(); + mf.setBandType(getDataType(dataType)); + // 鏁版嵁棰滆壊琛� + ColorTable colorTable = ds.GetRasterBand(1).GetRasterColorTable(); + mf.setCt(null == colorTable ? null : colorTable.toString()); + // 楂樼▼鍩哄噯 + mf.sethDatum(null); + + // 鍒嗚鲸鐜� + double[] transform = new double[6]; + ds.GetGeoTransform(transform); + mf.setResolution(String.format("%f,%f", transform[1], transform[5])); + + if (!StaticData.EPSGS.contains(mf.getEpsg())) { + return; + } Geometry minPoint = getMinPoint(ds); Geometry maxPoint = getMaxPoint(ds); double xmin = minPoint.GetX(0); @@ -90,6 +106,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 +117,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; -- Gitblit v1.9.3