source: anuga_core/source/anuga/utilities/test_polygon.py @ 5119

Last change on this file since 5119 was 5119, checked in by ole, 16 years ago

Fiddling

File size: 23.8 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5from Numeric import zeros, array, allclose
6from math import sqrt, pi
7from anuga.utilities.numerical_tools import ensure_numeric
8from anuga.utilities.system_tools import get_pathname_from_package
9
10from polygon import *
11from anuga.coordinate_transforms.geo_reference import Geo_reference
12from anuga.geospatial_data.geospatial_data import Geospatial_data
13
14def test_function(x, y):
15    return x+y
16
17class 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    def zzztest_inside_polygon_main(self): 
581        print "inside",inside
582        print "outside",outside
583       
584        assert not inside_polygon( (0.5, 1.5), polygon )
585        assert not inside_polygon( (0.5, -0.5), polygon )
586        assert not inside_polygon( (-0.5, 0.5), polygon )
587        assert not inside_polygon( (1.5, 0.5), polygon )
588
589        #Try point on borders
590        assert inside_polygon( (1., 0.5), polygon, closed=True)
591        assert inside_polygon( (0.5, 1), polygon, closed=True)
592        assert inside_polygon( (0., 0.5), polygon, closed=True)
593        assert inside_polygon( (0.5, 0.), polygon, closed=True)
594
595        assert not inside_polygon( (0.5, 1), polygon, closed=False)
596        assert not inside_polygon( (0., 0.5), polygon, closed=False)
597        assert not inside_polygon( (0.5, 0.), polygon, closed=False)
598        assert not inside_polygon( (1., 0.5), polygon, closed=False)
599
600
601
602        #From real example (that failed)
603        polygon = [[20,20], [40,20], [40,40], [20,40]]
604        points = [ [40, 50] ]
605        res = inside_polygon(points, polygon)
606        assert len(res) == 0
607
608        polygon = [[20,20], [40,20], [40,40], [20,40]]
609        points = [ [25, 25], [30, 20], [40, 50], [90, 20], [40, 90] ]
610        res = inside_polygon(points, polygon)
611        assert len(res) == 2
612        assert allclose(res, [0,1])
613
614    def test_polygon_area(self):
615
616        #Simplest case: Polygon is the unit square
617        polygon = [[0,0], [1,0], [1,1], [0,1]]
618        assert polygon_area(polygon) == 1
619
620        #Simple case: Polygon is a rectangle
621        polygon = [[0,0], [1,0], [1,4], [0,4]]
622        assert polygon_area(polygon) == 4
623
624        #Simple case: Polygon is a unit triangle
625        polygon = [[0,0], [1,0], [0,1]]
626        assert polygon_area(polygon) == 0.5
627
628        #Simple case: Polygon is a diamond
629        polygon = [[0,0], [1,1], [2,0], [1, -1]]
630        assert polygon_area(polygon) == 2.0
631
632    def test_poly_xy(self):
633 
634        #Simplest case: Polygon is the unit square
635        polygon = [[0,0], [1,0], [1,1], [0,1]]
636        x, y = poly_xy(polygon)
637        assert len(x) == len(polygon)+1
638        assert len(y) == len(polygon)+1
639        assert x[0] == 0
640        assert x[1] == 1
641        assert x[2] == 1
642        assert x[3] == 0
643        assert y[0] == 0
644        assert y[1] == 0
645        assert y[2] == 1
646        assert y[3] == 1
647
648        #Arbitrary polygon
649        polygon = [[1,5], [1,1], [100,10], [1,10], [3,6]]
650        x, y = poly_xy(polygon)
651        assert len(x) == len(polygon)+1
652        assert len(y) == len(polygon)+1
653        assert x[0] == 1
654        assert x[1] == 1
655        assert x[2] == 100
656        assert x[3] == 1
657        assert x[4] == 3
658        assert y[0] == 5
659        assert y[1] == 1
660        assert y[2] == 10
661        assert y[3] == 10
662        assert y[4] == 6
663
664    # Disabled   
665    def xtest_plot_polygons(self):
666       
667        import os
668       
669        #Simplest case: Polygon is the unit square
670        polygon1 = [[0,0], [1,0], [1,1], [0,1]]
671        polygon2 = [[1,1], [2,1], [3,2], [2,2]]
672        v = plot_polygons([polygon1, polygon2],'test1')
673        assert len(v) == 4
674        assert v[0] == 0
675        assert v[1] == 3
676        assert v[2] == 0
677        assert v[3] == 2
678
679        #Another case
680        polygon3 = [[1,5], [10,1], [100,10], [50,10], [3,6]]
681        v = plot_polygons([polygon2,polygon3],'test2')
682        assert len(v) == 4
683        assert v[0] == 1
684        assert v[1] == 100
685        assert v[2] == 1
686        assert v[3] == 10
687
688        os.remove('test1.png')
689        os.remove('test2.png')
690
691       
692    def test_inside_polygon_geospatial(self):
693
694
695        polygon_absolute = [[0,0], [1,0], [1,1], [0,1]]
696        poly_geo_ref = Geo_reference(57,100,100)
697       
698
699
700
701        #Simplest case: Polygon is the unit square
702        polygon_absolute = [[0,0], [1,0], [1,1], [0,1]]
703        poly_geo_ref = Geo_reference(57,100,100)
704        polygon = poly_geo_ref.change_points_geo_ref(polygon_absolute)
705        poly_spatial = Geospatial_data(polygon,
706                                       geo_reference=poly_geo_ref)
707       
708        points_absolute = (0.5, 0.5)
709        points_geo_ref = Geo_reference(57,78,-56)
710        points = points_geo_ref.change_points_geo_ref(points_absolute)
711        points_spatial = Geospatial_data(points,
712                                         geo_reference=points_geo_ref) 
713       
714        assert is_inside_polygon(points_absolute, polygon_absolute)
715        assert is_inside_polygon(ensure_numeric(points_absolute),
716                                 ensure_numeric(polygon_absolute))
717        assert is_inside_polygon(points_absolute, poly_spatial)
718        assert is_inside_polygon(points_spatial, poly_spatial)
719        assert is_inside_polygon(points_spatial, polygon_absolute)
720
721        assert is_inside_polygon(points_absolute, polygon_absolute)
722
723
724    def NOtest_decimate_polygon(self):
725
726        polygon = [[0,0], [10,10], [15,5], [20, 10],
727                   [25,0], [30,10], [40,-10], [35, -5]]
728
729        #plot_polygons([polygon], figname='test')
730       
731        dpoly = decimate_polygon(polygon, factor=2)
732
733        print dpoly
734       
735        assert len(dpoly)*2==len(polygon)
736
737        minx = maxx = polygon[0][0]
738        miny = maxy = polygon[0][1]
739        for point in polygon[1:]:
740            x, y = point
741           
742            if x < minx: minx = x
743            if x > maxx: maxx = x
744            if y < miny: miny = y
745            if y > maxy: maxy = y
746           
747
748        assert [minx, miny] in polygon
749        print minx, maxy
750        assert [minx, maxy] in polygon
751        assert [maxx, miny] in polygon
752        assert [maxx, maxy] in polygon               
753       
754
755       
756#-------------------------------------------------------------
757if __name__ == "__main__":
758    suite = unittest.makeSuite(Test_Polygon,'test')
759    #suite = unittest.makeSuite(Test_Polygon,'test_inside_polygon_geo_ref')
760    runner = unittest.TextTestRunner()
761    runner.run(suite)
762
763
764
765
Note: See TracBrowser for help on using the repository browser.