Changeset 6120
- Timestamp:
- Jan 6, 2009, 4:26:24 PM (16 years ago)
- Location:
- anuga_core/source/anuga/shallow_water
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/shallow_water/data_manager.py
r6114 r6120 996 996 997 997 998 def 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 1037 def 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 998 1116 ## 999 1117 # @brief Convert CSV file to a dictionary of arrays. -
anuga_core/source/anuga/shallow_water/test_data_manager.py
r6086 r6120 11042 11042 assert (polygon == [[0.0, 0.0],[1.0, 0.0],[0.0, 1.0]]) 11043 11043 11044 11045 11046 11044 11045 def test_csv2polygons(self): 11046 """test_csv2polygons 11047 """ 11048 11049 path = get_pathname_from_package('anuga.shallow_water') 11050 testfile = os.path.join(path, 'polygon_values_example.csv') 11051 11052 polygons, values = csv2polygons(testfile, 11053 value_name='floors') 11054 11055 assert len(polygons) == 7, 'Must have seven polygons' 11056 assert len(values) == 7, 'Must have seven values' 11057 11058 # Known floor values 11059 floors = {'1': 2, '2': 0, '3': 1, '4': 2, '5': 0, '8': 1, '9': 1} 11060 11061 # Known polygon values 11062 known_polys = {'1': [[422681.61,871117.55], 11063 [422691.02,871117.60], 11064 [422690.87,871084.23], 11065 [422649.36,871081.85], 11066 [422649.36,871080.39], 11067 [422631.86,871079.50], 11068 [422631.72,871086.75], 11069 [422636.75,871087.20], 11070 [422636.75,871091.50], 11071 [422649.66,871092.09], 11072 [422649.83,871084.91], 11073 [422652.94,871084.90], 11074 [422652.84,871092.39], 11075 [422681.83,871093.73], 11076 [422681.61,871117.55]], 11077 '2': [[422664.22,870785.46], 11078 [422672.48,870780.14], 11079 [422668.17,870772.62], 11080 [422660.35,870777.17], 11081 [422664.22,870785.46]], 11082 '3': [[422661.30,871215.06], 11083 [422667.50,871215.70], 11084 [422668.30,871204.86], 11085 [422662.21,871204.33], 11086 [422661.30,871215.06]], 11087 '4': [[422473.44,871191.22], 11088 [422478.33,871192.26], 11089 [422479.52,871186.03], 11090 [422474.78,871185.14], 11091 [422473.44,871191.22]], 11092 '5': [[422369.69,871049.29], 11093 [422378.63,871053.58], 11094 [422383.91,871044.51], 11095 [422374.97,871040.32], 11096 [422369.69,871049.29]], 11097 '8': [[422730.56,871203.13], 11098 [422734.10,871204.90], 11099 [422735.26,871202.18], 11100 [422731.87,871200.58], 11101 [422730.56,871203.13]], 11102 '9': [[422659.85,871213.80], 11103 [422660.91,871210.97], 11104 [422655.42,871208.85], 11105 [422654.36,871211.68], 11106 [422659.85,871213.80]] 11107 } 11108 11109 11110 11111 11112 for id in ['1', '2', '3', '4', '5' ,'8' ,'9']: 11113 assert id in polygons.keys() 11114 assert id in values.keys() 11115 11116 assert int(values[id]) == int(floors[id]) 11117 assert len(polygons[id]) == len(known_polys[id]) 11118 assert allclose(polygons[id], known_polys[id]) 11119 11120 11121 11122 def test_csv2building_polygons(self): 11123 """test_csv2building_polygons 11124 """ 11125 11126 path = get_pathname_from_package('anuga.shallow_water') 11127 testfile = os.path.join(path, 'polygon_values_example.csv') 11128 11129 polygons, values = csv2building_polygons(testfile, 11130 floor_height=3) 11131 11132 assert len(polygons) == 7, 'Must have seven polygons' 11133 assert len(values) == 7, 'Must have seven values' 11134 11135 # Known floor values 11136 floors = {'1': 6, '2': 0, '3': 3, '4': 6, '5': 0, '8': 3, '9': 3} 11137 11138 11139 for id in ['1', '2', '3', '4', '5' ,'8' ,'9']: 11140 assert id in polygons.keys() 11141 assert id in values.keys() 11142 11143 assert float(values[id]) == float(floors[id]) 11144 11145 11146 11047 11147 #------------------------------------------------------------- 11048 11148 if __name__ == "__main__": 11049 11149 11050 11150 suite = unittest.makeSuite(Test_Data_Manager,'test') 11051 # suite = unittest.makeSuite(Test_Data_Manager,'test_export_gridIII')11052 11151 #suite = unittest.makeSuite(Test_Data_Manager,'test_file_boundary_stsIV_sinewave_ordering') 11053 11152 #suite = unittest.makeSuite(Test_Data_Manager,'test_get_flow_through_cross_section_with_geo')
Note: See TracChangeset
for help on using the changeset viewer.