forked from sgillies/topojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_topojson.py
102 lines (93 loc) · 2.74 KB
/
test_topojson.py
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
import json
from unittest import TestCase
from topojson import coordinates, geometry, rel2abs
data = """{
"arcs":
[[[3028, 4277],
[-1, -2],
[0, 1],
[0, 1],
[-1, 3],
[0, 2],
[0, 2],
[1, 3],
[0, 2],
[1, -3],
[0, -6],
[0, -3]],
[[3014, 4287],
[0, -2],
[0, 1],
[0, 1],
[0, 2],
[1, 0],
[0, 1],
[1, 0],
[0, 2],
[0, -1],
[0, -1],
[0, -1],
[0, -2],
[-1, 0],
[-1, 0]]],
"transform": {
"scale": [0.035896033450880604, 0.005251163636665131],
"translate": [-179.14350338367416, 18.906117143691233]
}
}"""
topology = json.loads(data)
topology_arcs = topology['arcs']
scale = topology['transform']['scale']
translate = topology['transform']['translate']
def test_rel2abs_noscale():
arc = [[0, 0], [1, 1]]
coords = list(rel2abs(arc, None, None))
assert coords[0] == (0, 0)
assert coords[1] == (1, 1)
def test_rel2abs():
arc = [[0, 0], [1, 1]]
coords = list(rel2abs(arc, (2.0, 2.0), (100.0, 100.0)))
assert coords[0] == (100.0, 100.0)
assert coords[1] == (102.0, 102.0)
class CoordinatesTest(TestCase):
def failUnlessCoordsAlmostEqual(self, a, b):
self.failUnlessAlmostEqual(a[0], b[0])
self.failUnlessAlmostEqual(a[1], b[1])
def test_one(self):
coords = coordinates([0], topology_arcs, scale, translate)
self.failUnlessEqual(len(coords), 12)
self.failUnlessCoordsAlmostEqual(
coords[0],
(-70.45031409440769, 41.365344017708) )
def test_one_rev(self):
coords = coordinates([-1], topology_arcs, scale, translate)
self.failUnlessEqual(len(coords), 12)
self.failUnlessCoordsAlmostEqual(
coords[-1],
(-70.45031409440769, 41.365344017708) )
def test_two(self):
coords = coordinates([0, 1], topology_arcs, scale, translate)
self.failUnlessEqual(len(coords), 26)
self.failUnlessCoordsAlmostEqual(
coords[0],
(-70.45031409440769, 41.365344017708) )
def test_two_rev(self):
coords = coordinates([-2, -1], topology_arcs, scale, translate)
self.failUnlessEqual(len(coords), 26)
self.failUnlessCoordsAlmostEqual(
coords[-1],
(-70.45031409440769, 41.365344017708) )
def test_ring(self):
coords = coordinates([[0, 1]], topology_arcs, scale, translate)
self.failUnlessEqual(len(coords), 1)
self.failUnlessEqual(len(coords[0]), 26)
self.failUnlessCoordsAlmostEqual(
coords[0][0],
(-70.45031409440769, 41.365344017708) )
def test_geometry():
geom = geometry(
{'type': "LineString", 'arcs': [0, 1]},
topology_arcs,
scale, translate )
assert geom['type'] == "LineString"
assert len(geom['coordinates']) == 26