Skip to content

Commit

Permalink
Adding documentationm function to load sources and load geojson to df
Browse files Browse the repository at this point in the history
  • Loading branch information
Renato César committed Mar 28, 2021
1 parent 97000a7 commit ded2e74
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
30 changes: 20 additions & 10 deletions mapshader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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,
Expand All @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions mapshader/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions mapshader/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,6 +92,7 @@ def test_render_graph():


@pytest.mark.parametrize('graph_key', [
'load_sources',
'geojson_to_df',
'proximity',
'slope',
Expand All @@ -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)
18 changes: 18 additions & 0 deletions mapshader/tests/test_flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ded2e74

Please sign in to comment.