Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
142 lines (124 loc) · 6.61 KB
/
Copy pathtest.py
File metadata and controls
142 lines (124 loc) · 6.61 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
from __future__ import print_function
import unittest
import json
import math
class Test(unittest.TestCase):
def test_linestrings_intersect(self):
from geojson_utils import linestrings_intersect
diagonal_up_str = '{ "type": "LineString","coordinates": [[0, 0], [10, 10]]}'
diagonal_down_str = '{ "type": "LineString","coordinates": [[10, 0], [0, 10]]}'
far_away_str = '{ "type": "LineString","coordinates": [[100, 100], [110, 110]]}'
diagonal_up = json.loads(diagonal_up_str)
diagonal_down = json.loads(diagonal_down_str)
far_away = json.loads(far_away_str)
self.assertEqual(linestrings_intersect(diagonal_up, diagonal_down), [{'type': 'Point', 'coordinates': [5, 5]}])
self.assertEqual(linestrings_intersect(diagonal_up, far_away), [])
def test_point_in_polygon(self):
from geojson_utils import point_in_polygon
in_str = '{"type": "Point", "coordinates": [5, 5]}'
out_str = '{"type": "Point", "coordinates": [15, 15]}'
box_str = '{"type": "Polygon","coordinates": [[ [0, 0], [10, 0], [10, 10], [0, 10] ]]}'
in_box = json.loads(in_str)
out_box = json.loads(out_str)
box = json.loads(box_str)
self.assertTrue(point_in_polygon(in_box, box))
self.assertFalse(point_in_polygon(out_box, box))
def test_point_in_multipolygon(self):
from geojson_utils import point_in_multipolygon
point_str = '{"type": "Point", "coordinates": [0.5, 0.5]}'
single_point_str = '{"type": "Point", "coordinates": [-1, -1]}'
multipoly_str = '{"type":"MultiPolygon","coordinates":[[[[0,0],[0,10],[10,10],[10,0],[0,0]]],[[[10,10],[10,20],[20,20],[20,10],[10,10]]]]}'
point = json.loads(point_str)
single_point = json.loads(single_point_str)
multipoly = json.loads(multipoly_str)
self.assertTrue(point_in_multipolygon(point, multipoly))
self.assertFalse(point_in_multipolygon(single_point, multipoly))
def test_drawCircle(self):
from geojson_utils import draw_circle
pt_center = json.loads('{"type": "Point", "coordinates": [0, 0]}')
self.assertEqual(
len(draw_circle(10, pt_center)['coordinates'][0]), 15)
self.assertEqual(
len(draw_circle(10, pt_center, 50)['coordinates'][0]), 50)
def test_rectangle_centroid(self):
from geojson_utils import rectangle_centroid
box_str = '{"type": "Polygon","coordinates": [[[0, 0],[10, 0],[10, 10],[0, 10]]]}'
box = json.loads(box_str)
centroid = rectangle_centroid(box)
self.assertEqual(centroid['coordinates'], [5, 5])
def test_point_distance(self):
from geojson_utils import point_distance
fairyland_str = '{"type": "Point", "coordinates": [-122.260000705719, 37.80919060818706]}'
navalbase_str = '{"type": "Point", "coordinates": [-122.32083320617676, 37.78774223089045]}'
fairyland = json.loads(fairyland_str)
navalbase = json.loads(navalbase_str)
self.assertEqual(math.floor(
point_distance(fairyland, navalbase)), 5852)
def test_geometry_radius(self):
from geojson_utils import geometry_within_radius
center_point_str = '{"type": "Point", "coordinates": [-122.260000705719, 37.80919060818706]}'
check_point_str = '{"type": "Point", "coordinates": [-122.32083320617676, 37.78774223089045]}'
center_point = json.loads(center_point_str)
check_point = json.loads(check_point_str)
self.assertTrue(geometry_within_radius(check_point, center_point, 5853))
def test_area(self):
from geojson_utils import area
box_str = '{"type": "Polygon","coordinates": [[ [0, 0], [10, 0], [10, 10], [0, 10] ]]}'
box = json.loads(box_str)
self.assertEqual(area(box), 100)
def test_centroid(self):
from geojson_utils import centroid
box_str = '{"type": "Polygon","coordinates": [[ [0, 0], [10, 0], [10, 10], [0, 10] ]]}'
box = json.loads(box_str)
self.assertEqual(centroid(box), {"type": "Point", "coordinates": [5, 5]})
def test_destination_point(self):
from geojson_utils import destination_point
startpoint_str = '{"type": "Point", "coordinates": [-122.260000705719, 37.80919060818706]}'
startpoint = json.loads(startpoint_str)
self.assertEqual(destination_point(startpoint, 180, 2000)["coordinates"][0], -122.26000070571902)
def test_distance_ellipsode(self):
from geojson_utils import point_distance_ellipsode
fairyland_str = '{"type": "Point", "coordinates": [-122.260000705719, 37.80919060818706]}'
navalbase_str = '{"type": "Point", "coordinates": [-122.32083320617676, 37.78774223089045]}'
fairyland = json.loads(fairyland_str)
navalbase = json.loads(navalbase_str)
self.assertAlmostEqual(math.floor(point_distance_ellipsode(fairyland,navalbase)),2380)
def test_featurecollection(self):
from geojson_utils import merge_featurecollection
with open('tests/first.json','r') as fp:
first = json.load(fp)
with open('tests/second.json','r') as fp:
second = json.load(fp)
with open('tests/result.json','r') as fp:
result = json.load(fp)
self.assertEqual(merge_featurecollection(first,second), result)
def test_convertor(self):
from geojson_utils import convertor
with open('tests/province_wgs.geojson', encoding='utf-8') as fp:
geojson = json.load(fp)
features = geojson['features']
for feature in features:
origin = feature['geometry']['coordinates'][0][0][0]
result = convertor(feature['geometry'])
self.assertNotEqual(origin,result['coordinates'][0][0][0])
def test_simplify_removes_points_inside_tolerance(self):
from geojson_utils import simplify
points = [
{'type': 'Point', 'coordinates': [0, 0]},
{'type': 'Point', 'coordinates': [0.001, 0.00001]},
{'type': 'Point', 'coordinates': [0.002, 0]},
]
self.assertEqual(simplify(points, 20), [
{'type': 'Point', 'coordinates': [0, 0]},
{'type': 'Point', 'coordinates': [0.002, 0]},
])
def test_simplify_keeps_points_outside_tolerance(self):
from geojson_utils import simplify
points = [
{'type': 'Point', 'coordinates': [0, 0]},
{'type': 'Point', 'coordinates': [0.001, 0.001]},
{'type': 'Point', 'coordinates': [0.002, 0]},
]
self.assertEqual(simplify(points, 20), points)
if __name__ == '__main__':
unittest.main()