Skip to content

Commit

Permalink
fixup: parse_input refactor
Browse files Browse the repository at this point in the history
fixup for 00f07f3
  • Loading branch information
Alice Rottersman committed Aug 31, 2017
1 parent 79478b2 commit 0db1f1b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 50 deletions.
33 changes: 11 additions & 22 deletions src/mmw/apps/geoprocessing_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from apps.core.models import Job
from apps.core.tasks import save_job_error, save_job_result
from apps.modeling import geoprocessing
from apps.modeling.views import parse_area_of_interest, parse_wkaoi
from apps.modeling.views import load_area_of_interest
from apps.geoprocessing_api import tasks


Expand Down Expand Up @@ -57,8 +57,8 @@ def start_rwd(request, format=None):
def start_analyze_land(request, format=None):
user = request.user if request.user.is_authenticated() else None

wkaoi_str = request.query_params.get('wkaoi', None)
area_of_interest, wkaoi = parse_input(request.data, wkaoi_str)
wkaoi = request.query_params.get('wkaoi', None)
area_of_interest = load_area_of_interest(request.data, wkaoi)

geop_input = {'polygon': [area_of_interest]}

Expand All @@ -73,8 +73,8 @@ def start_analyze_land(request, format=None):
def start_analyze_soil(request, format=None):
user = request.user if request.user.is_authenticated() else None

wkaoi_str = request.query_params.get('wkaoi', None)
area_of_interest, wkaoi = parse_input(request.data, wkaoi_str)
wkaoi = request.query_params.get('wkaoi', None)
area_of_interest = load_area_of_interest(request.data, wkaoi)

geop_input = {'polygon': [area_of_interest]}

Expand All @@ -89,8 +89,8 @@ def start_analyze_soil(request, format=None):
def start_analyze_animals(request, format=None):
user = request.user if request.user.is_authenticated() else None

wkaoi_str = request.query_params.get('wkaoi', None)
area_of_interest, __ = parse_input(request.data, wkaoi_str)
wkaoi = request.query_params.get('wkaoi', None)
area_of_interest = load_area_of_interest(request.data, wkaoi)

