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

Import shapes with new management command #256

Merged
merged 5 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ There are tens of thousands of bills and hundreds of events in Chicago legislati
pupa update chicago bills window=0 && pupa update chicago events window=0
```

Finally, make sure to load shapes into the database so that Posts can be associated with their corresponding boundaries:

```bash
python manage.py import_shapes data/final/shapes/chicago_shapes.json
```

## Setup CSS, Images, and other static resources
```bash
python manage.py collectstatic
Expand Down
8 changes: 8 additions & 0 deletions data/Makefile
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 > $@
1 change: 1 addition & 0 deletions data/final/shapes/chicago_shapes.json

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions data/scripts/extract_shapes.py
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.
Copy link
Contributor Author

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.

Copy link
Member

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)

"""
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)