-
Notifications
You must be signed in to change notification settings - Fork 16
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
Import shapes with new management command #256
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9cbe0c4
Extract Post shapes from the OCD API
5b661cc
Update README with instructions for import_shapes command
4f27bcc
Store and load Post.shapes as GeoJSON
fc689e1
Extract boundaries file from Chicago open data portal instead of OCD API
bb33a1e
Remove deprecated extract_shapes script
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.PHONY: all clean | ||
all: final/shapes/chicago_shapes.json | ||
clean: | ||
rm -Rf final/shapes/chicago_shapes.json | ||
|
||
final/shapes/chicago_shapes.json: | ||
mkdir -p $(dir $@) | ||
python3 scripts/extract_shapes.py > $@ |
Large diffs are not rendered by default.
Oops, something went wrong.
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,45 @@ | ||
import json | ||
import sys | ||
|
||
import requests | ||
|
||
BOUNDARY_SET = ['chicago-wards-2015'] | ||
BASE_URL = 'https://ocd.datamade.us' | ||
|
||
session = requests.Session() | ||
|
||
|
||
def get_response(url, params=None, timeout=60, **kwargs): | ||
""" | ||
The OCD API has intermittently thrown 502 and 504 errors, so only proceed | ||
when receiving an 'ok' status. | ||
""" | ||
response = session.get(url, params=params, timeout=timeout, **kwargs) | ||
|
||
if response.ok: | ||
return response | ||
else: | ||
message = '{url} returned a bad response - {status}'.format( | ||
url=url, | ||
status=response.status_code | ||
) | ||
raise requests.exceptions.HTTPError('ERROR: {0}'.format(message)) | ||
|
||
|
||
if __name__ == '__main__': | ||
""" | ||
Extract shapes for each OCD Division entity from the OCD API. | ||
""" | ||
shapes = {} | ||
for boundary in BOUNDARY_SET: | ||
bndry_set_url = BASE_URL + '/boundaries/' + boundary | ||
|
||
page_res = get_response(bndry_set_url + '/?limit=0') | ||
page_json = json.loads(page_res.text) | ||
|
||
for bndry_json in page_json['objects']: | ||
shape_url = BASE_URL + bndry_json['url'] + 'shape' | ||
shape_res = get_response(shape_url) | ||
shapes[bndry_json['external_id']] = json.loads(shape_res.text) | ||
|
||
json.dump(shapes, sys.stdout) |
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.
I adapted this function from https://github.com/datamade/django-councilmatic/blob/5387ae85ea289b22c4e0ea566fa89a1c43155ed0/councilmatic_core/management/commands/import_data.py#L3250-L3261, including the docstring. I'm not actually sure whether the docstring accurately represents what's going on in the function -- it seems to me that the method is simply a thin wrapper around
requests
to make sure that it A) uses a session and B) raises errors from non-200 status codes -- but the docstring seems to perhaps be communicating an aspect of the OCD API that is useful to know.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.
I think we don't want this. Let's just get https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Wards-2015-/sp34-6z76 and get into the right shape (see discussion in the other PR)