forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeFileProviderTests.cs
More file actions
319 lines (274 loc) · 14.5 KB
/
ShapeFileProviderTests.cs
File metadata and controls
319 lines (274 loc) · 14.5 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
namespace UnitTests.Data.Providers
{
[NUnit.Framework.TestFixture]
public class ShapeFileProviderTests : ProviderTest
{
private long _msLineal;
private long _msVector;
private const int NumberOfRenderCycles = 1;
[NUnit.Framework.OneTimeTearDown]
public void OneTimeTearDown()
{
System.Diagnostics.Trace.WriteLine("Speed comparison:");
System.Diagnostics.Trace.WriteLine("VectorLayer\tLinealLayer\tRatio");
System.Diagnostics.Trace.WriteLine(string.Format("{0}\t{1}\t{2:N}", _msVector, _msLineal,
((double)_msLineal / _msVector * 100)));
}
[NUnit.Framework.Test]
public void TestReadValueZFromPointZShapeFile()
{
string file = TestUtility.GetPathToTestFile("Point_With_Z.shp");
var sh = new SharpMap.Data.Providers.ShapeFile(file, true);
sh.Open();
int fc = sh.GetFeatureCount();
NUnit.Framework.Assert.AreEqual(1149, fc);
NUnit.Framework.Assert.AreEqual(0, sh.GetObjectIDsInView(sh.GetExtents())[0]);
var featsInView = sh.GetGeometriesInView(new NetTopologySuite.Geometries.Envelope(sh.GetExtents()));
NUnit.Framework.Assert.AreEqual(1149, featsInView.Count);
foreach (var item in featsInView)
{
NUnit.Framework.Assert.IsNotNull(item.Coordinate.Z);
NUnit.Framework.Assert.IsFalse(double.IsNaN(item.Coordinate.Z));
}
NUnit.Framework.Assert.AreEqual(featsInView[0].Coordinate.Z, 146.473);
NUnit.Framework.Assert.AreEqual(featsInView[1].Coordinate.Z, 181.374);
NUnit.Framework.Assert.AreEqual(featsInView[2].Coordinate.Z, 146.676);
NUnit.Framework.Assert.AreEqual(featsInView[3].Coordinate.Z, 181.087);
NUnit.Framework.Assert.AreEqual(featsInView[4].Coordinate.Z, 169.948);
NUnit.Framework.Assert.AreEqual(featsInView[5].Coordinate.Z, 169.916);
sh.Close();
}
[NUnit.Framework.Test]
public void TestReadValueZFromLineStringZShapeFile()
{
string file = TestUtility.GetPathToTestFile("LineString_With_Z.shp");
var sh = new SharpMap.Data.Providers.ShapeFile(file, true);
sh.Open();
int fc = sh.GetFeatureCount();
NUnit.Framework.Assert.AreEqual(1221, fc);
NUnit.Framework.Assert.AreEqual(0, sh.GetObjectIDsInView(sh.GetExtents())[0]);
var featsInView = sh.GetGeometriesInView(new NetTopologySuite.Geometries.Envelope(sh.GetExtents()));
NUnit.Framework.Assert.AreEqual(1221, featsInView.Count);
foreach (var item in featsInView)
{
NUnit.Framework.Assert.IsNotNull(item.Coordinate.Z);
NUnit.Framework.Assert.IsFalse(double.IsNaN(item.Coordinate.Z));
}
NUnit.Framework.Assert.AreEqual(featsInView[0].Coordinates[0].Z, 35.865);
NUnit.Framework.Assert.AreEqual(featsInView[0].Coordinates[1].Z, 35.743);
NUnit.Framework.Assert.AreEqual(featsInView[1].Coordinates[0].Z, 35.518);
NUnit.Framework.Assert.AreEqual(featsInView[1].Coordinates[1].Z, 35.518);
NUnit.Framework.Assert.AreEqual(featsInView[2].Coordinates[0].Z, 37.438);
NUnit.Framework.Assert.AreEqual(featsInView[2].Coordinates[1].Z, 37.441);
NUnit.Framework.Assert.AreEqual(featsInView[3].Coordinates[0].Z, 37.441);
NUnit.Framework.Assert.AreEqual(featsInView[3].Coordinates[1].Z, 37.441);
sh.Close();
}
[NUnit.Framework.Test]
public void UsingTest()
{
using (var s = System.IO.File.Open(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp"), System.IO.FileMode.Open))
{
using (var reader = new System.IO.BinaryReader(s))
{
System.Diagnostics.Trace.WriteLine(reader.ReadInt32());
}
NUnit.Framework.Assert.Throws<System.ObjectDisposedException>(() => System.Diagnostics.Trace.WriteLine(s.Position));
}
}
private static void CopyShapeFile(string path, out string tmp)
{
tmp = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ".shp");
if (!System.IO.File.Exists(path)) throw new NUnit.Framework.IgnoreException("File not found");
foreach (var file in System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path) + ".*"))
{
var copyFile = System.IO.Path.ChangeExtension(tmp, System.IO.Path.GetExtension(file));
if (System.IO.File.Exists(copyFile)) System.IO.File.Delete(copyFile);
System.IO.File.Copy(file, copyFile);
}
}
[NUnit.Framework.Test]
public void TestDeleteAfterClose()
{
string test;
CopyShapeFile(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp"), out test);
var shp = new SharpMap.Data.Providers.ShapeFile(test);
shp.Open();
shp.Close();
var succeeded = true;
foreach (var file in System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(test), System.IO.Path.GetFileNameWithoutExtension(test) + ".*"))
{
try
{
System.IO.File.Delete(file);
}
catch (System.Exception)
{
System.Diagnostics.Trace.WriteLine("Failed to delete '{0}'", file);
succeeded = false;
}
}
NUnit.Framework.Assert.IsTrue(succeeded);
}
[NUnit.Framework.Test]
public void TestReadPointZShapeFile()
{
string file = TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp");
var sh = new SharpMap.Data.Providers.ShapeFile(file, true);
sh.Open();
int fc = sh.GetFeatureCount();
NUnit.Framework.Assert.AreEqual(4342, fc);
NUnit.Framework.Assert.AreEqual(0, sh.GetObjectIDsInView(sh.GetExtents())[0]);
var featsInView = sh.GetGeometriesInView(new NetTopologySuite.Geometries.Envelope(sh.GetExtents()));
NUnit.Framework.Assert.AreEqual(4342, featsInView.Count);
sh.Close();
}
[NUnit.Framework.Test]
public void TestPerformanceVectorLayer()
{
NUnit.Framework.Assert.IsTrue(System.IO.File.Exists(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp")),
"Specified shapefile is not present!");
var map = new SharpMap.Map(new System.Drawing.Size(1024, 768));
var shp = new SharpMap.Data.Providers.ShapeFile(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp"), false, false);
var lyr = new SharpMap.Layers.VectorLayer("Roads", shp);
map.Layers.Add(lyr);
map.ZoomToExtents();
RepeatedRendering(map, shp.GetFeatureCount(), NumberOfRenderCycles, out _msVector);
string path = System.IO.Path.Combine(UnitTestsFixture.GetImageDirectory(this), "SPATIAL_F_SKARVMUFF.shp");
path = System.IO.Path.ChangeExtension(path, ".vector.png");
using (var res = map.GetMap())
res.Save(path, System.Drawing.Imaging.ImageFormat.Png);
System.Diagnostics.Trace.WriteLine("\nResult saved at file://" + path.Replace('\\', '/'));
}
[NUnit.Framework.Test]
public void TestPerformanceLinealLayer()
{
NUnit.Framework.Assert.IsTrue(System.IO.File.Exists(TestUtility.GetPathToTestFile("roads_ugl.shp")),
"Specified shapefile is not present!");
var map = new SharpMap.Map(new System.Drawing.Size(1024, 768));
var shp = new SharpMap.Data.Providers.ShapeFile(TestUtility.GetPathToTestFile("roads_ugl.shp"), false, false);
var lyr = new SharpMap.Layers.Symbolizer.LinealVectorLayer("Roads", shp)
{
Symbolizer =
new SharpMap.Rendering.Symbolizer.BasicLineSymbolizer
{ Line = new System.Drawing.Pen(System.Drawing.Color.Black) }
};
map.Layers.Add(lyr);
map.ZoomToExtents();
RepeatedRendering(map, shp.GetFeatureCount(), NumberOfRenderCycles, out _msLineal);
System.Diagnostics.Trace.WriteLine("\nWith testing if record is deleted ");
shp.CheckIfRecordIsDeleted = true;
long tmp;
RepeatedRendering(map, shp.GetFeatureCount(), 1, out tmp);
var path = System.IO.Path.Combine(UnitTestsFixture.GetImageDirectory(this), "SPATIAL_F_SKARVMUFF.shp");
path = System.IO.Path.ChangeExtension(path, "lineal.png");
using (var res = map.GetMap())
res.Save(path, System.Drawing.Imaging.ImageFormat.Png);
System.Diagnostics.Trace.WriteLine("\nResult saved at file://" + path.Replace('\\', '/'));
}
private static void RepeatedRendering(SharpMap.Map map, int numberOfFeatures, int numberOfTimes, out long avgRenderTime)
{
System.Diagnostics.Trace.WriteLine("Rendering Map with " + numberOfFeatures + " features");
var totalRenderTime = 0L;
var sw = new System.Diagnostics.Stopwatch();
for (var i = 1; i <= numberOfTimes; i++)
{
System.Diagnostics.Trace.Write(string.Format("Rendering {0}x time(s)", i));
sw.Start();
map.GetMap();
sw.Stop();
System.Diagnostics.Trace.WriteLine(" in " +
sw.ElapsedMilliseconds.ToString(
System.Globalization.NumberFormatInfo.CurrentInfo) + "ms.");
totalRenderTime += sw.ElapsedMilliseconds;
sw.Reset();
}
avgRenderTime = totalRenderTime / numberOfTimes;
System.Diagnostics.Trace.WriteLine("\n Average rendering time:" + avgRenderTime.ToString(
System.Globalization.NumberFormatInfo.CurrentInfo) + "ms.");
}
[NUnit.Framework.Test]
public void TestGetFeature()
{
NUnit.Framework.Assert.IsTrue(System.IO.File.Exists(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp")),
"Specified shapefile is not present!");
var shp = new SharpMap.Data.Providers.ShapeFile(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp"), false, false);
shp.Open();
var feat = shp.GetFeature(0);
NUnit.Framework.Assert.IsNotNull(feat);
shp.Close();
}
[NUnit.Framework.Test]
public void TestExecuteIntersectionQuery()
{
NUnit.Framework.Assert.IsTrue(System.IO.File.Exists(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp")),
"Specified shapefile is not present!");
var shp = new SharpMap.Data.Providers.ShapeFile(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp"), false, false);
shp.Open();
var fds = new SharpMap.Data.FeatureDataSet();
var bbox = shp.GetExtents();
//narrow it down
bbox.ExpandBy(-0.425 * bbox.Width, -0.425 * bbox.Height);
//Just to avoid that initial query does not impose performance penalty
shp.DoTrueIntersectionQuery = false;
shp.ExecuteIntersectionQuery(bbox, fds);
fds.Tables.RemoveAt(0);
//Perform query once more
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
shp.ExecuteIntersectionQuery(bbox, fds);
sw.Stop();
System.Diagnostics.Trace.WriteLine("Queried using just envelopes:\n" + fds.Tables[0].Rows.Count + " in " +
sw.ElapsedMilliseconds + "ms.");
fds.Tables.RemoveAt(0);
shp.DoTrueIntersectionQuery = true;
sw.Reset();
sw.Start();
shp.ExecuteIntersectionQuery(bbox, fds);
sw.Stop();
System.Diagnostics.Trace.WriteLine("Queried using prepared geometries:\n" + fds.Tables[0].Rows.Count + " in " +
sw.ElapsedMilliseconds + "ms.");
}
[NUnit.Framework.Test]
public void TestExecuteIntersectionQueryWithFilterDelegate()
{
NUnit.Framework.Assert.IsTrue(System.IO.File.Exists(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp")),
"Specified shapefile is not present!");
var shp = new SharpMap.Data.Providers.ShapeFile(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp"), false, false);
shp.Open();
var fds = new SharpMap.Data.FeatureDataSet();
var bbox = shp.GetExtents();
//narrow it down
bbox.ExpandBy(-0.425 * bbox.Width, -0.425 * bbox.Height);
//Just to avoid that initial query does not impose performance penalty
shp.DoTrueIntersectionQuery = false;
shp.FilterDelegate = JustTracks;
shp.ExecuteIntersectionQuery(bbox, fds);
fds.Tables.RemoveAt(0);
//Perform query once more
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
shp.ExecuteIntersectionQuery(bbox, fds);
sw.Stop();
System.Diagnostics.Trace.WriteLine("Queried using just envelopes:\n" + fds.Tables[0].Rows.Count + " in " +
sw.ElapsedMilliseconds + "ms.");
fds.Tables.RemoveAt(0);
shp.DoTrueIntersectionQuery = true;
sw.Reset();
sw.Start();
shp.ExecuteIntersectionQuery(bbox, fds);
sw.Stop();
System.Diagnostics.Trace.WriteLine("Queried using prepared geometries:\n" + fds.Tables[0].Rows.Count + " in " +
sw.ElapsedMilliseconds + "ms.");
}
public static bool JustTracks(SharpMap.Data.FeatureDataRow fdr)
{
//System.Diagnostics.Trace.WriteLine(fdr [0] + ";"+ fdr[4]);
var s = fdr[4] as string;
if (s != null)
return s == "track";
return true;
}
}
}