forked from NetTopologySuite/ProjNet4GeoAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapProjection.cs
More file actions
1210 lines (1067 loc) · 44 KB
/
MapProjection.cs
File metadata and controls
1210 lines (1067 loc) · 44 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2005 - 2009 - Morten Nielsen (www.sharpgis.net)
//
// 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 ProjNet; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// SOURCECODE IS MODIFIED FROM ANOTHER WORK AND IS ORIGINALLY BASED ON GeoTools.NET:
/*
* Copyright (C) 2002 Urban Science Applications, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using ProjNet.CoordinateSystems.Transformations;
namespace ProjNet.CoordinateSystems.Projections
{
/// <summary>
/// Projections inherit from this abstract class to get access to useful mathematical functions.
/// </summary>
[Serializable]
public abstract class MapProjection : MathTransform, IProjection
{
/// <summary>
/// EPS10 => 1e-10.
/// </summary>
protected const double EPS10 = 1e-10;
/// <summary>
/// EPS7 => 1e-7.
/// </summary>
protected const double EPS7 = 1e-7;
/// <summary>
/// HUGE_VAL => double.NaN.
/// </summary>
protected const double HUGE_VAL = double.NaN;
// ReSharper disable InconsistentNaming
/// <summary>
/// Eccentricity
/// </summary>
protected readonly double _e;
/// <summary>
/// Eccentricity squared <c>_e * _e</c>
/// </summary>
protected readonly double _es;
/// <summary>
/// Length of semi major axis of ellipse
/// </summary>
protected readonly double _semiMajor;
/// <summary>
/// Length of semi minor axis of ellipse
/// </summary>
protected readonly double _semiMinor;
/// <summary>
/// Meters per unit
/// </summary>
protected readonly double _metersPerUnit;
/// <summary>
/// Reciprocal meters per unit <c>1.0 / <see cref="_metersPerUnit"/></c>
/// </summary>
protected readonly double _reciprocalMetersPerUnit;
/// <summary>
/// Scale factor
/// </summary>
protected readonly double scale_factor; /* scale factor */
/// <summary>
/// Center longitude (projection center)
/// </summary>
protected double central_meridian; /* Center longitude (projection center) */
/// <summary>
/// Substitute for <see cref="central_meridian"/>
/// </summary>
protected double lon_origin { get { return central_meridian; } set { central_meridian = value; } }
/// <summary>
/// Center latitude (projection center), same as lat_origin
/// </summary>
protected double central_parallel { get { return lat_origin; } }
/// <summary>
/// Center latitude (projection center), same as lat_origin
/// </summary>
protected double phi0 { get { return lat_origin; } }
/// <summary>
/// Center latitude
/// </summary>
protected readonly double lat_origin; /* center latitude */
/// <summary>
/// Y offset in meters
/// </summary>
protected readonly double false_northing; /* y offset in meters */
/// <summary>
/// X offset in meters
/// </summary>
protected readonly double false_easting; /* x offset in meters */
/// <summary>
/// Constants for <see cref="mlfn(double,double,double,double,double)"/>
/// </summary>
protected readonly double en0, en1, en2, en3, en4;
/// <summary>
/// A set of projection parameters for this projection
/// </summary>
protected readonly ProjectionParameterSet _Parameters;
/// <summary>
/// The inverse <see cref="MathTransform"/>
/// </summary>
protected MathTransform _inverse;
// ReSharper restore InconsistentNaming
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="parameters">An enumeration of projection parameters</param>
/// <param name="inverse">Indicator if this projection is inverse</param>
protected MapProjection(IEnumerable<ProjectionParameter> parameters, MapProjection inverse)
: this(parameters)
{
_inverse = inverse;
if (_inverse != null)
{
inverse._inverse = this;
IsInverse = !inverse.IsInverse;
}
}
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="parameters">An enumeration of projection parameters</param>
protected MapProjection(IEnumerable<ProjectionParameter> parameters)
{
_Parameters = new ProjectionParameterSet(parameters);
_semiMajor = _Parameters.GetParameterValue("semi_major");
_semiMinor = _Parameters.GetParameterValue("semi_minor");
//_es = 1.0 - (_semiMinor * _semiMinor) / (_semiMajor * _semiMajor);
_es = EccentricySquared(_semiMajor, _semiMinor);
_e = Math.Sqrt(_es);
scale_factor = _Parameters.GetOptionalParameterValue("scale_factor", 1);
central_meridian = DegreesToRadians(_Parameters.GetParameterValue("central_meridian", "longitude_of_center"));
lat_origin = DegreesToRadians(_Parameters.GetOptionalParameterValue("latitude_of_origin", 0d, "latitude_of_center"));
_metersPerUnit = _Parameters.GetParameterValue("unit");
_reciprocalMetersPerUnit = 1 / _metersPerUnit;
false_easting = _Parameters.GetOptionalParameterValue("false_easting", 0) * _metersPerUnit;
false_northing = _Parameters.GetOptionalParameterValue("false_northing", 0) * _metersPerUnit;
// TODO: Should really convert to the correct linear units??
// Compute constants for the mlfn
double t;
en0 = C00 - _es * (C02 + _es *
(C04 + _es * (C06 + _es * C08)));
en1 = _es * (C22 - _es *
(C04 + _es * (C06 + _es * C08)));
en2 = (t = _es * _es) *
(C44 - _es * (C46 + _es * C48));
en3 = (t *= _es) * (C66 - _es * C68);
en4 = t * _es * C88;
}
/// <summary>
/// Returns a list of projection "cloned" projection parameters
/// </summary>
/// <returns></returns>
protected internal static List<ProjectionParameter> CloneParametersList(
IEnumerable<ProjectionParameter> projectionParameters)
{
var res = new List<ProjectionParameter>();
foreach (var pp in projectionParameters)
res.Add(new ProjectionParameter(pp.Name, pp.Value));
return res;
}
#region Implementation of IProjection
/// <summary>
/// Gets the projection classification name (e.g. 'Transverse_Mercator').
/// </summary>
public string ClassName
{
get { return Name; }
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public ProjectionParameter GetParameter(int index)
{
return _Parameters.GetAtIndex(index);
}
/// <summary>
/// Gets an named parameter of the projection.
/// </summary>
/// <remarks>The parameter name is case insensitive</remarks>
/// <param name="name">Name of parameter</param>
/// <returns>parameter or null if not found</returns>
public ProjectionParameter GetParameter(string name)
{
return _Parameters.Find(name);
}
/// <summary>
///
/// </summary>
public int NumParameters
{
get { return _Parameters.Count; }
}
/// <summary>
/// Gets or sets the abbreviation of the object.
/// </summary>
public string Abbreviation { get; set; }
/// <summary>
/// Gets or sets the alias of the object.
/// </summary>
public string Alias { get; set; }
/// <summary>
/// Gets or sets the authority name for this object, e.g., "EPSG",
/// is this is a standard object with an authority specific
/// identity code. Returns "CUSTOM" if this is a custom object.
/// </summary>
public string Authority { get; set; }
/// <summary>
/// Gets or sets the authority specific identification code of the object
/// </summary>
public long AuthorityCode { get; set; }
/// <summary>
/// Gets or sets the name of the object.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the provider-supplied remarks for the object.
/// </summary>
public string Remarks { get; set; }
/// <summary>
/// Returns the Well-known text for this object
/// as defined in the simple features specification.
/// </summary>
public override string WKT
{
get
{
var sb = new StringBuilder();
if (IsInverse)
sb.Append("INVERSE_MT[");
sb.AppendFormat("PARAM_MT[\"{0}\"", Name);
for (int i = 0; i < NumParameters; i++)
sb.AppendFormat(", {0}", GetParameter(i).WKT);
//if (!string.IsNullOrWhiteSpace(Authority) && AuthorityCode > 0)
// sb.AppendFormat(", AUTHORITY[\"{0}\", \"{1}\"]", Authority, AuthorityCode);
sb.Append("]");
if (IsInverse)
sb.Append("]");
return sb.ToString();
}
}
/// <summary>
/// Gets an XML representation of this object
/// </summary>
public override string XML
{
get
{
var sb = new StringBuilder();
sb.Append("<CT_MathTransform>");
sb.AppendFormat(
IsInverse
? "<CT_InverseTransform Name=\"{0}\">"
: "<CT_ParameterizedMathTransform Name=\"{0}\">", ClassName);
for (int i = 0; i < NumParameters; i++)
sb.AppendFormat(GetParameter(i).XML);
sb.Append(IsInverse ? "</CT_InverseTransform>" : "</CT_ParameterizedMathTransform>");
sb.Append("</CT_MathTransform>");
return sb.ToString();
}
}
#endregion
#region IMathTransform
/// <inheritdoc/>
public sealed override int DimSource
{
get { return 2; }
}
/// <inheritdoc/>
public sealed override int DimTarget
{
get { return 2; }
}
#region Transform overrides
/// <inheritdoc />
public sealed override void Transform(ref double x, ref double y, ref double z)
{
if (IsInverse)
{
SourceToDegrees(ref x, ref y);
}
else
{
DegreesToTarget(ref x, ref y);
}
}
/// <inheritdoc />
protected sealed override void TransformCore(Span<double> xs, Span<double> ys, Span<double> zs, int strideX, int strideY, int strideZ)
{
if (IsInverse)
SourceToDegrees(xs, ys, strideX, strideY);
else
DegreesToTarget(xs, ys, strideX, strideY);
}
#endregion
#region Forward methods
/// <summary>
/// Abstract method to convert a point (lon, lat) in radians to (x, y) in meters
/// </summary>
/// <param name="lon">The longitude of the point in radians when entering, its x-ordinate in meters after exit.</param>
/// <param name="lat">The latitude of the point in radians when entering, its y-ordinate in meters after exit.</param>
protected abstract void RadiansToMeters(ref double lon, ref double lat);
/// <summary>
/// Method to convert a series of points defined by (lon, lat) in radians to (x, y) in meters
/// </summary>
/// <param name="lons">The longitudes of the points in radians when entering, their x-ordinates in meters after exit.</param>
/// <param name="lats">The latitudes of the points in radians when entering, their y-ordinates in meters after exit.</param>
/// <param name="strideX">A stride value for longitude-ordinates</param>
/// <param name="strideY">A stride value for latitude-ordinates</param>
protected virtual void RadiansToMeters(Span<double> lons, Span<double> lats, int strideX, int strideY)
{
for (int i = 0, j = 0; i < lons.Length; i += strideX, j += strideY)
{
RadiansToMeters(ref lons[i], ref lats[j]);
}
}
/// <summary>
/// Converts a point (lon, lat) in degrees to (x, y) in meters
/// </summary>
/// <param name="lon">The longitude in degree</param>
/// <param name="lat">The latitude in degree</param>
protected virtual void DegreesToMeters(ref double lon, ref double lat)
{
lon = DegreesToRadians(lon);
lat = DegreesToRadians(lat);
RadiansToMeters(ref lon, ref lat);
}
/// <summary>
/// Converts points (lon, lat) in degrees to (x, y) in meters
/// </summary>
/// <param name="lons">The longitudes of the points in degree when entering, their x-ordinates in meters after exit.</param>
/// <param name="lats">The latitudes of the points in degree when entering, their y-ordinates in meters after exit.</param>
/// <param name="strideX">A stride value for longitude-ordinates</param>
/// <param name="strideY">A stride value for latitude-ordinates</param>
protected virtual void DegreesToMeters(Span<double> lons, Span<double> lats, int strideX, int strideY)
{
DegreesToRadians(lons, strideX);
DegreesToRadians(lats, strideY);
RadiansToMeters(lons, lats, strideX, strideY);
}
/// <summary>
/// Converts a point from degrees to target units
/// </summary>
/// <param name="lon">The longitude in degree</param>
/// <param name="lat">The latitude in degree</param>
protected virtual void DegreesToTarget(ref double lon, ref double lat)
{
DegreesToMeters(ref lon, ref lat);
MetersToTarget(ref lon, ref lat);
}
/// <summary>
/// Converts a series of points from degrees to target units to degrees
/// </summary>
/// <param name="lons">A series of x-ordinate values</param>
/// <param name="lats">A series of y-ordinate values</param>
/// <param name="strideX">A stride value for x-ordinates</param>
/// <param name="strideY">A stride value for y-ordinates</param>
protected virtual void DegreesToTarget(Span<double> lons, Span<double> lats,
int strideX, int strideY)
{
DegreesToMeters(lons, lats, strideX, strideY);
MetersToTarget(lons, lats, strideX, strideY);
}
/// <summary>
/// Transforms point from meters to unit of output coordinate. This is done by
/// adding <see cref="false_easting"/> or <see cref="false_northing"/> and
/// multiplying with <see cref="_reciprocalMetersPerUnit"/>
/// </summary>
/// <param name="x">A x-ordinate</param>
/// <param name="y">A y-ordinate</param>
/// <returns>A point.</returns>
protected void MetersToTarget(ref double x, ref double y)
{
x = (x + false_easting) * _reciprocalMetersPerUnit;
y = (y + false_northing) * _reciprocalMetersPerUnit;
}
/// <summary>
/// Transforms point from meters to unit of output coordinate. This is done by
/// adding <see cref="false_easting"/> or <see cref="false_northing"/> and
/// multiplying with <see cref="_reciprocalMetersPerUnit"/>
/// </summary>
/// <param name="xs">A x-ordinates</param>
/// <param name="ys">A y-ordinates</param>
/// <param name="strideX">A stride value for x-ordinates</param>
/// <param name="strideY">A stride value for y-ordinates</param>
/// <returns>A point.</returns>
protected void MetersToTarget(Span<double> xs, Span<double> ys, int strideX, int strideY)
{
AddThenMultiplyInPlace(xs, strideX, false_easting, _reciprocalMetersPerUnit);
AddThenMultiplyInPlace(ys, strideY, false_northing, _reciprocalMetersPerUnit);
}
#endregion
#region Reverse methods
/// <summary>
/// Abstract method to convert a point from meters to radians
/// </summary>
/// <param name="x">The x-ordinate when entering, the longitude value upon exit.</param>
/// <param name="y">The y-ordinate when entering, the latitude value upon exit.</param>
protected abstract void MetersToRadians(ref double x, ref double y);
/// <summary>
/// Method to convert a series of points defined by (x, y) in meters to (lon, lat) in radians
/// </summary>
/// <param name="xs">The x-ordinates of the points in meters when entering, their longitudes in radians after exit.</param>
/// <param name="ys">The y-ordinates of the points in meters when entering, their latitudes in radians after exit.</param>
/// <param name="strideX">A stride value for x-ordinates</param>
/// <param name="strideY">A stride value for y-ordinates</param>
protected virtual void MetersToRadians(Span<double> xs, Span<double> ys, int strideX, int strideY)
{
for (int i = 0, j = 0; i < xs.Length; i += strideX, j += strideY)
{
MetersToRadians(ref xs[i], ref ys[j]);
}
}
/// <summary>
/// Method to convert a point from meters to degrees
/// </summary>
/// <param name="x">The x-ordinate when entering, the longitude value upon exit.</param>
/// <param name="y">The y-ordinate when entering, the latitude value upon exit.</param>
protected virtual void MetersToDegrees(ref double x, ref double y)
{
MetersToRadians(ref x, ref y);
x = RadiansToDegrees(x);
y = RadiansToDegrees(y);
}
/// <summary>
/// Method to convert a point from meters to degrees
/// </summary>
/// <param name="xs">The x-ordinate values when entering, the longitude values upon exit</param>
/// <param name="ys">The y-ordinate values when entering, the latitude values upon exit</param>
/// <param name="strideX"></param>
/// <param name="strideY"></param>
protected virtual void MetersToDegrees(Span<double> xs, Span<double> ys, int strideX, int strideY)
{
MetersToRadians(xs, ys, strideX, strideY);
RadiansToDegrees(xs, strideX);
RadiansToDegrees(ys, strideY);
}
/// <summary>
/// Converts a point from source units to degrees
/// </summary>
/// <param name="x">The x-ordinate</param>
/// <param name="y">The y-ordinate</param>
/// <returns>Converted point.</returns>
protected virtual void SourceToDegrees(ref double x, ref double y)
{
SourceToMeters(ref x, ref y);
MetersToDegrees(ref x, ref y);
}
/// <summary>
/// Converts a series of points from source units to degrees
/// </summary>
/// <param name="xs">A series of x-ordinate values</param>
/// <param name="ys">A series of y-ordinate values</param>
/// <param name="strideX">A stride value for x-ordinates</param>
/// <param name="strideY">A stride value for y-ordinates</param>
protected virtual void SourceToDegrees(Span<double> xs, Span<double> ys,
int strideX, int strideY)
{
SourceToMeters(xs, ys, strideX, strideY);
MetersToDegrees(xs, ys, strideX, strideY);
}
/// <summary>
/// Transforms unit of input coordinates to meters. This is done by multiplying with
/// <see cref="_metersPerUnit"/> and subtracting <see cref="false_easting"/>
/// or <see cref="false_northing"/>
/// </summary>
/// <param name="xs">A series of x-ordinates</param>
/// <param name="ys">A series of y-ordinates</param>
/// <param name="strideX">A stride value for x-ordinates</param>
/// <param name="strideY">A stride value for y-ordinates</param>
protected void SourceToMeters(Span<double> xs, Span<double> ys, int strideX, int strideY)
{
MultiplyThenAddInPlace(xs, strideX, _metersPerUnit, -false_easting);
MultiplyThenAddInPlace(ys, strideY, _metersPerUnit, -false_northing);
}
/// <summary>
/// Transforms unit of input coordinate to meters. This is done by multiplying with
/// <see cref="_metersPerUnit"/> and subtracting <see cref="false_easting"/>
/// or <see cref="false_northing"/>
/// </summary>
/// <param name="x">A x-ordinate</param>
/// <param name="y">A y-ordinate</param>
/// <returns>A point.</returns>
protected void SourceToMeters(ref double x, ref double y)
{
x = x * _metersPerUnit - false_easting;
y = y * _metersPerUnit - false_northing;
}
#endregion
/// <summary>
/// Reverses the transformation
/// </summary>
public override void Invert()
{
IsInverse = !IsInverse;
if (_inverse != null) ((MapProjection)_inverse).Invert(false);
}
/// <summary>
/// Reverses this transformation
/// </summary>
/// <param name="invertInverse">A flag indicating to reverse the <see cref="_inverse"/>"/> projection as well.</param>
protected void Invert(bool invertInverse)
{
IsInverse = !IsInverse;
if (invertInverse && _inverse != null) ((MapProjection)_inverse).Invert(false);
}
/// <summary>
/// Returns true if this projection is inverted.
/// Most map projections define forward projection as "from geographic to projection", and backwards
/// as "from projection to geographic". If this projection is inverted, this will be the other way around.
/// </summary>
protected internal bool IsInverse { get; private set; }
/// <summary>
/// Checks whether the values of this instance is equal to the values of another instance.
/// Only parameters used for coordinate system are used for comparison.
/// Name, abbreviation, authority, alias and remarks are ignored in the comparison.
/// </summary>
/// <param name="obj"></param>
/// <returns>True if equal</returns>
public bool EqualParams(object obj)
{
if (!(obj is MapProjection))
return false;
var proj = obj as MapProjection;
if (!_Parameters.Equals(proj._Parameters))
return false;
/*
if (proj.NumParameters != NumParameters)
return false;
for (var i = 0; i < _Parameters.Count; i++)
{
var param = _Parameters.Find(par => par.Name.Equals(proj.GetParameter(i).Name, StringComparison.OrdinalIgnoreCase));
if (param == null)
return false;
if (param.Value != proj.GetParameter(i).Value)
return false;
}
*/
return IsInverse == proj.IsInverse;
}
#endregion
#region Helper mathmatical functions
// defines some useful constants that are used in the projection routines
// ReSharper disable InconsistentNaming
/// <summary>
/// PI
/// </summary>
protected const double PI = Math.PI;
/// <summary>
/// A fourth of <see cref="Math.PI"/>
/// </summary>
protected const double FORT_PI = (PI * 0.25);
/// <summary>
/// Half of PI
/// </summary>
protected const double HALF_PI = (PI * 0.5);
/// <summary>
/// PI * 2
/// </summary>
protected const double TWO_PI = (PI * 2.0);
/// <summary>
/// EPSLN
/// </summary>
protected const double EPSLN = EPS10;
/// <summary>
/// S2R
/// </summary>
protected const double S2R = 4.848136811095359e-6;
/// <summary>
/// MAX_VAL
/// </summary>
protected const double MAX_VAL = 4;
/// <summary>
/// prjMAXLONG
/// </summary>
protected const double prjMAXLONG = 2147483647;
/// <summary>
/// DBLLONG
/// </summary>
protected const double DBLLONG = 4.61168601e18;
/// <summary>
/// Returns the cube of a number.
/// </summary>
/// <param name="x"> </param>
protected static double CUBE(double x)
{
return Math.Pow(x, 3); /* x^3 */
}
/// <summary>
/// Returns the quad of a number.
/// </summary>
/// <param name="x"> </param>
protected static double QUAD(double x)
{
return Math.Pow(x, 4); /* x^4 */
}
/// <summary>
///
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
/// <returns></returns>
protected static double GMAX(ref double A, ref double B)
{
return Math.Max(A, B); /* assign maximum of a and b */
}
/// <summary>
///
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
/// <returns></returns>
protected static double GMIN(ref double A, ref double B)
{
return ((A) < (B) ? (A) : (B)); /* assign minimum of a and b */
}
/// <summary>
/// IMOD
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
/// <returns></returns>
protected static double IMOD(double A, double B)
{
return (A) - (((A) / (B)) * (B)); /* Integer mod function */
}
///<summary>
///Function to return the sign of an argument
///</summary>
protected static double sign(double x)
{
if (x < 0.0)
return (-1);
else return (1);
}
/// <summary>
///
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
protected static double adjust_lon(double x)
{
long count = 0;
for (; ; )
{
if (Math.Abs(x) <= PI)
break;
else if (((long)Math.Abs(x / Math.PI)) < 2)
x = x - (sign(x) * TWO_PI);
else if (((long)Math.Abs(x / TWO_PI)) < prjMAXLONG)
{
x = x - (((long)(x / TWO_PI)) * TWO_PI);
}
else if (((long)Math.Abs(x / (prjMAXLONG * TWO_PI))) < prjMAXLONG)
{
x = x - (((long)(x / (prjMAXLONG * TWO_PI))) * (TWO_PI * prjMAXLONG));
}
else if (((long)Math.Abs(x / (DBLLONG * TWO_PI))) < prjMAXLONG)
{
x = x - (((long)(x / (DBLLONG * TWO_PI))) * (TWO_PI * DBLLONG));
}
else
x = x - (sign(x) * TWO_PI);
count++;
if (count > MAX_VAL)
break;
}
return (x);
}
/// <summary>
/// Function to compute the constant small m which is the radius of
/// a parallel of latitude, phi, divided by the semimajor axis.
/// </summary>
protected static double msfnz(double eccent, double sinphi, double cosphi)
{
double con;
con = eccent * sinphi;
return ((cosphi / (Math.Sqrt(1.0 - con * con))));
}
/// <summary>
/// Function to compute constant small q which is the radius of a
/// parallel of latitude, phi, divided by the semimajor axis.
/// </summary>
protected static double qsfnz(double sinphi, double eccent)
{
if (eccent > 1.0e-7)
{
double con = eccent * sinphi;
return ((1.0 - eccent * eccent) * (sinphi / (1.0 - con * con) - (.5 / eccent) *
Math.Log((1.0 - con) / (1.0 + con))));
}
return 2.0 * sinphi;
}
/// <summary>
/// Function to compute constant small q which is the radius of a
/// parallel of latitude, phi, divided by the semimajor axis.
/// </summary>
protected static double qsfn(double sinphi, double eccent, double one_es)
{
if (eccent >= EPS7)
{
double con = eccent * sinphi;
double div1 = 1.0 - con * con;
double div2 = 1.0 + con;
/* avoid zero division, fail gracefully */
if (div1 == 0.0 || div2 == 0.0)
return HUGE_VAL;
return (one_es * (sinphi / div1 - (.5 / eccent) * Math.Log((1.0 - con) / div2)));
}
else
return (sinphi + sinphi);
}
/// <summary>
/// Function to calculate the sine and cosine in one call. Some computer
/// systems have implemented this function, resulting in a faster implementation
/// than calling each function separately. It is provided here for those
/// computer systems which don`t implement this function
/// </summary>
protected static void sincos(double val, out double sin_val, out double cos_val)
{
sin_val = Math.Sin(val);
cos_val = Math.Cos(val);
}
/// <summary>
/// Function to compute the constant small t for use in the forward
/// computations in the Lambert Conformal Conic and the Polar
/// Stereographic projections.
/// </summary>
protected static double tsfnz(double eccent, double phi, double sinphi)
{
double con;
double com;
con = eccent * sinphi;
com = .5 * eccent;
con = Math.Pow(((1.0 - con) / (1.0 + con)), com);
return (Math.Tan(.5 * (HALF_PI - phi)) / con);
}
/// <summary>
///
///
/// </summary>
/// <param name="eccent"></param>
/// <param name="qs"></param>
/// <param name="flag"></param>
/// <returns></returns>
protected static double phi1z(double eccent, double qs, out long flag)
{
double eccnts;
double dphi;
double con;
double com;
double sinpi;
double cospi;
double phi;
flag = 0;
//double asinz();
long i;
phi = asinz(.5 * qs);
if (eccent < EPSLN)
return (phi);
eccnts = eccent * eccent;
for (i = 1; i <= 25; i++)
{
sincos(phi, out sinpi, out cospi);
con = eccent * sinpi;
com = 1.0 - con * con;
dphi = .5 * com * com / cospi * (qs / (1.0 - eccnts) - sinpi / com +
.5 / eccent * Math.Log((1.0 - con) / (1.0 + con)));
phi = phi + dphi;
if (Math.Abs(dphi) <= 1e-7)
return (phi);
}
//p_error ("Convergence error","phi1z-conv");
//ASSERT(FALSE);
throw new ArgumentException("Convergence error.");
}
///<summary>
///Function to eliminate roundoff errors in asin
///</summary>
protected static double asinz(double con)
{
if (Math.Abs(con) > 1.0)
{
if (con > 1.0)
con = 1.0;
else
con = -1.0;
}
return (Math.Asin(con));
}
/// <summary>
/// Function to compute the latitude angle, phi2, for the inverse of the
/// Lambert Conformal Conic and Polar Stereographic projections.
/// </summary>
/// <param name="eccent">Spheroid eccentricity</param>
/// <param name="ts">Constant value t</param>
/// <param name="flag">Error flag number</param>
protected static double phi2z(double eccent, double ts, out long flag)
{
double con;
double dphi;
double sinpi;
long i;
flag = 0;
double eccnth = .5 * eccent;
double chi = HALF_PI - 2 * Math.Atan(ts);
for (i = 0; i <= 15; i++)
{
sinpi = Math.Sin(chi);
con = eccent * sinpi;
dphi = HALF_PI - 2 * Math.Atan(ts * (Math.Pow(((1.0 - con) / (1.0 + con)), eccnth))) - chi;
chi += dphi;
if (Math.Abs(dphi) <= .0000000001)
return (chi);
}
throw new ArgumentException("Convergence error - phi2z-conv");
}
private const double C00 = 1.0,
C02 = 0.25,
C04 = 0.046875,
C06 = 0.01953125,
C08 = 0.01068115234375,
C22 = 0.75,
C44 = 0.46875,
C46 = 0.01302083333333333333,
C48 = 0.00712076822916666666,
C66 = 0.36458333333333333333,
C68 = 0.00569661458333333333,
C88 = 0.3076171875;
///<summary>
///Functions to compute the constants e0, e1, e2, and e3 which are used
///in a series for calculating the distance along a meridian. The
///input x represents the eccentricity squared.
///</summary>
protected static double e0fn(double x)
{
return (1.0 - 0.25 * x * (1.0 + x / 16.0 * (3.0 + 1.25 * x)));
}
/// <summary>
///
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
protected static double e1fn(double x)
{
return (0.375 * x * (1.0 + 0.25 * x * (1.0 + 0.46875 * x)));
}
/// <summary>