| | |
| | | } |
| | | |
| | | ds = gdal.Open(file, gdalconst.GA_ReadOnly); |
| | | if (null == ds || ds.getRasterCount() == 0) { |
| | | if (null == ds || ds.getRasterCount() == 0 || null == ds.GetSpatialRef()) { |
| | | 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.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.sethDatum(null); |
| | | |
| | | // 分辨率 |
| | | double[] transform = new double[6]; |
| | | ds.GetGeoTransform(transform); |
| | | mf.setResolution(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(mf.getEpsg())) { |
| | | 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); |
| | |
| | | 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; |
| | | } |