1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from Numeric import zeros, array, allclose |
---|
6 | from math import sqrt, pi |
---|
7 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
8 | from anuga.utilities.system_tools import get_pathname_from_package |
---|
9 | |
---|
10 | from polygon import * |
---|
11 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
12 | from anuga.geospatial_data.geospatial_data import Geospatial_data |
---|
13 | |
---|
14 | def test_function(x, y): |
---|
15 | return x+y |
---|
16 | |
---|
17 | class Test_Polygon(unittest.TestCase): |
---|
18 | def setUp(self): |
---|
19 | pass |
---|
20 | |
---|
21 | def tearDown(self): |
---|
22 | pass |
---|
23 | |
---|
24 | |
---|
25 | def test_that_C_extension_compiles(self): |
---|
26 | FN = 'polygon_ext.c' |
---|
27 | try: |
---|
28 | import polygon_ext |
---|
29 | except: |
---|
30 | from compile import compile |
---|
31 | |
---|
32 | try: |
---|
33 | compile(FN) |
---|
34 | except: |
---|
35 | raise 'Could not compile %s' %FN |
---|
36 | else: |
---|
37 | import polygon_ext |
---|
38 | |
---|
39 | |
---|
40 | # Polygon stuff |
---|
41 | def test_polygon_function_constants(self): |
---|
42 | p1 = [[0,0], [10,0], [10,10], [0,10]] |
---|
43 | p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
44 | |
---|
45 | f = Polygon_function( [(p1, 1.0)] ) |
---|
46 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 |
---|
47 | assert allclose(z, [1,1,0,0]) |
---|
48 | |
---|
49 | |
---|
50 | f = Polygon_function( [(p2, 2.0)] ) |
---|
51 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) # First and last inside p2 |
---|
52 | assert allclose(z, [2,0,0,2]) |
---|
53 | |
---|
54 | |
---|
55 | #Combined |
---|
56 | f = Polygon_function( [(p1, 1.0), (p2, 2.0)] ) |
---|
57 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
58 | assert allclose(z, [2,1,0,2]) |
---|
59 | |
---|
60 | def test_polygon_function_csvfile(self): |
---|
61 | from os import sep, getenv |
---|
62 | |
---|
63 | |
---|
64 | # Get path where this test is run |
---|
65 | path = get_pathname_from_package('anuga.utilities') |
---|
66 | |
---|
67 | # Form absolute filename and read |
---|
68 | filename = path + sep + 'mainland_only.csv' |
---|
69 | p1 = read_polygon(filename) |
---|
70 | |
---|
71 | f = Polygon_function( [(p1, 10.0)] ) |
---|
72 | z = f([430000,480000], [490000,7720000]) # first outside, second inside |
---|
73 | |
---|
74 | assert allclose(z, [0,10]) |
---|
75 | |
---|
76 | def test_polygon_function_georef(self): |
---|
77 | """Check that georeferencing works |
---|
78 | """ |
---|
79 | |
---|
80 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
81 | |
---|
82 | geo = Geo_reference(56, 200, 1000) |
---|
83 | |
---|
84 | # Make points 'absolute' |
---|
85 | p1 = [[200,1000], [210,1000], [210,1010], [200,1010]] |
---|
86 | p2 = [[200,1000], [210,1010], [215,1005], [220, 1010], [225,1000], |
---|
87 | [230,1010], [240,990]] |
---|
88 | |
---|
89 | f = Polygon_function( [(p1, 1.0)], geo_reference=geo) |
---|
90 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 |
---|
91 | |
---|
92 | assert allclose(z, [1,1,0,0]) |
---|
93 | |
---|
94 | |
---|
95 | f = Polygon_function( [(p2, 2.0)], geo_reference=geo) |
---|
96 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2 |
---|
97 | assert allclose(z, [2,0,0,2]) |
---|
98 | |
---|
99 | |
---|
100 | # Combined |
---|
101 | f = Polygon_function( [(p1, 1.0), (p2, 2.0)], geo_reference=geo) |
---|
102 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
103 | assert allclose(z, [2,1,0,2]) |
---|
104 | |
---|
105 | |
---|
106 | # Check that it would fail without geo |
---|
107 | f = Polygon_function( [(p1, 1.0), (p2, 2.0)]) |
---|
108 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
109 | assert not allclose(z, [2,1,0,2]) |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | def test_polygon_function_callable(self): |
---|
114 | """Check that values passed into Polygon_function can be callable |
---|
115 | themselves. |
---|
116 | """ |
---|
117 | p1 = [[0,0], [10,0], [10,10], [0,10]] |
---|
118 | p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
119 | |
---|
120 | f = Polygon_function( [(p1, test_function)] ) |
---|
121 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 |
---|
122 | assert allclose(z, [10,14,0,0]) |
---|
123 | |
---|
124 | # Combined |
---|
125 | f = Polygon_function( [(p1, test_function), (p2, 2.0)] ) |
---|
126 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
127 | assert allclose(z, [2,14,0,2]) |
---|
128 | |
---|
129 | |
---|
130 | # Combined w default |
---|
131 | f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14) |
---|
132 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
133 | assert allclose(z, [2,14,3.14,2]) |
---|
134 | |
---|
135 | |
---|
136 | # Combined w default func |
---|
137 | f = Polygon_function( [(p1, test_function), (p2, 2.0)], |
---|
138 | default = test_function) |
---|
139 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
140 | assert allclose(z, [2,14,35,2]) |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | def test_point_on_line(self): |
---|
145 | |
---|
146 | # Endpoints first |
---|
147 | assert point_on_line( [0, 0], [[0,0], [1,0]] ) |
---|
148 | assert point_on_line( [1, 0], [[0,0], [1,0]] ) |
---|
149 | |
---|
150 | # Then points on line |
---|
151 | assert point_on_line( [0.5, 0], [[0,0], [1,0]] ) |
---|
152 | assert point_on_line( [0, 0.5], [[0,1], [0,0]] ) |
---|
153 | assert point_on_line( [1, 0.5], [[1,1], [1,0]] ) |
---|
154 | assert point_on_line( [0.5, 0.5], [[0,0], [1,1]] ) |
---|
155 | |
---|
156 | # Then points not on line |
---|
157 | assert not point_on_line( [0.5, 0], [[0,1], [1,1]] ) |
---|
158 | assert not point_on_line( [0, 0.5], [[0,0], [1,1]] ) |
---|
159 | |
---|
160 | # From real example that failed |
---|
161 | assert not point_on_line( [40,50], [[40,20], [40,40]] ) |
---|
162 | |
---|
163 | |
---|
164 | # From real example that failed |
---|
165 | assert not point_on_line( [40,19], [[40,20], [40,40]] ) |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | def test_is_inside_polygon_main(self): |
---|
171 | |
---|
172 | |
---|
173 | # Simplest case: Polygon is the unit square |
---|
174 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
175 | |
---|
176 | assert is_inside_polygon( (0.5, 0.5), polygon ) |
---|
177 | assert not is_inside_polygon( (0.5, 1.5), polygon ) |
---|
178 | assert not is_inside_polygon( (0.5, -0.5), polygon ) |
---|
179 | assert not is_inside_polygon( (-0.5, 0.5), polygon ) |
---|
180 | assert not is_inside_polygon( (1.5, 0.5), polygon ) |
---|
181 | |
---|
182 | # Try point on borders |
---|
183 | assert is_inside_polygon( (1., 0.5), polygon, closed=True) |
---|
184 | assert is_inside_polygon( (0.5, 1), polygon, closed=True) |
---|
185 | assert is_inside_polygon( (0., 0.5), polygon, closed=True) |
---|
186 | assert is_inside_polygon( (0.5, 0.), polygon, closed=True) |
---|
187 | |
---|
188 | assert not is_inside_polygon( (0.5, 1), polygon, closed=False) |
---|
189 | assert not is_inside_polygon( (0., 0.5), polygon, closed=False) |
---|
190 | assert not is_inside_polygon( (0.5, 0.), polygon, closed=False) |
---|
191 | assert not is_inside_polygon( (1., 0.5), polygon, closed=False) |
---|
192 | |
---|
193 | |
---|
194 | def test_inside_polygon_main(self): |
---|
195 | |
---|
196 | # Simplest case: Polygon is the unit square |
---|
197 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
198 | |
---|
199 | # From real example (that failed) |
---|
200 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
201 | points = [ [40, 50] ] |
---|
202 | res = inside_polygon(points, polygon) |
---|
203 | assert len(res) == 0 |
---|
204 | |
---|
205 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
206 | points = [ [25, 25], [30, 20], [40, 50], [90, 20], [40, 90] ] |
---|
207 | res = inside_polygon(points, polygon) |
---|
208 | assert len(res) == 2 |
---|
209 | assert allclose(res, [0,1]) |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | # More convoluted and non convex polygon |
---|
214 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
215 | assert is_inside_polygon( (0.5, 0.5), polygon ) |
---|
216 | assert is_inside_polygon( (1, -0.5), polygon ) |
---|
217 | assert is_inside_polygon( (1.5, 0), polygon ) |
---|
218 | |
---|
219 | assert not is_inside_polygon( (0.5, 1.5), polygon ) |
---|
220 | assert not is_inside_polygon( (0.5, -0.5), polygon ) |
---|
221 | |
---|
222 | |
---|
223 | # Very convoluted polygon |
---|
224 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
225 | assert is_inside_polygon( (5, 5), polygon ) |
---|
226 | assert is_inside_polygon( (17, 7), polygon ) |
---|
227 | assert is_inside_polygon( (27, 2), polygon ) |
---|
228 | assert is_inside_polygon( (35, -5), polygon ) |
---|
229 | assert not is_inside_polygon( (15, 7), polygon ) |
---|
230 | assert not is_inside_polygon( (24, 3), polygon ) |
---|
231 | assert not is_inside_polygon( (25, -10), polygon ) |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | # Another combination (that failed) |
---|
236 | polygon = [[0,0], [10,0], [10,10], [0,10]] |
---|
237 | assert is_inside_polygon( (5, 5), polygon ) |
---|
238 | assert is_inside_polygon( (7, 7), polygon ) |
---|
239 | assert not is_inside_polygon( (-17, 7), polygon ) |
---|
240 | assert not is_inside_polygon( (7, 17), polygon ) |
---|
241 | assert not is_inside_polygon( (17, 7), polygon ) |
---|
242 | assert not is_inside_polygon( (27, 8), polygon ) |
---|
243 | assert not is_inside_polygon( (35, -5), polygon ) |
---|
244 | |
---|
245 | |
---|
246 | |
---|
247 | |
---|
248 | # Multiple polygons |
---|
249 | |
---|
250 | polygon = [[0,0], [1,0], [1,1], [0,1], [0,0], |
---|
251 | [10,10], [11,10], [11,11], [10,11], [10,10]] |
---|
252 | assert is_inside_polygon( (0.5, 0.5), polygon ) |
---|
253 | assert is_inside_polygon( (10.5, 10.5), polygon ) |
---|
254 | |
---|
255 | #FIXME: Fails if point is 5.5, 5.5 |
---|
256 | assert not is_inside_polygon( (0, 5.5), polygon ) |
---|
257 | |
---|
258 | # Polygon with a hole |
---|
259 | polygon = [[-1,-1], [2,-1], [2,2], [-1,2], [-1,-1], |
---|
260 | [0,0], [1,0], [1,1], [0,1], [0,0]] |
---|
261 | |
---|
262 | assert is_inside_polygon( (0, -0.5), polygon ) |
---|
263 | assert not is_inside_polygon( (0.5, 0.5), polygon ) |
---|
264 | |
---|
265 | |
---|
266 | |
---|
267 | def test_duplicate_points_being_ok(self): |
---|
268 | |
---|
269 | |
---|
270 | # Simplest case: Polygon is the unit square |
---|
271 | polygon = [[0,0], [1,0], [1,0], [1,0], [1,1], [0,1], [0,0]] |
---|
272 | |
---|
273 | assert is_inside_polygon( (0.5, 0.5), polygon ) |
---|
274 | assert not is_inside_polygon( (0.5, 1.5), polygon ) |
---|
275 | assert not is_inside_polygon( (0.5, -0.5), polygon ) |
---|
276 | assert not is_inside_polygon( (-0.5, 0.5), polygon ) |
---|
277 | assert not is_inside_polygon( (1.5, 0.5), polygon ) |
---|
278 | |
---|
279 | # Try point on borders |
---|
280 | assert is_inside_polygon( (1., 0.5), polygon, closed=True) |
---|
281 | assert is_inside_polygon( (0.5, 1), polygon, closed=True) |
---|
282 | assert is_inside_polygon( (0., 0.5), polygon, closed=True) |
---|
283 | assert is_inside_polygon( (0.5, 0.), polygon, closed=True) |
---|
284 | |
---|
285 | assert not is_inside_polygon( (0.5, 1), polygon, closed=False) |
---|
286 | assert not is_inside_polygon( (0., 0.5), polygon, closed=False) |
---|
287 | assert not is_inside_polygon( (0.5, 0.), polygon, closed=False) |
---|
288 | assert not is_inside_polygon( (1., 0.5), polygon, closed=False) |
---|
289 | |
---|
290 | # From real example |
---|
291 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
292 | points = [ [40, 50] ] |
---|
293 | res = inside_polygon(points, polygon) |
---|
294 | assert len(res) == 0 |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | def test_inside_polygon_vector_version(self): |
---|
299 | # Now try the vector formulation returning indices |
---|
300 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
301 | points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
302 | res = inside_polygon( points, polygon, verbose=False ) |
---|
303 | |
---|
304 | assert allclose( res, [0,1,2] ) |
---|
305 | |
---|
306 | def test_outside_polygon(self): |
---|
307 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
308 | |
---|
309 | assert not is_outside_polygon( [0.5, 0.5], U ) |
---|
310 | # evaluate to False as the point 0.5, 0.5 is inside the unit square |
---|
311 | |
---|
312 | assert is_outside_polygon( [1.5, 0.5], U ) |
---|
313 | # evaluate to True as the point 1.5, 0.5 is outside the unit square |
---|
314 | |
---|
315 | indices = outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) |
---|
316 | assert allclose( indices, [1] ) |
---|
317 | |
---|
318 | # One more test of vector formulation returning indices |
---|
319 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
320 | points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
321 | res = outside_polygon( points, polygon ) |
---|
322 | |
---|
323 | assert allclose( res, [3, 4] ) |
---|
324 | |
---|
325 | |
---|
326 | |
---|
327 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
328 | points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
329 | res = outside_polygon( points, polygon ) |
---|
330 | |
---|
331 | assert allclose( res, [0, 4, 5] ) |
---|
332 | |
---|
333 | def test_outside_polygon2(self): |
---|
334 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
335 | |
---|
336 | assert not outside_polygon( [0.5, 1.0], U, closed = True ) |
---|
337 | # evaluate to False as the point 0.5, 1.0 is inside the unit square |
---|
338 | |
---|
339 | assert is_outside_polygon( [0.5, 1.0], U, closed = False ) |
---|
340 | # evaluate to True as the point 0.5, 1.0 is outside the unit square |
---|
341 | |
---|
342 | def test_all_outside_polygon(self): |
---|
343 | """Test case where all points are outside poly |
---|
344 | """ |
---|
345 | |
---|
346 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
347 | |
---|
348 | points = [[2,2], [1,3], [-1,1], [0,2]] #All outside |
---|
349 | |
---|
350 | |
---|
351 | indices, count = separate_points_by_polygon(points, U) |
---|
352 | #print indices, count |
---|
353 | assert count == 0 #None inside |
---|
354 | assert allclose(indices, [3,2,1,0]) |
---|
355 | |
---|
356 | indices = outside_polygon(points, U, closed = True) |
---|
357 | assert allclose(indices, [0,1,2,3]) |
---|
358 | |
---|
359 | indices = inside_polygon(points, U, closed = True) |
---|
360 | assert allclose(indices, []) |
---|
361 | |
---|
362 | |
---|
363 | def test_all_inside_polygon(self): |
---|
364 | """Test case where all points are inside poly |
---|
365 | """ |
---|
366 | |
---|
367 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
368 | |
---|
369 | points = [[0.5,0.5], [0.2,0.3], [0,0.5]] #All inside (or on edge) |
---|
370 | |
---|
371 | |
---|
372 | indices, count = separate_points_by_polygon(points, U) |
---|
373 | assert count == 3 #All inside |
---|
374 | assert allclose(indices, [0,1,2]) |
---|
375 | |
---|
376 | indices = outside_polygon(points, U, closed = True) |
---|
377 | assert allclose(indices, []) |
---|
378 | |
---|
379 | indices = inside_polygon(points, U, closed = True) |
---|
380 | assert allclose(indices, [0,1,2]) |
---|
381 | |
---|
382 | |
---|
383 | def test_separate_points_by_polygon(self): |
---|
384 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
385 | |
---|
386 | indices, count = separate_points_by_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) |
---|
387 | assert allclose( indices, [0,2,1] ) |
---|
388 | assert count == 2 |
---|
389 | |
---|
390 | #One more test of vector formulation returning indices |
---|
391 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
392 | points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
393 | res, count = separate_points_by_polygon( points, polygon ) |
---|
394 | |
---|
395 | assert allclose( res, [0,1,2,4,3] ) |
---|
396 | assert count == 3 |
---|
397 | |
---|
398 | |
---|
399 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
400 | points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
401 | res, count = separate_points_by_polygon( points, polygon ) |
---|
402 | |
---|
403 | assert allclose( res, [1,2,3,5,4,0] ) |
---|
404 | assert count == 3 |
---|
405 | |
---|
406 | |
---|
407 | def test_populate_polygon(self): |
---|
408 | |
---|
409 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
410 | points = populate_polygon(polygon, 5) |
---|
411 | |
---|
412 | assert len(points) == 5 |
---|
413 | for point in points: |
---|
414 | assert is_inside_polygon(point, polygon) |
---|
415 | |
---|
416 | |
---|
417 | #Very convoluted polygon |
---|
418 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
419 | |
---|
420 | points = populate_polygon(polygon, 5) |
---|
421 | |
---|
422 | assert len(points) == 5 |
---|
423 | for point in points: |
---|
424 | assert is_inside_polygon(point, polygon) |
---|
425 | |
---|
426 | |
---|
427 | def test_populate_polygon_with_exclude(self): |
---|
428 | |
---|
429 | |
---|
430 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
431 | ex_poly = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter |
---|
432 | points = populate_polygon(polygon, 5, exclude = [ex_poly]) |
---|
433 | |
---|
434 | assert len(points) == 5 |
---|
435 | for point in points: |
---|
436 | assert is_inside_polygon(point, polygon) |
---|
437 | assert not is_inside_polygon(point, ex_poly) |
---|
438 | |
---|
439 | |
---|
440 | #overlap |
---|
441 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
442 | ex_poly = [[-1,-1], [0.5,0], [0.5, 0.5], [-1,0.5]] |
---|
443 | points = populate_polygon(polygon, 5, exclude = [ex_poly]) |
---|
444 | |
---|
445 | assert len(points) == 5 |
---|
446 | for point in points: |
---|
447 | assert is_inside_polygon(point, polygon) |
---|
448 | assert not is_inside_polygon(point, ex_poly) |
---|
449 | |
---|
450 | #Multiple |
---|
451 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
452 | ex_poly1 = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter |
---|
453 | ex_poly2 = [[0.5,0.5], [0.5,1], [1, 1], [1,0.5]] #NE quarter |
---|
454 | |
---|
455 | points = populate_polygon(polygon, 20, exclude = [ex_poly1, ex_poly2]) |
---|
456 | |
---|
457 | assert len(points) == 20 |
---|
458 | for point in points: |
---|
459 | assert is_inside_polygon(point, polygon) |
---|
460 | assert not is_inside_polygon(point, ex_poly1) |
---|
461 | assert not is_inside_polygon(point, ex_poly2) |
---|
462 | |
---|
463 | |
---|
464 | #Very convoluted polygon |
---|
465 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
466 | ex_poly = [[-1,-1], [5,0], [5, 5], [-1,5]] |
---|
467 | points = populate_polygon(polygon, 20, exclude = [ex_poly]) |
---|
468 | |
---|
469 | assert len(points) == 20 |
---|
470 | for point in points: |
---|
471 | assert is_inside_polygon(point, polygon) |
---|
472 | assert not is_inside_polygon(point, ex_poly), '%s' %str(point) |
---|
473 | |
---|
474 | |
---|
475 | def test_populate_polygon_with_exclude2(self): |
---|
476 | |
---|
477 | |
---|
478 | min_outer = 0 |
---|
479 | max_outer = 1000 |
---|
480 | polygon_outer = [[min_outer,min_outer],[max_outer,min_outer], |
---|
481 | [max_outer,max_outer],[min_outer,max_outer]] |
---|
482 | |
---|
483 | delta = 10 |
---|
484 | min_inner1 = min_outer + delta |
---|
485 | max_inner1 = max_outer - delta |
---|
486 | inner1_polygon = [[min_inner1,min_inner1],[max_inner1,min_inner1], |
---|
487 | [max_inner1,max_inner1],[min_inner1,max_inner1]] |
---|
488 | |
---|
489 | |
---|
490 | density_inner2 = 1000 |
---|
491 | min_inner2 = min_outer + 2*delta |
---|
492 | max_inner2 = max_outer - 2*delta |
---|
493 | inner2_polygon = [[min_inner2,min_inner2],[max_inner2,min_inner2], |
---|
494 | [max_inner2,max_inner2],[min_inner2,max_inner2]] |
---|
495 | |
---|
496 | points = populate_polygon(polygon_outer, 20, exclude = [inner1_polygon, inner2_polygon]) |
---|
497 | |
---|
498 | assert len(points) == 20 |
---|
499 | for point in points: |
---|
500 | assert is_inside_polygon(point, polygon_outer) |
---|
501 | assert not is_inside_polygon(point, inner1_polygon) |
---|
502 | assert not is_inside_polygon(point, inner2_polygon) |
---|
503 | |
---|
504 | |
---|
505 | #Very convoluted polygon |
---|
506 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
507 | ex_poly = [[-1,-1], [5,0], [5, 5], [-1,5]] |
---|
508 | points = populate_polygon(polygon, 20, exclude = [ex_poly]) |
---|
509 | |
---|
510 | assert len(points) == 20 |
---|
511 | for point in points: |
---|
512 | assert is_inside_polygon(point, polygon) |
---|
513 | assert not is_inside_polygon(point, ex_poly), '%s' %str(point) |
---|
514 | |
---|
515 | def test_point_in_polygon(self): |
---|
516 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
517 | point = point_in_polygon(polygon) |
---|
518 | assert is_inside_polygon(point, polygon) |
---|
519 | |
---|
520 | #this may get into a vicious loop |
---|
521 | #polygon = [[1e32,1e54], [1,0], [1,1], [0,1]] |
---|
522 | |
---|
523 | polygon = [[1e15,1e7], [1,0], [1,1], [0,1]] |
---|
524 | point = point_in_polygon(polygon) |
---|
525 | assert is_inside_polygon(point, polygon) |
---|
526 | |
---|
527 | |
---|
528 | polygon = [[0,0], [1,0], [1,1], [1e8,1e8]] |
---|
529 | point = point_in_polygon(polygon) |
---|
530 | assert is_inside_polygon(point, polygon) |
---|
531 | |
---|
532 | |
---|
533 | polygon = [[1e32,1e54], [-1e32,1e54], [1e32,-1e54]] |
---|
534 | point = point_in_polygon(polygon) |
---|
535 | assert is_inside_polygon(point, polygon) |
---|
536 | |
---|
537 | |
---|
538 | polygon = [[1e18,1e15], [1,0], [0,1]] |
---|
539 | point = point_in_polygon(polygon) |
---|
540 | assert is_inside_polygon(point, polygon) |
---|
541 | |
---|
542 | def test_in_and_outside_polygon_main(self): |
---|
543 | |
---|
544 | |
---|
545 | #Simplest case: Polygon is the unit square |
---|
546 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
547 | |
---|
548 | inside, outside = in_and_outside_polygon( (0.5, 0.5), polygon ) |
---|
549 | assert inside[0] == 0 |
---|
550 | assert len(outside) == 0 |
---|
551 | |
---|
552 | inside, outside = in_and_outside_polygon( (1., 0.5), polygon, closed=True) |
---|
553 | assert inside[0] == 0 |
---|
554 | assert len(outside) == 0 |
---|
555 | |
---|
556 | inside, outside = in_and_outside_polygon( (1., 0.5), polygon, closed=False) |
---|
557 | assert len(inside) == 0 |
---|
558 | assert outside[0] == 0 |
---|
559 | |
---|
560 | points = [(1., 0.25),(1., 0.75) ] |
---|
561 | inside, outside = in_and_outside_polygon( points, polygon, closed=True) |
---|
562 | assert (inside, [0,1]) |
---|
563 | assert len(outside) == 0 |
---|
564 | |
---|
565 | inside, outside = in_and_outside_polygon( points, polygon, closed=False) |
---|
566 | assert len(inside) == 0 |
---|
567 | assert (outside, [0,1]) |
---|
568 | |
---|
569 | |
---|
570 | points = [(100., 0.25),(0.5, 0.5) ] |
---|
571 | inside, outside = in_and_outside_polygon( points, polygon) |
---|
572 | assert (inside, [1]) |
---|
573 | assert outside[0] == 0 |
---|
574 | |
---|
575 | points = [(100., 0.25),(0.5, 0.5), (39,20), (0.6,0.7),(56,43),(67,90) ] |
---|
576 | inside, outside = in_and_outside_polygon( points, polygon) |
---|
577 | assert (inside, [1,3]) |
---|
578 | assert (outside, [0,2,4,5]) |
---|
579 | |
---|
580 | |
---|
581 | def test_intersection1(self): |
---|
582 | line0 = [[-1,0], [1,0]] |
---|
583 | line1 = [[0,-1], [0,1]] |
---|
584 | |
---|
585 | status, value = intersection(line0, line1) |
---|
586 | assert status == 1 |
---|
587 | assert allclose(value, [0.0, 0.0]) |
---|
588 | |
---|
589 | def test_intersection2(self): |
---|
590 | line0 = [[0,0], [24,12]] |
---|
591 | line1 = [[0,12], [24,0]] |
---|
592 | |
---|
593 | status, value = intersection(line0, line1) |
---|
594 | assert status == 1 |
---|
595 | assert allclose(value, [12.0, 6.0]) |
---|
596 | |
---|
597 | # Swap direction of one line |
---|
598 | line1 = [[24,0], [0,12]] |
---|
599 | |
---|
600 | status, value = intersection(line0, line1) |
---|
601 | assert status == 1 |
---|
602 | assert allclose(value, [12.0, 6.0]) |
---|
603 | |
---|
604 | # Swap order of lines |
---|
605 | status, value = intersection(line1, line0) |
---|
606 | assert status == 1 |
---|
607 | assert allclose(value, [12.0, 6.0]) |
---|
608 | |
---|
609 | def test_intersection3(self): |
---|
610 | line0 = [[0,0], [24,12]] |
---|
611 | line1 = [[0,17], [24,0]] |
---|
612 | |
---|
613 | status, value = intersection(line0, line1) |
---|
614 | assert status == 1 |
---|
615 | assert allclose(value, [14.068965517, 7.0344827586]) |
---|
616 | |
---|
617 | # Swap direction of one line |
---|
618 | line1 = [[24,0], [0,17]] |
---|
619 | |
---|
620 | status, value = intersection(line0, line1) |
---|
621 | assert status == 1 |
---|
622 | assert allclose(value, [14.068965517, 7.0344827586]) |
---|
623 | |
---|
624 | # Swap order of lines |
---|
625 | status, value = intersection(line1, line0) |
---|
626 | assert status == 1 |
---|
627 | assert allclose(value, [14.068965517, 7.0344827586]) |
---|
628 | |
---|
629 | |
---|
630 | def test_intersection_direction_invariance(self): |
---|
631 | """This runs through a number of examples and checks that direction of lines don't matter. |
---|
632 | """ |
---|
633 | |
---|
634 | line0 = [[0,0], [100,100]] |
---|
635 | |
---|
636 | common_end_point = [20, 150] |
---|
637 | |
---|
638 | for i in range(100): |
---|
639 | x = 20 + i * 1.0/100 |
---|
640 | |
---|
641 | line1 = [[x,0], common_end_point] |
---|
642 | status, p1 = intersection(line0, line1) |
---|
643 | assert status == 1 |
---|
644 | |
---|
645 | |
---|
646 | # Swap direction of line1 |
---|
647 | line1 = [common_end_point, [x,0]] |
---|
648 | status, p2 = intersection(line0, line1) |
---|
649 | assert status == 1 |
---|
650 | |
---|
651 | msg = 'Orientation of line shouldn not matter.\n' |
---|
652 | msg += 'However, segment [%f,%f], [%f, %f]' %(x, |
---|
653 | 0, |
---|
654 | common_end_point[0], |
---|
655 | common_end_point[1]) |
---|
656 | msg += ' gave %s, \nbut when reversed we got %s' %(p1, p2) |
---|
657 | assert allclose(p1, p2), msg |
---|
658 | |
---|
659 | # Swap order of lines |
---|
660 | status, p3 = intersection(line1, line0) |
---|
661 | assert status == 1 |
---|
662 | msg = 'Order of lines gave different results' |
---|
663 | assert allclose(p1, p3), msg |
---|
664 | |
---|
665 | |
---|
666 | def test_no_intersection(self): |
---|
667 | line0 = [[-1,1], [1,1]] |
---|
668 | line1 = [[0,-1], [0,0]] |
---|
669 | |
---|
670 | status, value = intersection(line0, line1) |
---|
671 | assert status == 0 |
---|
672 | assert value is None |
---|
673 | |
---|
674 | |
---|
675 | def test_intersection_parallel(self): |
---|
676 | line0 = [[-1,1], [1,1]] |
---|
677 | line1 = [[-1,0], [5,0]] |
---|
678 | |
---|
679 | status, value = intersection(line0, line1) |
---|
680 | assert status == 4 |
---|
681 | assert value is None |
---|
682 | |
---|
683 | |
---|
684 | line0 = [[0,0], [10,100]] |
---|
685 | line1 = [[-10,5], [0,105]] |
---|
686 | |
---|
687 | status, value = intersection(line0, line1) |
---|
688 | assert status == 4 |
---|
689 | assert value is None |
---|
690 | |
---|
691 | |
---|
692 | def test_intersection_coincide(self): |
---|
693 | """def test_intersection_coincide(self): |
---|
694 | Test what happens whe two lines partly coincide |
---|
695 | """ |
---|
696 | |
---|
697 | # Overlap 1 |
---|
698 | line0 = [[0,0], [5,0]] |
---|
699 | line1 = [[-3,0], [3,0]] |
---|
700 | |
---|
701 | status, value = intersection(line0, line1) |
---|
702 | assert status == 2 |
---|
703 | assert allclose(value, [[0,0], [3,0]]) |
---|
704 | |
---|
705 | # Overlap 2 |
---|
706 | line0 = [[-10,0], [5,0]] |
---|
707 | line1 = [[-3,0], [10,0]] |
---|
708 | |
---|
709 | status, value = intersection(line0, line1) |
---|
710 | assert status == 2 |
---|
711 | assert allclose(value, [[-3, 0], [5,0]]) |
---|
712 | |
---|
713 | # Inclusion 1 |
---|
714 | line0 = [[0,0], [5,0]] |
---|
715 | line1 = [[2,0], [3,0]] |
---|
716 | |
---|
717 | status, value = intersection(line0, line1) |
---|
718 | assert status == 2 |
---|
719 | assert allclose(value, line1) |
---|
720 | |
---|
721 | # Inclusion 2 |
---|
722 | line0 = [[1,0], [5,0]] |
---|
723 | line1 = [[-10,0], [15,0]] |
---|
724 | |
---|
725 | status, value = intersection(line0, line1) |
---|
726 | assert status == 2 |
---|
727 | assert allclose(value, line0) |
---|
728 | |
---|
729 | |
---|
730 | # Exclusion (no intersection) |
---|
731 | line0 = [[-10,0], [1,0]] |
---|
732 | line1 = [[3,0], [15,0]] |
---|
733 | |
---|
734 | status, value = intersection(line0, line1) |
---|
735 | assert status == 3 |
---|
736 | assert value is None |
---|
737 | |
---|
738 | |
---|
739 | # Try examples with some slope (y=2*x+5) |
---|
740 | |
---|
741 | # Overlap |
---|
742 | line0 = [[0,5], [7,19]] |
---|
743 | line1 = [[1,7], [10,25]] |
---|
744 | status, value = intersection(line0, line1) |
---|
745 | assert status == 2 |
---|
746 | assert allclose(value, [[1, 7], [7, 19]]) |
---|
747 | |
---|
748 | status, value = intersection(line1, line0) |
---|
749 | assert status == 2 |
---|
750 | assert allclose(value, [[1, 7], [7, 19]]) |
---|
751 | |
---|
752 | # Swap direction |
---|
753 | line0 = [[7,19], [0,5]] |
---|
754 | line1 = [[1,7], [10,25]] |
---|
755 | status, value = intersection(line0, line1) |
---|
756 | assert status == 2 |
---|
757 | assert allclose(value, [[7, 19], [1, 7]]) |
---|
758 | |
---|
759 | line0 = [[0,5], [7,19]] |
---|
760 | line1 = [[10,25], [1,7]] |
---|
761 | status, value = intersection(line0, line1) |
---|
762 | assert status == 2 |
---|
763 | assert allclose(value, [[1, 7], [7, 19]]) |
---|
764 | |
---|
765 | |
---|
766 | # Inclusion |
---|
767 | line0 = [[1,7], [7,19]] |
---|
768 | line1 = [[0,5], [10,25]] |
---|
769 | status, value = intersection(line0, line1) |
---|
770 | assert status == 2 |
---|
771 | assert allclose(value, [[1,7], [7, 19]]) |
---|
772 | |
---|
773 | line0 = [[0,5], [10,25]] |
---|
774 | line1 = [[1,7], [7,19]] |
---|
775 | status, value = intersection(line0, line1) |
---|
776 | assert status == 2 |
---|
777 | assert allclose(value, [[1,7], [7, 19]]) |
---|
778 | |
---|
779 | |
---|
780 | line0 = [[0,5], [10,25]] |
---|
781 | line1 = [[7,19], [1,7]] |
---|
782 | status, value = intersection(line0, line1) |
---|
783 | assert status == 2 |
---|
784 | assert allclose(value, [[7, 19], [1, 7]]) |
---|
785 | |
---|
786 | |
---|
787 | def zzztest_inside_polygon_main(self): \ |
---|
788 | |
---|
789 | #FIXME (Ole): Why is this disabled? |
---|
790 | print "inside",inside |
---|
791 | print "outside",outside |
---|
792 | |
---|
793 | assert not inside_polygon( (0.5, 1.5), polygon ) |
---|
794 | assert not inside_polygon( (0.5, -0.5), polygon ) |
---|
795 | assert not inside_polygon( (-0.5, 0.5), polygon ) |
---|
796 | assert not inside_polygon( (1.5, 0.5), polygon ) |
---|
797 | |
---|
798 | #Try point on borders |
---|
799 | assert inside_polygon( (1., 0.5), polygon, closed=True) |
---|
800 | assert inside_polygon( (0.5, 1), polygon, closed=True) |
---|
801 | assert inside_polygon( (0., 0.5), polygon, closed=True) |
---|
802 | assert inside_polygon( (0.5, 0.), polygon, closed=True) |
---|
803 | |
---|
804 | assert not inside_polygon( (0.5, 1), polygon, closed=False) |
---|
805 | assert not inside_polygon( (0., 0.5), polygon, closed=False) |
---|
806 | assert not inside_polygon( (0.5, 0.), polygon, closed=False) |
---|
807 | assert not inside_polygon( (1., 0.5), polygon, closed=False) |
---|
808 | |
---|
809 | |
---|
810 | |
---|
811 | #From real example (that failed) |
---|
812 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
813 | points = [ [40, 50] ] |
---|
814 | res = inside_polygon(points, polygon) |
---|
815 | assert len(res) == 0 |
---|
816 | |
---|
817 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
818 | points = [ [25, 25], [30, 20], [40, 50], [90, 20], [40, 90] ] |
---|
819 | res = inside_polygon(points, polygon) |
---|
820 | assert len(res) == 2 |
---|
821 | assert allclose(res, [0,1]) |
---|
822 | |
---|
823 | def test_polygon_area(self): |
---|
824 | |
---|
825 | #Simplest case: Polygon is the unit square |
---|
826 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
827 | assert polygon_area(polygon) == 1 |
---|
828 | |
---|
829 | #Simple case: Polygon is a rectangle |
---|
830 | polygon = [[0,0], [1,0], [1,4], [0,4]] |
---|
831 | assert polygon_area(polygon) == 4 |
---|
832 | |
---|
833 | #Simple case: Polygon is a unit triangle |
---|
834 | polygon = [[0,0], [1,0], [0,1]] |
---|
835 | assert polygon_area(polygon) == 0.5 |
---|
836 | |
---|
837 | #Simple case: Polygon is a diamond |
---|
838 | polygon = [[0,0], [1,1], [2,0], [1, -1]] |
---|
839 | assert polygon_area(polygon) == 2.0 |
---|
840 | |
---|
841 | def test_poly_xy(self): |
---|
842 | |
---|
843 | #Simplest case: Polygon is the unit square |
---|
844 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
845 | x, y = poly_xy(polygon) |
---|
846 | assert len(x) == len(polygon)+1 |
---|
847 | assert len(y) == len(polygon)+1 |
---|
848 | assert x[0] == 0 |
---|
849 | assert x[1] == 1 |
---|
850 | assert x[2] == 1 |
---|
851 | assert x[3] == 0 |
---|
852 | assert y[0] == 0 |
---|
853 | assert y[1] == 0 |
---|
854 | assert y[2] == 1 |
---|
855 | assert y[3] == 1 |
---|
856 | |
---|
857 | #Arbitrary polygon |
---|
858 | polygon = [[1,5], [1,1], [100,10], [1,10], [3,6]] |
---|
859 | x, y = poly_xy(polygon) |
---|
860 | assert len(x) == len(polygon)+1 |
---|
861 | assert len(y) == len(polygon)+1 |
---|
862 | assert x[0] == 1 |
---|
863 | assert x[1] == 1 |
---|
864 | assert x[2] == 100 |
---|
865 | assert x[3] == 1 |
---|
866 | assert x[4] == 3 |
---|
867 | assert y[0] == 5 |
---|
868 | assert y[1] == 1 |
---|
869 | assert y[2] == 10 |
---|
870 | assert y[3] == 10 |
---|
871 | assert y[4] == 6 |
---|
872 | |
---|
873 | # Disabled |
---|
874 | def xtest_plot_polygons(self): |
---|
875 | |
---|
876 | import os |
---|
877 | |
---|
878 | #Simplest case: Polygon is the unit square |
---|
879 | polygon1 = [[0,0], [1,0], [1,1], [0,1]] |
---|
880 | polygon2 = [[1,1], [2,1], [3,2], [2,2]] |
---|
881 | v = plot_polygons([polygon1, polygon2],'test1') |
---|
882 | assert len(v) == 4 |
---|
883 | assert v[0] == 0 |
---|
884 | assert v[1] == 3 |
---|
885 | assert v[2] == 0 |
---|
886 | assert v[3] == 2 |
---|
887 | |
---|
888 | #Another case |
---|
889 | polygon3 = [[1,5], [10,1], [100,10], [50,10], [3,6]] |
---|
890 | v = plot_polygons([polygon2,polygon3],'test2') |
---|
891 | assert len(v) == 4 |
---|
892 | assert v[0] == 1 |
---|
893 | assert v[1] == 100 |
---|
894 | assert v[2] == 1 |
---|
895 | assert v[3] == 10 |
---|
896 | |
---|
897 | os.remove('test1.png') |
---|
898 | os.remove('test2.png') |
---|
899 | |
---|
900 | |
---|
901 | def test_inside_polygon_geospatial(self): |
---|
902 | |
---|
903 | |
---|
904 | polygon_absolute = [[0,0], [1,0], [1,1], [0,1]] |
---|
905 | poly_geo_ref = Geo_reference(57,100,100) |
---|
906 | |
---|
907 | |
---|
908 | |
---|
909 | |
---|
910 | #Simplest case: Polygon is the unit square |
---|
911 | polygon_absolute = [[0,0], [1,0], [1,1], [0,1]] |
---|
912 | poly_geo_ref = Geo_reference(57,100,100) |
---|
913 | polygon = poly_geo_ref.change_points_geo_ref(polygon_absolute) |
---|
914 | poly_spatial = Geospatial_data(polygon, |
---|
915 | geo_reference=poly_geo_ref) |
---|
916 | |
---|
917 | points_absolute = (0.5, 0.5) |
---|
918 | points_geo_ref = Geo_reference(57,78,-56) |
---|
919 | points = points_geo_ref.change_points_geo_ref(points_absolute) |
---|
920 | points_spatial = Geospatial_data(points, |
---|
921 | geo_reference=points_geo_ref) |
---|
922 | |
---|
923 | assert is_inside_polygon(points_absolute, polygon_absolute) |
---|
924 | assert is_inside_polygon(ensure_numeric(points_absolute), |
---|
925 | ensure_numeric(polygon_absolute)) |
---|
926 | assert is_inside_polygon(points_absolute, poly_spatial) |
---|
927 | assert is_inside_polygon(points_spatial, poly_spatial) |
---|
928 | assert is_inside_polygon(points_spatial, polygon_absolute) |
---|
929 | |
---|
930 | assert is_inside_polygon(points_absolute, polygon_absolute) |
---|
931 | |
---|
932 | |
---|
933 | def NOtest_decimate_polygon(self): |
---|
934 | |
---|
935 | polygon = [[0,0], [10,10], [15,5], [20, 10], |
---|
936 | [25,0], [30,10], [40,-10], [35, -5]] |
---|
937 | |
---|
938 | #plot_polygons([polygon], figname='test') |
---|
939 | |
---|
940 | dpoly = decimate_polygon(polygon, factor=2) |
---|
941 | |
---|
942 | print dpoly |
---|
943 | |
---|
944 | assert len(dpoly)*2==len(polygon) |
---|
945 | |
---|
946 | minx = maxx = polygon[0][0] |
---|
947 | miny = maxy = polygon[0][1] |
---|
948 | for point in polygon[1:]: |
---|
949 | x, y = point |
---|
950 | |
---|
951 | if x < minx: minx = x |
---|
952 | if x > maxx: maxx = x |
---|
953 | if y < miny: miny = y |
---|
954 | if y > maxy: maxy = y |
---|
955 | |
---|
956 | |
---|
957 | assert [minx, miny] in polygon |
---|
958 | print minx, maxy |
---|
959 | assert [minx, maxy] in polygon |
---|
960 | assert [maxx, miny] in polygon |
---|
961 | assert [maxx, maxy] in polygon |
---|
962 | |
---|
963 | |
---|
964 | |
---|
965 | #------------------------------------------------------------- |
---|
966 | if __name__ == "__main__": |
---|
967 | suite = unittest.makeSuite(Test_Polygon,'test') |
---|
968 | #suite = unittest.makeSuite(Test_Polygon,'test_inside_polygon_geo_ref') |
---|
969 | runner = unittest.TextTestRunner() |
---|
970 | runner.run(suite) |
---|
971 | |
---|
972 | |
---|
973 | |
---|
974 | |
---|