Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Change log

### 0.2.9
- Retain existing `incline` and `climb` tags when converting OSW to OSM
- Add `climb` tag when missing and `incline` is provided
- Expanded unit tests for incline/climb handling

### 0.2.8
- Fixed [BUG 2040](https://dev.azure.com/TDEI-UW/TDEI/_workitems/edit/2040)
- Removing the width tag if the width is not float or integer
- Removing the width tag if the width is not float or integer
- Added unit test cases

### 0.2.7
Expand Down
14 changes: 12 additions & 2 deletions src/osm_osw_reformatter/serializer/osm/osm_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ def filter_tags(self, tags):
tags.pop('_u_id', '')
tags.pop('_v_id', '')
tags.pop('_w_id', '')
tags.pop('incline', '')
tags.pop('length', '')
if 'foot' in tags and tags['foot'] == 'yes' and 'highway' in tags and tags['highway'] in self.OSM_IMPLIED_FOOTWAYS:
tags.pop('foot', '')

# OSW fields with similar OSM field names
tags['incline'] = tags.pop('climb', '')
if 'incline' in tags:
try:
incline_val = float(str(tags['incline']).rstrip('%'))
except (ValueError, TypeError):
incline_val = None
else:
tags['incline'] = str(incline_val)
if 'climb' not in tags:
if incline_val > 0:
tags['climb'] = 'up'
elif incline_val < 0:
tags['climb'] = 'down'

self._check_datatypes(tags)

Expand Down
2 changes: 1 addition & 1 deletion src/osm_osw_reformatter/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.2.8'
__version__ = '0.2.9'
Binary file not shown.
41 changes: 41 additions & 0 deletions tests/unit_tests/test_osw2osm/test_osw2osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
OUTPUT_DIR = os.path.join(os.path.dirname(os.path.dirname(ROOT_DIR)), 'output')
TEST_ZIP_FILE = os.path.join(ROOT_DIR, 'test_files/osw.zip')
TEST_WIDTH_ZIP_FILE = os.path.join(ROOT_DIR, 'test_files/width-test.zip')
TEST_DATA_WITH_INCLINE_ZIP_FILE = os.path.join(ROOT_DIR, 'test_files/dataset_with_incline.zip')


class TestOSW2OSM(unittest.IsolatedAsyncioTestCase):
Expand Down Expand Up @@ -64,6 +65,46 @@ async def test_convert_error(self):
result = osw2osm.convert()
self.assertFalse(result.status)

def test_generated_file_contains_climb_tag(self):
zip_file = TEST_DATA_WITH_INCLINE_ZIP_FILE
osw2osm = OSW2OSM(zip_file_path=zip_file, workdir=OUTPUT_DIR, prefix='test')
result = osw2osm.convert()
xml_file_path = result.generated_files

tree = ET.parse(xml_file_path)
root = tree.getroot()
self.assertGreater(len(root.findall(".//tag[@k='climb']")), 0)

os.remove(result.generated_files)

def test_generated_file_contains_incline_tag(self):
zip_file = TEST_DATA_WITH_INCLINE_ZIP_FILE
osw2osm = OSW2OSM(zip_file_path=zip_file, workdir=OUTPUT_DIR, prefix='test')
result = osw2osm.convert()
xml_file_path = result.generated_files

tree = ET.parse(xml_file_path)
root = tree.getroot()
self.assertGreater(len(root.findall(".//tag[@k='incline']")), 0)

os.remove(result.generated_files)

def test_incline_tags_have_climb(self):
zip_file = TEST_DATA_WITH_INCLINE_ZIP_FILE
osw2osm = OSW2OSM(zip_file_path=zip_file, workdir=OUTPUT_DIR, prefix='test')
result = osw2osm.convert()
xml_file_path = result.generated_files

tree = ET.parse(xml_file_path)
root = tree.getroot()

for element in root.findall('.//way') + root.findall('.//node') + root.findall('.//relation'):
tags = {tag.get('k'): tag.get('v') for tag in element.findall('tag')}
if 'incline' in tags and float(tags.get('incline', 0) or 0) != 0:
self.assertIn('climb', tags)

os.remove(result.generated_files)


if __name__ == '__main__':
unittest.main()
43 changes: 42 additions & 1 deletion tests/unit_tests/test_serializer/test_osm_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,45 @@ def test_preserves_width_when_value_is_float_with_string(self):
self.assertIsInstance(normalizer, dict)
self.assertIn('highway', normalizer)
self.assertIn('width', normalizer)
self.assertEqual(normalizer['width'], '1.525')
self.assertEqual(normalizer['width'], '1.525')


class TestOSMNormalizeInclineField(unittest.TestCase):
def setUp(self):
self.normalizer = OSMNormalizer()

def test_retains_existing_incline_and_climb(self):
tags = {"highway": "footway", "incline": 0.014, "climb": "up"}
normalizer = self.normalizer.filter_tags(tags)
self.assertEqual(normalizer["incline"], "0.014")
self.assertEqual(normalizer["climb"], "up")

def test_derives_climb_from_positive_incline(self):
tags = {"highway": "footway", "incline": 0.014}
normalizer = self.normalizer.filter_tags(tags)
self.assertEqual(normalizer["climb"], "up")
self.assertEqual(normalizer["incline"], "0.014")

def test_derives_climb_from_negative_incline(self):
tags = {"highway": "footway", "incline": -0.014}
normalizer = self.normalizer.filter_tags(tags)
self.assertEqual(normalizer["climb"], "down")
self.assertEqual(normalizer["incline"], "-0.014")

def test_does_not_derive_climb_from_zero_incline(self):
tags = {"highway": "footway", "incline": 0}
normalizer = self.normalizer.filter_tags(tags)
self.assertEqual(normalizer["incline"], "0.0")
self.assertNotIn("climb", normalizer)

def test_retains_climb_without_incline(self):
tags = {"highway": "footway", "climb": "down"}
normalizer = self.normalizer.filter_tags(tags)
self.assertEqual(normalizer["climb"], "down")
self.assertNotIn("incline", normalizer)

def test_retains_non_numeric_incline_without_climb(self):
tags = {"highway": "footway", "incline": "steep"}
normalizer = self.normalizer.filter_tags(tags)
self.assertEqual(normalizer["incline"], "steep")
self.assertNotIn("climb", normalizer)
Loading