diff --git a/bpeng/dimensions/parse_dimensions.py b/bpeng/dimensions/parse_dimensions.py index a1280f559b57f8b3999adc3dfb596d97d1808296..6bcbdcea26c981ed059d0b264757af0e6e46af38 100644 --- a/bpeng/dimensions/parse_dimensions.py +++ b/bpeng/dimensions/parse_dimensions.py @@ -10,10 +10,10 @@ class ParseDimensions: """ DIRECTION_DICT = { - 'South': 1, 'south': 1, - 'East': 2, 'east': 2, - 'West': 3, 'west': 3, - 'North': 4, 'north': 4, + 'South': 1, 'south': 1, 'S': 1, + 'East': 2, 'east': 2, 'E': 2, + 'West': 3, 'west': 3, 'W': 3, + 'North': 4, 'north': 4, 'N': 4, } FEATURE_DICT = { @@ -21,6 +21,9 @@ class ParseDimensions: 'Window': 2, 'Building Point': 3, 'Roof Point': 4, + 'Adjacent Building 1 Point': 5, + 'Adjacent Building 2 Point': 6, + 'Adjacent Building 3 Point': 7, } @staticmethod @@ -46,41 +49,46 @@ class ParseDimensions: workbook = xlrd.open_workbook(file_contents=dimensions_file) worksheet = workbook.sheet_by_index(0) - perimeter = worksheet.cell(4, 1).value - area = worksheet.cell(5, 1).value - num_floors = worksheet.cell(4, 3).value - ground_elevation = worksheet.cell(5, 3).value - north_adjacency = worksheet.cell(5, 16).value - south_adjacency = worksheet.cell(5, 22).value - west_adjacency = worksheet.cell(20, 16).value - east_adjacency = worksheet.cell(20, 22).value + num_floors = worksheet.cell(1, 1).value + perimeter = worksheet.cell(1, 3).value + area = worksheet.cell(1, 5).value building_dimensions = { 'perimeter': float(perimeter), 'area': float(area), 'num_floors': int(num_floors), - 'ground_elevation': int(ground_elevation), - 'north_adjacency': north_adjacency, - 'south_adjacency': south_adjacency, - 'west_adjacency': west_adjacency, - 'east_adjacency': east_adjacency, } point_list = [] - # Building and roof points have seperate starting places on the excel spreadsheet, + # Building, roof and adjacent building points have seperate starting places on the excel spreadsheet, start_points = [ { - 'row_start': 8, + 'row_start': 5, 'column_start': 1, 'feature': ParseDimensions.FEATURE_DICT['Building Point'], }, { - 'row_start': 14, - 'column_start': 7, + 'row_start': 3, + 'column_start': 23, 'feature': ParseDimensions.FEATURE_DICT['Roof Point'], }, + { + 'row_start': 3, + 'column_start': 28, + 'feature': ParseDimensions.FEATURE_DICT['Adjacent Building 1 Point'], + }, + { + 'row_start': 3, + 'column_start': 33, + 'feature': ParseDimensions.FEATURE_DICT['Adjacent Building 2 Point'], + }, + { + 'row_start': 3, + 'column_start': 38, + 'feature': ParseDimensions.FEATURE_DICT['Adjacent Building 3 Point'], + }, ] - # Get the building and roof points + # Get the building, roof and adjacent building points for start_point in start_points: row_start = start_point['row_start'] column_start = start_point['column_start'] @@ -97,94 +105,91 @@ class ParseDimensions: row_start + row_counter, column_start + column_counter ).value - column_counter += 1 - height = worksheet.cell( - row_start + row_counter, - column_start + column_counter - ).value - point_list.append( - { - 'latitude': float(lat), - 'longitude': float(longt), - 'elevation': int(elevation), - 'corresponding_height': int(height), - 'feature': start_point['feature'], - } - ) + point_data = { + 'latitude': float(lat), + 'longitude': float(longt), + 'elevation': float(elevation), + 'feature': start_point['feature'], + } + # The row counter also serves to count the wall we are on, + # but this is only relevant for building points + if start_point['feature'] == ParseDimensions.FEATURE_DICT['Building Point']: + point_data['wall_num_first'] = row_counter + point_data['wall_num_second'] = row_counter + 1 + point_list.append(point_data) row_counter += 1 lat = worksheet.cell(row_start + row_counter, column_start).value + # Once we've finished parsing we should update the first point to reflect + # the actual corresponding wall + if start_point['feature'] == ParseDimensions.FEATURE_DICT['Building Point']: + point_list[0]['wall_num_first'] = row_counter + windows_and_doors = [] - # Window and door data has many start points - start_points = [ + # Places where windows and doors height and width data rae stored + feature_start_points = [ + { + 'column_start': 9, + 'row_start': 4, + 'feature': ParseDimensions.FEATURE_DICT['Window'], + }, { - 'row_start': 7, - 'column_start': 12, - 'direction': ParseDimensions.DIRECTION_DICT['north'], + 'column_start': 11, + 'row_start': 4, + 'feature': ParseDimensions.FEATURE_DICT['Window'], }, { - 'row_start': 7, - 'column_start': 18, - 'direction': ParseDimensions.DIRECTION_DICT['south'], + 'column_start': 13, + 'row_start': 4, + 'feature': ParseDimensions.FEATURE_DICT['Window'], }, { - 'row_start': 22, - 'column_start': 12, - 'direction': ParseDimensions.DIRECTION_DICT['west'], + 'column_start': 15, + 'row_start': 4, + 'feature': ParseDimensions.FEATURE_DICT['Window'], }, { - 'row_start': 22, - 'column_start': 18, - 'direction': ParseDimensions.DIRECTION_DICT['east'], + 'column_start': 17, + 'row_start': 4, + 'feature': ParseDimensions.FEATURE_DICT['Window'], + }, + { + 'column_start': 19, + 'row_start': 4, + 'feature': ParseDimensions.FEATURE_DICT['Door'], }, ] - # Get Window & Door data - for start_point in start_points: - row_start = start_point['row_start'] - column_start = start_point['column_start'] - row_counter = 0 - feature_string = worksheet.cell(row_start, column_start).value - while feature_string: - # The cell telling us whether or not it is a door or a window is 1 before height - if feature_string.startswith('Window'): - feature = ParseDimensions.FEATURE_DICT['Window'] - elif feature_string.startswith('Door'): - feature = ParseDimensions.FEATURE_DICT['Door'] - else: - # We shouldn't be in this row - break - column_counter = 1 - height = worksheet.cell( - row_start + row_counter, - column_start + column_counter - ).value - column_counter += 1 - width = worksheet.cell( - row_start + row_counter, - column_start + column_counter - ).value - column_counter += 1 + # The start points for window and doors data + row_start = 6 + column_start = 8 + orientation = worksheet.cell(row_start, column_start).value + row_counter = 0 + while(orientation): + for start_point in feature_start_points: + # Get the quantity for this window/door type quantity = worksheet.cell( row_start + row_counter, - column_start + column_counter + start_point['column_start'], ).value - column_counter += 1 - orientation = worksheet.cell( - row_start + row_counter, - column_start + column_counter - ).value - if height: - windows_and_doors.append( - { - 'height': float(height), - 'width': float(width), - 'quantity': int(quantity), - 'orientation': ParseDimensions.DIRECTION_DICT[orientation], - 'feature': feature, - 'direction': start_point['direction'], - } - ) - row_counter += 1 - feature_string = worksheet.cell(row_start + row_counter, column_start).value + if quantity and int(quantity) > 0: + # Get the height and width for this window/door type + height = worksheet.cell( + start_point['row_start'], + start_point['column_start'], + ).value + width = worksheet.cell( + start_point['row_start'], + start_point['column_start'] + 1, + ).value + windows_and_doors.append({ + 'height': float(height), + 'width': float(width), + 'quantity': int(quantity), + 'orientation': ParseDimensions.DIRECTION_DICT[orientation], + 'wall_num': row_counter + 1, + 'feature': start_point['feature'], + }) + row_counter += 1 + orientation = worksheet.cell(row_start + row_counter, column_start).value return windows_and_doors, point_list, building_dimensions diff --git a/bpeng/tests/data/test_dimensions_parsing.xlsx b/bpeng/tests/data/test_dimensions_parsing.xlsx old mode 100755 new mode 100644 index f2a9f217d779625e48313f4d31766ea6e43b7b25..b73c419208198e20f296dc79329f7bd0a01c2b02 Binary files a/bpeng/tests/data/test_dimensions_parsing.xlsx and b/bpeng/tests/data/test_dimensions_parsing.xlsx differ diff --git a/bpeng/tests/data/test_dimensions_parsing2.xlsx b/bpeng/tests/data/test_dimensions_parsing2.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..27124f824c665b95913e588defa80ae4309201e6 Binary files /dev/null and b/bpeng/tests/data/test_dimensions_parsing2.xlsx differ diff --git a/bpeng/tests/test_dimensions.py b/bpeng/tests/test_dimensions.py index 71908c72b52d194796e982532ef2904805d83735..b66b5b5a3cc2d6fd92b9ccfb51630bdc1a34be97 100644 --- a/bpeng/tests/test_dimensions.py +++ b/bpeng/tests/test_dimensions.py @@ -10,34 +10,35 @@ class TestDimensions: self.pd = ParseDimensions() def test_parse(self): - with open(os.path.join(BASE_DIR, 'data/test_dimensions_parsing.xlsx'), 'r+b') as f: + with open(os.path.join(BASE_DIR, 'data/test_dimensions_parsing2.xlsx'), 'r+b') as f: windows_doors, points, building_dimensions = self.pd.parse(f.read()) # Test points - assert len(points) == 13 + assert len(points) == 18 first_point = points[0] - assert first_point['elevation'] == 713 - assert first_point['longitude'] == -85.281127 - assert first_point['latitude'] == 35.034178 - assert first_point['corresponding_height'] == 12 + assert first_point['elevation'] == 6.00083812356748 + assert first_point['wall_num_first'] == 5 + assert first_point['wall_num_second'] == 1 + assert first_point['longitude'] == 40.6607523958424 + assert first_point['latitude'] == -73.9593929825736 assert first_point['feature'] == 3 + first_roof_point = points[5] + assert first_roof_point['elevation'] == 6.11965341026875 + assert first_roof_point['longitude'] == 40.6607556006893 + assert first_roof_point['latitude'] == -73.9593867748827 + assert first_roof_point['feature'] == 4 + + # Test window_doors - assert len(windows_doors) == 9 + assert len(windows_doors) == 12 first_window_door = windows_doors[0] assert first_window_door['feature'] == 2 - assert first_window_door['orientation'] == 4 - assert first_window_door['height'] == 5.0 - assert first_window_door['direction'] == 4 - assert first_window_door['width'] == 6.0 + assert first_window_door['orientation'] == 1 + assert first_window_door['height'] == 4.0 + assert first_window_door['width'] == 2.5 assert first_window_door['quantity'] == 2 - # Test building_dimensions - assert building_dimensions['num_floors'] == 1 - assert building_dimensions['ground_elevation'] == 701 - assert building_dimensions['area'] == 2348.23 - assert building_dimensions['perimeter'] == 213.29 - assert building_dimensions['north_adjacency'] == 0.5 - assert building_dimensions['south_adjacency'] == 0.0 - assert building_dimensions['west_adjacency'] == 1.0 - assert building_dimensions['east_adjacency'] == 0.0 + assert building_dimensions['num_floors'] == 4 + assert building_dimensions['perimeter'] == 500.0 + assert building_dimensions['area'] == 500.0