管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-07-18 9f044bbbf3fa5b67fd1d120140f15f0b3729f3e8
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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 SimuTools.Tools
{
    public class GdalHelper
    {
        private static bool isInited;
 
        private static GdalHelper instance;
 
        private static readonly object obj = new object();
 
        public static SpatialReference sr4326 { set; get; }
 
        public static SpatialReference sr4490 { set; get; }
 
        public static readonly String CGCS2000 = "CGCS2000";
 
        /// <summary>
        /// 构造函数
        /// </summary>
        private GdalHelper()
        {
            lock (obj)
            {
                if (!isInited)
                {
                    RegisterGDal();
 
                    sr4326 = new SpatialReference(null);
                    sr4326.ImportFromEPSG(4326);
 
                    sr4490 = new SpatialReference(null);
                    sr4490.ImportFromEPSG(4490);
 
                    isInited = true;
                }
            }
        }
 
        /// <summary>
        /// 实例
        /// </summary>
        public static GdalHelper Instance
        {
            get
            {
                lock (obj)
                {
                    if (null == instance)
                    {
                        instance = new GdalHelper();
                    }
 
                    return instance;
                }
            }
        }
 
        /// <summary>
        /// 注册GDAL
        /// </summary>
        public void RegisterGDal()
        {
            string gdalData = Path.Combine(Handle.BaseDir, "gdal-data");
            Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
 
            string proj7 = Path.Combine(Handle.BaseDir, "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();
        }
 
        /**
         * 获取Dataset的最小点
         */
        public static Geometry GetMinPoint(Dataset ds)
        {
            double[] transform = new double[6];
            ds.GetGeoTransform(transform);
 
            double xMin = transform[0];
            double yMin = transform[3] - ds.RasterYSize * transform[1];
 
            Geometry point = new Geometry(wkbGeometryType.wkbPoint);
            point.AddPoint(xMin, yMin, 0);
 
            return Transform(ds, point);
        }
 
        /**
         * 获取Dataset的最大点
         */
        public static 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);
 
            double xMax = transform[0] + (ds.RasterYSize * transform[1]);
            double yMax = transform[3];
 
            Geometry point = new Geometry(wkbGeometryType.wkbPoint);
            point.AddPoint(xMax, yMax, 0);
 
            return Transform(ds, point);
        }
 
        /**
         * 坐标转换
         */
        public static Geometry Transform(Dataset ds, Geometry point)
        {
            point.AssignSpatialReference(ds.GetSpatialRef());
            if (ds.GetSpatialRef().IsGeographic() > 0)
            {
                return point;
            }
 
            String srsName = ds.GetSpatialRef().GetName();
            if (srsName.Contains(CGCS2000))
            {
                point.TransformTo(sr4490);
            }
            else
            {
                point.TransformTo(sr4326);
            }
            point.SwapXY();
 
            return point;
        }
 
        /**
         * 创建金字塔
         */
        public static void CreatePyramid(String file)
        {
            Dataset ds = null;
            try
            {
                if (!File.Exists(file)) return;
 
                ds = Gdal.Open(file, Access.GA_ReadOnly);
                if (null == ds) return;
 
                Band band = ds.GetRasterBand(1);
                if (0 == band.GetOverviewCount())
                {
                    ds.BuildOverviews("nearest", new int[] { 2, 4, 6, 8, 16 });
                }
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message);
            }
            finally
            {
                if (null != ds) ds.Dispose();
            }
        }
    }
}