-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathGeoProjectionsTests.cs
More file actions
237 lines (192 loc) · 8.99 KB
/
Copy pathGeoProjectionsTests.cs
File metadata and controls
237 lines (192 loc) · 8.99 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
using System.Text;
namespace MapWinGisTests.FunctionalTests.Projections;
[Collection(nameof(NotThreadSafeResourceCollection))]
public class GeoProjectionsTests
{
private readonly ITestOutputHelper _testOutputHelper;
public GeoProjectionsTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Fact]
public void CloneTest()
{
var geoProjection = new GeoProjection();
geoProjection.ShouldNotBeNull();
var retVal = geoProjection.ImportFromEPSG(28992);
retVal.ShouldBeTrue();
var original = geoProjection.ExportToWktEx();
_testOutputHelper.WriteLine("Original:");
_testOutputHelper.WriteLine(original);
var geoProjectionCloned = geoProjection.Clone();
var cloned = geoProjectionCloned.ExportToWktEx();
_testOutputHelper.WriteLine("Cloned:");
_testOutputHelper.WriteLine(cloned);
cloned.ShouldBe(original);
// Change original:
retVal = geoProjection.ImportFromEPSG(4326);
retVal.ShouldBeTrue();
var original2 = geoProjection.ExportToWktEx();
_testOutputHelper.WriteLine("Original2:");
_testOutputHelper.WriteLine(original2);
// use the same instance:
var cloned2 = geoProjectionCloned.ExportToWktEx();
_testOutputHelper.WriteLine("Cloned2:");
_testOutputHelper.WriteLine(cloned2);
original2.ShouldNotBe(cloned2);
}
[Fact]
public void IsEmptyTest()
{
var geoProjection = new GeoProjection();
geoProjection.ShouldNotBeNull();
geoProjection.IsEmpty.ShouldBeTrue();
// Import projected:
var retVal = geoProjection.ImportFromEPSG(3857);
retVal.ShouldBeTrue();
geoProjection.IsProjected.ShouldBeTrue();
geoProjection.IsEmpty.ShouldBeFalse();
// Reset:
retVal = geoProjection.Clear();
retVal.ShouldBeTrue();
geoProjection.IsEmpty.ShouldBeTrue();
// Import geographic:
retVal = geoProjection.ImportFromEPSG(4326);
retVal.ShouldBeTrue();
geoProjection.IsGeographic.ShouldBeTrue();
geoProjection.IsEmpty.ShouldBeFalse();
}
[Fact]
public void IsSameTest()
{
System.Diagnostics.Debug.WriteLine("IsSameTest() start");
// Setup:
var geoProjection3857 = new GeoProjection();
geoProjection3857.ShouldNotBeNull();
var geoProjection28992 = new GeoProjection();
geoProjection28992.ShouldNotBeNull();
// Check:
geoProjection3857.IsSame[geoProjection28992].ShouldBeFalse("GeoProjections should not be the same.");
// Import first projection:
var retVal = geoProjection3857.ImportFromEPSG(3857);
retVal.ShouldBeTrue();
// Check:
geoProjection3857.IsSame[geoProjection28992].ShouldBeFalse("GeoProjections should not be the same.");
geoProjection28992.IsSame[geoProjection3857].ShouldBeFalse("GeoProjections should not be the same.");
// Import second projection:
retVal = geoProjection28992.ImportFromEPSG(28992);
retVal.ShouldBeTrue();
// Check:
geoProjection3857.IsSame[geoProjection28992].ShouldBeFalse("GeoProjections should not be the same.");
geoProjection28992.IsSame[geoProjection3857].ShouldBeFalse("GeoProjections should not be the same.");
// Import 3rd projection:
var geoProjection3857B = new GeoProjection();
geoProjection3857B.ShouldNotBeNull();
retVal = geoProjection3857B.ImportFromEPSG(3857);
retVal.ShouldBeTrue();
// Check:
geoProjection3857.IsSame[geoProjection3857B].ShouldBeTrue("GeoProjections should be the same.");
geoProjection3857B.IsSame[geoProjection3857].ShouldBeTrue("GeoProjections should be the same.");
// Load prj-file:
var geoProjectionPrj = ReadPrjFile("Amersfoort.prj");
geoProjection3857.IsSame[geoProjectionPrj].ShouldBeFalse("GeoProjections should not be the same.");
geoProjection28992.IsSame[geoProjectionPrj].ShouldBeTrue("GeoProjections should be the same.");
geoProjectionPrj.IsSame[geoProjection28992].ShouldBeTrue("GeoProjections should be the same.");
System.Diagnostics.Debug.WriteLine("IsSameTest() end");
}
[Fact]
public void ImportFromAutoDetectTest()
{
var geoProjection = new GeoProjection();
geoProjection.ShouldNotBeNull();
// Use string from https://epsg.io/4326
// Use proj.4 string:
var retVal = geoProjection.ImportFromAutoDetect("+proj=longlat +datum=WGS84 +no_defs");
retVal.ShouldBeTrue();
_testOutputHelper.WriteLine(geoProjection.ExportToWktEx());
// Use OGC WKT string:
retVal = geoProjection.ImportFromAutoDetect("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
retVal.ShouldBeTrue();
var wkt = geoProjection.ExportToWktEx();
_testOutputHelper.WriteLine(wkt);
wkt.StartsWith("GEOGCRS[\"WGS 84\",").ShouldBeTrue();
// Read prj file:
var prjFileLocationAmersfoort = Path.Combine(Helpers.GetTestDataLocation(), "Amersfoort.prj");
File.Exists(prjFileLocationAmersfoort).ShouldBeTrue("prjFileLocationAmersfoort doesn't exists.");
// Read file:
var prjString = File.ReadAllText(prjFileLocationAmersfoort, Encoding.UTF8);
prjString.ShouldNotBeNullOrEmpty();
retVal = geoProjection.ImportFromAutoDetect(prjString);
retVal.ShouldBeTrue();
wkt = geoProjection.ExportToWktEx();
_testOutputHelper.WriteLine(wkt);
wkt.StartsWith("PROJCRS[\"Amersfoort_RD_New\",").ShouldBeTrue();
}
[Fact]
public void TryAutoDetectEpsgTest()
{
var geoProjection = new GeoProjection();
geoProjection.ShouldNotBeNull();
// Projected
var retVal = geoProjection.ImportFromEPSG(3857);
retVal.ShouldBeTrue();
Helpers.CheckEpsgCode(geoProjection, 3857, false);
// Reset:
retVal = geoProjection.Clear();
retVal.ShouldBeTrue();
geoProjection.IsEmpty.ShouldBeTrue();
retVal = geoProjection.TryAutoDetectEpsg(out var epsgCode2);
retVal.ShouldBeFalse();
epsgCode2.ShouldBe(-1);
// Geopgraphic
retVal = geoProjection.ImportFromEPSG(4326);
retVal.ShouldBeTrue();
Helpers.CheckEpsgCode(geoProjection, 4326);
// Reset:
retVal = geoProjection.Clear();
retVal.ShouldBeTrue();
geoProjection.IsEmpty.ShouldBeTrue();
// Using Well known enums:
retVal = geoProjection.SetWellKnownGeogCS(tkCoordinateSystem.csWGS_84);
retVal.ShouldBeTrue();
Helpers.CheckEpsgCode(geoProjection, 4326);
// Using OGR WKT from https://epsg.io/4326
retVal = geoProjection.ImportFromAutoDetect("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
retVal.ShouldBeTrue();
Helpers.CheckEpsgCode(geoProjection, 4326);
// Amersfoort:
retVal = geoProjection.ImportFromEPSG(28992);
retVal.ShouldBeTrue();
Helpers.CheckEpsgCode(geoProjection, 28992, false);
}
[Fact]
public void ImportFromEsriTest()
{
// As mentioned in https://mapwindow.discourse.group/t/importfromesri-crash-in-64-bits/984
var geoProjection = new GeoProjection();
geoProjection.ShouldNotBeNull();
const string proj = "GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]";
var retVal = geoProjection.ImportFromESRI(proj);
retVal.ShouldBeTrue();
_testOutputHelper.WriteLine(geoProjection.ExportToWktEx());
Helpers.CheckEpsgCode(geoProjection, 4326);
_testOutputHelper.WriteLine(geoProjection.ExportToWktEx());
}
[Fact]
public void ReadFromFile()
{
var geoProjection = ReadPrjFile("Amersfoort.prj");
Helpers.CheckEpsgCode(geoProjection, 28992, false);
_testOutputHelper.WriteLine(geoProjection.ExportToWktEx());
}
private GeoProjection ReadPrjFile(string prjFileLocation)
{
var geoProjection = new GeoProjection();
geoProjection.ShouldNotBeNull();
var prjFileLocationAmersfoort = Path.Combine(Helpers.GetTestDataLocation(), prjFileLocation);
File.Exists(prjFileLocationAmersfoort).ShouldBeTrue("prjFileLocationAmersfoort doesn't exists.");
var retVal = geoProjection.ReadFromFile(prjFileLocationAmersfoort);
retVal.ShouldBeTrue("geoProjection.ReadFromFile failed");
return geoProjection;
}
}