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 | |
---|
9 | from polygon import * |
---|
10 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
11 | from anuga.geospatial_data.geospatial_data import Geospatial_data |
---|
12 | |
---|
13 | def test_function(x, y): |
---|
14 | return x+y |
---|
15 | |
---|
16 | class Test_Polygon(unittest.TestCase): |
---|
17 | def setUp(self): |
---|
18 | pass |
---|
19 | |
---|
20 | def tearDown(self): |
---|
21 | pass |
---|
22 | |
---|
23 | |
---|
24 | def test_that_C_extension_compiles(self): |
---|
25 | FN = 'polygon_ext.c' |
---|
26 | try: |
---|
27 | import polygon_ext |
---|
28 | except: |
---|
29 | from compile import compile |
---|
30 | |
---|
31 | try: |
---|
32 | compile(FN) |
---|
33 | except: |
---|
34 | raise 'Could not compile %s' %FN |
---|
35 | else: |
---|
36 | import polygon_ext |
---|
37 | |
---|
38 | |
---|
39 | #Polygon stuff |
---|
40 | def test_polygon_function_constants(self): |
---|
41 | p1 = [[0,0], [10,0], [10,10], [0,10]] |
---|
42 | p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
43 | |
---|
44 | f = Polygon_function( [(p1, 1.0)] ) |
---|
45 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 |
---|
46 | assert allclose(z, [1,1,0,0]) |
---|
47 | |
---|
48 | |
---|
49 | f = Polygon_function( [(p2, 2.0)] ) |
---|
50 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2 |
---|
51 | assert allclose(z, [2,0,0,2]) |
---|
52 | |
---|
53 | |
---|
54 | #Combined |
---|
55 | f = Polygon_function( [(p1, 1.0), (p2, 2.0)] ) |
---|
56 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
57 | assert allclose(z, [2,1,0,2]) |
---|
58 | |
---|
59 | |
---|
60 | def test_polygon_function_georef(self): |
---|
61 | """Check that georeferencing works |
---|
62 | """ |
---|
63 | |
---|
64 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
65 | |
---|
66 | geo = Geo_reference(56, 200, 1000) |
---|
67 | |
---|
68 | #Make points 'absolute' |
---|
69 | p1 = [[200,1000], [210,1000], [210,1010], [200,1010]] |
---|
70 | p2 = [[200,1000], [210,1010], [215,1005], [220, 1010], [225,1000], |
---|
71 | [230,1010], [240,990]] |
---|
72 | |
---|
73 | f = Polygon_function( [(p1, 1.0)], geo_reference = geo ) |
---|
74 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 |
---|
75 | |
---|
76 | assert allclose(z, [1,1,0,0]) |
---|
77 | |
---|
78 | |
---|
79 | f = Polygon_function( [(p2, 2.0)], geo_reference = geo ) |
---|
80 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2 |
---|
81 | assert allclose(z, [2,0,0,2]) |
---|
82 | |
---|
83 | |
---|
84 | #Combined |
---|
85 | f = Polygon_function( [(p1, 1.0), (p2, 2.0)], geo_reference = geo ) |
---|
86 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
87 | assert allclose(z, [2,1,0,2]) |
---|
88 | |
---|
89 | |
---|
90 | #Check that it would fail without geo |
---|
91 | f = Polygon_function( [(p1, 1.0), (p2, 2.0)]) |
---|
92 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
93 | assert not allclose(z, [2,1,0,2]) |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | def test_polygon_function_callable(self): |
---|
98 | """Check that values passed into Polygon_function can be callable |
---|
99 | themselves. |
---|
100 | """ |
---|
101 | p1 = [[0,0], [10,0], [10,10], [0,10]] |
---|
102 | p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
103 | |
---|
104 | f = Polygon_function( [(p1, test_function)] ) |
---|
105 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 |
---|
106 | assert allclose(z, [10,14,0,0]) |
---|
107 | |
---|
108 | #Combined |
---|
109 | f = Polygon_function( [(p1, test_function), (p2, 2.0)] ) |
---|
110 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
111 | assert allclose(z, [2,14,0,2]) |
---|
112 | |
---|
113 | |
---|
114 | #Combined w default |
---|
115 | f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14) |
---|
116 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
117 | assert allclose(z, [2,14,3.14,2]) |
---|
118 | |
---|
119 | |
---|
120 | #Combined w default func |
---|
121 | f = Polygon_function( [(p1, test_function), (p2, 2.0)], |
---|
122 | default = test_function) |
---|
123 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
124 | assert allclose(z, [2,14,35,2]) |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | def test_point_on_line(self): |
---|
129 | |
---|
130 | #Endpoints first |
---|
131 | assert point_on_line( 0, 0, 0,0, 1,0 ) |
---|
132 | assert point_on_line( 1, 0, 0,0, 1,0 ) |
---|
133 | |
---|
134 | #Then points on line |
---|
135 | assert point_on_line( 0.5, 0, 0,0, 1,0 ) |
---|
136 | assert point_on_line( 0, 0.5, 0,1, 0,0 ) |
---|
137 | assert point_on_line( 1, 0.5, 1,1, 1,0 ) |
---|
138 | assert point_on_line( 0.5, 0.5, 0,0, 1,1 ) |
---|
139 | |
---|
140 | #Then points not on line |
---|
141 | assert not point_on_line( 0.5, 0, 0,1, 1,1 ) |
---|
142 | assert not point_on_line( 0, 0.5, 0,0, 1,1 ) |
---|
143 | |
---|
144 | #From real example that failed |
---|
145 | assert not point_on_line( 40,50, 40,20, 40,40 ) |
---|
146 | |
---|
147 | |
---|
148 | #From real example that failed |
---|
149 | assert not point_on_line( 40,19, 40,20, 40,40 ) |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | def test_is_inside_polygon_main(self): |
---|
155 | |
---|
156 | |
---|
157 | #Simplest case: Polygon is the unit square |
---|
158 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
159 | |
---|
160 | assert is_inside_polygon( (0.5, 0.5), polygon ) |
---|
161 | assert not is_inside_polygon( (0.5, 1.5), polygon ) |
---|
162 | assert not is_inside_polygon( (0.5, -0.5), polygon ) |
---|
163 | assert not is_inside_polygon( (-0.5, 0.5), polygon ) |
---|
164 | assert not is_inside_polygon( (1.5, 0.5), polygon ) |
---|
165 | |
---|
166 | #Try point on borders |
---|
167 | assert is_inside_polygon( (1., 0.5), polygon, closed=True) |
---|
168 | assert is_inside_polygon( (0.5, 1), polygon, closed=True) |
---|
169 | assert is_inside_polygon( (0., 0.5), polygon, closed=True) |
---|
170 | assert is_inside_polygon( (0.5, 0.), polygon, closed=True) |
---|
171 | |
---|
172 | assert not is_inside_polygon( (0.5, 1), polygon, closed=False) |
---|
173 | assert not is_inside_polygon( (0., 0.5), polygon, closed=False) |
---|
174 | assert not is_inside_polygon( (0.5, 0.), polygon, closed=False) |
---|
175 | assert not is_inside_polygon( (1., 0.5), polygon, closed=False) |
---|
176 | |
---|
177 | |
---|
178 | def test_inside_polygon_main(self): |
---|
179 | |
---|
180 | #Simplest case: Polygon is the unit square |
---|
181 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
182 | |
---|
183 | #From real example (that failed) |
---|
184 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
185 | points = [ [40, 50] ] |
---|
186 | res = inside_polygon(points, polygon) |
---|
187 | assert len(res) == 0 |
---|
188 | |
---|
189 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
190 | points = [ [25, 25], [30, 20], [40, 50], [90, 20], [40, 90] ] |
---|
191 | res = inside_polygon(points, polygon) |
---|
192 | assert len(res) == 2 |
---|
193 | assert allclose(res, [0,1]) |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | #More convoluted and non convex polygon |
---|
198 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
199 | assert is_inside_polygon( (0.5, 0.5), polygon ) |
---|
200 | assert is_inside_polygon( (1, -0.5), polygon ) |
---|
201 | assert is_inside_polygon( (1.5, 0), polygon ) |
---|
202 | |
---|
203 | assert not is_inside_polygon( (0.5, 1.5), polygon ) |
---|
204 | assert not is_inside_polygon( (0.5, -0.5), polygon ) |
---|
205 | |
---|
206 | |
---|
207 | #Very convoluted polygon |
---|
208 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
209 | assert is_inside_polygon( (5, 5), polygon ) |
---|
210 | assert is_inside_polygon( (17, 7), polygon ) |
---|
211 | assert is_inside_polygon( (27, 2), polygon ) |
---|
212 | assert is_inside_polygon( (35, -5), polygon ) |
---|
213 | assert not is_inside_polygon( (15, 7), polygon ) |
---|
214 | assert not is_inside_polygon( (24, 3), polygon ) |
---|
215 | assert not is_inside_polygon( (25, -10), polygon ) |
---|
216 | |
---|
217 | |
---|
218 | |
---|
219 | #Another combination (that failed) |
---|
220 | polygon = [[0,0], [10,0], [10,10], [0,10]] |
---|
221 | assert is_inside_polygon( (5, 5), polygon ) |
---|
222 | assert is_inside_polygon( (7, 7), polygon ) |
---|
223 | assert not is_inside_polygon( (-17, 7), polygon ) |
---|
224 | assert not is_inside_polygon( (7, 17), polygon ) |
---|
225 | assert not is_inside_polygon( (17, 7), polygon ) |
---|
226 | assert not is_inside_polygon( (27, 8), polygon ) |
---|
227 | assert not is_inside_polygon( (35, -5), polygon ) |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | #Multiple polygons |
---|
233 | |
---|
234 | polygon = [[0,0], [1,0], [1,1], [0,1], [0,0], |
---|
235 | [10,10], [11,10], [11,11], [10,11], [10,10]] |
---|
236 | assert is_inside_polygon( (0.5, 0.5), polygon ) |
---|
237 | assert is_inside_polygon( (10.5, 10.5), polygon ) |
---|
238 | |
---|
239 | #FIXME: Fails if point is 5.5, 5.5 |
---|
240 | assert not is_inside_polygon( (0, 5.5), polygon ) |
---|
241 | |
---|
242 | #Polygon with a hole |
---|
243 | polygon = [[-1,-1], [2,-1], [2,2], [-1,2], [-1,-1], |
---|
244 | [0,0], [1,0], [1,1], [0,1], [0,0]] |
---|
245 | |
---|
246 | assert is_inside_polygon( (0, -0.5), polygon ) |
---|
247 | assert not is_inside_polygon( (0.5, 0.5), polygon ) |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | def test_duplicate_points_being_ok(self): |
---|
252 | |
---|
253 | |
---|
254 | #Simplest case: Polygon is the unit square |
---|
255 | polygon = [[0,0], [1,0], [1,0], [1,0], [1,1], [0,1], [0,0]] |
---|
256 | |
---|
257 | assert is_inside_polygon( (0.5, 0.5), polygon ) |
---|
258 | assert not is_inside_polygon( (0.5, 1.5), polygon ) |
---|
259 | assert not is_inside_polygon( (0.5, -0.5), polygon ) |
---|
260 | assert not is_inside_polygon( (-0.5, 0.5), polygon ) |
---|
261 | assert not is_inside_polygon( (1.5, 0.5), polygon ) |
---|
262 | |
---|
263 | #Try point on borders |
---|
264 | assert is_inside_polygon( (1., 0.5), polygon, closed=True) |
---|
265 | assert is_inside_polygon( (0.5, 1), polygon, closed=True) |
---|
266 | assert is_inside_polygon( (0., 0.5), polygon, closed=True) |
---|
267 | assert is_inside_polygon( (0.5, 0.), polygon, closed=True) |
---|
268 | |
---|
269 | assert not is_inside_polygon( (0.5, 1), polygon, closed=False) |
---|
270 | assert not is_inside_polygon( (0., 0.5), polygon, closed=False) |
---|
271 | assert not is_inside_polygon( (0.5, 0.), polygon, closed=False) |
---|
272 | assert not is_inside_polygon( (1., 0.5), polygon, closed=False) |
---|
273 | |
---|
274 | #From real example |
---|
275 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
276 | points = [ [40, 50] ] |
---|
277 | res = inside_polygon(points, polygon) |
---|
278 | assert len(res) == 0 |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | def test_inside_polygon_vector_version(self): |
---|
283 | #Now try the vector formulation returning indices |
---|
284 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
285 | points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
286 | res = inside_polygon( points, polygon, verbose=False ) |
---|
287 | |
---|
288 | assert allclose( res, [0,1,2] ) |
---|
289 | |
---|
290 | def test_outside_polygon(self): |
---|
291 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
292 | |
---|
293 | assert not is_outside_polygon( [0.5, 0.5], U ) |
---|
294 | #evaluate to False as the point 0.5, 0.5 is inside the unit square |
---|
295 | |
---|
296 | assert is_outside_polygon( [1.5, 0.5], U ) |
---|
297 | #evaluate to True as the point 1.5, 0.5 is outside the unit square |
---|
298 | |
---|
299 | indices = outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) |
---|
300 | assert allclose( indices, [1] ) |
---|
301 | |
---|
302 | #One more test of vector formulation returning indices |
---|
303 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
304 | points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
305 | res = outside_polygon( points, polygon ) |
---|
306 | |
---|
307 | assert allclose( res, [3, 4] ) |
---|
308 | |
---|
309 | |
---|
310 | |
---|
311 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
312 | points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
313 | res = outside_polygon( points, polygon ) |
---|
314 | |
---|
315 | assert allclose( res, [0, 4, 5] ) |
---|
316 | |
---|
317 | def test_outside_polygon2(self): |
---|
318 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
319 | |
---|
320 | assert not outside_polygon( [0.5, 1.0], U, closed = True ) |
---|
321 | #evaluate to False as the point 0.5, 1.0 is inside the unit square |
---|
322 | |
---|
323 | assert is_outside_polygon( [0.5, 1.0], U, closed = False ) |
---|
324 | #evaluate to True as the point 0.5, 1.0 is outside the unit square |
---|
325 | |
---|
326 | def test_all_outside_polygon(self): |
---|
327 | """Test case where all points are outside poly |
---|
328 | """ |
---|
329 | |
---|
330 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
331 | |
---|
332 | points = [[2,2], [1,3], [-1,1], [0,2]] #All outside |
---|
333 | |
---|
334 | |
---|
335 | indices, count = separate_points_by_polygon(points, U) |
---|
336 | #print indices, count |
---|
337 | assert count == 0 #None inside |
---|
338 | assert allclose(indices, [3,2,1,0]) |
---|
339 | |
---|
340 | indices = outside_polygon(points, U, closed = True) |
---|
341 | assert allclose(indices, [0,1,2,3]) |
---|
342 | |
---|
343 | indices = inside_polygon(points, U, closed = True) |
---|
344 | assert allclose(indices, []) |
---|
345 | |
---|
346 | |
---|
347 | def test_all_inside_polygon(self): |
---|
348 | """Test case where all points are inside poly |
---|
349 | """ |
---|
350 | |
---|
351 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
352 | |
---|
353 | points = [[0.5,0.5], [0.2,0.3], [0,0.5]] #All inside (or on edge) |
---|
354 | |
---|
355 | |
---|
356 | indices, count = separate_points_by_polygon(points, U) |
---|
357 | assert count == 3 #All inside |
---|
358 | assert allclose(indices, [0,1,2]) |
---|
359 | |
---|
360 | indices = outside_polygon(points, U, closed = True) |
---|
361 | assert allclose(indices, []) |
---|
362 | |
---|
363 | indices = inside_polygon(points, U, closed = True) |
---|
364 | assert allclose(indices, [0,1,2]) |
---|
365 | |
---|
366 | |
---|
367 | def test_separate_points_by_polygon(self): |
---|
368 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
369 | |
---|
370 | indices, count = separate_points_by_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) |
---|
371 | assert allclose( indices, [0,2,1] ) |
---|
372 | assert count == 2 |
---|
373 | |
---|
374 | #One more test of vector formulation returning indices |
---|
375 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
376 | points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
377 | res, count = separate_points_by_polygon( points, polygon ) |
---|
378 | |
---|
379 | assert allclose( res, [0,1,2,4,3] ) |
---|
380 | assert count == 3 |
---|
381 | |
---|
382 | |
---|
383 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
384 | points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
385 | res, count = separate_points_by_polygon( points, polygon ) |
---|
386 | |
---|
387 | assert allclose( res, [1,2,3,5,4,0] ) |
---|
388 | assert count == 3 |
---|
389 | |
---|
390 | |
---|
391 | def test_populate_polygon(self): |
---|
392 | |
---|
393 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
394 | points = populate_polygon(polygon, 5) |
---|
395 | |
---|
396 | assert len(points) == 5 |
---|
397 | for point in points: |
---|
398 | assert is_inside_polygon(point, polygon) |
---|
399 | |
---|
400 | |
---|
401 | #Very convoluted polygon |
---|
402 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
403 | |
---|
404 | points = populate_polygon(polygon, 5) |
---|
405 | |
---|
406 | assert len(points) == 5 |
---|
407 | for point in points: |
---|
408 | assert is_inside_polygon(point, polygon) |
---|
409 | |
---|
410 | |
---|
411 | def test_populate_polygon_with_exclude(self): |
---|
412 | |
---|
413 | |
---|
414 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
415 | ex_poly = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter |
---|
416 | points = populate_polygon(polygon, 5, exclude = [ex_poly]) |
---|
417 | |
---|
418 | assert len(points) == 5 |
---|
419 | for point in points: |
---|
420 | assert is_inside_polygon(point, polygon) |
---|
421 | assert not is_inside_polygon(point, ex_poly) |
---|
422 | |
---|
423 | |
---|
424 | #overlap |
---|
425 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
426 | ex_poly = [[-1,-1], [0.5,0], [0.5, 0.5], [-1,0.5]] |
---|
427 | points = populate_polygon(polygon, 5, exclude = [ex_poly]) |
---|
428 | |
---|
429 | assert len(points) == 5 |
---|
430 | for point in points: |
---|
431 | assert is_inside_polygon(point, polygon) |
---|
432 | assert not is_inside_polygon(point, ex_poly) |
---|
433 | |
---|
434 | #Multiple |
---|
435 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
436 | ex_poly1 = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter |
---|
437 | ex_poly2 = [[0.5,0.5], [0.5,1], [1, 1], [1,0.5]] #NE quarter |
---|
438 | |
---|
439 | points = populate_polygon(polygon, 20, exclude = [ex_poly1, ex_poly2]) |
---|
440 | |
---|
441 | assert len(points) == 20 |
---|
442 | for point in points: |
---|
443 | assert is_inside_polygon(point, polygon) |
---|
444 | assert not is_inside_polygon(point, ex_poly1) |
---|
445 | assert not is_inside_polygon(point, ex_poly2) |
---|
446 | |
---|
447 | |
---|
448 | #Very convoluted polygon |
---|
449 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
450 | ex_poly = [[-1,-1], [5,0], [5, 5], [-1,5]] |
---|
451 | points = populate_polygon(polygon, 20, exclude = [ex_poly]) |
---|
452 | |
---|
453 | assert len(points) == 20 |
---|
454 | for point in points: |
---|
455 | assert is_inside_polygon(point, polygon) |
---|
456 | assert not is_inside_polygon(point, ex_poly), '%s' %str(point) |
---|
457 | |
---|
458 | |
---|
459 | def test_populate_polygon_with_exclude2(self): |
---|
460 | |
---|
461 | |
---|
462 | min_outer = 0 |
---|
463 | max_outer = 1000 |
---|
464 | polygon_outer = [[min_outer,min_outer],[max_outer,min_outer], |
---|
465 | [max_outer,max_outer],[min_outer,max_outer]] |
---|
466 | |
---|
467 | delta = 10 |
---|
468 | min_inner1 = min_outer + delta |
---|
469 | max_inner1 = max_outer - delta |
---|
470 | inner1_polygon = [[min_inner1,min_inner1],[max_inner1,min_inner1], |
---|
471 | [max_inner1,max_inner1],[min_inner1,max_inner1]] |
---|
472 | |
---|
473 | |
---|
474 | density_inner2 = 1000 |
---|
475 | min_inner2 = min_outer + 2*delta |
---|
476 | max_inner2 = max_outer - 2*delta |
---|
477 | inner2_polygon = [[min_inner2,min_inner2],[max_inner2,min_inner2], |
---|
478 | [max_inner2,max_inner2],[min_inner2,max_inner2]] |
---|
479 | |
---|
480 | points = populate_polygon(polygon_outer, 20, exclude = [inner1_polygon, inner2_polygon]) |
---|
481 | |
---|
482 | assert len(points) == 20 |
---|
483 | for point in points: |
---|
484 | assert is_inside_polygon(point, polygon_outer) |
---|
485 | assert not is_inside_polygon(point, inner1_polygon) |
---|
486 | assert not is_inside_polygon(point, inner2_polygon) |
---|
487 | |
---|
488 | |
---|
489 | #Very convoluted polygon |
---|
490 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
491 | ex_poly = [[-1,-1], [5,0], [5, 5], [-1,5]] |
---|
492 | points = populate_polygon(polygon, 20, exclude = [ex_poly]) |
---|
493 | |
---|
494 | assert len(points) == 20 |
---|
495 | for point in points: |
---|
496 | assert is_inside_polygon(point, polygon) |
---|
497 | assert not is_inside_polygon(point, ex_poly), '%s' %str(point) |
---|
498 | |
---|
499 | def test_point_in_polygon(self): |
---|
500 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
501 | point = point_in_polygon(polygon) |
---|
502 | assert is_inside_polygon(point, polygon) |
---|
503 | |
---|
504 | #this may get into a vicious loop |
---|
505 | #polygon = [[1e32,1e54], [1,0], [1,1], [0,1]] |
---|
506 | |
---|
507 | polygon = [[1e15,1e7], [1,0], [1,1], [0,1]] |
---|
508 | point = point_in_polygon(polygon) |
---|
509 | assert is_inside_polygon(point, polygon) |
---|
510 | |
---|
511 | |
---|
512 | polygon = [[0,0], [1,0], [1,1], [1e8,1e8]] |
---|
513 | point = point_in_polygon(polygon) |
---|
514 | assert is_inside_polygon(point, polygon) |
---|
515 | |
---|
516 | |
---|
517 | polygon = [[1e32,1e54], [-1e32,1e54], [1e32,-1e54]] |
---|
518 | point = point_in_polygon(polygon) |
---|
519 | assert is_inside_polygon(point, polygon) |
---|
520 | |
---|
521 | |
---|
522 | polygon = [[1e18,1e15], [1,0], [0,1]] |
---|
523 | point = point_in_polygon(polygon) |
---|
524 | assert is_inside_polygon(point, polygon) |
---|
525 | |
---|
526 | def test_in_and_outside_polygon_main(self): |
---|
527 | |
---|
528 | |
---|
529 | #Simplest case: Polygon is the unit square |
---|
530 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
531 | |
---|
532 | inside, outside = in_and_outside_polygon( (0.5, 0.5), polygon ) |
---|
533 | assert inside[0] == 0 |
---|
534 | assert len(outside) == 0 |
---|
535 | |
---|
536 | inside, outside = in_and_outside_polygon( (1., 0.5), polygon, closed=True) |
---|
537 | assert inside[0] == 0 |
---|
538 | assert len(outside) == 0 |
---|
539 | |
---|
540 | inside, outside = in_and_outside_polygon( (1., 0.5), polygon, closed=False) |
---|
541 | assert len(inside) == 0 |
---|
542 | assert outside[0] == 0 |
---|
543 | |
---|
544 | points = [(1., 0.25),(1., 0.75) ] |
---|
545 | inside, outside = in_and_outside_polygon( points, polygon, closed=True) |
---|
546 | assert (inside, [0,1]) |
---|
547 | assert len(outside) == 0 |
---|
548 | |
---|
549 | inside, outside = in_and_outside_polygon( points, polygon, closed=False) |
---|
550 | assert len(inside) == 0 |
---|
551 | assert (outside, [0,1]) |
---|
552 | |
---|
553 | |
---|
554 | points = [(100., 0.25),(0.5, 0.5) ] |
---|
555 | inside, outside = in_and_outside_polygon( points, polygon) |
---|
556 | assert (inside, [1]) |
---|
557 | assert outside[0] == 0 |
---|
558 | |
---|
559 | points = [(100., 0.25),(0.5, 0.5), (39,20), (0.6,0.7),(56,43),(67,90) ] |
---|
560 | inside, outside = in_and_outside_polygon( points, polygon) |
---|
561 | assert (inside, [1,3]) |
---|
562 | assert (outside, [0,2,4,5]) |
---|
563 | |
---|
564 | def zzztest_inside_polygon_main(self): |
---|
565 | print "inside",inside |
---|
566 | print "outside",outside |
---|
567 | |
---|
568 | assert not inside_polygon( (0.5, 1.5), polygon ) |
---|
569 | assert not inside_polygon( (0.5, -0.5), polygon ) |
---|
570 | assert not inside_polygon( (-0.5, 0.5), polygon ) |
---|
571 | assert not inside_polygon( (1.5, 0.5), polygon ) |
---|
572 | |
---|
573 | #Try point on borders |
---|
574 | assert inside_polygon( (1., 0.5), polygon, closed=True) |
---|
575 | assert inside_polygon( (0.5, 1), polygon, closed=True) |
---|
576 | assert inside_polygon( (0., 0.5), polygon, closed=True) |
---|
577 | assert inside_polygon( (0.5, 0.), polygon, closed=True) |
---|
578 | |
---|
579 | assert not inside_polygon( (0.5, 1), polygon, closed=False) |
---|
580 | assert not inside_polygon( (0., 0.5), polygon, closed=False) |
---|
581 | assert not inside_polygon( (0.5, 0.), polygon, closed=False) |
---|
582 | assert not inside_polygon( (1., 0.5), polygon, closed=False) |
---|
583 | |
---|
584 | |
---|
585 | |
---|
586 | #From real example (that failed) |
---|
587 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
588 | points = [ [40, 50] ] |
---|
589 | res = inside_polygon(points, polygon) |
---|
590 | assert len(res) == 0 |
---|
591 | |
---|
592 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
593 | points = [ [25, 25], [30, 20], [40, 50], [90, 20], [40, 90] ] |
---|
594 | res = inside_polygon(points, polygon) |
---|
595 | assert len(res) == 2 |
---|
596 | assert allclose(res, [0,1]) |
---|
597 | |
---|
598 | def test_polygon_area(self): |
---|
599 | |
---|
600 | #Simplest case: Polygon is the unit square |
---|
601 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
602 | assert polygon_area(polygon) == 1 |
---|
603 | |
---|
604 | #Simple case: Polygon is a rectangle |
---|
605 | polygon = [[0,0], [1,0], [1,4], [0,4]] |
---|
606 | assert polygon_area(polygon) == 4 |
---|
607 | |
---|
608 | #Simple case: Polygon is a unit triangle |
---|
609 | polygon = [[0,0], [1,0], [0,1]] |
---|
610 | assert polygon_area(polygon) == 0.5 |
---|
611 | |
---|
612 | #Simple case: Polygon is a diamond |
---|
613 | polygon = [[0,0], [1,1], [2,0], [1, -1]] |
---|
614 | assert polygon_area(polygon) == 2.0 |
---|
615 | |
---|
616 | def test_poly_xy(self): |
---|
617 | |
---|
618 | #Simplest case: Polygon is the unit square |
---|
619 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
620 | x, y = poly_xy(polygon) |
---|
621 | assert len(x) == len(polygon)+1 |
---|
622 | assert len(y) == len(polygon)+1 |
---|
623 | assert x[0] == 0 |
---|
624 | assert x[1] == 1 |
---|
625 | assert x[2] == 1 |
---|
626 | assert x[3] == 0 |
---|
627 | assert y[0] == 0 |
---|
628 | assert y[1] == 0 |
---|
629 | assert y[2] == 1 |
---|
630 | assert y[3] == 1 |
---|
631 | |
---|
632 | #Arbitrary polygon |
---|
633 | polygon = [[1,5], [1,1], [100,10], [1,10], [3,6]] |
---|
634 | x, y = poly_xy(polygon) |
---|
635 | assert len(x) == len(polygon)+1 |
---|
636 | assert len(y) == len(polygon)+1 |
---|
637 | assert x[0] == 1 |
---|
638 | assert x[1] == 1 |
---|
639 | assert x[2] == 100 |
---|
640 | assert x[3] == 1 |
---|
641 | assert x[4] == 3 |
---|
642 | assert y[0] == 5 |
---|
643 | assert y[1] == 1 |
---|
644 | assert y[2] == 10 |
---|
645 | assert y[3] == 10 |
---|
646 | assert y[4] == 6 |
---|
647 | |
---|
648 | # Disabled |
---|
649 | def xtest_plot_polygons(self): |
---|
650 | |
---|
651 | import os |
---|
652 | |
---|
653 | #Simplest case: Polygon is the unit square |
---|
654 | polygon1 = [[0,0], [1,0], [1,1], [0,1]] |
---|
655 | polygon2 = [[1,1], [2,1], [3,2], [2,2]] |
---|
656 | v = plot_polygons([polygon1, polygon2],'test1') |
---|
657 | assert len(v) == 4 |
---|
658 | assert v[0] == 0 |
---|
659 | assert v[1] == 3 |
---|
660 | assert v[2] == 0 |
---|
661 | assert v[3] == 2 |
---|
662 | |
---|
663 | #Another case |
---|
664 | polygon3 = [[1,5], [10,1], [100,10], [50,10], [3,6]] |
---|
665 | v = plot_polygons([polygon2,polygon3],'test2') |
---|
666 | assert len(v) == 4 |
---|
667 | assert v[0] == 1 |
---|
668 | assert v[1] == 100 |
---|
669 | assert v[2] == 1 |
---|
670 | assert v[3] == 10 |
---|
671 | |
---|
672 | os.remove('test1.png') |
---|
673 | os.remove('test2.png') |
---|
674 | |
---|
675 | |
---|
676 | def test_inside_polygon_geospatial(self): |
---|
677 | |
---|
678 | |
---|
679 | polygon_absolute = [[0,0], [1,0], [1,1], [0,1]] |
---|
680 | poly_geo_ref = Geo_reference(57,100,100) |
---|
681 | |
---|
682 | |
---|
683 | |
---|
684 | |
---|
685 | #Simplest case: Polygon is the unit square |
---|
686 | polygon_absolute = [[0,0], [1,0], [1,1], [0,1]] |
---|
687 | poly_geo_ref = Geo_reference(57,100,100) |
---|
688 | polygon = poly_geo_ref.change_points_geo_ref(polygon_absolute) |
---|
689 | poly_spatial = Geospatial_data(polygon, |
---|
690 | geo_reference=poly_geo_ref) |
---|
691 | |
---|
692 | points_absolute = (0.5, 0.5) |
---|
693 | points_geo_ref = Geo_reference(57,78,-56) |
---|
694 | points = points_geo_ref.change_points_geo_ref(points_absolute) |
---|
695 | points_spatial = Geospatial_data(points, |
---|
696 | geo_reference=points_geo_ref) |
---|
697 | |
---|
698 | assert is_inside_polygon(points_absolute, polygon_absolute) |
---|
699 | assert is_inside_polygon(ensure_numeric(points_absolute), |
---|
700 | ensure_numeric(polygon_absolute)) |
---|
701 | assert is_inside_polygon(points_absolute, poly_spatial) |
---|
702 | assert is_inside_polygon(points_spatial, poly_spatial) |
---|
703 | assert is_inside_polygon(points_spatial, polygon_absolute) |
---|
704 | |
---|
705 | assert is_inside_polygon(points_absolute, polygon_absolute) |
---|
706 | |
---|
707 | |
---|
708 | #------------------------------------------------------------- |
---|
709 | if __name__ == "__main__": |
---|
710 | suite = unittest.makeSuite(Test_Polygon,'test') |
---|
711 | #suite = unittest.makeSuite(Test_Polygon,'test_inside_polygon_geo_ref') |
---|
712 | runner = unittest.TextTestRunner() |
---|
713 | runner.run(suite) |
---|
714 | |
---|
715 | |
---|
716 | |
---|
717 | |
---|