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

Added pre- and post-processors to HoloViews operations #118

Merged
merged 1 commit into from
Jan 8, 2018
Merged
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
54 changes: 53 additions & 1 deletion geoviews/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,66 @@

from cartopy import crs as ccrs
from cartopy.img_transform import warp_array, _determine_bounds
from holoviews.core import Element
from holoviews.core.util import cartesian_product, get_param_values
from holoviews.operation import Operation
from shapely.geometry import Polygon, LineString

from . import element as gv_element
from .element import (Image, Shape, Polygons, Path, Points, Contours,
RGB, Graph, Nodes, EdgePaths, QuadMesh)
RGB, Graph, Nodes, EdgePaths, QuadMesh, _Element)
from .util import project_extents, geom_to_array

geo_ops = []
try:
from holoviews.operation.datashader import (
ResamplingOperation, shade, stack, dynspread)
geo_ops += [ResamplingOperation, shade, stack, dynspread]
except:
pass

from holoviews.operation.element import contours
from holoviews.operation.stats import bivariate_kde

geo_ops += [contours, bivariate_kde]

def convert_to_geotype(element, crs=None):
"""
Converts a HoloViews element type to the equivalent GeoViews
element if given a coordinate reference system.
"""
geotype = getattr(gv_element, type(element).__name__, None)
if crs is None or geotype is None or isinstance(element, _Element):
return element
return geotype(element, crs=crs)


def find_crs(element):
"""
Traverses the supplied object looking for coordinate reference
systems (crs). If multiple clashing reference systems are found
it will throw an error.
"""
crss = element.traverse(lambda x: x.crs, [_Element])
crss = [crs for crs in crss if crs is not None]
if any(crss[0] != crs for crs in crss[1:] if crs is not None):
raise ValueError('Cannot datashade Elements in different '
'coordinate reference systems.')
return {'crs': crss[0] if crss else None}


def add_crs(element, **kwargs):
"""
Converts any elements in the input to their equivalent geotypes
if given a coordinate reference system.
"""
return element.map(lambda x: convert_to_geotype(x, kwargs.get('crs')), Element)


for op in geo_ops:
op._preprocess_hooks = op._preprocess_hooks + [find_crs]
op._postprocess_hooks = op._postprocess_hooks + [add_crs]


class _project_operation(Operation):
"""
Expand Down