Skip to content

Commit

Permalink
Adding new functions to render_graph and basic testing them
Browse files Browse the repository at this point in the history
  • Loading branch information
Renato César committed Apr 6, 2021
1 parent ffd3a6c commit be0f925
Show file tree
Hide file tree
Showing 7 changed files with 77,961 additions and 24 deletions.
56 changes: 43 additions & 13 deletions mapshader/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List
from collections.abc import Iterable

import json
import sys
Expand All @@ -11,7 +12,7 @@

import datashader.transfer_functions as tf
import datashader.reductions as rd
from datashader.colors import rgb
from datashader.colors import rgb, inferno

import xarray as xr

Expand All @@ -35,7 +36,7 @@ def create_agg(source: MapSource,
xmin: float = None, ymin: float = None,
xmax: float = None, ymax: float = None,
x: float = None, y: float = None,
z: float = None,
z: float = None, data: dict = None,
height: int = 256, width: int = 256):

if x is not None and y is not None and z is not None:
Expand Down Expand Up @@ -350,22 +351,46 @@ def load_sources(sources):


def load_geojson(geojson_string):
df = gpd.GeoDataFrame(json.loads(geojson_string))
geojson = json.loads(geojson_string)
df = gpd.GeoDataFrame.from_features(geojson['features'])
return df


def points_to_raster(points_feature_df):
pass
def split_geometry(geodf):
coords = geodf['geometry']
geodf['x'] = np.array(coords.x, dtype='float64')
geodf['y'] = np.array(coords.y, dtype='float64')
return geodf


def calculate_proximity(point_raster_arr):
pass
def create_canvas(xmin: float = None, ymin: float = None,
xmax: float = None, ymax: float = None,
width: int = 1600, height: int = 800):
return ds.Canvas(plot_width=width, plot_height=height,
x_range=(xmin, xmax), y_range=(ymin, ymax))


def proximity(point_raster):
return proximity(point_raster, distance_metric='GREAT_CIRCLE')


def slope(point_raster_arr):
pass


def natural_breaks(point_raster_arr):
point_raster_arr.data[~np.isfinite(point_raster_arr.data)] = 0.0
point_raster_arr = natural_breaks(point_raster_arr, k=5)


def shade(proximity_classifed):
return shade(proximity_classifed, cmap=inferno, alpha=255)


def set_background(image, color):
return set_background(image, color)


def output(value):
return str(value)

Expand All @@ -378,8 +403,13 @@ def debug(value):
functions_map = {
'load_sources': load_sources,
'geojson_to_df': load_geojson,
'point_raster_arr': points_to_raster,
'proximity': calculate_proximity,
'split_geometry': split_geometry,
'create_agg': create_canvas,
'point_aggregation': point_aggregation,
'proximity': proximity,
'natural_breaks': natural_breaks,
'shade': shade,
'set_background': set_background,
'slope': slope,
'output': output,
'debug': debug,
Expand All @@ -406,13 +436,13 @@ def render_graph(graph: dict, process: str = 'output',
for key in graph:
if graph[key][0] == 'load_sources':
# switch mapsources keys to mapsources if available
graph[key] = (graph[key][0], [
graph[key] = (graph[key][0] *[
src for src in available_sources
if src.key in graph[key][1]
if src.key in graph[key][1:]
])
continue

graph[key] = (functions_map[graph[key][0]], graph[key][1])

graph[key] = (functions_map[graph[key][0]], *graph[key][1:])
return multiprocessing.get(graph, process)


Expand Down
12 changes: 8 additions & 4 deletions mapshader/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def flask_to_legend(source: MapSource):
def flask_to_dag(source: List[MapSource],
xmin=-20e6, ymin=-20e6,
xmax=20e6, ymax=20e6):
graph = json.loads(request.args['graph'])
graph = request.json
process = request.args.get('process', 'output')

resp = render_graph(
Expand Down Expand Up @@ -241,14 +241,18 @@ def configure_app(app, user_source_filepath=None, contains=None):
for service in get_services(config_path=user_source_filepath, contains=contains):
view_func = view_func_creators[service.service_type]

if service.service_type == 'dag':
app.add_url_rule(service.service_url,
service.name,
partial(view_func, source=service.source),
methods=['POST', ])
continue

# add operational endpoint
app.add_url_rule(service.service_url,
service.name,
partial(view_func, source=service.source))

if service.service_type == 'dag':
continue

services.append(service)

# add legend endpoint
Expand Down
1 change: 1 addition & 0 deletions mapshader/tests/fixtures/bucees_stores.geojson

Large diffs are not rendered by default.

Loading

0 comments on commit be0f925

Please sign in to comment.