forked from jbrwn/NET-Mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorTileTests.cs
313 lines (270 loc) · 12 KB
/
VectorTileTests.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
using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NETMapnik;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace NETMapnik.Test
{
[TestClass]
public class VectorTileTests
{
[TestMethod]
public void VectorTile_Init()
{
VectorTile v = new VectorTile(0, 0, 0);
Assert.AreEqual(256, v.Width());
Assert.AreEqual(256, v.Height());
Assert.IsFalse(v.Painted());
Assert.AreEqual("", v.IsSolid());
Assert.IsTrue(v.Empty());
Assert.AreEqual(0, v.GetData().Length);
}
[TestMethod]
public void VectorTile_Init_WidithHeight()
{
VectorTile v = new VectorTile(0, 0, 0, 512, 512);
Assert.AreEqual(512, v.Width());
Assert.AreEqual(512, v.Height());
}
[TestMethod]
public void VectorTile_SimpleComposite()
{
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "shape.input"));
Map m = new Map(256, 256);
m.Load(@".\data\layer.xml");
m.ZoomAll();
VectorTile v1 = new VectorTile(0, 0, 0, 256, 256);
m.Render(v1);
int v1before = v1.GetData().Length;
VectorTile v2 = new VectorTile(0, 0, 0, 256, 256);
m.Render(v2);
v1.Composite(new List<VectorTile>() { v2 });
int v1after = v1.GetData().Length;
Assert.AreEqual(v1before * 2, v1after);
}
[TestMethod]
public void VectorTile_OverzoomComposite()
{
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "shape.input"));
Map m = new Map(256, 256);
m.Load(@".\data\layer.xml");
VectorTile v1 = new VectorTile(1, 0, 0, 256, 256);
m.ZoomToBox(-20037508.34, 0, 0, 20037508.34);
m.Render(v1);
int v1before = v1.GetData().Length;
VectorTile v2 = new VectorTile(0, 0, 0, 256, 256);
m.ZoomToBox(-20037508.34, -20037508.34, 20037508.34, 20037508.34);
m.Render(v2);
v1.Composite(new List<VectorTile>() { v2 });
int v1after = v1.GetData().Length;
//composite bytes will actually be a little bit bigger than 2* original
//Assert.AreEqual(v1before * 2, v1after);
}
[TestMethod]
public void VectorTile_Names()
{
VectorTile v = new VectorTile(9,112,195);
v.SetData(File.ReadAllBytes(@".\data\9.112.195.pbf"));
CollectionAssert.AreEquivalent(new List<string>() { "world" }, v.Names().ToList());
}
[TestMethod]
public void VectorTile_GetSetData()
{
VectorTile v = new VectorTile(9,112,195);
byte[] bytes = File.ReadAllBytes(@".\data\9.112.195.pbf");
v.SetData(bytes);
Assert.IsFalse(v.Empty());
Assert.IsTrue(v.Painted());
byte[] actual = v.GetData();
CollectionAssert.AreEquivalent(bytes, actual);
}
[TestMethod]
public void VectorTile_AddData()
{
VectorTile v = new VectorTile(9,112,195);
byte[] bytes = File.ReadAllBytes(@".\data\9.112.195.pbf");
v.SetData(bytes);
v.AddData(bytes);
Assert.IsFalse(v.Empty());
Assert.IsTrue(v.Painted());
CollectionAssert.AreEquivalent(new List<string>() { "world", "world" }, v.Names().ToList());
byte[] actual = v.GetData();
Assert.AreEqual(bytes.Length * 2, actual.Length);
}
[TestMethod]
public void VectorTile_AddImage()
{
VectorTile v = new VectorTile(1, 0, 0);
byte[] bytes = File.ReadAllBytes(@".\data\world_1.0.0.png");
v.AddImage(bytes, "raster");
Assert.IsFalse(v.Empty());
Assert.IsTrue(v.Painted());
CollectionAssert.AreEquivalent(new List<string>() { "raster" }, v.Names().ToList());
Map m = new Map(256, 256);
m.Load(@".\data\raster_style.xml");
Image i = new Image(256, 256);
v.Render(m, i);
Assert.AreEqual(0,i.Compare(Image.FromBytes(bytes)));
}
[TestMethod]
public void VectorTile_GeoJSON()
{
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "geojson.input"));
string json = @"{""type"":""FeatureCollection"",""name"":""layer"",""features"":[{""type"":""Feature"",""geometry"":{""type"":""Point"",""coordinates"":[-121.9921875,47.9899216674142]},""properties"":{""name"":""geojson data""}}]}";
VectorTile v = new VectorTile(0, 0, 0);
v.AddGeoJSON(json, "layer");
byte[] bytes = v.GetData();
Assert.AreEqual(53, bytes.Length);
Assert.IsFalse(v.Empty());
Assert.IsTrue(v.Painted());
CollectionAssert.AreEquivalent(new List<string>() { "layer" }, v.Names().ToList());
//ToGeoJSON
dynamic expected = JObject.Parse(json);
var results = new List<JObject>()
{
JObject.Parse(v.ToGeoJSON("layer")),
JObject.Parse(v.ToGeoJSON(0))
};
foreach (dynamic actual in results)
{
Assert.AreEqual(expected.FeatureCollection, actual.FeatureCollection);
Assert.AreEqual(expected.features[0].properties.name, actual.features[0].properties.name);
Assert.AreEqual(expected.features[0].geometry.type, actual.features[0].geometry.type);
Assert.AreEqual(expected.features[0].geometry.coordinates[0], actual.features[0].geometry.coordinates[0]);
Assert.AreEqual(expected.features[0].geometry.coordinates[1], actual.features[0].geometry.coordinates[1]);
}
}
[TestMethod]
public void VectorTile_ToJSON()
{
VectorTile v = new VectorTile(9, 112, 195);
byte[] bytes = File.ReadAllBytes(@".\data\9.112.195.pbf");
v.SetData(bytes);
IEnumerable<VectorTileLayer> vtJSON = v.ToJSON();
string json = JsonConvert.SerializeObject(
vtJSON,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
string expected = @"[{""Features"":[{""Properties"":{""AREA"":915896,""FIPS"":""US"",""ISO2"":""US"",""ISO3"":""USA"",""LAT"":39.622,""LON"":-98.606,""NAME"":""United States"",""POP2005"":299846449,""REGION"":19,""SUBREGION"":21,""UN"":840},""Geometry"":[9,8192,0,58,0,0,0,8192,0,8191,0,8192,0,0,8191,0,0,8191,15],""Type"":3,""Id"":207}],""Version"":1,""Extent"":4096,""Name"":""world""}]";
Assert.AreEqual(expected, json);
}
[TestMethod]
public void VectorTile_IsSolid()
{
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "shape.input"));
Map m = new Map(256, 256);
m.Load(@".\data\layer.xml");
m.Extent = new double[] { -11271098.442818949, 4696291.017841229, -11192826.925854929, 4774562.534805249 };
VectorTile v = new VectorTile(9, 112, 195);
m.Render(v);
Assert.IsTrue(v.Painted());
Assert.AreEqual("world", v.IsSolid());
}
[TestMethod]
public void VectorTile_Clear()
{
VectorTile v = new VectorTile(9,112,195);
byte[] bytes = File.ReadAllBytes(@".\data\9.112.195.pbf");
v.SetData(bytes);
Assert.IsFalse(v.Empty());
Assert.IsTrue(v.Painted());
CollectionAssert.AreEquivalent(new List<string>() { "world" }, v.Names().ToList());
byte[] actual = v.GetData();
Assert.AreEqual(bytes.Length, actual.Length);
v.Clear();
Assert.IsTrue(v.Empty());
Assert.IsFalse(v.Painted());
Assert.AreEqual("",v.IsSolid());
CollectionAssert.AreEquivalent(new List<string>(), v.Names().ToList());
actual = v.GetData();
Assert.AreEqual(0, actual.Length);
}
[TestMethod]
public void VectorTile_Query()
{
VectorTile v = new VectorTile(9, 112, 195);
byte[] bytes = File.ReadAllBytes(@".\data\9.112.195.pbf");
v.SetData(bytes);
IEnumerable<VectorQueryResult> results = v.Query(-100.8576, 39.1181);
VectorQueryResult result = results.ToList()[0];
Assert.AreEqual("world", result.Layer);
Assert.AreEqual(0, result.Distance);
Assert.AreEqual(207, result.Feature.Id());
IDictionary<string, object> attributes = result.Feature.Attributes();
Assert.AreEqual("United States", (string)attributes["NAME"]);
}
[TestMethod]
public void VectorTile_Render_Image()
{
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "shape.input"));
Map m = new Map(256, 256);
m.Load(@".\data\layer.xml");
m.Extent = new double[] { -20037508.34, 0, 0, 20037508.34 };
VectorTile v = new VectorTile(1, 0, 0);
m.Render(v);
VectorTile v2 = new VectorTile(1, 0, 0);
v2.SetData(v.GetData());
Map m2 = new Map(256, 256);
m2.Load(@".\data\style.xml");
Image i = new Image(256, 256);
v2.Render(m2, i);
Assert.AreEqual(0,i.Compare(Image.Open(@".\data\world_1.0.0.png")));
}
[TestMethod]
public void VectorTile_Render_Grid()
{
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "shape.input"));
Map m = new Map(256, 256);
m.Load(@".\data\layer.xml");
m.Extent = new double[] { -20037508.34, 0, 0, 20037508.34 };
VectorTile v = new VectorTile(1, 0, 0);
m.Render(v);
VectorTile v2 = new VectorTile(1, 0, 0);
v2.SetData(v.GetData());
Map m2 = new Map(256, 256);
m2.Load(@".\data\style.xml");
Grid g = new Grid(256, 256);
var options = new Dictionary<string, object>()
{
{"Fields", new List<string>() { "FIPS" } },
{"Layer", "world" }
};
v2.Render(m2, g, options);
Dictionary<string, object > grid = g.Encode();
Assert.AreEqual(grid.Keys.Count, 3);
//Test for keys
List<string> keyList = (List<string>)grid["keys"];
Assert.AreNotEqual(keyList.Count, 0);
//Test for data
Dictionary<string, object> dataDict = (Dictionary<string, object>)grid["data"];
Assert.AreNotEqual(dataDict.Count, 0);
//data count should equal keys + 1
Assert.AreEqual(keyList.Count, dataDict.Count + 1);
}
[TestMethod]
public void VectorTile_Overzoom_Render()
{
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "shape.input"));
Map m = new Map(256, 256);
m.Load(@".\data\layer.xml");
m.ZoomToBox(-20037508.34, -20037508.34, 20037508.34, 20037508.34);
VectorTile v1 = new VectorTile(0, 0, 0, 256, 256);
m.Render(v1);
m.ZoomToBox(-20037508.34, 0, 0, 20037508.34);
VectorTile v2 = new VectorTile(1, 0, 0, 256, 256);
m.Render(v2);
Map renderMap = new Map(256, 256);
renderMap.Load(@".\data\style.xml");
Image i1 = new Image(256, 256);
Image i2 = new Image(256, 256);
Dictionary<string, object> options = new Dictionary<string, object>();
options["Z"] = 1;
v1.Render(renderMap, i1, options);
v2.Render(renderMap, i2);
//Small diff showing up between images
Assert.IsTrue(i1.Compare(i2)-500 < 0);
}
}
}