Skip to content

Commit

Permalink
Merge branch 'release/1.33.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadain committed Feb 3, 2022
2 parents 08ba936 + 5a0d80a commit 4b82f10
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
14 changes: 10 additions & 4 deletions src/mmw/apps/geoprocessing_api/calcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ def stream_data(results, geojson, datasource='nhdhr'):
sql = f'''
WITH stream_intersection AS (
SELECT ST_Length(ST_Transform(
ST_Intersection(geom,
ST_SetSRID(ST_GeomFromGeoJSON(%s),
4326)),
CASE
WHEN ST_CoveredBy(geom,
ST_SetSRID(ST_GeomFromGeoJSON(%s),
4326))
THEN geom
ELSE ST_Intersection(geom,
ST_SetSRID(ST_GeomFromGeoJSON(%s),
4326))
END,
5070)) AS lengthm,
stream_order,
slope
Expand All @@ -82,7 +88,7 @@ def stream_data(results, geojson, datasource='nhdhr'):
'''

with connection.cursor() as cursor:
cursor.execute(sql, [geojson, geojson])
cursor.execute(sql, [geojson, geojson, geojson])

if cursor.rowcount:
columns = [col[0] for col in cursor.description]
Expand Down
21 changes: 19 additions & 2 deletions src/mmw/apps/geoprocessing_api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import requests

from django.conf import settings
from django.core.cache import cache

from mmw.settings import layer_classmaps

Expand Down Expand Up @@ -85,12 +86,28 @@ def start_rwd_job(location, snapping, simplify, data_source):


@shared_task
def analyze_streams(results, area_of_interest, datasource='nhdhr'):
def analyze_streams(results, area_of_interest, datasource='nhdhr', wkaoi=None):
"""
Given geoprocessing results with stream data and an area of interest,
returns the streams and stream order within it.
If a wkaoi is specified and caching is enabled, the results will be
cached and reused.
"""
return {'survey': stream_data(results, area_of_interest, datasource)}
key = None

if wkaoi and settings.GEOP['cache']:
key = f'db_{wkaoi}__{datasource}__stream_data'
cached = cache.get(key)
if cached:
return {'survey': cached}

survey = stream_data(results, area_of_interest, datasource)

if key:
cache.set(key, survey, None)

return {'survey': survey}


@shared_task
Expand Down
2 changes: 1 addition & 1 deletion src/mmw/apps/geoprocessing_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def start_analyze_streams(request, datasource, format=None):
wkaoi,
cache_key=datasource),
nlcd_streams.s(),
tasks.analyze_streams.s(area_of_interest, datasource)
tasks.analyze_streams.s(area_of_interest, datasource, wkaoi)
], area_of_interest, user)


Expand Down
11 changes: 8 additions & 3 deletions src/mmw/apps/modeling/mapshed/calcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,21 @@ def stream_length(geom, datasource='nhdhr'):
sql = f'''
SELECT ROUND(SUM(ST_Length(
ST_Transform(
ST_Intersection(geom,
ST_SetSRID(ST_GeomFromText(%s), 4326)),
CASE
WHEN ST_CoveredBy(geom,
ST_SetSRID(ST_GeomFromText(%s), 4326))
THEN geom
ELSE ST_Intersection(geom,
ST_SetSRID(ST_GeomFromText(%s), 4326))
END,
5070))))
FROM {settings.STREAM_TABLES[datasource]}
WHERE ST_Intersects(geom,
ST_SetSRID(ST_GeomFromText(%s), 4326));
'''

with connection.cursor() as cursor:
cursor.execute(sql, [geom.wkt, geom.wkt])
cursor.execute(sql, [geom.wkt, geom.wkt, geom.wkt])

return cursor.fetchone()[0] or 0 # Aggregate query returns singleton

Expand Down
3 changes: 2 additions & 1 deletion src/mmw/apps/modeling/mapshed/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from copy import deepcopy
from celery import shared_task
from django.conf import settings

Expand Down Expand Up @@ -50,7 +51,7 @@ def collect_data(geop_results, geojson, watershed_id=None, weather=None,
area = geom.transform(5070, clone=True).area # Square Meters

# Data Model is called z by convention
z = settings.GWLFE_DEFAULTS.copy()
z = deepcopy(settings.GWLFE_DEFAULTS)

z['watershed_id'] = watershed_id

Expand Down
3 changes: 2 additions & 1 deletion src/mmw/apps/user/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import rollbar

from copy import deepcopy
from uuid import uuid1

from django.contrib.auth import (authenticate,
Expand Down Expand Up @@ -110,7 +111,7 @@ def make_successful_response_data(user):
@decorators.api_view(['POST'])
@decorators.permission_classes((IsAuthenticated, ))
def profile(request):
data = request.data.copy()
data = deepcopy(request.data)
if 'was_skipped' in data:
data['was_skipped'] = str(data['was_skipped']).lower() == 'true'
data['is_complete'] = not data['was_skipped']
Expand Down

0 comments on commit 4b82f10

Please sign in to comment.