Skip to content

Commit

Permalink
Add VectorField project operation
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Feb 10, 2019
1 parent 56784cd commit 3672a37
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
43 changes: 41 additions & 2 deletions geoviews/operation/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def _process_element(self, element):

class project_points(_project_operation):

supported_types = [Points, Nodes, VectorField, HexTiles, Labels]
supported_types = [Points, Nodes, HexTiles, Labels]

def _process_element(self, element):
if not len(element):
Expand All @@ -181,6 +181,44 @@ def _process_element(self, element):
crs=self.p.projection, datatype=datatype)


class project_vectorfield(_project_operation):

supported_types = [VectorField]

def _process_element(self, element):
if not len(element):
return element.clone(crs=self.p.projection)
xdim, ydim, adim, mdim = element.dimensions()[:4]
xs, ys, ang, ms = (element.dimension_values(i) for i in range(4))
coordinates = self.p.projection.transform_points(element.crs, xs, ys)
mask = np.isfinite(coordinates[:, 0])
new_data = {k: v[mask] for k, v in element.columns().items()}
new_data[xdim.name] = coordinates[mask, 0]
new_data[ydim.name] = coordinates[mask, 1]
datatype = [element.interface.datatype]+element.datatype
us = np.sin(ang) * ms
vs = np.cos(ang) * ms
ut, vt = self.p.projection.transform_vectors(element.crs, xs, ys, us, vs)
with np.errstate(divide='ignore', invalid='ignore'):
angle = np.arctan2(vt, ut)
mag = np.sqrt(ut**2+vt**2)

new_data[adim.name] = angle[mask]
new_data[mdim.name] = mag[mask]

if len(new_data[xdim.name]) == 0:
self.warning('While projecting a %s element from a %s coordinate '
'reference system (crs) to a %s projection none of '
'the projected paths were contained within the bounds '
'specified by the projection. Ensure you have specified '
'the correct coordinate system for your data.' %
(type(element).__name__, type(element.crs).__name__,
type(self.p.projection).__name__))

return element.clone(tuple(new_data[d.name] for d in element.dimensions()),
crs=self.p.projection, datatype=datatype)



class project_graph(_project_operation):

Expand Down Expand Up @@ -392,7 +430,8 @@ class project(Operation):
Projection the image type is projected to.""")

_operations = [project_path, project_image, project_shape,
project_graph, project_quadmesh, project_points]
project_graph, project_quadmesh, project_vectorfield,
project_points]

def _process(self, element, key=None):
for op in self._operations:
Expand Down
4 changes: 2 additions & 2 deletions geoviews/plotting/bokeh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Graph, TriMesh, QuadMesh, VectorField, Labels,
HexTiles, LineContours, FilledContours)
from ...operation import (project_image, project_points, project_path,
project_graph, project_quadmesh)
project_graph, project_quadmesh, project_vectorfield)
from ...tile_sources import _ATTRIBUTIONS
from ...util import poly_types, line_types
from .plot import GeoPlot, GeoOverlayPlot
Expand Down Expand Up @@ -91,7 +91,7 @@ class GeoPointPlot(GeoPlot, PointPlot):

class GeoVectorFieldPlot(GeoPlot, VectorFieldPlot):

_project_operation = project_points
_project_operation = project_vectorfield


class GeoQuadMeshPlot(GeoPlot, QuadMeshPlot):
Expand Down
16 changes: 9 additions & 7 deletions geoviews/plotting/mpl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
from holoviews.plotting.mpl.util import get_raster_array


from ...element import (Image, Points, Feature, WMTS, Tiles, Text,
LineContours, FilledContours, is_geographic,
Path, Polygons, Shape, RGB, Contours, Nodes,
EdgePaths, Graph, TriMesh, QuadMesh, VectorField,
HexTiles, Labels)
from ...element import (
Image, Points, Feature, WMTS, Tiles, Text, LineContours,
FilledContours, is_geographic, Path, Polygons, Shape, RGB,
Contours, Nodes, EdgePaths, Graph, TriMesh, QuadMesh, VectorField,
HexTiles, Labels)
from ...util import geo_mesh
from ..plot import ProjectionPlot

from ...operation import project_points, project_path, project_graph, project_quadmesh
from ...operation import (
project_points, project_path, project_graph, project_quadmesh,
project_vectorfield)



Expand Down Expand Up @@ -238,7 +240,7 @@ class GeoVectorFieldPlot(GeoPlot, VectorFieldPlot):

apply_ranges = param.Boolean(default=True)

_project_operation = project_points
_project_operation = project_vectorfield


class GeometryPlot(GeoPlot):
Expand Down

0 comments on commit 3672a37

Please sign in to comment.