forked from jbrwn/NET-Mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometryTests.cs
110 lines (100 loc) · 4.17 KB
/
GeometryTests.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
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
namespace NETMapnik.Test
{
[TestClass]
public class GeometryTests
{
[TestMethod]
public void Geometry_ToWKT()
{
string input = @"{
type: ""Feature"",
properties: {},
geometry: {
type: ""Polygon"",
coordinates: [[[1,1],[1,2],[2,2],[2,1],[1,1]]]
}
}";
JObject feature = JObject.Parse(input);
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "csv.input"));
Dictionary<string, object> options = new Dictionary<string, object>()
{
{ "type","csv"},
{ "inline", "geojson\n'" + feature["geometry"].ToString(Formatting.None) + "'" }
};
Datasource ds = new Datasource(options);
Feature f = ds.Featureset().Next();
string expected = "POLYGON((1 1,1 2,2 2,2 1,1 1))";
Assert.AreEqual(expected, f.Geometry().ToWKT());
}
[TestMethod]
public void Geometry_ToWKB()
{
string input = @"{
type: ""Feature"",
properties: {},
geometry: {
type: ""Polygon"",
coordinates: [[[1,1],[1,2],[2,2],[2,1],[1,1]]]
}
}";
JObject feature = JObject.Parse(input);
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "csv.input"));
Dictionary<string, object> options = new Dictionary<string, object>()
{
{ "type","csv"},
{ "inline", "geojson\n'" + feature["geometry"].ToString(Formatting.None) + "'" }
};
Datasource ds = new Datasource(options);
Feature f = ds.Featureset().Next();
string hex = "01030000000100000005000000000000000000f03f000000000000f03f000000000000f03f0000000000000040000000000000004000000000000000400000000000000040000000000000f03f000000000000f03f000000000000f03f";
byte[] expected = Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
byte[] actual = f.Geometry().ToWKB();
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
public void Geometry_ToJSON()
{
string expected = @"{""type"":""Polygon"",""coordinates"":[[[1,1],[1,2],[2,2],[2,1],[1,1]]]}";
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "csv.input"));
var options = new Dictionary<string, object>()
{
{ "type","csv"},
{ "inline", "geojson\n'" + expected + "'" }
};
Datasource ds = new Datasource(options);
Feature f = ds.Featureset().Next();
string actual = f.Geometry().ToJSON();
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void Geometry_ToJSON_ProjTransform()
{
string expected = @"{""type"":""Polygon"",""coordinates"":[[[1,1],[1,2],[2,2],[2,1],[1,1]]]}";
Mapnik.RegisterDatasource(Path.Combine(Mapnik.Paths["InputPlugins"], "csv.input"));
var options = new Dictionary<string, object>()
{
{ "type","csv"},
{ "inline", "geojson\n'" + expected + "'" }
};
Datasource ds = new Datasource(options);
Feature f = ds.Featureset().Next();
string actual = f.Geometry().ToJSON();
Assert.AreEqual(expected, actual);
Projection src = new Projection("+init=epsg:4326");
Projection dest = new Projection("+init=epsg:3857");
ProjTransform tran = new ProjTransform(src, dest);
string transformed = f.Geometry().ToJSON(tran);
Assert.AreNotEqual(expected, transformed);
}
}
}