管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2023-08-16 6db54cb373ae0a692f64ca916712f63d312e397d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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 };
        }
    }
}