Ignore:
Timestamp:
Jan 6, 2009, 4:26:24 PM (15 years ago)
Author:
ole
Message:

Added functionality to read multiple polygons with associated values from
a single csv file.

File:
1 edited

Legend:

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

    r6114 r6120  
    996996
    997997
     998def csv2building_polygons(file_name, floor_height=3):
     999    """
     1000    Convert CSV files of the form:
     1001
     1002    easting,northing,id,floors
     1003    422664.22,870785.46,2,0
     1004    422672.48,870780.14,2,0
     1005    422668.17,870772.62,2,0
     1006    422660.35,870777.17,2,0
     1007    422664.22,870785.46,2,0
     1008    422661.30,871215.06,3,1
     1009    422667.50,871215.70,3,1
     1010    422668.30,871204.86,3,1
     1011    422662.21,871204.33,3,1
     1012    422661.30,871215.06,3,1
     1013
     1014    to a dictionary of polygons with id as key.
     1015    The associated number of floors are converted to m above MSL and
     1016    returned as a separate dictionary also keyed by id.
     1017   
     1018    Optional parameter floor_height is the height of each building story.
     1019   
     1020    See csv2polygons for more details
     1021    """
     1022
     1023    polygons, values = csv2polygons(file_name, value_name='floors')   
     1024
     1025   
     1026    heights = {}
     1027    for key in values.keys():
     1028        v = float(values[key])
     1029        heights[key] = v*floor_height
     1030       
     1031    return polygons, heights               
     1032           
     1033
     1034##
     1035# @brief Convert CSV file into a dictionary of polygons and associated values.
     1036# @param filename The path to the file to read, value_name name for the 4th column
     1037def csv2polygons(file_name, value_name='value'):
     1038    """
     1039    Convert CSV files of the form:
     1040
     1041    easting,northing,id,value
     1042    422664.22,870785.46,2,0
     1043    422672.48,870780.14,2,0
     1044    422668.17,870772.62,2,0
     1045    422660.35,870777.17,2,0
     1046    422664.22,870785.46,2,0
     1047    422661.30,871215.06,3,1
     1048    422667.50,871215.70,3,1
     1049    422668.30,871204.86,3,1
     1050    422662.21,871204.33,3,1
     1051    422661.30,871215.06,3,1
     1052
     1053    to a dictionary of polygons with id as key.
     1054    The associated values are returned as a separate dictionary also keyed by id.
     1055
     1056
     1057    easting: x coordinate relative to zone implied by the model
     1058    northing: y coordinate relative to zone implied by the model   
     1059    id: tag for polygon comprising points with this tag
     1060    value: numeral associated with each polygon. These must be the same for all points in each polygon.
     1061   
     1062    The last header, value, can take on other names such as roughness, floors, etc - or it can be omitted
     1063    in which case the returned values will be None
     1064   
     1065    Eastings and Northings will be returned as floating point values while
     1066    id and values will be returned as strings.
     1067   
     1068    See underlying function csv2dict for more details.
     1069    """
     1070
     1071    X, _ = csv2dict(file_name)
     1072
     1073    msg = 'Polygon csv file must have 3 or 4 columns'
     1074    assert len(X.keys()) in [3, 4], msg
     1075   
     1076    msg = 'Did not find expected column header: easting'
     1077    assert 'easting' in X.keys(), msg
     1078   
     1079    msg = 'Did not find expected column header: northing'   
     1080    assert 'northing' in X.keys(), northing
     1081   
     1082    msg = 'Did not find expected column header: northing'       
     1083    assert 'id' in X.keys(), msg
     1084   
     1085    if value_name is not None:
     1086        msg = 'Did not find expected column header: %s' % value_name       
     1087        assert value_name in X.keys(), msg   
     1088   
     1089    polygons = {}
     1090    if len(X.keys()) == 4:
     1091        values = {}
     1092    else:
     1093        values = None
     1094
     1095    # Loop through entries and compose polygons
     1096    for i, id in enumerate(X['id']):
     1097       
     1098        if id not in polygons:
     1099            # Start new polygon
     1100            polygons[id] = []
     1101            if values is not None:
     1102                values[id] = X[value_name][i]
     1103           
     1104        # Append this point to current polygon
     1105        point = [float(X['easting'][i]), float(X['northing'][i])]
     1106        polygons[id].append(point)   
     1107           
     1108        # Check that value is the same across each polygon
     1109        assert values[id] == X[value_name][i]
     1110       
     1111    return polygons, values
     1112
     1113
     1114           
     1115           
    9981116##
    9991117# @brief Convert CSV file to a dictionary of arrays.
Note: See TracChangeset for help on using the changeset viewer.