From 2c32ef440ae497d81879de1ec96c12ee17c88298 Mon Sep 17 00:00:00 2001 From: Al Date: Wed, 20 Jul 2016 14:59:35 -0400 Subject: [PATCH] [osm] Splitting place keys into those that override admin_level and those that are fallbacks when admin_level, etc. are not present --- scripts/geodata/osm/components.py | 44 ++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/scripts/geodata/osm/components.py b/scripts/geodata/osm/components.py index 4cbeff9d..068dba47 100644 --- a/scripts/geodata/osm/components.py +++ b/scripts/geodata/osm/components.py @@ -21,14 +21,9 @@ class OSMAddressComponents(object): ADMIN_LEVEL = 'admin_level' - # These keys override country-level - global_keys = { + # These keys override country-level + global_keys_override = { 'place': { - 'country': AddressFormatter.COUNTRY, - 'state': AddressFormatter.STATE, - 'region': AddressFormatter.STATE, - 'province': AddressFormatter.STATE, - 'county': AddressFormatter.STATE_DISTRICT, 'island': AddressFormatter.ISLAND, 'islet': AddressFormatter.ISLAND, 'municipality': AddressFormatter.CITY, @@ -44,6 +39,17 @@ class OSMAddressComponents(object): } } + # These keys are fallback in case we haven't added a country or there is no admin_level= + global_keys = { + 'place': { + 'country': AddressFormatter.COUNTRY, + 'state': AddressFormatter.STATE, + 'region': AddressFormatter.STATE, + 'province': AddressFormatter.STATE, + 'county': AddressFormatter.STATE_DISTRICT, + } + } + def __init__(self, boundaries_dir=OSM_BOUNDARIES_DIR): self.config = {} @@ -60,10 +66,15 @@ class OSMAddressComponents(object): self.config[country_code] = data def component(self, country, prop, value): - props = self.global_keys.get(prop, {}) - if not props: - props = self.config.get(country, {}).get(prop, {}) - return props.get(value, None) + component = self.global_keys_override.get(prop, {}).get(value, None) + if component is not None: + return component + + component = self.config.get(country, {}).get(prop, {}).get(value, None) + if component is not None: + return component + + return self.global_keys.get(prop, {}).get(value, None) def component_from_properties(self, country, properties, containing=()): country_config = self.config.get(country, {}) @@ -90,10 +101,11 @@ class OSMAddressComponents(object): config.update(config_updates) break + # Necessary to do this. for k, v in six.iteritems(properties): - containing_component = self.global_keys.get(k, {}).get(v, None) + override_component = self.global_keys_override.get(k, {}).get(v, None) - if containing_component is not None: + if override_component is not None: return containing_component for k, v in six.iteritems(properties): @@ -103,6 +115,12 @@ class OSMAddressComponents(object): if containing_component is not None: return containing_component + for k, v in six.iteritems(properties): + containing_component = self.global_keys.get(k, {}).get(v, None) + + if containing_component is not None: + return containing_component + return None osm_address_components = OSMAddressComponents()