Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject wikidata from RAWR tile into OSM features. #377

Merged
merged 1 commit into from
May 2, 2019
Merged
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
32 changes: 30 additions & 2 deletions tilequeue/query/rawr.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,19 @@ class _LayersIndex(object):
pyramid.
"""

def __init__(self, layers, tile_pyramid):
def __init__(self, layers, tile_pyramid, wikidata):
self.layers = layers
self.tile_pyramid = tile_pyramid
self.tile_index = defaultdict(list)
self.delayed_features = []
self.wikidata = wikidata

def add_row(self, fid, shape_wkb, props):
# extend props with wikidata, if there's a wikidata ID
wd_id = props.get('wikidata')
if wd_id:
props.update(self.wikidata(wd_id))

shape = _LazyShape(shape_wkb)
# single object (hence single id()) will be shared amongst all layers.
# this allows us to easily and quickly de-duplicate at later layers in
Expand Down Expand Up @@ -513,12 +519,34 @@ def __call__(self, tile):
return self.tile_index.get(tile, [])


class WikidataIndex(object):
"""
Indexes a RAWR tile's wikidata data for lookup by ID.
"""

def __init__(self, rows):
data = {}
for wd_id, props in rows:
data[wd_id] = props

self.data = data

def __call__(self, wd_id):
return self.data.get(wd_id, {})


def osm_index(layers, tables, tile_pyramid):
from raw_tiles.index.index import index_table

table_indexes = defaultdict(list)

index = _LayersIndex(layers, tile_pyramid)
# try to get wikidata, but use empty wikidata if it couldn't be found.
try:
wikidata = WikidataIndex(tables('wikidata').rows)
except Exception:
wikidata = WikidataIndex([])

index = _LayersIndex(layers, tile_pyramid, wikidata)
for shape_type in ('point', 'line', 'polygon'):
table_name = 'planet_osm_' + shape_type
table_indexes[table_name].append(index)
Expand Down