-
Notifications
You must be signed in to change notification settings - Fork 120
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
Update WOF import pipeline. #1868
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
bucket: tilezen-assets | ||
datestamp: 20190326 | ||
datestamp: 20190412 | ||
|
||
shapefiles: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
from os.path import basename | ||
from os.path import splitext | ||
from os.path import join as path_join | ||
from tilequeue.wof import Neighbourhood | ||
from tilequeue.wof import NeighbourhoodFailure | ||
from tilequeue.wof import NeighbourhoodMeta | ||
from tilequeue.wof import create_neighbourhood_from_json | ||
from tilequeue.wof import write_neighbourhood_data_to_file | ||
import json | ||
import tarfile | ||
import requests | ||
from tqdm import tqdm | ||
|
||
|
||
def _parse_wof_id(s): | ||
wof_id, ext = splitext(basename(s)) | ||
assert ext == '.geojson' | ||
return int(wof_id) | ||
|
||
|
||
def _parse_neighbourhood(file_name, data, placetype, file_hash): | ||
wof_id = _parse_wof_id(file_name) | ||
meta = NeighbourhoodMeta(wof_id, placetype, None, file_hash, None) | ||
json_data = json.loads(data) | ||
n = create_neighbourhood_from_json(json_data, meta) | ||
return n | ||
|
||
|
||
class WOFArchiveReader(object): | ||
""" | ||
Collects WOF parsed data items (mostly neighbourhoods) from a series of | ||
tar.gz "bundles" as distributed by WOF. | ||
""" | ||
|
||
def __init__(self): | ||
self.wof_items = [] | ||
|
||
def add_archive(self, archive, file_hash, count): | ||
""" | ||
Adds the GeoJSON files in the tar.gz archive to the list of wof_items. | ||
|
||
Displays a progress bar, with count being the expected number of items | ||
in the tar.gz. | ||
""" | ||
|
||
with tqdm(total=count) as pbar: | ||
with tarfile.open(archive) as tar: | ||
for info in tar: | ||
if info.isfile() and info.name.endswith('.geojson'): | ||
self._parse_file( | ||
info.name, tar.extractfile(info).read(), file_hash) | ||
pbar.update(1) | ||
|
||
def _parse_file(self, file_name, data, file_hash): | ||
n_or_fail = _parse_neighbourhood(file_name, data, placetype, file_hash) | ||
if isinstance(n_or_fail, Neighbourhood): | ||
self.wof_items.append(n_or_fail) | ||
elif isinstance(n_or_fail, NeighbourhoodFailure): | ||
if n_or_fail.skipped or n_or_fail.funky or n_or_fail.superseded: | ||
pass | ||
else: | ||
raise ValueError("Failed to parse neighbourhood: %s " | ||
"(because: %s)" | ||
% (n_or_fail.message, n_or_fail.reason)) | ||
else: | ||
raise ValueError("Unexpected %r" % (n_or_fail,)) | ||
|
||
|
||
class tmpdownload(object): | ||
""" | ||
Downloads a file to a temporary location and yields its absolute path. Once | ||
the scope exits, deletes the temporary file. | ||
""" | ||
|
||
def __init__(self, url, expected_size): | ||
import tempfile | ||
self.tempdir = tempfile.mkdtemp() | ||
|
||
fname = url.split('/')[-1] | ||
abs_fname = path_join(self.tempdir, fname) | ||
|
||
# see https://stackoverflow.com/questions/16694907/#16696317 | ||
with requests.get(url, stream=True) as response: | ||
response.raise_for_status() | ||
|
||
with tqdm(total=expected_size) as pbar: | ||
with open(abs_fname, 'wb') as fh: | ||
for chunk in response.iter_content(chunk_size=16384): | ||
if chunk: | ||
fh.write(chunk) | ||
pbar.update(len(chunk)) | ||
|
||
self.abs_fname = abs_fname | ||
|
||
def __enter__(self): | ||
return self.abs_fname | ||
|
||
def __exit__(self, type, value, traceback): | ||
import shutil | ||
shutil.rmtree(self.tempdir) | ||
|
||
|
||
WOF_INVENTORY = 'https://dist.whosonfirst.org/bundles/inventory.json' | ||
WOF_BUNDLE_PREFIX = 'https://dist.whosonfirst.org/bundles/' | ||
|
||
|
||
if __name__ == '__main__': | ||
inventory = requests.get(WOF_INVENTORY).json() | ||
reader = WOFArchiveReader() | ||
|
||
for placetype in ('neighbourhood', 'macrohood', 'microhood', 'borough'): | ||
fname = 'whosonfirst-data-%s-latest.tar.bz2' % (placetype,) | ||
|
||
matching = [item for item in inventory | ||
if item['name_compressed'] == fname] | ||
assert len(matching) == 1 | ||
item = matching[0] | ||
|
||
version = item['last_updated'] | ||
count = item['count'] | ||
download_size = item['size_compressed'] | ||
|
||
print "Downloading %r with %d entries" % (placetype, count) | ||
with tmpdownload(WOF_BUNDLE_PREFIX + fname, download_size) as fname: | ||
print "Parsing WOF data" | ||
reader.add_archive(fname, version, count) | ||
|
||
print "Writing output SQL" | ||
with open('wof_snapshot.sql', 'w') as fh: | ||
fh.write("COPY public.wof_neighbourhood (" | ||
"wof_id, placetype, name, hash, n_photos, area, min_zoom, " | ||
"max_zoom, is_landuse_aoi, label_position, geometry, " | ||
"inception, cessation, is_visible, l10n_name) FROM stdin;\n") | ||
write_neighbourhood_data_to_file(fh, reader.wof_items) | ||
fh.write("\\.\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOTE: the
$^
line noise means everything after the colon on the rule line, in this case thetgt_shapefile_zips
and thewof_snapshot.sql
.