source: inundation/utilities/test_polygon.py @ 3158

Last change on this file since 3158 was 3054, checked in by duncan, 19 years ago

removing geo-ref from inside_polygon

File size: 20.9 KB
RevLine 
[1910]1#!/usr/bin/env python
2
3
4import unittest
5from Numeric import zeros, array, allclose
6from math import sqrt, pi
[3054]7from utilities.numerical_tools import ensure_numeric
[1910]8
9from polygon import *
[3051]10from coordinate_transforms.geo_reference import Geo_reference
[3054]11from geospatial_data.geospatial_data import Geospatial_data
[1910]12
13def test_function(x, y):
14    return x+y
15
16class 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
[2375]60    def test_polygon_function_georef(self):
61        """Check that georeferencing works
62        """
63
64        from 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
[1910]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
[2375]127
[1910]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
[3052]154    def test_is_inside_polygon_main(self):
[1910]155
156
157        #Simplest case: Polygon is the unit square
158        polygon = [[0,0], [1,0], [1,1], [0,1]]
159
[3052]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 )
[1910]165
166        #Try point on borders
[3052]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)
[1910]171
[3052]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)
[1910]176
177
[3052]178    def test_inside_polygon_main(self):
[1910]179
[3052]180        #Simplest case: Polygon is the unit square
181        polygon = [[0,0], [1,0], [1,1], [0,1]]       
182
[1910]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]]
[3052]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 )
[1910]202
[3052]203        assert not is_inside_polygon( (0.5, 1.5), polygon )
204        assert not is_inside_polygon( (0.5, -0.5), polygon )
[1910]205
206
207        #Very convoluted polygon
208        polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
[3052]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 )
[1910]216
217
218
219        #Another combination (that failed)
220        polygon = [[0,0], [10,0], [10,10], [0,10]]
[3052]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 )
[1910]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]]
[3052]236        assert is_inside_polygon( (0.5, 0.5), polygon )
237        assert is_inside_polygon( (10.5, 10.5), polygon )
[1910]238
239        #FIXME: Fails if point is 5.5, 5.5
[3052]240        assert not is_inside_polygon( (0, 5.5), polygon )
[1910]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
[3052]246        assert is_inside_polygon( (0, -0.5), polygon )
247        assert not is_inside_polygon( (0.5, 0.5), polygon )
[1910]248
[2442]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
[3052]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 )
[2442]262
263        #Try point on borders
[3052]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)
[2442]268
[3052]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)
[2442]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
[1910]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
[3052]293        assert not is_outside_polygon( [0.5, 0.5], U )
[1910]294        #evaluate to False as the point 0.5, 0.5 is inside the unit square
295       
[3052]296        assert is_outside_polygon( [1.5, 0.5], U )
[1910]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       
[3052]323        assert is_outside_polygon( [0.5, 1.0], U, closed = False )
[1910]324        #evaluate to True as the point 0.5, 1.0 is outside the unit square
325
[2062]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
[1910]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:
[3052]398            assert is_inside_polygon(point, polygon)
[1910]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:
[3052]408            assert is_inside_polygon(point, polygon)
[1910]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:
[3052]420            assert is_inside_polygon(point, polygon)
421            assert not is_inside_polygon(point, ex_poly)           
[1910]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:
[3052]431            assert is_inside_polygon(point, polygon)
432            assert not is_inside_polygon(point, ex_poly)                       
[1910]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:
[3052]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)                               
[1910]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:
[3052]455            assert is_inside_polygon(point, polygon)
456            assert not is_inside_polygon(point, ex_poly), '%s' %str(point)                       
[1910]457
[2200]458    def test_point_in_polygon(self):
459        polygon = [[0,0], [1,0], [1,1], [0,1]]
460        point = point_in_polygon(polygon)
[3052]461        assert is_inside_polygon(point, polygon)
[1910]462
[2310]463        #this may get into a vicious loop
464        #polygon = [[1e32,1e54], [1,0], [1,1], [0,1]]
465       
[2311]466        polygon = [[1e15,1e7], [1,0], [1,1], [0,1]]
[2200]467        point = point_in_polygon(polygon)
[3052]468        assert is_inside_polygon(point, polygon)
[1910]469
[2200]470
471        polygon = [[0,0], [1,0], [1,1], [1e8,1e8]]
472        point = point_in_polygon(polygon)
[3052]473        assert is_inside_polygon(point, polygon)
[2311]474
[2200]475       
[2310]476        polygon = [[1e32,1e54], [-1e32,1e54], [1e32,-1e54]]
[2200]477        point = point_in_polygon(polygon)
[3052]478        assert is_inside_polygon(point, polygon)
[2311]479
[2200]480       
[2311]481        polygon = [[1e18,1e15], [1,0], [0,1]]
[2200]482        point = point_in_polygon(polygon)
[3052]483        assert is_inside_polygon(point, polygon)
[2200]484
[2655]485    def test_in_and_outside_polygon_main(self):
486
487
488        #Simplest case: Polygon is the unit square
489        polygon = [[0,0], [1,0], [1,1], [0,1]]
490
491        inside, outside =  in_and_outside_polygon( (0.5, 0.5), polygon )
492        assert inside[0] == 0
493        assert len(outside) == 0
494       
495        inside, outside =  in_and_outside_polygon(  (1., 0.5), polygon, closed=True)
496        assert inside[0] == 0
497        assert len(outside) == 0
498       
499        inside, outside =  in_and_outside_polygon(  (1., 0.5), polygon, closed=False)
500        assert len(inside) == 0
501        assert outside[0] == 0
502
503        points =  [(1., 0.25),(1., 0.75) ]
504        inside, outside =  in_and_outside_polygon( points, polygon, closed=True)
505        assert (inside, [0,1])
506        assert len(outside) == 0
507       
508        inside, outside =  in_and_outside_polygon( points, polygon, closed=False)
509        assert len(inside) == 0
510        assert (outside, [0,1])
511
512       
513        points =  [(100., 0.25),(0.5, 0.5) ] 
514        inside, outside =  in_and_outside_polygon( points, polygon)
515        assert (inside, [1])
516        assert outside[0] == 0
517       
518        points =  [(100., 0.25),(0.5, 0.5), (39,20), (0.6,0.7),(56,43),(67,90) ] 
519        inside, outside =  in_and_outside_polygon( points, polygon)
520        assert (inside, [1,3])
521        assert (outside, [0,2,4,5])
522       
523    def zzztest_inside_polygon_main(self): 
524        print "inside",inside
525        print "outside",outside
526       
527        assert not inside_polygon( (0.5, 1.5), polygon )
528        assert not inside_polygon( (0.5, -0.5), polygon )
529        assert not inside_polygon( (-0.5, 0.5), polygon )
530        assert not inside_polygon( (1.5, 0.5), polygon )
531
532        #Try point on borders
533        assert inside_polygon( (1., 0.5), polygon, closed=True)
534        assert inside_polygon( (0.5, 1), polygon, closed=True)
535        assert inside_polygon( (0., 0.5), polygon, closed=True)
536        assert inside_polygon( (0.5, 0.), polygon, closed=True)
537
538        assert not inside_polygon( (0.5, 1), polygon, closed=False)
539        assert not inside_polygon( (0., 0.5), polygon, closed=False)
540        assert not inside_polygon( (0.5, 0.), polygon, closed=False)
541        assert not inside_polygon( (1., 0.5), polygon, closed=False)
542
543
544
545        #From real example (that failed)
546        polygon = [[20,20], [40,20], [40,40], [20,40]]
547        points = [ [40, 50] ]
548        res = inside_polygon(points, polygon)
549        assert len(res) == 0
550
551        polygon = [[20,20], [40,20], [40,40], [20,40]]
552        points = [ [25, 25], [30, 20], [40, 50], [90, 20], [40, 90] ]
553        res = inside_polygon(points, polygon)
554        assert len(res) == 2
555        assert allclose(res, [0,1])
[2912]556
557    def test_polygon_area(self):
558
559        #Simplest case: Polygon is the unit square
560        polygon = [[0,0], [1,0], [1,1], [0,1]]
561        assert polygon_area(polygon) == 1
562
563        #Simple case: Polygon is a rectangle
564        polygon = [[0,0], [1,0], [1,4], [0,4]]
565        assert polygon_area(polygon) == 4
566
567        #Simple case: Polygon is a unit triangle
568        polygon = [[0,0], [1,0], [0,1]]
569        assert polygon_area(polygon) == 0.5
570
571        #Simple case: Polygon is a diamond
572        polygon = [[0,0], [1,1], [2,0], [1, -1]]
573        assert polygon_area(polygon) == 2.0
574
575    def test_poly_xy(self):
576 
577        #Simplest case: Polygon is the unit square
578        polygon = [[0,0], [1,0], [1,1], [0,1]]
579        x, y = poly_xy(polygon)
580        assert len(x) == len(polygon)+1
581        assert len(y) == len(polygon)+1
582        assert x[0] == 0
583        assert x[1] == 1
584        assert x[2] == 1
585        assert x[3] == 0
586        assert y[0] == 0
587        assert y[1] == 0
588        assert y[2] == 1
589        assert y[3] == 1
590
591        #Arbitrary polygon
592        polygon = [[1,5], [1,1], [100,10], [1,10], [3,6]]
593        x, y = poly_xy(polygon)
594        assert len(x) == len(polygon)+1
595        assert len(y) == len(polygon)+1
596        assert x[0] == 1
597        assert x[1] == 1
598        assert x[2] == 100
599        assert x[3] == 1
600        assert x[4] == 3
601        assert y[0] == 5
602        assert y[1] == 1
603        assert y[2] == 10
604        assert y[3] == 10
605        assert y[4] == 6
606
[3052]607    # Disabled   
608    def xtest_plot_polygons(self):
609       
[2912]610        import os
[2655]611       
[2912]612        #Simplest case: Polygon is the unit square
613        polygon1 = [[0,0], [1,0], [1,1], [0,1]]
614        polygon2 = [[1,1], [2,1], [3,2], [2,2]]
615        v = plot_polygons([polygon1, polygon2],'test1')
616        assert len(v) == 4
617        assert v[0] == 0
618        assert v[1] == 3
619        assert v[2] == 0
620        assert v[3] == 2
621
622        #Another case
623        polygon3 = [[1,5], [10,1], [100,10], [50,10], [3,6]]
624        v = plot_polygons([polygon2,polygon3],'test2')
625        assert len(v) == 4
626        assert v[0] == 1
627        assert v[1] == 100
628        assert v[2] == 1
629        assert v[3] == 10
630
631        os.remove('test1.png')
632        os.remove('test2.png')
[3051]633
[2912]634       
[3054]635    def test_inside_polygon_geospatial(self):
[3051]636
637
[3054]638        polygon_absolute = [[0,0], [1,0], [1,1], [0,1]]
639        poly_geo_ref = Geo_reference(57,100,100)
640       
641
642
643
[3051]644        #Simplest case: Polygon is the unit square
645        polygon_absolute = [[0,0], [1,0], [1,1], [0,1]]
646        poly_geo_ref = Geo_reference(57,100,100)
647        polygon = poly_geo_ref.change_points_geo_ref(polygon_absolute)
[3054]648        poly_spatial = Geospatial_data(polygon,
649                                       geo_reference=poly_geo_ref)
650       
[3051]651        points_absolute = (0.5, 0.5)
652        points_geo_ref = Geo_reference(57,78,-56)
653        points = points_geo_ref.change_points_geo_ref(points_absolute)
[3054]654        points_spatial = Geospatial_data(points,
655                                         geo_reference=points_geo_ref) 
656       
657        assert is_inside_polygon(points_absolute, polygon_absolute)
658        assert is_inside_polygon(ensure_numeric(points_absolute),
659                                 ensure_numeric(polygon_absolute))
660        assert is_inside_polygon(points_absolute, poly_spatial)
661        assert is_inside_polygon(points_spatial, poly_spatial)
662        assert is_inside_polygon(points_spatial, polygon_absolute)
[3051]663
[3052]664        assert is_inside_polygon(points_absolute, polygon_absolute)
[3051]665       
[3054]666       
[1910]667#-------------------------------------------------------------
668if __name__ == "__main__":
669    suite = unittest.makeSuite(Test_Polygon,'test')
[3051]670    #suite = unittest.makeSuite(Test_Polygon,'test_inside_polygon_geo_ref')
[1910]671    runner = unittest.TextTestRunner()
672    runner.run(suite)
673
674
675
676
Note: See TracBrowser for help on using the repository browser.