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

Improvement of annotators #583

Merged
merged 13 commits into from
Jan 16, 2023
10 changes: 5 additions & 5 deletions examples/user_guide/Annotators.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"tiles = gv.tile_sources.Wikipedia()\n",
"\n",
"sample_points = dict(\n",
" Longitude = [-10131185, -10131943, -10131766, -10131032],\n",
" Latitude = [ 3805587, 3803182, 3801073, 3799778])\n",
" Longitude = [-10131185, -10131943, -10131766, -10131032, -10129766],\n",
" Latitude = [ 3805587, 3803182, 3801073, 3799778, 3798878])\n",
"\n",
"points = gv.Points(sample_points, crs=ccrs.GOOGLE_MERCATOR).opts(\n",
" size=10, line_color='black', responsive=True, min_height=600\n",
Expand Down Expand Up @@ -88,7 +88,7 @@
"metadata": {},
"outputs": [],
"source": [
"point_annotate.annotated.geom()"
"point_annotate.annotated.geom(projection=ccrs.PlateCarree())"
]
},
{
Expand Down Expand Up @@ -207,7 +207,7 @@
"metadata": {},
"outputs": [],
"source": [
"path_annotate.annotated.geom()"
"path_annotate.annotated.geom(projection=ccrs.PlateCarree())"
]
},
{
Expand Down Expand Up @@ -264,7 +264,7 @@
"metadata": {},
"outputs": [],
"source": [
"poly_annotate.annotated.iloc[0].geom()"
"poly_annotate.annotated.iloc[0].geom(projection=ccrs.PlateCarree())"
]
}
],
Expand Down
44 changes: 35 additions & 9 deletions geoviews/element/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

from ..util import (
path_to_geom_dicts, polygons_to_geom_dicts, load_tiff, from_xarray,
poly_types, expand_geoms
poly_types, expand_geoms, transform_shapely
)

geographic_types = (GoogleTiles, cFeature, BaseGeometry)
Expand Down Expand Up @@ -266,14 +266,16 @@ class Points(_Element, HvPoints):

group = param.String(default='Points')

def geom(self, union=False):
def geom(self, union=False, projection=None):
"""
Converts the Points to a shapely geometry.

Parameters
----------
union: boolean (default=False)
Whether to compute a union between the geometries
projection : EPSG string | Cartopy CRS | None
Whether to project the geometry to other coordinate system

Returns
-------
Expand All @@ -287,6 +289,8 @@ def geom(self, union=False):
geom = points[0]
else:
geom = MultiPoint(points)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand Down Expand Up @@ -518,14 +522,16 @@ class Path(_Element, HvPath):

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

def geom(self, union=False):
def geom(self, union=False, projection=None):
"""
Converts the Path to a shapely geometry.

Parameters
----------
union: boolean (default=False)
Whether to compute a union between the geometries
projection : EPSG string | Cartopy CRS | None
Whether to project the geometry to other coordinate system

Returns
-------
Expand All @@ -539,6 +545,8 @@ def geom(self, union=False):
geom = geoms[0]
else:
geom = MultiLineString(geoms)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand Down Expand Up @@ -661,14 +669,16 @@ class Contours(_Element, HvContours):

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

def geom(self, union=False):
def geom(self, union=False, projection=None):
"""
Converts the Contours to a shapely geometry.

Parameters
----------
union: boolean (default=False)
Whether to compute a union between the geometries
projection : EPSG string | Cartopy CRS | None
Whether to project the geometry to other coordinate system

Returns
-------
Expand All @@ -682,6 +692,8 @@ def geom(self, union=False):
geom = geoms[0]
else:
geom = MultiLineString(geoms)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand All @@ -694,14 +706,16 @@ class Polygons(_Element, HvPolygons):

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

def geom(self, union=False):
def geom(self, union=False, projection=None):
"""
Converts the Path to a shapely geometry.

Parameters
----------
union: boolean (default=False)
Whether to compute a union between the geometries
projection : EPSG string | Cartopy CRS | None
Whether to project the geometry to other coordinate system

