| | |
| | | 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; |
| | |
| | | /** |
| | | * 读取栅格信息 |
| | | */ |
| | | public void readRasterInfo(MetaEntity me, String file) { |
| | | public void readRasterInfo(MetaFileEntity mf, String file) { |
| | | Dataset ds = null; |
| | | try { |
| | | File f = new File(file); |
| | |
| | | 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); |
| | |
| | | |
| | | // 四至范围 |
| | | 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 { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取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; |