using OSGeo.GDAL; using OSGeo.OGR; using OSGeo.OSR; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DataLoader.CS { public class GdalHelper { private static bool isInited = false; private static GdalHelper instance = null; private static readonly object obj = new object(); public static SpatialReference sr4326 = null; public static SpatialReference sr104903 = null; public static string MOON200 = "GCS_Moon_2000"; private GdalHelper() { lock (obj) { if (!isInited) { RegisterGDal(); sr4326 = new SpatialReference(null); sr4326.ImportFromEPSG(4326); string wkt = "GEOGCS[\"GCS_Moon_2000\",\r\n" + " DATUM[\"D_Moon_2000\",\r\n" + " SPHEROID[\"Moon_2000_IAU_IAG\",1737400,0,\r\n" + " AUTHORITY[\"ESRI\",\"107903\"]],\r\n" + " AUTHORITY[\"ESRI\",\"106903\"]],\r\n" + " PRIMEM[\"Reference_Meridian\",0,\r\n" + " AUTHORITY[\"ESRI\",\"108900\"]],\r\n" + " UNIT[\"degree\",0.0174532925199433,\r\n" + " AUTHORITY[\"EPSG\",\"9122\"]],\r\n" + " AUTHORITY[\"ESRI\",\"104903\"]]"; sr104903 = new SpatialReference(wkt); //sr104903.ImportFromWkt(ref wkt); isInited = true; } } } public static GdalHelper Instance { get { lock (obj) { if (null == instance) instance = new GdalHelper(); return instance; } } } public void RegisterGDal() { string gdalData = Path.Combine(StaticData.BasePath, "gdal-data"); Environment.SetEnvironmentVariable("GDAL_DATA", gdalData); string proj7 = Path.Combine(StaticData.BasePath, "proj7\\share"); Environment.SetEnvironmentVariable("PROJ_LIB", proj7); Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); // NO,YES Gdal.SetConfigOption("SHAPE_ENCODING", ""); // 空,gb2312,CP936 Ogr.RegisterAll(); Gdal.AllRegister(); } public void ReadTiff() { string tif = "D:\\Moon\\data\\dom_tif\\moon.tif"; Dataset ds = Gdal.Open(tif, Access.GA_ReadOnly); int x = ds.RasterXSize; int y = ds.RasterYSize; SpatialReference sr = ds.GetSpatialRef(); string srName = sr.GetName(); // GCS_Moon_2000 // string code = sr4326.GetAuthorityCode(null); // 4326 if (MOON200 != srName) return; double[] lt = ImageToGeoSpace(ds, 0, 0); // -179.99998351102391, 89.999983511056882 double[] rb = ImageToGeoSpace(ds, x, y); // 179.999788852709, -89.9999026708093 string polygon = string.Format("", ""); } // 从地理空间转换到像素空间 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) { 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]; return new double[] { x, y }; } } }