From a955e13d33579ed03ffdc8ce2f09671425a89920 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 23 八月 2023 17:34:20 +0800 Subject: [PATCH] 添加读取栅格信息的接口 --- DataLoader/CS/GdalHelper.cs | 191 ++++++++++++++++++++++++++++++++++++----------- DataLoader/CS/Importor.cs | 17 +++ 2 files changed, 162 insertions(+), 46 deletions(-) diff --git a/DataLoader/CS/GdalHelper.cs b/DataLoader/CS/GdalHelper.cs index 02f508e..1d74d74 100644 --- a/DataLoader/CS/GdalHelper.cs +++ b/DataLoader/CS/GdalHelper.cs @@ -1,14 +1,11 @@ -锘縰sing OSGeo.GDAL; +锘縰sing DataLoader.Model; +using OSGeo.GDAL; using OSGeo.OGR; using OSGeo.OSR; using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Linq; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Markup; namespace DataLoader.CS { @@ -23,10 +20,17 @@ public static SpatialReference sr4326 = null; + public static SpatialReference sr4490 = null; + public static SpatialReference sr104903 = null; public static string MOON200 = "GCS_Moon_2000"; + public static List<string> EPSGS = new List<string>() { "EPSG:4326", "EPSG:4490", "ESRI:104903" }; + + /// <summary> + /// 鏋勯�犲嚱鏁� + /// </summary> private GdalHelper() { lock (obj) @@ -37,6 +41,9 @@ sr4326 = new SpatialReference(null); sr4326.ImportFromEPSG(4326); + + sr4490 = new SpatialReference(null); + sr4490.ImportFromEPSG(4490); string wkt = "GEOGCS[\"GCS_Moon_2000\",\r\n" + " DATUM[\"D_Moon_2000\",\r\n" + @@ -56,6 +63,9 @@ } } + /// <summary> + /// 瀹炰緥 + /// </summary> public static GdalHelper Instance { get @@ -69,6 +79,9 @@ } } + /// <summary> + /// 娉ㄥ唽GDAL + /// </summary> public void RegisterGDal() { string gdalData = Path.Combine(Tools.BaseDir, "gdal-data"); @@ -85,57 +98,147 @@ } #endregion - public void ReadTiff() + #region 璇诲彇鏍呮牸淇℃伅 + /// <summary> + /// 璇诲彇鏍呮牸淇℃伅 + /// </summary> + public void ReadRasterInfo(ViewData vd) { - string outPath = "D:\\xyz\\ce\\xyz"; - string tif = "D:\\Moon\\data\\dom_tif\\moon.tif"; + Dataset ds = null; + try + { + ds = Gdal.Open(vd.FilePath, Access.GA_ReadOnly); + if (null == ds || ds.RasterCount == 0) return; - Dataset ds = Gdal.Open(tif, Access.GA_ReadOnly); - int x = ds.RasterXSize; - int y = ds.RasterYSize; + vd.Meta.gridsize = string.Format("{0},{1}", ds.RasterXSize, ds.RasterYSize); // 琛屽垪鏁� - SpatialReference sr = ds.GetSpatialRef(); - string srName = sr.GetName(); // GCS_Moon_2000 - // string code = sr4326.GetAuthorityCode(null); // 4326 - if (MOON200 != srName) return; + SpatialReference sr = ds.GetSpatialRef(); + if (sr != null) + { + vd.Meta.coor_sys = sr.GetName(); // 鍧愭爣绯荤粺 + if (MOON200 == vd.Meta.coor_sys) + { + vd.Meta.epsg = "ESRI:104903"; // EPSG缂栫爜 + } + else + { + string code = sr.GetAuthorityCode(null); + vd.Meta.epsg = string.IsNullOrEmpty(code) ? null : "EPSG:" + code; // EPSG缂栫爜 + } + } - double[] lt = ImageToGeoSpace(ds, 0, 0); // -179.99998351102391, 89.999983511056882 - double[] rb = ImageToGeoSpace(ds, x, y); // 179.999788852709, -89.9999026708093 + vd.Meta.bands = ds.RasterCount.ToString(); // 娉㈡鏁� + vd.Meta.band_type = Enum.GetName(typeof(DataType), ds.GetRasterBand(1).DataType); // 鏁版嵁绫诲瀷 + vd.Meta.ct = ds.GetRasterBand(1).GetRasterColorTable().ToString(); // 鏁版嵁棰滆壊琛� + vd.Meta.h_datum = null; // 楂樼▼鍩哄噯 - string polygon = string.Format("", ""); + double[] transform = new double[6]; + ds.GetGeoTransform(transform); + vd.Meta.resolution = string.Format("{0},{1}", transform[1], transform[5]); // 鍒嗚鲸鐜� + + if (!EPSGS.Contains(vd.Meta.epsg)) return; + + Geometry minPoint = GetMinPoint(ds); + Geometry maxPoint = GetMaxPoint(ds); + double xmin = minPoint.GetX(0); + double ymin = minPoint.GetY(0); + double xmax = maxPoint.GetX(0); + double ymax = maxPoint.GetY(0); + string wkt = string.Format("POLYGON (({0} {1},{2} {3},{4} {5},{6} {7},{0} {1}))", xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin); + + vd.Meta.geom = wkt; // 鍥涜嚦鑼冨洿 + } + catch (Exception ex) + { + LogOut.Error(ex.StackTrace); + } + finally + { + if (ds != null) ds.Dispose(); + } } - // 浠庡湴鐞嗙┖闂磋浆鎹㈠埌鍍忕礌绌洪棿 - private int[] Geo2ImageSpace(Dataset ds, double x, double y) - { - double[] transform = new double[6]; - ds.GetGeoTransform(transform); // 褰卞儚鍧愭爣鍙樻崲鍙傛暟 - - int col = (int)((y * transform[1] - x * transform[4] + transform[0] * transform[4] - transform[3] * transform[1]) / (transform[5] * transform[1] - transform[2] * transform[4])); // 鍍忕礌鎵�鍦ㄥ垪 - int row = (int)((x - transform[0] - col * transform[2]) / transform[1]); // 鍍忕礌鎵�鍦ㄨ - - return new int[] { row, col }; - } - - // 浠庡湴鐞嗙┖闂磋浆鎹㈠埌鍍忕礌绌洪棿 - private int[] Geo2ImageSpace(double[] transform, double x, double y) - { - int col = (int)((y * transform[1] - x * transform[4] + transform[0] * transform[4] - transform[3] * transform[1]) / (transform[5] * transform[1] - transform[2] * transform[4])); // 鍍忕礌鎵�鍦ㄥ垪 - int row = (int)((x - transform[0] - col * transform[2]) / transform[1]); // 鍍忕礌鎵�鍦ㄨ - - return new int[] { row, col }; - } - - // 浠庡儚绱犵┖闂磋浆鎹㈠埌鍦扮悊绌洪棿 - private double[] ImageToGeoSpace(Dataset ds, int row, int col) + /// <summary> + /// 鑾峰彇Dataset鐨勬渶灏忕偣 + /// </summary> + private Geometry GetMinPoint(Dataset ds) { double[] transform = new double[6]; ds.GetGeoTransform(transform); - double x = transform[0] + row * transform[1] + col * transform[2]; - double y = transform[3] + row * transform[4] + col * transform[5]; + string epsg = ds.GetSpatialRef().GetAuthorityCode(null); + double xMin = transform[0]; + double yMin = transform[3]; - return new double[] { x, y }; + Geometry point = new Geometry(wkbGeometryType.wkbPoint); + point.AddPoint(xMin, yMin, 0); + + return Transform(ds, point, epsg); } + + /// <summary> + /// 鑾峰彇Dataset鐨勬渶澶х偣 + /// </summary> + private Geometry GetMaxPoint(Dataset ds) + { + double[] transform = new double[6]; + ds.GetGeoTransform(transform); + + string epsg = ds.GetSpatialRef().GetAuthorityCode(null); + double xMax = transform[0] + (ds.RasterXSize * transform[1]); + double yMax = transform[3] + (ds.RasterYSize * transform[1]); + + Geometry point = new Geometry(wkbGeometryType.wkbPoint); + point.AddPoint(xMax, yMax, 0); + + return Transform(ds, point, epsg); + } + + /// <summary> + /// 鍧愭爣杞崲 + /// </summary> + private Geometry Transform(Dataset ds, Geometry point, string epsg) + { + if (string.IsNullOrEmpty(epsg)) + { + point.AssignSpatialReference(sr104903); + return point; + } + if ("4326" == epsg) + { + point.AssignSpatialReference(sr4326); + return point; + } + if ("4490" == epsg) + { + point.AssignSpatialReference(sr4490); + return point; + } + + point.AssignSpatialReference(ds.GetSpatialRef()); + if (ds.GetSpatialRef().GetName().Contains("CGCS2000")) + { + point.TransformTo(sr4490); + } + else + { + point.TransformTo(sr4326); + } + + return point; + } + + /// <summary> + /// 鍒涘缓澶氳竟褰� + /// </summary> + public Geometry CreatePolygon(double xmin, double xmax, double ymin, double ymax, SpatialReference sr) + { + string kwt = string.Format("POLYGON (({0} {1},{2} {3},{4} {5},{6} {7},{0} {1}))", xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin); + Geometry geo = Geometry.CreateFromWkt(kwt); + geo.AssignSpatialReference(sr); + + return geo; + } + #endregion } } diff --git a/DataLoader/CS/Importor.cs b/DataLoader/CS/Importor.cs index d9fa9c5..5a717d0 100644 --- a/DataLoader/CS/Importor.cs +++ b/DataLoader/CS/Importor.cs @@ -239,7 +239,7 @@ { try { - vd.Status = "鐢熸垚MD5..."; + vd.Status = "鐢熸垚MD5鐮�..."; vd.Meta.guid = GetFilesMD5(vd); if (!string.IsNullOrEmpty(vd.Meta.guid) && DBHelper.IsFileExists(vd.Meta.guid)) @@ -248,10 +248,13 @@ return; } + vd.Status = "璇诲彇鏍呮牸淇℃伅..."; + ReadRasterInfo(vd); + vd.Status = "澶嶅埗鏂囦欢..."; CopyFiles(vd); - vd.Status = "鏁版嵁鍏ュ簱..."; + vd.Status = "鍏冩暟鎹叆搴�..."; int id = DBHelper.InsertMeta(vd.Meta); vd.Status = id > 0 ? "鎴愬姛銆�" : "澶辫触锛�"; } @@ -315,6 +318,16 @@ } /// <summary> + /// 璇诲彇鏍呮牸淇℃伅 + /// </summary> + private static void ReadRasterInfo(ViewData vd) + { + if (!StaticData.RASTER_EXT.Contains(vd.Ext) || !File.Exists(vd.FilePath)) return; + + GdalHelper.Instance.ReadRasterInfo(vd); + } + + /// <summary> /// 澶嶅埗鏂囦欢 /// </summary> private static void CopyFiles(ViewData vd) -- Gitblit v1.9.3