Skip to content

Commit

Permalink
Inject wikidata from RAWR tile into OSM features.
Browse files Browse the repository at this point in the history
  • Loading branch information
zerebubuth committed May 2, 2019
1 parent d7b9484 commit 4c1bb68
Showing 1 changed file with 30 additions and 2 deletions.
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

0 comments on commit 4c1bb68

Please sign in to comment.