return start_celery_job([
tasks.analyze_animals.s(area_of_interest)
Expand All @@ -102,8 +102,8 @@ def start_analyze_animals(request, format=None):
def start_analyze_pointsource(request, format=None):
user = request.user if request.user.is_authenticated() else None

wkaoi_str = request.query_params.get('wkaoi', None)
area_of_interest, __ = parse_input(request.data, wkaoi_str)
wkaoi = request.query_params.get('wkaoi', None)
area_of_interest = load_area_of_interest(request.data, wkaoi)

return start_celery_job([
tasks.analyze_pointsource.s(area_of_interest)
Expand All @@ -115,8 +115,8 @@ def start_analyze_pointsource(request, format=None):
def start_analyze_catchment_water_quality(request, format=None):
user = request.user if request.user.is_authenticated() else None

wkaoi_str = request.query_params.get('wkaoi', None)
area_of_interest, __ = parse_input(request.data, wkaoi_str)
wkaoi = request.query_params.get('wkaoi', None)
area_of_interest = load_area_of_interest(request.data, wkaoi)

return start_celery_job([
tasks.analyze_catchment_water_quality.s(area_of_interest)
Expand Down Expand Up @@ -164,14 +164,3 @@ def start_celery_job(task_list, job_input, user=None):
},
headers={'Location': reverse('get_job', args=[task_chain.id])}
)


def parse_input(aoi_json=None, wkaoi_str=None):
if (aoi_json):
return parse_area_of_interest(aoi_json), None

if (wkaoi_str):
return parse_wkaoi(wkaoi_str)

raise ParseError(detail='Must supply either a GeoJSON request body, ' +
'or a wkaoi url parameter.')
82 changes: 54 additions & 28 deletions src/mmw/apps/modeling/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def export_gms(request, format=None):
@decorators.api_view(['POST'])
@decorators.permission_classes((AllowAny, ))
def start_tr55(request, format=None):

user = request.user if request.user.is_authenticated() else None
created = now()

Expand Down Expand Up @@ -281,7 +282,8 @@ def _construct_tr55_job_chain(model_input, job_id):

job_chain = []

aoi, wkaoi = parse_input(model_input, geojson=False)
aoi_json_str, wkaoi = parse_input(model_input)
aoi = json.loads(aoi_json_str)
aoi_census = model_input.get('aoi_census')
modification_censuses = model_input.get('modification_censuses')
# Non-overlapping polygons derived from the modifications
Expand Down Expand Up @@ -541,29 +543,61 @@ def get_layer_shape(table_code, id):
return None


def parse_area_of_interest(area_of_interest, geojson=True):
# Area of Interest is expected to be GeoJSON in 4326
def parse_area_of_interest(area_of_interest):
"""
Returns a geojson string of the provided area of interest, formatted
as a one-ring multipolygon if necessary.
Args:
area_of_interest (dict): valid geojson. If MultiPolygon, can only have a
single ring.
"""
try:
shape = geoprocessing.to_one_ring_multipolygon(area_of_interest)
except:
raise ParseError(detail='Area of interest must be valid GeoJSON')

return json.dumps(shape) if geojson else shape
return json.dumps(shape)


def load_wkaoi(wkaoi):
"""
Returns a geojson string of a wellknown AoI's shape
def parse_wkaoi(wkaoi, geojson=True):
# WKAoI is expected to be in the format '{table}__{id}'
Args:
wkaoi (string): '{table}__{id}', where table is the table to look up the
wellknown AOIs shape in, and id is the shape's id
"""
table, id = wkaoi.split('__')
shape = get_layer_shape(table, id)

if shape:
result = shape if geojson else json.loads(shape)
return result, wkaoi
else:
if not shape:
raise ParseError(detail='Invalid wkaoi: {}'.format(wkaoi))

return shape

def parse_input(model_input, geojson=True):

def load_area_of_interest(aoi_geojson=None, wkaoi=None):
"""
Returns a geojson string of the area of interest, either loaded from the
wkaoi, or processed from the aoi_geojson
Args:
aoi_geojson (dict): valid GeoJSON. If MultiPolygon can only have a single
ring. No re-projection, expects EPSG: 4326
wkaoi (string): '{table}__{id}'
"""
if (aoi_geojson):
return parse_area_of_interest(aoi_geojson)

if (wkaoi):
return load_wkaoi(wkaoi)

raise ParseError(detail='Must supply either the area of interest (GeoJSON), ' +
'or a WKAoI ID.')


def parse_input(model_input):
"""
Parse input into tuple of AoI JSON and WKAoI id.
Expand All @@ -572,24 +606,16 @@ def parse_input(model_input, geojson=True):
from the appropriate database, and returned with the value of 'wkaoi' as
the WKAoI.
If the geojson parameter is set to False, the area of interest is returned
as a dict instead of a JSON string.
Args:
model_input: a dictionary, only one of the keys is necessary
{
'area_of_interest': { <geojson dict> }
'wkaoi': '{table}__{id}',
}
"""
if not model_input:
raise ParseError(detail='model_input cannot be empty')

if isinstance(model_input, basestring):
# Input is string, assumed JSON. Convert to dict before proceeding
model_input = json.loads(model_input)

if 'area_of_interest' in model_input:
result = parse_area_of_interest(model_input['area_of_interest'],
geojson)
return result, None

if 'wkaoi' in model_input:
return parse_wkaoi(model_input['wkaoi'], geojson)

# If neither 'area_of_interest' nor 'wkaoi' were found, report
raise ParseError(detail='model_input must have either ' +
'area_of_interest or wkaoi')
wkaoi = model_input.get('wkaoi', None)
return load_area_of_interest(model_input.get('area_of_interest', None),
wkaoi), wkaoi

0 comments on commit 0db1f1b

Please sign in to comment.