Skip to content

Commit

Permalink
Port custom tools, streams and links from EarthSim (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Jan 4, 2020
1 parent ef14244 commit f19cb0d
Show file tree
Hide file tree
Showing 30 changed files with 1,436 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
install:
- doit env_create $CHANS_DEV --python=$PYTHON_VERSION
- source activate test-environment
- doit develop_install $CHANS_DEV -o recommended
- doit develop_install $CHANS_DEV -o recommended -o build
- doit env_capture
- bokeh sampledata
script:
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
include LICENSE
include README.md
include geoviews/.version
include geoviews/models/*.ts
include geoviews/icons/*.png

graft examples
global-exclude *.py[co]
Expand Down
11 changes: 6 additions & 5 deletions geoviews/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
except:
pass

from .element import (_Element, Feature, Tiles, # noqa (API import)
WMTS, LineContours, FilledContours, Text, Image,
Points, Path, Polygons, Shape, Dataset, RGB,
Contours, Graph, TriMesh, Nodes, EdgePaths,
QuadMesh, VectorField, HexTiles, Labels)
from .element import ( # noqa (API import)
_Element, Feature, Tiles, WMTS, LineContours, FilledContours,
Text, Image, Points, Path, Polygons, Shape, Dataset, RGB,
Contours, Graph, TriMesh, Nodes, EdgePaths, QuadMesh, VectorField,
HexTiles, Labels, Rectangles, Segments
)
from .util import load_tiff, from_xarray # noqa (API import)
from .operation import project # noqa (API import)
from . import data # noqa (API import)
Expand Down
82 changes: 82 additions & 0 deletions geoviews/annotators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import param

import cartopy.crs as ccrs

from holoviews.annotators import (
Annotator, PathAnnotator, PolyAnnotator, PointAnnotator, BoxAnnotator # noqa
)
from holoviews.plotting.links import DataLink, VertexTableLink as hvVertexTableLink
from panel.util import param_name

from .models.custom_tools import CheckpointTool, RestoreTool, ClearTool
from .links import VertexTableLink, PointTableLink, HvRectanglesTableLink, RectanglesTableLink
from .operation import project
from .streams import PolyVertexDraw, PolyVertexEdit

Annotator._tools = [CheckpointTool, RestoreTool, ClearTool]
Annotator.table_transforms.append(project.instance(projection=ccrs.PlateCarree()))

def get_point_table_link(self, source, target):
if hasattr(source.callback.inputs[0], 'crs'):
return PointTableLink(source, target)
else:
return DataLink(source, target)

PointAnnotator._link_type = get_point_table_link

def get_rectangles_table_link(self, source, target):
if hasattr(source.callback.inputs[0], 'crs'):
return RectanglesTableLink(source, target)
else:
return HvRectanglesTableLink(source, target)

BoxAnnotator._link_type = get_rectangles_table_link

def get_vertex_table_link(self, source, target):
if hasattr(source.callback.inputs[0], 'crs'):
return VertexTableLink(source, target)
else:
return hvVertexTableLink(source, target)

PathAnnotator._vertex_table_link = get_vertex_table_link
PolyAnnotator._vertex_table_link = get_vertex_table_link

def initialize_tools(plot, element):
"""
Initializes the Checkpoint and Restore tools.
"""
cds = plot.handles['source']
checkpoint = plot.state.select(type=CheckpointTool)
restore = plot.state.select(type=RestoreTool)
clear = plot.state.select(type=ClearTool)
if checkpoint:
checkpoint[0].sources.append(cds)
if restore:
restore[0].sources.append(cds)
if clear:
clear[0].sources.append(cds)

Annotator._extra_opts['hooks'] = [initialize_tools]


class PathBreakingAnnotator(PathAnnotator):

feature_style = param.Dict(default={'fill_color': 'blue', 'size': 10}, doc="""
Styling to apply to the feature vertices.""")

node_style = param.Dict(default={'fill_color': 'indianred', 'size': 6}, doc="""
Styling to apply to the node vertices.""")

def _init_stream(self):
name = param_name(self.name)
style_kwargs = dict(node_style=self.node_style, feature_style=self.feature_style)
self._stream = PolyVertexDraw(
source=self.plot, data={}, num_objects=self.num_objects,
show_vertices=self.show_vertices, tooltip='%s Tool' % name,
**style_kwargs
)
if self.edit_vertices:
self._vertex_stream = PolyVertexEdit(
source=self.plot, tooltip='%s Edit Tool' % name,
**style_kwargs
)
1 change: 1 addition & 0 deletions geoviews/bokeh.ext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 1 addition & 1 deletion geoviews/element/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
WMTS, Points, Image, Text, LineContours, RGB,
FilledContours, Path, Polygons, Shape, Dataset,
Contours, TriMesh, Graph, Nodes, EdgePaths, QuadMesh,
VectorField, Labels, HexTiles)
VectorField, Labels, HexTiles, Rectangles, Segments)


class GeoConversion(ElementConversion):
Expand Down
38 changes: 35 additions & 3 deletions geoviews/element/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
RGB as HvRGB, Text as HvText, TriMesh as HvTriMesh,
QuadMesh as HvQuadMesh, Points as HvPoints,
VectorField as HvVectorField, HexTiles as HvHexTiles,
Labels as HvLabels)
Labels as HvLabels, Rectangles as HvRectangles,
Segments as HvSegments
)

from shapely.geometry.base import BaseGeometry

Expand Down Expand Up @@ -50,7 +52,7 @@ def is_geographic(element, kdims=None):
else:
kdims = element.kdims

if len(kdims) != 2 and not isinstance(element, (Graph, Nodes)):
if len(kdims) != 2 and not isinstance(element, (Graph, Nodes, Rectangles, Segments)):
return False
if isinstance(element.data, geographic_types) or isinstance(element, (WMTS, Feature)):
return True
Expand Down Expand Up @@ -586,7 +588,6 @@ def __init__(self, data, kdims=None, vdims=None, **params):
super(TriMesh, self).__init__(data, kdims, vdims, **params)
self.nodes.crs = crs


@property
def edgepaths(self):
"""
Expand Down Expand Up @@ -630,6 +631,37 @@ def geom(self):
return polygon_to_geom(self)


class Rectangles(_Element, HvRectangles):
"""
Rectangles represent a collection of axis-aligned rectangles in 2D space.
"""

group = param.String(default='Rectangles', constant=True)

kdims = param.List(default=[Dimension('lon0'), Dimension('lat0'),
Dimension('lon1'), Dimension('lat1')],
bounds=(4, 4), constant=True, doc="""
The key dimensions of the Rectangles element represent the
bottom-left (lon0, lat0) and top right (lon1, lat1) coordinates
of each box.""")



class Segments(_Element, HvSegments):
"""
Segments represent a collection of lines in 2D space.
"""

group = param.String(default='Segments', constant=True)

kdims = param.List(default=[Dimension('lon0'), Dimension('lat0'),
Dimension('lon1'), Dimension('lat1')],
bounds=(4, 4), constant=True, doc="""
The key dimensions of the Segments element represent the
bottom-left (lon0, lat0) and top-right (lon1, lat1) coordinates
of each segment.""")


class Shape(Dataset):
"""
Shape wraps any shapely geometry type.
Expand Down
Binary file added geoviews/icons/DenoteBackground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added geoviews/icons/DenoteForeground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added geoviews/icons/PolyBreak.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions geoviews/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as GeoViews from "./models"
export {GeoViews}

import {register_models} from "@bokehjs/base"
register_models(GeoViews as any)
Loading

0 comments on commit f19cb0d

Please sign in to comment.