From ded2e74be9d2eed52df88dd9b14cc84fb3fa4607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renato=20C=C3=A9sar?= Date: Sun, 28 Mar 2021 02:08:10 -0300 Subject: [PATCH] Adding documentationm function to load sources and load geojson to df --- mapshader/core.py | 30 ++++++++++++++++++++---------- mapshader/flask_app.py | 4 ++++ mapshader/tests/test_core.py | 11 +++++++++++ mapshader/tests/test_flask_app.py | 18 ++++++++++++++++++ 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/mapshader/core.py b/mapshader/core.py index 6108c1f..3d57496 100644 --- a/mapshader/core.py +++ b/mapshader/core.py @@ -342,20 +342,16 @@ def render_map(source: MapSource, def load_sources(sources): for src in sources: - pass + if not src.is_loaded: + src.load() -def load_geojson(graph, point_geojson_string): - # df = geopandas.GeoDataFrame(json.loads(point_geojson_string)) - # return df - pass +def load_geojson(geojson_string): + df = gpd.GeoDataFrame(json.loads(geojson_string)) + return df def points_to_raster(points_feature_df): - # make datashader canvas - # rasterize points - # return xr.DataArray - # return xr.DataArray(...) pass @@ -375,6 +371,7 @@ def debug(value): return value +# Map from keys to functions available on graph functions_map = { 'load_sources': load_sources, 'geojson_to_df': load_geojson, @@ -386,9 +383,22 @@ def debug(value): } -def render_graph(graph: dict, process: str, +def render_graph(graph: dict, process: str = 'output', xmin: float = None, ymin: float = None, xmax: float = None, ymax: float = None): + """Return process result for given graph + + Parameters + ---------- + graph : dict + The graph to be processed + process : str + The process output (default is output) + xmin : float + ymin : float + xmax : float + ymax : float + """ return multiprocessing.get(graph, process) diff --git a/mapshader/flask_app.py b/mapshader/flask_app.py index 8f11688..61a16ba 100644 --- a/mapshader/flask_app.py +++ b/mapshader/flask_app.py @@ -101,6 +101,10 @@ def flask_to_dag(source: List[MapSource]): process = request.args.get('process', 'output') for key, value in graph.items(): + if value[0] == 'load_sources': + graph[key] = (functions_map[value[0]], [ + src for src in source if source.key in value[1] + ]) graph[key] = (functions_map[value[0]], value[1]) resp = render_graph( diff --git a/mapshader/tests/test_core.py b/mapshader/tests/test_core.py index 8ffe110..ef303f4 100644 --- a/mapshader/tests/test_core.py +++ b/mapshader/tests/test_core.py @@ -7,10 +7,12 @@ import numpy as np import xarray as xr +import geopandas as gpd from datashader.transfer_functions import Image from mapshader.sources import MapSource +from mapshader.core import load_geojson from mapshader.core import render_graph from mapshader.core import render_geojson from mapshader.core import render_map @@ -90,6 +92,7 @@ def test_render_graph(): @pytest.mark.parametrize('graph_key', [ + 'load_sources', 'geojson_to_df', 'proximity', 'slope', @@ -98,3 +101,11 @@ def test_render_graph(): ]) def test_valid_graph_keys(graph_key): assert graph_key in functions_map + + +def test_load_geojson(): + geojson = """ + {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102.0,0.5]},"properties":{"prop0":"value0"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[102.0,0.0],[103.0,1.0],[104.0,0.0],[105.0,1.0]]},"properties":{"prop0":"value0","prop1":0.0}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]},"properties":{"prop0":"value0","prop1":{"this":"that"}}}]} + """ + result = load_geojson(geojson) + assert isinstance(result, gpd.GeoDataFrame) diff --git a/mapshader/tests/test_flask_app.py b/mapshader/tests/test_flask_app.py index 63c8910..87227d2 100644 --- a/mapshader/tests/test_flask_app.py +++ b/mapshader/tests/test_flask_app.py @@ -62,3 +62,21 @@ def test_site_index(): def test_geoprocessing_service(service): resp = CLIENT.get(service.default_url) assert resp.status_code == 200 + + +@pytest.mark.parametrize("service", [s for s in DEFAULT_SERVICES if s.service_type == 'dag']) +def test_geoprocessing_service_load_sources(service): + first_two = service.sources[:2] + sources_key = [source.key for source in first_two] + + data = { + 'graph': { + 'load_srcs': ('load_sources', sources_key), + } + } + assert not first_two[0].is_loaded + assert not first_two[1].is_loaded + resp = CLIENT.get(service.default_url, data) + assert resp.status_code == 200 + assert first_two[0].is_loaded + assert first_two[1].is_loaded