From 3d0d38f8da5dac18f53fbc8df7cd87ce12e544f4 Mon Sep 17 00:00:00 2001 From: Al Date: Wed, 18 May 2016 18:09:26 -0400 Subject: [PATCH] [osm] Adding OSM definitions to quickly determine if a set of properties meets one of the libpostal definitions (valid amenity, etc.) --- scripts/geodata/osm/definitions.py | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 scripts/geodata/osm/definitions.py diff --git a/scripts/geodata/osm/definitions.py b/scripts/geodata/osm/definitions.py new file mode 100644 index 00000000..6899f7a0 --- /dev/null +++ b/scripts/geodata/osm/definitions.py @@ -0,0 +1,61 @@ +import os +import re +import six + +from collections import defaultdict + +this_dir = os.path.realpath(os.path.dirname(__file__)) + +DEFAULT_SCRIPT_PATH = os.path.join(this_dir, 'fetch_osm_address_data.sh') + +valid_key_regex = re.compile('VALID_(.*?)_KEYS="(.*)"') +kv_regex = re.compile('([^\s]*)=([^\s]*)') + + +class OSMDefinitions(object): + ALL = '*' + + ADMIN_BORDER = 'admin_border' + AEROWAY = 'aeroway' + AMENITY = 'amenity' + BUILDING = 'building' + HISTORIC = 'historic' + LANDUSE = 'landuse' + NATURAL = 'natural' + NEIGHBORHOOD = 'neighborhood' + OFFICE = 'office' + PLACE = 'place' + SHOP = 'shop' + TOURISM = 'tourism' + WATERWAY = 'waterway' + + def __init__(self, filename=DEFAULT_SCRIPT_PATH): + script = open(filename).read() + + definitions = {} + + for definition, text in valid_key_regex.findall(script): + definition = definition.lower() + + kvs = defaultdict(set) + + for k, v in kv_regex.findall(text): + if v != '': + kvs[k].add(v) + else: + kvs[k].add(self.ALL) + + definitions[definition] = kvs + + self.definitions = definitions + + def meets_definition(self, props, category): + defs = self.definitions.get(category, {}) + if not defs: + return False + for k, v in six.iteritems(props): + if v in defs.get(k, set()): + return True + return False + +osm_definitions = OSMDefinitions()