Changeset 6224


Ignore:
Timestamp:
Jan 21, 2009, 4:39:27 PM (15 years ago)
Author:
ole
Message:

Added functionality for clipping buildings by a list of polygons.
This is not finished and test is only rudimentary.

Location:
anuga_core/source/anuga/shallow_water
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/shallow_water/data_manager.py

    r6215 r6224  
    988988
    989989
    990 def csv2building_polygons(file_name, floor_height=3):
     990def csv2building_polygons(file_name,
     991                          floor_height=3,
     992                          clipping_polygons=None):
    991993    """
    992994    Convert CSV files of the form:
     
    10091011   
    10101012    Optional parameter floor_height is the height of each building story.
     1013    Optional parameter clipping_olygons is a list of polygons selecting
     1014    buildings. Any building not in these polygons will be omitted.
    10111015   
    10121016    See csv2polygons for more details
    10131017    """
    10141018
    1015     polygons, values = csv2polygons(file_name, value_name='floors')   
     1019    polygons, values = csv2polygons(file_name,
     1020                                    value_name='floors',
     1021                                    clipping_polygons=None)   
    10161022
    10171023   
     
    10271033# @brief Convert CSV file into a dictionary of polygons and associated values.
    10281034# @param filename The path to the file to read, value_name name for the 4th column
    1029 def csv2polygons(file_name, value_name='value'):
     1035def csv2polygons(file_name,
     1036                 value_name='value',
     1037                 clipping_polygons=None):
    10301038    """
    10311039    Convert CSV files of the form:
     
    10571065    Eastings and Northings will be returned as floating point values while
    10581066    id and values will be returned as strings.
     1067
     1068    Optional argument: clipping_polygons will select only those polygons that are
     1069    fully within one or more of the clipping_polygons. In other words any polygon from
     1070    the csv file which has at least one point not inside one of the clipping polygons
     1071    will be excluded
    10591072   
    10601073    See underlying function csv2dict for more details.
     
    10861099
    10871100    # Loop through entries and compose polygons
     1101    excluded_polygons={}
    10881102    past_ids = {}
    10891103    last_id = None
     
    11071121        # Append this point to current polygon
    11081122        point = [float(X['easting'][i]), float(X['northing'][i])]
     1123
     1124        if clipping_polygons is not None:
     1125            exclude=True
     1126            for clipping_polygon in clipping_polygons:
     1127                if inside_polygon(point, clipping_polygon):
     1128                    exclude=False
     1129                    break
     1130               
     1131            if exclude is True:
     1132                excluded_polygons[id]=True
     1133
    11091134        polygons[id].append(point)   
    11101135           
     
    11151140
    11161141        last_id = id
     1142
     1143    # Weed out polygons that were not wholly inside clipping polygons
     1144    for id in excluded_polygons:
     1145        del polygons[id]
    11171146       
    11181147    return polygons, values
  • anuga_core/source/anuga/shallow_water/test_data_manager.py

    r6215 r6224  
    1122211222
    1122311223
     11224    def test_csv2polygons_with_clipping(self):
     11225        """test_csv2polygons with optional clipping
     11226        """
     11227        #FIXME(Ole): Not Done!!
     11228       
     11229        path = get_pathname_from_package('anuga.shallow_water')               
     11230        testfile = os.path.join(path, 'polygon_values_example.csv')               
     11231
     11232        polygons, values = csv2polygons(testfile,
     11233                                        value_name='floors',
     11234                                        clipping_polygons=None)
     11235
     11236        assert len(polygons) == 7, 'Must have seven polygons'
     11237        assert len(values) == 7, 'Must have seven values'
     11238           
     11239        # Known floor values
     11240        floors = {'1': 2, '2': 0, '3': 1, '4': 2, '5': 0, '8': 1, '9': 1}
     11241       
     11242        # Known polygon values
     11243        known_polys = {'1': [[422681.61,871117.55],
     11244                             [422691.02,871117.60],
     11245                             [422690.87,871084.23],
     11246                             [422649.36,871081.85],
     11247                             [422649.36,871080.39],
     11248                             [422631.86,871079.50],
     11249                             [422631.72,871086.75],
     11250                             [422636.75,871087.20],
     11251                             [422636.75,871091.50],
     11252                             [422649.66,871092.09],
     11253                             [422649.83,871084.91],
     11254                             [422652.94,871084.90],
     11255                             [422652.84,871092.39],
     11256                             [422681.83,871093.73],
     11257                             [422681.61,871117.55]],
     11258                       '2': [[422664.22,870785.46],
     11259                             [422672.48,870780.14],
     11260                             [422668.17,870772.62],
     11261                             [422660.35,870777.17],
     11262                             [422664.22,870785.46]],
     11263                       '3': [[422661.30,871215.06],
     11264                             [422667.50,871215.70],
     11265                             [422668.30,871204.86],
     11266                             [422662.21,871204.33],
     11267                             [422661.30,871215.06]],
     11268                       '4': [[422473.44,871191.22],
     11269                             [422478.33,871192.26],
     11270                             [422479.52,871186.03],
     11271                             [422474.78,871185.14],
     11272                             [422473.44,871191.22]],
     11273                       '5': [[422369.69,871049.29],
     11274                             [422378.63,871053.58],
     11275                             [422383.91,871044.51],
     11276                             [422374.97,871040.32],
     11277                             [422369.69,871049.29]],
     11278                       '8': [[422730.56,871203.13],
     11279                             [422734.10,871204.90],
     11280                             [422735.26,871202.18],
     11281                             [422731.87,871200.58],
     11282                             [422730.56,871203.13]],
     11283                       '9': [[422659.85,871213.80],
     11284                             [422660.91,871210.97],
     11285                             [422655.42,871208.85],
     11286                             [422654.36,871211.68],
     11287                             [422659.85,871213.80]]
     11288                       }       
     11289       
     11290
     11291       
     11292               
     11293        for id in ['1', '2', '3', '4', '5' ,'8' ,'9']:
     11294            assert id in polygons.keys()
     11295            assert id in values.keys()           
     11296
     11297            assert int(values[id]) == int(floors[id])
     11298            assert len(polygons[id]) == len(known_polys[id])
     11299            assert num.allclose(polygons[id], known_polys[id])
     11300
     11301
     11302
     11303
    1122411304   
    1122511305    def test_csv2building_polygons(self):
Note: See TracChangeset for help on using the changeset viewer.