Returns
-------
Expand All @@ -715,6 +729,8 @@ def geom(self, union=False):
geom = geoms[0]
else:
geom = MultiPolygon(geoms)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand All @@ -732,14 +748,16 @@ class Rectangles(_Element, HvRectangles):
bottom-left (lon0, lat0) and top right (lon1, lat1) coordinates
of each box.""")

def geom(self, union=False):
def geom(self, union=False, projection=None):
"""
Converts the Rectangles to a shapely geometry.

Parameters
----------
union: boolean (default=False)
Whether to compute a union between the geometries
projection : EPSG string | Cartopy CRS | None
Whether to project the geometry to other coordinate system

Returns
-------
Expand All @@ -753,6 +771,8 @@ def geom(self, union=False):
geom = boxes[0]
else:
geom = MultiPolygon(boxes)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand All @@ -770,7 +790,7 @@ class Segments(_Element, HvSegments):
bottom-left (lon0, lat0) and top-right (lon1, lat1) coordinates
of each segment.""")

def geom(self, union=False):
def geom(self, union=False, projection=None):
"""
Converts the Segments to a shapely geometry.
"""
Expand All @@ -783,6 +803,8 @@ def geom(self, union=False):
geom = lines[0]
else:
geom = MultiLineString(lines)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand All @@ -806,7 +828,7 @@ def __init__(self, data, kdims=None, vdims=None, **params):
if params.get('level') is not None:
if vdims is None:
vdims = [Dimension('Level')]
self.warning('Supplying a level to a Shape is deprecated '
self.param.warning('Supplying a level to a Shape is deprecated '
'provide the value as part of a dictionary of '
'the form {\'geometry\': <shapely.Geometry>, '
'\'level\': %s} instead' % params['level'])
Expand Down Expand Up @@ -963,18 +985,22 @@ def from_records(cls, records, dataset=None, on=None, value=None,
return element(data, vdims=kdims+vdims, **kwargs).opts(color=value)


def geom(self, union=False):
def geom(self, union=False, projection=None):
"""
Returns the Shape as a shapely geometry

Parameters
----------
union: boolean (default=False)
Whether to compute a union between the geometries
projection : EPSG string | Cartopy CRS | None
Whether to project the geometry to other coordinate system

