-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathCoordinateSystemServices.cs
368 lines (322 loc) · 14.8 KB
/
CoordinateSystemServices.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2015 - Spartaco Giubbolini, Felix Obermaier (www.ivv-aachen.de)
//
// This file is part of ProjNet.
// ProjNet is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// ProjNet is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
namespace ProjNet
{
/// <summary>
/// A coordinate system services class
/// </summary>
public class CoordinateSystemServices // : ICoordinateSystemServices
{
//private static ICoordinateSequenceFactory _coordinateSequenceFactory;
///// <summary>
///// Gets or sets a default coordinate sequence factory
///// </summary>
//public static ICoordinateSequenceFactory CoordinateSequenceFactory
//{
// get { return _coordinateSequenceFactory ?? new CoordinateArraySequenceFactory(); }
// set { _coordinateSequenceFactory = value; }
//}
private readonly Dictionary<int, CoordinateSystem> _csBySrid;
private readonly Dictionary<IInfo, int> _sridByCs;
private readonly CoordinateSystemFactory _coordinateSystemFactory;
private readonly CoordinateTransformationFactory _ctFactory;
private readonly ManualResetEvent _initialization = new ManualResetEvent(false);
#region CsEqualityComparer class
private class CsEqualityComparer : EqualityComparer<IInfo>
{
public override bool Equals(IInfo x, IInfo y)
{
return x.AuthorityCode == y.AuthorityCode &&
string.Compare(x.Authority, y.Authority, StringComparison.OrdinalIgnoreCase) == 0;
}
public override int GetHashCode(IInfo obj)
{
if (obj == null) return 0;
return Convert.ToInt32(obj.AuthorityCode) + (obj.Authority != null ? obj.Authority.GetHashCode() : 0);
}
}
#endregion
#region CoordinateSystemKey class
private class CoordinateSystemKey : IInfo
{
public CoordinateSystemKey(string authority, long authorityCode)
{
Authority = authority;
AuthorityCode = authorityCode;
}
public bool EqualParams(object obj)
{
throw new NotSupportedException();
}
public string Name { get { return null; } }
public string Authority { get; private set; }
public long AuthorityCode { get; private set; }
public string Alias { get { return null; } }
public string Abbreviation { get { return null; } }
public string Remarks { get { return null; } }
public string WKT { get { return null; } }
public string XML { get { return null; } }
}
#endregion
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="coordinateSystemFactory">The coordinate sequence factory to use.</param>
/// <param name="coordinateTransformationFactory">The coordinate transformation factory to use</param>
public CoordinateSystemServices(CoordinateSystemFactory coordinateSystemFactory,
CoordinateTransformationFactory coordinateTransformationFactory)
: this(coordinateSystemFactory, coordinateTransformationFactory, null)
{
}
/// <summary>
/// Creates an instance of this class.
/// </summary>
/// <param name="definitions">An enumeration of coordinate system definitions (WKT)</param>
public CoordinateSystemServices(IEnumerable<KeyValuePair<int, string>> definitions)
: this(new CoordinateSystemFactory(), new CoordinateTransformationFactory(), definitions)
{
}
/// <summary>
/// Creates an instance of this class
/// </summary>
public CoordinateSystemServices()
: this(new CoordinateSystemFactory(), new CoordinateTransformationFactory(), null)
{
}
//public Func<string, long, string> GetDefinition { get; set; }
/*
public static string GetFromSpatialReferenceOrg(string authority, long code)
{
var url = string.Format("http://spatialreference.org/ref/{0}/{1}/ogcwkt/",
authority.ToLowerInvariant(),
code);
var req = (HttpWebRequest) WebRequest.Create(url);
using (var resp = req.GetResponse())
{
using (var resps = resp.GetResponseStream())
{
if (resps != null)
{
using (var sr = new StreamReader(resps))
return sr.ReadToEnd();
}
}
}
return null;
}
*/
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="coordinateSystemFactory">The coordinate sequence factory to use.</param>
/// <param name="coordinateTransformationFactory">The coordinate transformation factory to use</param>
/// <param name="enumeration">An enumeration of coordinate system definitions (WKT)</param>
public CoordinateSystemServices(CoordinateSystemFactory coordinateSystemFactory,
CoordinateTransformationFactory coordinateTransformationFactory,
IEnumerable<KeyValuePair<int, string>> enumeration)
{
if (coordinateSystemFactory == null)
throw new ArgumentNullException(nameof(coordinateSystemFactory));
_coordinateSystemFactory = coordinateSystemFactory;
if (coordinateTransformationFactory == null)
throw new ArgumentNullException(nameof(coordinateTransformationFactory));
_ctFactory = coordinateTransformationFactory;
_csBySrid = new Dictionary<int, CoordinateSystem>();
_sridByCs = new Dictionary<IInfo, int>(new CsEqualityComparer());
object enumObj = (object)enumeration ?? DefaultInitialization();
_initialization = new ManualResetEvent(false);
System.Threading.Tasks.Task.Run(() => FromEnumeration((new[] { this, enumObj })));
}
//private CoordinateSystemServices(ICoordinateSystemFactory coordinateSystemFactory,
// ICoordinateTransformationFactory coordinateTransformationFactory,
// IEnumerable<KeyValuePair<int, ICoordinateSystem>> enumeration)
// : this(coordinateSystemFactory, coordinateTransformationFactory)
//{
// var enumObj = (object)enumeration ?? DefaultInitialization();
// _initialization = new ManualResetEvent(false);
// ThreadPool.QueueUserWorkItem(FromEnumeration, new[] { this, enumObj });
//}
private static CoordinateSystem CreateCoordinateSystem(CoordinateSystemFactory coordinateSystemFactory, string wkt)
{
try
{
return coordinateSystemFactory.CreateFromWkt(wkt.Replace("ELLIPSOID", "SPHEROID"));
}
catch (Exception)
{
// as a fallback we ignore projections not supported
return null;
}
}
private static IEnumerable<KeyValuePair<int, CoordinateSystem>> DefaultInitialization()
{
yield return new KeyValuePair<int, CoordinateSystem>(4326, GeographicCoordinateSystem.WGS84);
yield return new KeyValuePair<int, CoordinateSystem>(3857, ProjectedCoordinateSystem.WebMercator);
}
private static void FromEnumeration(CoordinateSystemServices css,
IEnumerable<KeyValuePair<int, CoordinateSystem>> enumeration)
{
foreach (var sridCs in enumeration)
{
css.AddCoordinateSystem(sridCs.Key, sridCs.Value);
}
}
private static IEnumerable<KeyValuePair<int, CoordinateSystem>> CreateCoordinateSystems(
CoordinateSystemFactory factory,
IEnumerable<KeyValuePair<int, string>> enumeration)
{
foreach (var sridWkt in enumeration)
{
var cs = CreateCoordinateSystem(factory, sridWkt.Value);
if (cs != null)
yield return new KeyValuePair<int, CoordinateSystem>(sridWkt.Key, cs);
}
}
private static void FromEnumeration(CoordinateSystemServices css,
IEnumerable<KeyValuePair<int, string>> enumeration)
{
FromEnumeration(css, CreateCoordinateSystems(css._coordinateSystemFactory, enumeration));
}
private static void FromEnumeration(object parameter)
{
object[] paras = (object[]) parameter;
var css = (CoordinateSystemServices) paras[0];
if (paras[1] is IEnumerable<KeyValuePair<int, string>>)
FromEnumeration(css, (IEnumerable<KeyValuePair<int, string>>) paras[1]);
else
FromEnumeration(css, (IEnumerable<KeyValuePair<int, CoordinateSystem>>)paras[1]);
css._initialization.Set();
}
/// <summary>
/// Returns the coordinate system by <paramref name="srid" /> identifier
/// </summary>
/// <param name="srid">The initialization for the coordinate system</param>
/// <returns>The coordinate system.</returns>
public CoordinateSystem GetCoordinateSystem(int srid)
{
_initialization.WaitOne();
return _csBySrid.TryGetValue(srid, out var cs) ? cs : null;
}
/// <summary>
/// Returns the coordinate system by <paramref name="authority" /> and <paramref name="code" />.
/// </summary>
/// <param name="authority">The authority for the coordinate system</param>
/// <param name="code">The code assigned to the coordinate system by <paramref name="authority" />.</param>
/// <returns>The coordinate system.</returns>
public CoordinateSystem GetCoordinateSystem(string authority, long code)
{
int? srid = GetSRID(authority, code);
if (srid.HasValue)
return GetCoordinateSystem(srid.Value);
return null;
}
/// <summary>
/// Method to get the identifier, by which this coordinate system can be accessed.
/// </summary>
/// <param name="authority">The authority name</param>
/// <param name="authorityCode">The code assigned by <paramref name="authority" /></param>
/// <returns>The identifier or <value>null</value></returns>
public int? GetSRID(string authority, long authorityCode)
{
var key = new CoordinateSystemKey(authority, authorityCode);
int srid;
_initialization.WaitOne();
if (_sridByCs.TryGetValue(key, out srid))
return srid;
return null;
}
/// <summary>
/// Method to create a coordinate transformation between two spatial reference systems, defined by their identifiers
/// </summary>
/// <remarks>This is a convenience function for <see cref="M:GeoAPI.ICoordinateSystemServices.CreateTransformation(GeoAPI.CoordinateSystems.ICoordinateSystem,GeoAPI.CoordinateSystems.ICoordinateSystem)" />.</remarks>
/// <param name="sourceSrid">The identifier for the source spatial reference system.</param>
/// <param name="targetSrid">The identifier for the target spatial reference system.</param>
/// <returns>A coordinate transformation, <value>null</value> if no transformation could be created.</returns>
public ICoordinateTransformation CreateTransformation(int sourceSrid, int targetSrid)
{
return CreateTransformation(GetCoordinateSystem(sourceSrid),
GetCoordinateSystem(targetSrid));
}
/// <summary>
/// Method to create a coordinate transformation between two spatial reference systems
/// </summary>
/// <param name="source">The source spatial reference system.</param>
/// <param name="target">The target spatial reference system.</param>
/// <returns>A coordinate transformation, <value>null</value> if no transformation could be created.</returns>
public ICoordinateTransformation CreateTransformation(CoordinateSystem source, CoordinateSystem target)
{
return _ctFactory.CreateFromCoordinateSystems(source, target);
}
protected void AddCoordinateSystem(int srid, CoordinateSystem coordinateSystem)
{
lock (((IDictionary) _csBySrid).SyncRoot)
{
lock (((IDictionary) _sridByCs).SyncRoot)
{
if (_sridByCs.ContainsKey(coordinateSystem))
return;
if (_csBySrid.ContainsKey(srid))
{
if (ReferenceEquals(coordinateSystem, _csBySrid[srid]))
return;
_sridByCs.Remove(_csBySrid[srid]);
_csBySrid[srid] = coordinateSystem;
_sridByCs.Add(coordinateSystem, srid);
}
else
{
_csBySrid.Add(srid, coordinateSystem);
_sridByCs.Add(coordinateSystem, srid);
}
}
}
}
protected virtual int AddCoordinateSystem(CoordinateSystem coordinateSystem)
{
int srid = (int) coordinateSystem.AuthorityCode;
AddCoordinateSystem(srid, coordinateSystem);
return srid;
}
protected void Clear()
{
_csBySrid.Clear();
}
protected int Count
{
get
{
_initialization.WaitOne();
return _sridByCs.Count;
}
}
public bool RemoveCoordinateSystem(int srid)
{
throw new NotSupportedException();
}
public IEnumerator<KeyValuePair<int, CoordinateSystem>> GetEnumerator()
{
_initialization.WaitOne();
return _csBySrid.GetEnumerator();
}
}
}