/
SqlProjection.cs
278 lines (247 loc) · 9.49 KB
/
SqlProjection.cs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//------------------------------------------------------------------------------
// Copyright (c) 2019 Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Types;
using SQLSpatialTools.Projections;
using SQLSpatialTools.Sinks.Geography;
using SQLSpatialTools.Sinks.Geometry;
using SQLSpatialTools.Utility;
namespace SQLSpatialTools.Types.SQL
{
[Serializable]
[SqlUserDefinedType(Format.UserDefined, IsByteOrdered = false, MaxByteSize = -1, IsFixedLength = false)]
public sealed class SqlProjection : INullable, IBinarySerialize
{
private Projection _projection;
public SqlProjection()
{
}
internal SqlProjection(Projection projection)
{
Debug.Assert(projection != null);
_projection = projection;
}
public static SqlProjection Null
{
[SqlMethod(IsDeterministic = true, IsPrecise = true)]
get => new SqlProjection();
}
// TODO use WKT projection description format
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public static SqlProjection Parse(SqlString s)
{
if (s.Value.Equals("NULL"))
return Null;
var a = s.Value.Split(' ');
var cons = Type.GetType(a[0])?.GetConstructor(new[] { typeof(Dictionary<string, double>) });
return cons != null ? new SqlProjection((Projection) cons.Invoke(new object[] {Projection.ParseParameters(a[1])})) : Null;
}
// TODO use WKT projection description format
[return: SqlFacet(MaxSize = -1)]
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public override string ToString()
{
if (_projection == null)
{
return "NULL";
}
else
{
return _projection.GetType().FullName + " " + _projection.Parameters;
}
}
public bool IsNull
{
[SqlMethod(IsDeterministic = true, IsPrecise = true)]
get => _projection == null;
}
public void Read(BinaryReader r)
{
if (r == null) return;
var name = r.ReadString();
if (string.IsNullOrEmpty(name))
{
_projection = null;
}
else
{
var cons = Type.GetType(name)?.GetConstructor(new[] { typeof(Dictionary<string, double>) });
if (cons != null)
_projection = (Projection) cons.Invoke(new object[] {Projection.ParseParameters(r.ReadString())});
}
}
public void Write(BinaryWriter w)
{
if (w == null) return;
if (_projection == null)
{
w.Write("");
}
else
{
w.Write(_projection.GetType().FullName ?? throw new InvalidOperationException());
w.Write(_projection.Parameters);
}
}
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public static SqlProjection AlbersEqualArea(double longitude0, double latitude0, double parallel1, double parallel2)
{
var parameters = new Dictionary<string, double>
{
["longitude0"] = longitude0,
["latitude0"] = latitude0,
["parallel1"] = parallel1,
["parallel2"] = parallel2
};
return new SqlProjection(new AlbersEqualAreaProjection(parameters));
}
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public static SqlProjection Equirectangular(double longitude0, double parallel)
{
var parameters = new Dictionary<string, double> {["longitude0"] = longitude0, ["parallel"] = parallel};
return new SqlProjection(new EquirectangularProjection(parameters));
}
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public static SqlProjection LambertConformalConic(double longitude0, double latitude, double fi1, double fi2)
{
var parameters = new Dictionary<string, double>
{
["longitude0"] = longitude0, ["latitude0"] = latitude, ["fi1"] = fi1, ["fi2"] = fi2
};
return new SqlProjection(new LambertConformalConicProjection(parameters));
}
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public static SqlProjection Mercator(double longitude0)
{
var parameters = new Dictionary<string, double> {["longitude0"] = longitude0};
return new SqlProjection(new MercatorProjection(parameters));
}
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public static SqlProjection ObliqueMercator(double longitude0, double fi1, double lambda1, double fi2, double lambda2)
{
var parameters = new Dictionary<string, double>
{
["longitude0"] = longitude0,
["fi1"] = fi1,
["lambda1"] = lambda1,
["fi2"] = fi2,
["lambda2"] = lambda2
};
return new SqlProjection(new ObliqueMercatorProjection(parameters));
}
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public static SqlProjection TransverseMercator(double longitude0)
{
var parameters = new Dictionary<string, double> {["longitude0"] = longitude0};
return new SqlProjection(new TransverseMercatorProjection(parameters));
}
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public static SqlProjection Gnomonic(double longitude, double latitude)
{
var parameters = new Dictionary<string, double>
{
["longitude0"] = 0, ["longitude1"] = longitude, ["latitude1"] = latitude
};
return new SqlProjection(new GnomonicProjection(parameters));
}
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
private static void ThrowIfArgumentNull(INullable argument, string name)
{
if (argument == null || argument.IsNull)
{
throw new ArgumentNullException(name);
}
}
// INSTANCE METHODS AND FIELDS
// Projects geography onto geometry.
// Returns valid projected SqlGeometry object.
// Constructed geometry will have the same SRID as geography.
//
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public SqlGeometry Project(SqlGeography geography)
{
ThrowIfArgumentNull(geography, "geography");
var builder = new SqlGeometryBuilder();
geography.Populate(new Projector(this, builder));
return builder.ConstructedGeometry;
}
// Unprojects geometry producing geography.
// Returns a valid unprojected SqlGeography object.
//
// SRID taken from the geometry object may not be valid for geography,
// in which case a new SRID must be given.
//
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public SqlGeography UnprojectWithSRID(SqlGeometry geometry, SqlInt32 newSrid)
{
ThrowIfArgumentNull(geometry, "geometry");
ThrowIfArgumentNull(newSrid, "newSrid");
var builder = new SqlGeographyBuilder();
geometry.Populate(new UnProjector(this, builder, newSrid.Value));
return builder.ConstructedGeography;
}
// Unprojects geometry producing geography.
// This method assumes that the SRID of geometry object is valid for geography.
// Returns a valid unprojected SqlGeography object.
//
[SqlMethod(IsDeterministic = true, IsPrecise = false)]
public SqlGeography Unproject(SqlGeometry geometry)
{
ThrowIfArgumentNull(geometry, "geometry");
var builder = new SqlGeographyBuilder();
geometry.Populate(new UnProjector(this, builder, geometry.STSrid.Value)); // NOTE srid will be reused
return builder.ConstructedGeography;
}
// Longitude and latitude are in degrees.
// Input Latitude must be in range [-90, 90].
// Input Longitude must be in range [-15069, 15069].
//
public void ProjectPoint(double latitudeDeg, double longitudeDeg, out double x, out double y)
{
var latitude = MathX.InputLat(latitudeDeg, 90, "latitude");
var longitude = MathX.NormalizeLongitudeRad(MathX.InputLong(longitudeDeg, 15069, "longitude") - _projection.CentralLongitudeRad);
_projection.Project(latitude, longitude, out x, out y);
if (double.IsNaN(x))
{
throw new ArgumentOutOfRangeException(nameof(x));
}
if (double.IsNaN(y))
{
throw new ArgumentOutOfRangeException(nameof(y));
}
}
// Longitude and latitude are in degrees.
// Output Latitude will be in range [-90, 90].
// Output Longitude will be in range [-180, 180].
//
public void UnprojectPoint(double x, double y, out double latitudeDeg, out double longitudeDeg)
{
if (double.IsNaN(x))
{
throw new ArgumentException(Resource.InputCoordinateIsNaN, nameof(x));
}
if (double.IsNaN(y))
{
throw new ArgumentException(Resource.InputCoordinateIsNaN, nameof(y));
}
_projection.Unproject(x, y, out var latitude, out var longitude);
if (double.IsNaN(latitude) || latitude < -Math.PI / 2 || latitude > Math.PI / 2)
{
throw new ArgumentOutOfRangeException(nameof(latitude), string.Format(CultureInfo.InvariantCulture, Resource.OutputLatitudeIsOutOfRange, latitude));
}
if (double.IsNaN(longitude) || longitude < -Math.PI || longitude > Math.PI)
{
throw new ArgumentOutOfRangeException(nameof(longitude), string.Format(CultureInfo.InvariantCulture, Resource.OutputLongitudeIsOutOfRange, longitude));
}
latitudeDeg = MathX.Clamp(90, SpatialUtil.ToDegrees(latitude));
longitudeDeg = MathX.NormalizeLongitudeDeg(SpatialUtil.ToDegrees(longitude + _projection.CentralLongitudeRad));
}
}
}