forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapTest.cs
More file actions
440 lines (385 loc) · 16.1 KB
/
MapTest.cs
File metadata and controls
440 lines (385 loc) · 16.1 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
using System;
using System.Collections.ObjectModel;
using System.Drawing;
using GeoAPI.Geometries;
using NUnit.Framework;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
using SharpMap;
using SharpMap.Data.Providers;
using Geometry = GeoAPI.Geometries.IGeometry;
using SharpMap.Layers;
using Point=GeoAPI.Geometries.Coordinate;
using BoundingBox = GeoAPI.Geometries.Envelope;
namespace UnitTests
{
[TestFixture]
public class MapTest
{
private static readonly IGeometryFactory Factory = new GeometryFactory();
private static readonly WKTReader WktReader = new WKTReader(Factory);
public static IGeometry GeomFromText(string wkt)
{
return WktReader.Read(wkt);
}
private static IProvider CreateDatasource()
{
var geoms = new Collection<Geometry>
{
GeomFromText("POINT EMPTY"),
GeomFromText(
"GEOMETRYCOLLECTION (POINT (10 10), POINT (30 30), LINESTRING (15 15, 20 20))"),
GeomFromText(
"MULTIPOLYGON (((0 0, 10 0, 10 10, 0 10, 0 0)), ((5 5, 7 5, 7 7, 5 7, 5 5)))"),
GeomFromText("LINESTRING (20 20, 20 30, 30 30, 30 20, 40 20)"),
GeomFromText(
"MULTILINESTRING ((10 10, 40 50), (20 20, 30 20), (20 20, 50 20, 50 60, 20 20))"),
GeomFromText(
"POLYGON ((20 20, 20 30, 30 30, 30 20, 20 20), (21 21, 29 21, 29 29, 21 29, 21 21), (23 23, 23 27, 27 27, 27 23, 23 23))"),
GeomFromText("POINT (20.564 346.3493254)"),
GeomFromText("MULTIPOINT (20.564 346.3493254, 45 32, 23 54)"),
GeomFromText("MULTIPOLYGON EMPTY"),
GeomFromText("MULTILINESTRING EMPTY"),
GeomFromText("MULTIPOINT EMPTY"),
GeomFromText("LINESTRING EMPTY")
};
return new FeatureProvider(geoms);
}
[Test]
public void MapSridEqualsFactorySrid()
{
var m = new Map();
Assert.AreEqual(m.SRID, m.Factory.SRID);
m.SRID = 10;
Assert.AreEqual(m.SRID, m.Factory.SRID);
}
[Test]
public void FindLayer_ReturnEnumerable()
{
Map map = new Map();
map.Layers.Add(new VectorLayer("Layer 1"));
map.Layers.Add(new VectorLayer("Layer 3"));
map.Layers.Add(new VectorLayer("Layer 2"));
map.Layers.Add(new VectorLayer("Layer 4"));
int count = 0;
foreach (ILayer lay in map.FindLayer("Layer 3"))
{
Assert.AreEqual("Layer 3", lay.LayerName);
count++;
}
Assert.AreEqual(1, count);
}
[Test]
public void TestClone()
{
Map map = new Map(new Size(2, 1));
map.Layers.Add(new VectorLayer("Layer 1"));
map.Layers.Add(new VectorLayer("Layer 3"));
map.Layers.Add(new VectorLayer("Layer 2"));
map.Layers.Add(new VectorLayer("Layer 4"));
var clone = map.Clone();
Assert.AreEqual(map.BackgroundLayer.Count, clone.BackgroundLayer.Count);
Assert.AreEqual(map.Layers.Count, clone.Layers.Count);
Assert.AreEqual(map.VariableLayers.Count, clone.VariableLayers.Count);
Assert.AreEqual(map.Decorations.Count, clone.Decorations.Count);
}
[Test]
[ExpectedException(typeof (InvalidOperationException))]
public void GetExtents_EmptyMap_ThrowInvalidOperationException()
{
Map map = new Map(new Size(2, 1));
map.ZoomToExtents();
}
[Test]
public void Map_InsertLayer()
{
Map m = new Map();
VectorLayer vlay1 = new VectorLayer("1");
m.Layers.Add(vlay1);
VectorLayer vlay2 = new VectorLayer("2");
m.Layers.Add(vlay2);
VectorLayer vlay3 = new VectorLayer("3");
m.Layers.Insert(1, vlay3);
Assert.AreEqual("1", m.Layers[0].LayerName);
Assert.AreEqual("3", m.Layers[1].LayerName);
Assert.AreEqual("2", m.Layers[2].LayerName);
}
[Test]
public void Map_GetLayerByNameInGroupLayer()
{
Map m = new Map();
VectorLayer vlay1 = new VectorLayer("1");
VectorLayer vlay2 = new VectorLayer("2");
VectorLayer vlay3 = new VectorLayer("3");
m.Layers.Add(vlay1);
LayerGroup lg = new LayerGroup("Group");
lg.Layers.Add(vlay2);
lg.Layers.Add(vlay3);
m.Layers.Add(lg);
var lay = m.GetLayerByName("1");
Assert.IsNotNull(lay);
Assert.AreEqual("1",lay.LayerName);
lay = m.GetLayerByName("2");
Assert.IsNotNull(lay);
Assert.AreEqual("2", lay.LayerName);
lay = m.GetLayerByName("3");
Assert.IsNotNull(lay);
Assert.AreEqual("3", lay.LayerName);
lay = m.GetLayerByName("Group");
Assert.IsNotNull(lay);
Assert.AreEqual("Group", lay.LayerName);
}
[Test]
public void GetExtents_ValidDatasource()
{
Map map = new Map(new Size(400, 200));
VectorLayer vLayer = new VectorLayer("Geom layer", CreateDatasource());
map.Layers.Add(vLayer);
BoundingBox box = map.GetExtents();
Assert.AreEqual(new BoundingBox(0, 50, 0, 346.3493254), box);
}
[Test]
public void GetLayerByName_Indexer()
{
Map map = new Map();
map.Layers.Add(new VectorLayer("Layer 1"));
map.Layers.Add(new VectorLayer("Layer 3"));
map.Layers.Add(new VectorLayer("Layer 2"));
ILayer layer = map.Layers["Layer 2"];
Assert.IsNotNull(layer);
Assert.AreEqual("Layer 2", layer.LayerName);
}
[Test]
public void GetLayerByName_ReturnCorrectLayer()
{
Map map = new Map();
map.Layers.Add(new VectorLayer("Layer 1"));
map.Layers.Add(new VectorLayer("Layer 3"));
map.Layers.Add(new VectorLayer("Layer 2"));
ILayer layer = map.GetLayerByName("Layer 2");
Assert.IsNotNull(layer);
Assert.AreEqual("Layer 2", layer.LayerName);
}
[Test]
public void GetMap_GeometryProvider_ReturnImage()
{
Map map = new Map(new Size(400, 200));
VectorLayer vLayer = new VectorLayer("Geom layer", CreateDatasource());
vLayer.Style.Outline = new Pen(Color.Red, 2f);
vLayer.Style.EnableOutline = true;
vLayer.Style.Line = new Pen(Color.Green, 2f);
vLayer.Style.Fill = Brushes.Yellow;
map.Layers.Add(vLayer);
VectorLayer vLayer2 = new VectorLayer("Geom layer 2", vLayer.DataSource);
vLayer2.Style.SymbolOffset = new PointF(3, 4);
vLayer2.Style.SymbolRotation = 45;
vLayer2.Style.SymbolScale = 0.4f;
map.Layers.Add(vLayer2);
VectorLayer vLayer3 = new VectorLayer("Geom layer 3", vLayer.DataSource);
vLayer3.Style.SymbolOffset = new PointF(3, 4);
vLayer3.Style.SymbolRotation = 45;
map.Layers.Add(vLayer3);
VectorLayer vLayer4 = new VectorLayer("Geom layer 4", vLayer.DataSource);
vLayer4.Style.SymbolOffset = new PointF(3, 4);
vLayer4.Style.SymbolScale = 0.4f;
vLayer4.ClippingEnabled = true;
map.Layers.Add(vLayer4);
map.ZoomToExtents();
Image img = map.GetMap();
Assert.IsNotNull(img);
map.Dispose();
img.Dispose();
}
[Test]
[ExpectedException(typeof (InvalidOperationException))]
public void GetMap_RenderEmptyMap_ThrowInvalidOperationException()
{
Map map = new Map(new Size(2, 1));
map.GetMap();
}
[Test]
[ExpectedException(typeof (ApplicationException))]
public void GetMap_RenderLayerWithoutDatasource_ThrowException()
{
Map map = new Map();
map.Layers.Add(new VectorLayer("Layer 1"));
map.GetMap();
}
[Test]
public void GetMapHeight_FixedZoom_Return1750()
{
Map map = new Map(new Size(400, 200));
map.Zoom = 3500;
Assert.AreEqual(1750, map.MapHeight);
}
[Test]
public void GetPixelSize_FixedZoom_Return8_75()
{
Map map = new Map(new Size(400, 200));
map.Zoom = 3500;
Assert.AreEqual(8.75, map.PixelSize);
}
[Test]
public void ImageToWorld()
{
Map map = new Map(new Size(1000, 500));
map.Zoom = 360;
map.Center = new Point(0, 0);
Assert.AreEqual(new Point(0, 0), map.ImageToWorld(new PointF(500, 250)));
Assert.AreEqual(new Point(-180, 90), map.ImageToWorld(new PointF(0, 0)));
Assert.AreEqual(new Point(-180, -90), map.ImageToWorld(new PointF(0, 500)));
Assert.AreEqual(new Point(180, 90), map.ImageToWorld(new PointF(1000, 0)));
Assert.AreEqual(new Point(180, -90), map.ImageToWorld(new PointF(1000, 500)));
}
[Test]
public void ImageToWorld_DefaultMap_ReturnValue()
{
Map map = new Map(new Size(500, 200));
map.Center = new Point(23, 34);
map.Zoom = 1000;
Point p = map.ImageToWorld(new PointF(242.5f, 92));
Assert.AreEqual(new Point(8, 50), p);
}
[Test]
public void Initalize_MapInstance()
{
Map map = new Map(new Size(2, 1));
Assert.IsNotNull(map);
Assert.IsNotNull(map.Layers);
Assert.AreEqual(2f, map.Size.Width);
Assert.AreEqual(1f, map.Size.Height);
Assert.AreEqual(Color.Transparent, map.BackColor);
Assert.AreEqual(double.MaxValue, map.MaximumZoom);
Assert.IsTrue(map.MinimumZoom > 0);
Assert.AreEqual(new Point(0, 0), map.Center, "map.Center should be initialized to (0,0)");
Assert.AreEqual(1, map.Zoom, "Map zoom should be initialized to 1.0");
}
[Test]
public void SetMaximumZoom_ValueLessThanMinimumZoom()
{
Map map = new Map();
map.MaximumZoom = -1;
Assert.IsTrue(map.MaximumZoom >= map.MinimumZoom);
}
[Test]
public void SetMaximumZoom_OKValue()
{
Map map = new Map();
map.MaximumZoom = 100.3;
Assert.AreEqual(100.3, map.MaximumZoom);
}
[Test]
public void SetMinimumZoom_ValueLessThanTwoEpsilon()
{
Map map = new Map();
map.MinimumZoom = -1;
Assert.IsTrue(map.MinimumZoom > 0);
map.MinimumZoom = Double.Epsilon;
Assert.IsTrue(map.MinimumZoom > Double.Epsilon);
}
[Test]
public void SetMinimumZoom_OKValue()
{
Map map = new Map();
map.MinimumZoom = 100.3;
Assert.AreEqual(100.3, map.MinimumZoom);
}
[Test]
public void SetZoom_ValueBelowMin()
{
Map map = new Map();
map.MinimumZoom = 100;
map.Zoom = 50;
Assert.AreEqual(100, map.MinimumZoom);
}
[Test]
public void SetZoom_ValueOutsideMax()
{
Map map = new Map();
map.MaximumZoom = 100;
map.Zoom = 150;
Assert.AreEqual(100, map.MaximumZoom);
}
[Test]
public void ZoomToBoxWithEnforcedMaximumExtents()
{
Map map = new Map();
//map.MaximumZoom = 100;
map.MaximumExtents = new Envelope(-180, 180, -90, 90);
map.EnforceMaximumExtents = true;
map.ZoomToBox(new Envelope(-200, 200, -100, 100));
Assert.IsTrue(map.MaximumExtents.Contains(map.Envelope));
Assert.AreEqual(new BoundingBox(-120, 120, -90, 90), map.Envelope);
}
[Test]
public void WorldToImage()
{
Map map = new Map(new Size(1000, 500));
map.Zoom = 360;
map.Center = new Point(0, 0);
Assert.AreEqual(new PointF(500, 250), map.WorldToImage(new Point(0, 0)));
Assert.AreEqual(new PointF(0, 0), map.WorldToImage(new Point(-180, 90)));
Assert.AreEqual(new PointF(0, 500), map.WorldToImage(new Point(-180, -90)));
Assert.AreEqual(new PointF(1000, 0), map.WorldToImage(new Point(180, 90)));
Assert.AreEqual(new PointF(1000, 500), map.WorldToImage(new Point(180, -90)));
}
[Test]
public void WorldToMap_DefaultMap_ReturnValue()
{
Map map = new Map(new Size(500, 200));
map.Center = new Point(23, 34);
map.Zoom = 1000;
PointF p = map.WorldToImage(new Point(8, 50));
Assert.AreEqual(new PointF(242.5f, 92), p);
}
[Test]
public void ZoomToBox_NoAspectCorrection()
{
Map map = new Map(new Size(400, 200));
map.ZoomToBox(new BoundingBox(20, 100, 50, 80));
Assert.AreEqual(new Point(60, 65), map.Center);
Assert.AreEqual(80, map.Zoom);
}
[Test]
public void ZoomToBox_WithAspectCorrection()
{
Map map = new Map(new Size(400, 200));
map.ZoomToBox(new BoundingBox(20, 100, 10, 180));
Assert.AreEqual(new Point(60, 95), map.Center);
Assert.AreEqual(340, map.Zoom);
}
[Test]
public void TestZoomToBoxRaisesMapViewOnChange()
{
var raised = false;
var map = new Map(new Size(400, 200));
map.MapViewOnChange += () => raised = true;
// ZoomToBox
map.ZoomToBox(new BoundingBox(20, 100, 10, 180));
Assert.IsTrue(raised, "MapViewOnChange not fired when calling Map.ZoomToBox(...).");
raised = false;
map.ZoomToBox(new BoundingBox(20, 100, 10, 180));
Assert.IsFalse(raised, "MapViewOnChange fired when calling Map.ZoomToBox(map.Envelope).");
// ZoomToExtents
// Note: Not needed as Map.ZoomToExtents() calls Map.ZoomToBox(Map.GetExtents())
//raised = false;
//map.ZoomToExtents();
//Assert.IsTrue(raised, "MapViewOnChange not fired when calling Map.ZoomToExtents()");
//raised = false;
//map.ZoomToExtents();
//Assert.IsFalse(raised, "MapViewOnChange fired when calling Map.ZoomToExtents() twice");
// Set Zoom
map.Zoom = map.Zoom * 0.9;
Assert.IsTrue(raised, "MapViewOnChange not fired when setting Map.Zoom.");
raised = false;
map.Zoom = map.Zoom;
Assert.IsFalse(raised, "MapViewOnChange not fired when setting Map.Zoom = Map.Zoom");
// Set Center
map.Center = new Coordinate(map.Center.X + 1, map.Center.Y);
Assert.IsTrue(raised, "MapViewOnChange not fired when setting Map.Center.");
raised = false;
map.Center = map.Center;
Assert.IsFalse(raised, "MapViewOnChange fired when setting Map.Center = Map.Center.");
}
}
}