[osm] Splitting place keys into those that override admin_level and those that are fallbacks when admin_level, etc. are not present
This commit is contained in:
@@ -21,14 +21,9 @@ class OSMAddressComponents(object):
|
|||||||
|
|
||||||
ADMIN_LEVEL = 'admin_level'
|
ADMIN_LEVEL = 'admin_level'
|
||||||
|
|
||||||
# These keys override country-level
|
# These keys override country-level
|
||||||
global_keys = {
|
global_keys_override = {
|
||||||
'place': {
|
'place': {
|
||||||
'country': AddressFormatter.COUNTRY,
|
|
||||||
'state': AddressFormatter.STATE,
|
|
||||||
'region': AddressFormatter.STATE,
|
|
||||||
'province': AddressFormatter.STATE,
|
|
||||||
'county': AddressFormatter.STATE_DISTRICT,
|
|
||||||
'island': AddressFormatter.ISLAND,
|
'island': AddressFormatter.ISLAND,
|
||||||
'islet': AddressFormatter.ISLAND,
|
'islet': AddressFormatter.ISLAND,
|
||||||
'municipality': AddressFormatter.CITY,
|
'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):
|
def __init__(self, boundaries_dir=OSM_BOUNDARIES_DIR):
|
||||||
self.config = {}
|
self.config = {}
|
||||||
|
|
||||||
@@ -60,10 +66,15 @@ class OSMAddressComponents(object):
|
|||||||
self.config[country_code] = data
|
self.config[country_code] = data
|
||||||
|
|
||||||
def component(self, country, prop, value):
|
def component(self, country, prop, value):
|
||||||
props = self.global_keys.get(prop, {})
|
component = self.global_keys_override.get(prop, {}).get(value, None)
|
||||||
if not props:
|
if component is not None:
|
||||||
props = self.config.get(country, {}).get(prop, {})
|
return component
|
||||||
return props.get(value, None)
|
|
||||||
|
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=()):
|
def component_from_properties(self, country, properties, containing=()):
|
||||||
country_config = self.config.get(country, {})
|
country_config = self.config.get(country, {})
|
||||||
@@ -90,10 +101,11 @@ class OSMAddressComponents(object):
|
|||||||
config.update(config_updates)
|
config.update(config_updates)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
# Necessary to do this.
|
||||||
for k, v in six.iteritems(properties):
|
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
|
return containing_component
|
||||||
|
|
||||||
for k, v in six.iteritems(properties):
|
for k, v in six.iteritems(properties):
|
||||||
@@ -103,6 +115,12 @@ class OSMAddressComponents(object):
|
|||||||
if containing_component is not None:
|
if containing_component is not None:
|
||||||
return containing_component
|
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
|
return None
|
||||||
|
|
||||||
osm_address_components = OSMAddressComponents()
|
osm_address_components = OSMAddressComponents()
|
||||||
|
|||||||
Reference in New Issue
Block a user