handle missing descriptiveGroup

This commit is contained in:
Ed Chalstrey 2022-03-18 15:44:19 +00:00
parent 158a09637b
commit 3f281a5e73
4 changed files with 12 additions and 7 deletions

View File

@ -14,6 +14,7 @@ csv.field_size_limit(sys.maxsize)
def main(mastermap_path): def main(mastermap_path):
mm_paths = sorted(glob.glob(os.path.join(mastermap_path, "*.gml.csv"))) mm_paths = sorted(glob.glob(os.path.join(mastermap_path, "*.gml.csv")))
for mm_path in mm_paths: for mm_path in mm_paths:
print(mm_path)
filter_mastermap(mm_path) filter_mastermap(mm_path)
@ -28,8 +29,11 @@ def filter_mastermap(mm_path):
w = csv.DictWriter(output_fh, fieldnames=output_fieldnames) w = csv.DictWriter(output_fh, fieldnames=output_fieldnames)
w.writeheader() w.writeheader()
for line in r: for line in r:
try:
if 'Building' in line['descriptiveGroup']: if 'Building' in line['descriptiveGroup']:
w.writerow(line) w.writerow(line)
except TypeError: # when descriptiveGroup is missing, ignore this Polygon
pass
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -7,17 +7,17 @@ def test_filter_mastermap():
"""Test that MasterMap CSV can be correctly filtered to include only buildings.""" """Test that MasterMap CSV can be correctly filtered to include only buildings."""
input_file = "tests/test_mastermap.gml.csv" # Test csv with one building and one non-building input_file = "tests/test_mastermap.gml.csv" # Test csv with one building and one non-building
output_file = input_file.replace('gml', 'filtered') output_file = input_file.replace('gml', 'filtered')
filter_mastermap(input_file) # creates test_mastermap.filtered.csv filter_mastermap(input_file) # creates output_file
with open(output_file, newline='') as csvfile: with open(output_file, newline='') as csvfile:
csv_array = list(csv.reader(csvfile)) csv_array = list(csv.reader(csvfile))
assert len(csv_array) == 2 # assert that length is 2 because just one row after header assert len(csv_array) == 2 # assert that length is 2 because just one row after header
def test_filter_mastermap_missing_type(): def test_filter_mastermap_missing_descriptivegroup():
"""Test that MasterMap CSV can be correctly filtered when the polygon does not have a type specified.""" """Test that MasterMap CSV can be correctly filtered when the polygon does not have a type specified."""
input_file = "tests/test_mastermap_missing_type.gml.csv" # Test csv with one building and one non-building input_file = "tests/test_mastermap_missing_descriptivegroup.gml.csv" # Test csv with one building and one non-building
output_file = input_file.replace('gml', 'filtered') output_file = input_file.replace('gml', 'filtered')
filter_mastermap(input_file) # creates test_mastermap.filtered.csv filter_mastermap(input_file) # creates output_file
with open(output_file, newline='') as csvfile: with open(output_file, newline='') as csvfile:
csv_array = list(csv.reader(csvfile)) csv_array = list(csv.reader(csvfile))
assert len(csv_array) == 2 # assert that length is 2 because just one row after header assert len(csv_array) == 1 # assert that length is 1 because just header

View File

@ -0,0 +1 @@
WKT,fid,descriptiveGroup
1 WKT fid descriptiveGroup

View File

Can't render this file because it contains an unexpected character in line 2 and column 1359.