-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from datamade/feature/jfc/import-geographies
Create management command for importing Post shapes
- Loading branch information
Showing
6 changed files
with
127 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import json | ||
|
||
from django.core.management.base import BaseCommand, CommandError | ||
from django.contrib.gis.geos import GEOSGeometry | ||
|
||
from councilmatic_core import models | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Import boundary shapefiles for Post entities" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'geojson_file', | ||
help=( | ||
'The location of the GeoJSON file containing shapes for each Division, ' | ||
'relative to the project root. The file should be formatted as a ' | ||
'GeoJSON FeatureCollection where each Feature A) corresponds to a distinct ' | ||
'Division and B) has a "division_id" attribute in the "properties" object. ' | ||
) | ||
) | ||
|
||
def handle(self, *args, **options): | ||
self.stdout.write('Populating shapes for Posts...') | ||
shapes_populated = 0 | ||
|
||
with open(options['geojson_file']) as shapef: | ||
shapes = json.load(shapef) | ||
|
||
features = self._get_or_raise( | ||
shapes, | ||
'features', | ||
'Could not find the "features" array in the input file.' | ||
) | ||
|
||
for feature in features: | ||
shape = self._get_or_raise( | ||
feature, | ||
'geometry', | ||
'Could not find a "geometry" key in the Feature.' | ||
) | ||
properties = self._get_or_raise( | ||
feature, | ||
'properties', | ||
'Could not find a "properties" key in the Feature.' | ||
) | ||
division_id = self._get_or_raise( | ||
properties, | ||
'division_id', | ||
'Could not find a "division_id" key in the Feature properties.' | ||
) | ||
|
||
models.Post.objects.filter(division_id=division_id).update( | ||
shape=GEOSGeometry(json.dumps(shape)) | ||
) | ||
shapes_populated += 1 | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS( | ||
'Populated {} shapes'.format(str(shapes_populated)) | ||
) | ||
) | ||
|
||
def _get_or_raise(self, dct, key, msg): | ||
""" | ||
Check to see if 'dct' has a key corresponding to 'key', and raise an | ||
error if it doesn't. | ||
""" | ||
format_prompt = ( | ||
'Is the input file formatted as a GeoJSON FeatureCollection ' | ||
'where each feature has a "division_id" property?' | ||
) | ||
if not dct.get(key): | ||
raise CommandError(msg + ' ' + format_prompt) | ||
else: | ||
return dct[key] |
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,31 @@ | ||
# Generated by Django 2.1.9 on 2019-06-26 21:17 | ||
|
||
import django.contrib.gis.db.models.fields | ||
import django.core.files.storage | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0004_auto_20171005_2028'), | ||
('councilmatic_core', '0047_update_filepath'), | ||
] | ||
|
||
operations = [ | ||
migrations.DeleteModel( | ||
name='Post', | ||
), | ||
migrations.CreateModel( | ||
name='Post', | ||
fields=[ | ||
('post', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='councilmatic_post', serialize=False, to='core.Post')), | ||
('shape', django.contrib.gis.db.models.fields.GeometryField(null=True, srid=4326)), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
bases=('core.post',), | ||
), | ||
] |
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
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