Changeset 1854


Ignore:
Timestamp:
Sep 28, 2005, 10:29:52 AM (19 years ago)
Author:
ole
Message:

Added functionality for excluding polygons in populate_polygon

Location:
inundation/pyvolution
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/data_manager.py

    r1835 r1854  
    221221    def __init__(self, domain, mode = 'w',\
    222222                 max_size = 2000000000,
    223                  recursion=False):
     223                 recursion = False):
    224224        from Scientific.IO.NetCDF import NetCDFFile
    225225        from Numeric import Int, Float, Float32
     
    16311631    from Scientific.IO.NetCDF import NetCDFFile
    16321632    from Numeric import Float, Int, Int32, searchsorted, zeros, array
     1633    from Numeric import allclose, around
     1634       
    16331635    precision = Float
    16341636
     
    16511653
    16521654
    1653     e_lat = file_e.variables['LAT'][:]
    1654 
     1655
     1656    #Precision used by most for lat/lon is 4 or 5 decimals
     1657    e_lat = around(file_e.variables['LAT'][:], 5)
     1658    e_lon = around(file_e.variables['LON'][:], 5)
    16551659
    16561660    #Check that files are compatible
    1657     from Numeric import allclose
    16581661    assert allclose(latitudes, file_u.variables['LAT'])
    1659     assert allclose(latitudes, file_v.variables['LAT'])   
    1660     assert allclose(latitudes, file_e.variables['LAT'])
     1662    assert allclose(latitudes, file_v.variables['LAT'])
     1663    assert allclose(latitudes, e_lat)
    16611664
    16621665    assert allclose(longitudes, file_u.variables['LON'])
    16631666    assert allclose(longitudes, file_v.variables['LON'])
    1664     assert allclose(longitudes, file_e.variables['LON'])   
     1667    assert allclose(longitudes, e_lon)   
    16651668
    16661669
  • inundation/pyvolution/test_util.py

    r1835 r1854  
    11121112
    11131113
     1114    def test_populate_polygon_with_exclude(self):
     1115       
     1116
     1117        polygon = [[0,0], [1,0], [1,1], [0,1]]
     1118        ex_poly = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter
     1119        points = populate_polygon(polygon, 5, exclude = [ex_poly])
     1120
     1121        assert len(points) == 5
     1122        for point in points:
     1123            assert inside_polygon(point, polygon)
     1124            assert not inside_polygon(point, ex_poly)           
     1125
     1126
     1127        #overlap
     1128        polygon = [[0,0], [1,0], [1,1], [0,1]]
     1129        ex_poly = [[-1,-1], [0.5,0], [0.5, 0.5], [-1,0.5]]
     1130        points = populate_polygon(polygon, 5, exclude = [ex_poly])
     1131
     1132        assert len(points) == 5
     1133        for point in points:
     1134            assert inside_polygon(point, polygon)
     1135            assert not inside_polygon(point, ex_poly)                       
     1136       
     1137        #Multiple
     1138        polygon = [[0,0], [1,0], [1,1], [0,1]]
     1139        ex_poly1 = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter
     1140        ex_poly2 = [[0.5,0.5], [0.5,1], [1, 1], [1,0.5]] #NE quarter       
     1141       
     1142        points = populate_polygon(polygon, 20, exclude = [ex_poly1, ex_poly2])
     1143
     1144        assert len(points) == 20
     1145        for point in points:
     1146            assert inside_polygon(point, polygon)
     1147            assert not inside_polygon(point, ex_poly1)
     1148            assert not inside_polygon(point, ex_poly2)                               
     1149       
     1150
     1151        #Very convoluted polygon
     1152        polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
     1153        ex_poly = [[-1,-1], [5,0], [5, 5], [-1,5]]
     1154        points = populate_polygon(polygon, 20, exclude = [ex_poly])
     1155       
     1156        assert len(points) == 20
     1157        for point in points:
     1158            assert inside_polygon(point, polygon)
     1159            assert not inside_polygon(point, ex_poly), '%s' %str(point)                       
     1160
     1161
    11141162
    11151163#-------------------------------------------------------------
  • inundation/pyvolution/util.py

    r1835 r1854  
    777777    return polygon
    778778
    779 def populate_polygon(polygon, number_of_points, seed = None):
     779def populate_polygon(polygon, number_of_points, seed = None, exclude = None):
    780780    """Populate given polygon with uniformly distributed points.
    781781
     
    784784       number_of_points - (optional) number of points
    785785       seed - seed for random number generator (default=None)
     786       exclude - list of polygons (inside main polygon) from where points should be excluded
    786787
    787788    Output:
     
    815816        y = uniform(min_y, max_y)
    816817
     818        append = False
    817819        if inside_polygon( [x,y], polygon ):
    818             points.append([x,y])
     820
     821            append = True
     822           
     823            #Check exclusions
     824            if exclude is not None:
     825                for ex_poly in exclude:
     826                    if inside_polygon( [x,y], ex_poly ):
     827                        append = False
     828                       
     829                   
     830        if append is True:               
     831            points.append([x,y])               
    819832
    820833    return points
     834
     835
    821836
    822837def tsh2sww(filename, verbose=True):
Note: See TracChangeset for help on using the changeset viewer.