Returns
-------
A shapely geometry
"""
geom = self.data['geometry']
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom
18 changes: 9 additions & 9 deletions geoviews/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class PointTableLinkCallback(LinkCallback):
const projected_xs = []
const projected_ys = []
for (let i = 0; i < xs_column.length; i++) {
const xv = xs_column[i]
const xv = xs_column[i]
const yv = ys_column[i]
const p = projections.wgs84_mercator.inverse([xv, yv])
const p = projections.wgs84_mercator.invert(xv, yv)
projected_xs.push(p[0])
projected_ys.push(p[1])
}
Expand All @@ -96,7 +96,7 @@ class PointTableLinkCallback(LinkCallback):
for (let i = 0; i < xs_column.length; i++) {
const xv = xs_column[i]
const yv = ys_column[i]
const p = projections.wgs84_mercator.forward([xv, yv])
const p = projections.wgs84_mercator.compute(xv, yv)
projected_xs.push(p[0])
projected_ys.push(p[1])
}
Expand Down Expand Up @@ -142,7 +142,7 @@ class VertexTableLinkCallback(LinkCallback):
for (let i = 0; i < xs_column.length; i++) {
const x = xs_column[i]
const y = ys_column[i]
const p = projections.wgs84_mercator.inverse([x, y])
const p = projections.wgs84_mercator.invert(x, y)
projected_xs.push(p[0])
projected_ys.push(p[1])
empty.push(null)
Expand Down Expand Up @@ -185,7 +185,7 @@ class VertexTableLinkCallback(LinkCallback):
for (let i = 0; i < xs_column.length; i++) {
const xv = xs_column[i]
const yv = ys_column[i]
const p = projections.wgs84_mercator.forward([xv, yv])
const p = projections.wgs84_mercator.compute(xv, yv)
projected_xs.push(p[0])
projected_ys.push(p[1])
points.push(i)
Expand Down Expand Up @@ -250,8 +250,8 @@ class RectanglesTableLinkCallback(HvRectanglesTableLinkCallback):
for (let i = 0; i < xs.length; i++) {
const hw = ws[i]/2.
const hh = hs[i]/2.
const p1 = projections.wgs84_mercator.inverse([xs[i]-hw, ys[i]-hh])
const p2 = projections.wgs84_mercator.inverse([xs[i]+hw, ys[i]+hh])
const p1 = projections.wgs84_mercator.invert(xs[i]-hw, ys[i]-hh)
const p2 = projections.wgs84_mercator.invert(xs[i]+hw, ys[i]+hh)
x0.push(p1[0])
x1.push(p2[0])
y0.push(p1[1])
Expand Down Expand Up @@ -279,8 +279,8 @@ class RectanglesTableLinkCallback(HvRectanglesTableLinkCallback):
const y0 = Math.min(y0s[i], y1s[i])
const x1 = Math.max(x0s[i], x1s[i])
const y1 = Math.max(y0s[i], y1s[i])
const p1 = projections.wgs84_mercator.forward([x0, y0])
const p2 = projections.wgs84_mercator.forward([x1, y1])
const p1 = projections.wgs84_mercator.compute(x0, y0)
const p2 = projections.wgs84_mercator.compute(x1, y1)
xs.push((p1[0]+p2[0])/2.)
ys.push((p1[1]+p2[1])/2.)
ws.push(p2[0]-p1[0])
Expand Down
6 changes: 3 additions & 3 deletions geoviews/operation/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _process_element(self, element):
projected.append(data)

if len(geoms) and len(projected) == 0:
self.warning('While projecting a %s element from a %s coordinate '
self.param.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 '
Expand Down Expand Up @@ -171,7 +171,7 @@ def _process_element(self, element):
new_data[ydim.name] = coordinates[mask, 1]

if len(new_data[xdim.name]) == 0:
self.warning('While projecting a %s element from a %s coordinate '
self.param.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 '
Expand Down Expand Up @@ -202,7 +202,7 @@ def _process_element(self, element):
new_data[y1d.name] = p2[mask, 1]

if len(new_data[x0d.name]) == 0:
self.warning('While projecting a %s element from a %s coordinate '
self.param.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 '
Expand Down
6 changes: 1 addition & 5 deletions geoviews/plotting/bokeh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ class GeoRasterPlot(GeoPlot, RasterPlot):
var projections = Bokeh.require("core/util/projections");
var x = special_vars.x
var y = special_vars.y
if (projections.wgs84_mercator.invert == null) {
var coords = projections.wgs84_mercator.inverse([x, y])
} else {
var coords = projections.wgs84_mercator.invert(x, y)
}
var coords = projections.wgs84_mercator.invert(x, y)
return "" + (coords[%d]).toFixed(4)
"""

Expand Down
10 changes: 4 additions & 6 deletions geoviews/plotting/bokeh/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import numpy as np
from pathlib import Path

from bokeh.models import CustomJS, CustomAction, PolyEditTool
from holoviews.core.ndmapping import UniformNdMapping
Expand Down Expand Up @@ -334,11 +334,9 @@ class PolyVertexEditCallback(GeoPolyEditCallback):
vcds.selection_manager.clear();
"""

icon = os.path.abspath(
os.path.join(
os.path.dirname(__file__), '..', '..', 'icons', 'PolyBreak.png'
)
)
icon = (
Path(__file__).parents[2] / "icons" / "PolyBreak.png"
).resolve()

def _create_vertex_split_link(self, action, poly_renderer,
vertex_renderer, vertex_tool):
Expand Down
6 changes: 1 addition & 5 deletions geoviews/plotting/bokeh/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ class GeoPlot(ProjectionPlot, ElementPlot):
var projections = Bokeh.require("core/util/projections");
var x = special_vars.data_x
var y = special_vars.data_y
if (projections.wgs84_mercator.invert == null) {
var coords = projections.wgs84_mercator.inverse([x, y])
} else {
var coords = projections.wgs84_mercator.invert(x, y)
}
var coords = projections.wgs84_mercator.invert(x, y)
return "" + (coords[%d]).toFixed(4)
"""

Expand Down
Loading