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

Combine downstream path queries into one query #8

Merged
merged 18 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pygeoapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ COPY ./pygeoapi-skin-dashboard /skin-dashboard
COPY ./plugin.py /pygeoapi/pygeoapi/plugin.py
COPY ./flask_app.py /pygeoapi/pygeoapi/flask_app.py
COPY ./river_runner.py /pygeoapi/pygeoapi/process/river_runner.py
COPY ./sqlite.py /pygeoapi/pygeoapi/provider/sqlite.py
COPY ./map.html /pygeoapi/pygeoapi/templates/processes/map.html
1 change: 1 addition & 0 deletions pygeoapi/Dockerfile_gcp
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ COPY ./pygeoapi-skin-dashboard /skin-dashboard
COPY ./plugin.py /pygeoapi/pygeoapi/plugin.py
COPY ./flask_app.py /pygeoapi/pygeoapi/flask_app.py
COPY ./river_runner.py /pygeoapi/pygeoapi/process/river_runner.py
COPY ./sqlite.py /pygeoapi/pygeoapi/provider/sqlite.py
COPY ./map.html /pygeoapi/pygeoapi/templates/processes/map.html
4 changes: 3 additions & 1 deletion pygeoapi/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ services:
- ./plugin.py:/pygeoapi/pygeoapi/plugin.py
- ./flask_app.py:/pygeoapi/pygeoapi/flask_app.py
- ./river_runner.py:/pygeoapi/pygeoapi/process/river_runner.py
- ./schemas.opengis.net:/opt/schemas.opengis.net
- ./sqlite.py:/pygeoapi/pygeoapi/provider/sqlite.py
- ./map.html:/pygeoapi/pygeoapi/templates/processes/map.html
- ./schemas.opengis.net:/opt/schemas.opengis.net
8 changes: 5 additions & 3 deletions pygeoapi/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,22 @@

$.ajax({
type:"POST",
url: "{{ config['server']['url'] }}/processes/river-runner/execution",
url: "/processes/river-runner/execution",
data: JSON.stringify(d),
contentType: 'application/json; charset=UTF-8',
processData: false,
success: function(response) {
popup.remove();
loading = false;
var geojson_data = response.value;
if (geojson_data.features.length === 0){
if (response.code !== 'success'){
alert('Server Error: ' + response.description);
} else if (geojson_data.features.length === 0){
alert('No flowpath found. :(')
} else {
var items = new L.GeoJSON(geojson_data, {
onEachFeature: function (feature, layer) {
var url = "{{ config['server']['url'] }}/collections/merit/items/" + feature.id + '?f=html';
var url = "/collections/merit/items/" + feature.id + '?f=html';
var html = '<span><a href="' + url + '">' + feature.properties.nameID + '</a></span>';
layer.bindPopup(html);
}
Expand Down
69 changes: 36 additions & 33 deletions pygeoapi/river_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def execute(self, data):
mimetype = 'application/json'
outputs = {
'id': 'echo',
'code': 'success',
'value': {
'type': 'FeatureCollection',
'features': []
Expand All @@ -159,62 +160,64 @@ def execute(self, data):
if k in ['latlng', 'bbox']:
data[k] = data[k].split(',')

if data.get('bbox', []):
bbox = data.get('bbox')
elif data.get('latlng', ''):
bbox = data.get('latlng')
if data.get('bbox', data.get('latlng')):
bbox = data.get('bbox', data.get('latlng'))
else:
bbox = (data.get('lng'), data.get('lat'))

bbox = bbox * 2 if len(bbox) == 2 else bbox
bbox = self._expand_bbox(bbox)

p = load_plugin('provider', PROVIDER_DEF)
p = load_plugin('provider', PROVIDER_DEF)
value = p.query(bbox=bbox)
i = 1
while len(value['features']) < 1 and i < 3:
if len(value['features']) < 1:
LOGGER.debug(f'No features in bbox {bbox}, expanding')
bbox = self._expand_bbox(bbox, e=0.5*i)
bbox = self._expand_bbox(bbox, e=0.5)
value = p.query(bbox=bbox)
i = i + 1

if len(value['features']) < 1:
LOGGER.debug('No features found')
return mimetype, outputs
if len(value['features']) < 1:
LOGGER.debug('No features found')
return mimetype, outputs

LOGGER.debug('fetching downstream features')
mh = self._compare(value, 'hydroseq', min)
out, trim = [], []
levelpaths = []
for i in (mh[P]['levelpathi'],
*mh[P]['down_levelpaths'].split(',')):
try:
i = int(float(i))
levelpaths.append(str(i))
except ValueError:
LOGGER.error(f'No Downstem Rivers found {i}')
continue
LOGGER.debug(f'No Downstem Rivers found {i}')

down = p.query(
properties=[('levelpathi', i), ], limit=1000
d = p.query(
properties=[('levelpathi', i) for i in levelpaths],
limit=100000, comp='OR'
)

out.extend(down['features'])
m = self._compare(down, 'hydroseq', min)
trim.append((m[P]['dnlevelpat'], m[P]['dnhydroseq']))
mins = {level: {} for level in levelpaths}
for f in d['features']:
key = str(f[P]['levelpathi'])
prev = mins[key].get(P, {}).get('hydroseq', None)

if prev is None or \
min(prev, f[P]['hydroseq']) != prev:
mins[key] = f

trim = [(mh[P]['levelpathi'], mh[P]['hydroseq'])]
for k, v in mins.items():
trim.append((v[P]['dnlevelpat'], v[P]['dnhydroseq']))

LOGGER.debug('keeping only mainstem flowpath')
trim.append((mh[P]['levelpathi'], mh[P]['hydroseq']))
outm = []
for seg in out:
for i in trim:
if seg[P]['levelpathi'] == i[0] and \
seg[P]['hydroseq'] <= i[1]:
outm.append(seg)

value['features'] = outm
outputs = {
'id': 'echo',
'value': value
}
for f in d['features']:
for t in trim:
if f[P]['levelpathi'] == t[0] and \
f[P]['hydroseq'] <= t[1]:
outm.append(f)

value.update({'features': outm})
outputs.update({'value': value})
return mimetype, outputs

def _compare(self, fc, prop, dir):
Expand All @@ -224,7 +227,7 @@ def _compare(self, fc, prop, dir):
val = f
return val

def _expand_bbox(self, bbox, e=0.125):
def _expand_bbox(self, bbox, e=0.25):
return [float(b) + e if i < 2 else float(b) - e
for (i, b) in enumerate(bbox)]

Expand Down
Loading