Skip to content

Commit

Permalink
Changed points from 4 to 5
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed Jul 26, 2022
1 parent 728409d commit 352259d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
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=\"wgs84\")"
]
},
{
Expand Down Expand Up @@ -207,7 +207,7 @@
"metadata": {},
"outputs": [],
"source": [
"path_annotate.annotated.geom()"
"path_annotate.annotated.geom(projection=\"wgs84\")"
]
},
{
Expand Down Expand Up @@ -264,7 +264,7 @@
"metadata": {},
"outputs": [],
"source": [
"poly_annotate.annotated.iloc[0].geom()"
"poly_annotate.annotated.iloc[0].geom(projection=\"wgs84\")"
]
}
],
Expand Down
49 changes: 34 additions & 15 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, transform_shapely_to_wsg84
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,7 +289,8 @@ def geom(self, union=False):
geom = points[0]
else:
geom = MultiPoint(points)
geom = transform_shapely_to_wsg84(geom, self.crs)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand Down Expand Up @@ -519,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 @@ -540,7 +545,8 @@ def geom(self, union=False):
geom = geoms[0]
else:
geom = MultiLineString(geoms)
geom = transform_shapely_to_wsg84(geom, self.crs)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand Down Expand Up @@ -663,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 @@ -684,7 +692,8 @@ def geom(self, union=False):
geom = geoms[0]
else:
geom = MultiLineString(geoms)
geom = transform_shapely_to_wsg84(geom, self.crs)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand All @@ -697,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 @@ -718,7 +729,8 @@ def geom(self, union=False):
geom = geoms[0]
else:
geom = MultiPolygon(geoms)
geom = transform_shapely_to_wsg84(geom, self.crs)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand All @@ -736,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 @@ -757,7 +771,8 @@ def geom(self, union=False):
geom = boxes[0]
else:
geom = MultiPolygon(boxes)
geom = transform_shapely_to_wsg84(geom, self.crs)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand All @@ -775,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 @@ -788,7 +803,8 @@ def geom(self, union=False):
geom = lines[0]
else:
geom = MultiLineString(lines)
geom = transform_shapely_to_wsg84(geom, self.crs)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom


Expand Down Expand Up @@ -969,19 +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']
geom = transform_shapely_to_wsg84(geom, self.crs)
if projection:
geom = transform_shapely(geom, self.crs, projection)
return unary_union(geom) if union else geom
12 changes: 7 additions & 5 deletions geoviews/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from cartopy.io.img_tiles import GoogleTiles, QuadtreeTiles
from holoviews.element import Tiles
from packaging.version import Version
from pyproj import CRS, Transformer
from pyproj import Transformer
from shapely.geometry import (
LinearRing, LineString, MultiLineString, MultiPoint,
MultiPolygon, Point, Polygon, box
Expand Down Expand Up @@ -779,8 +779,10 @@ def get_tile_rgb(tile_source, bbox, zoom_level, bbox_crs=ccrs.PlateCarree()):
).clone(datatype=['grid', 'xarray', 'iris'])[l:r, b:t]


def transform_shapely_to_wsg84(geom, crs_from):
crs_to = CRS('WGS84')
def transform_shapely(geom, crs_from, crs_to):
if isinstance(crs_to, str):
crs_to = ccrs.CRS(crs_to)
if isinstance(crs_from, str):
crs_from = ccrs.CRS(crs_from)
project = Transformer.from_crs(crs_from, crs_to).transform
flip = lambda x, y: (y, x)
return transform(flip, transform(project, geom))
return transform(project, geom)

0 comments on commit 352259d

Please sign in to comment.