Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
supported_types = [Shape]
def _process_element(self, element):
if not len(element):
return element.clone(crs=self.p.projection)
geom = element.geom()
vertices = geom_to_array(geom)
if isinstance(geom, (MultiPolygon, Polygon)):
obj = Polygons([vertices])
else:
obj = Path([vertices])
geom = project_path(obj, projection=self.p.projection).geom()
return element.clone(geom, crs=self.p.projection)
class project_points(_project_operation):
supported_types = [Points, Nodes, VectorField, HexTiles, Labels]
def _process_element(self, element):
if not len(element):
return element.clone(crs=self.p.projection)
xdim, ydim = element.dimensions()[:2]
xs, ys = (element.dimension_values(i) for i in range(2))
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
if len(new_data[xdim.name]) == 0:
x, y = element.kdims
item = element.data[0] if element.data else None
if item is None or (isinstance(item, dict) and 'geometry' in item):
return element.clone(projected, crs=self.p.projection)
projected = [geom_dict_to_array_dict(p, [x.name, y.name]) for p in projected]
if any('holes' in p for p in projected):
pass
elif pd and isinstance(item, pd.DataFrame):
projected = [pd.DataFrame(p, columns=item.columns) for p in projected]
elif isinstance(item, np.ndarray):
projected = [np.column_stack([p[d.name] for d in element.dimensions()])
for p in projected]
return element.clone(projected, crs=self.p.projection)
class project_shape(_project_operation):
"""
Projects Shape Element from the source coordinate reference system
to the supplied projection.
"""
supported_types = [Shape]
def _process_element(self, element):
if not len(element):
return element.clone(crs=self.p.projection)
geom = element.geom()
vertices = geom_to_array(geom)
if isinstance(geom, (MultiPolygon, Polygon)):
obj = Polygons([vertices])
else:
obj = Path([vertices])
mask = np.zeros(zs.shape, dtype=np.bool)
mask[:, 1:][to_mask] = True
mask[:, 2:][to_mask[:, :-1]] = True
mask[:, :-1][to_mask] = True
mask[:, :-2][to_mask[:, 1:]] = True
mask[1:, 1:][to_mask[:-1]] = True
mask[1:, :-1][to_mask[:-1]] = True
mask[:-1, 1:][to_mask[1:]] = True
mask[:-1, :-1][to_mask[1:]] = True
zs[mask] = np.NaN
params = get_param_values(element)
return element.clone((PX, PY, zs), crs=self.p.projection, **params)
class project_image(_project_operation):
"""
Projects an geoviews Image to the specified projection,
returning a regular HoloViews Image type. Works by
regridding the data along projected bounds. Only supports
rectangular projections.
"""
fast = param.Boolean(default=False, doc="""
Whether to enable fast reprojection with (much) better
performance but poorer handling in polar regions.""")
width = param.Integer(default=None, doc="""
Width of the reprojectd Image""")
height = param.Integer(default=None, doc="""
Height of the reprojected Image""")
supported_types = [Shape]
def _process_element(self, element):
if not len(element):
return element.clone(crs=self.p.projection)
geom = element.geom()
vertices = geom_to_array(geom)
if isinstance(geom, (MultiPolygon, Polygon)):
obj = Polygons([vertices])
else:
obj = Path([vertices])
geom = project_path(obj, projection=self.p.projection).geom()
return element.clone(geom, crs=self.p.projection)
class project_points(_project_operation):
supported_types = [Points, Nodes, VectorField, HexTiles, Labels]
def _process_element(self, element):
if not len(element):
return element.clone(crs=self.p.projection)
xdim, ydim = element.dimensions()[:2]
xs, ys = (element.dimension_values(i) for i in range(2))
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
if len(new_data[xdim.name]) == 0:
datatype = [element.interface.datatype]+element.datatype
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):
supported_types = [Graph]
def _process_element(self, element):
proj = self.p.projection
nodes = project_points(element.nodes, projection=proj)
data = (element.data, nodes)
if element._edgepaths:
data = data + (project_path(element.edgepaths, projection=proj),)
return element.clone(data, crs=proj)
class project_quadmesh(_project_operation):
supported_types = [QuadMesh]
source coordinate reference system to the supplied projection.
"""
projection = param.ClassSelector(default=ccrs.GOOGLE_MERCATOR,
class_=ccrs.Projection,
instantiate=False, doc="""
Projection the shape type is projected to.""")
# Defines the types of elements supported by the operation
supported_types = []
def _process(self, element, key=None):
return element.map(self._process_element, self.supported_types)
class project_path(_project_operation):
"""
Projects Polygons and Path Elements from their source coordinate
reference system to the supplied projection.
"""
supported_types = [Polygons, Path, Contours, EdgePaths]
def _process_element(self, element):
if not len(element):
return element.clone(crs=self.p.projection)
crs = element.crs
cylindrical = isinstance(crs, ccrs._CylindricalProjection)
proj = self.p.projection
if isinstance(proj, ccrs.CRS) and not isinstance(proj, ccrs.Projection):
raise ValueError('invalid transform:'
class project_graph(_project_operation):
supported_types = [Graph]
def _process_element(self, element):
proj = self.p.projection
nodes = project_points(element.nodes, projection=proj)
data = (element.data, nodes)
if element._edgepaths:
data = data + (project_path(element.edgepaths, projection=proj),)
return element.clone(data, crs=proj)
class project_quadmesh(_project_operation):
supported_types = [QuadMesh]
def _process_element(self, element):
proj = self.p.projection
irregular = any(element.interface.irregular(element, kd)
for kd in element.kdims)
zs = element.dimension_values(2, flat=False)
if irregular:
X, Y = [np.asarray(element.interface.coords(
element, kd, expanded=True, edges=False))
for kd in element.kdims]
else:
X = element.interface.coords(element, 0, True, True, False)
if np.all(X[0, 1:] < X[0, :-1]):
class project_graph(_project_operation):
supported_types = [Graph]
def _process_element(self, element):
nodes = project_points(element.nodes, projection=self.projection)
data = (element.data, nodes)
if element._edgepaths:
data = data + (project_path(element.edgepaths, projection=self.projection),)
return element.clone(data, crs=self.projection)
class project_quadmesh(_project_operation):
supported_types = [QuadMesh]
def _process_element(self, element):
proj = self.p.projection
irregular = any(element.interface.irregular(element, kd)
for kd in element.kdims)
zs = element.dimension_values(2, flat=False)
if irregular:
X, Y = [np.asarray(element.interface.coords(
element, kd, expanded=True, edges=False))
for kd in element.kdims]
else:
X = element.interface.coords(element, 0, True, True, False)
if np.all(X[0, 1:] < X[0, :-1]):
X = X[:, ::-1]
x, y = element.kdims
item = element.data[0] if element.data else None
if item is None or (isinstance(item, dict) and 'geometry' in item):
return element.clone(projected, crs=self.p.projection)
projected = [geom_dict_to_array_dict(p, [x.name, y.name]) for p in projected]
if any('holes' in p for p in projected):
pass
elif pd and isinstance(item, pd.DataFrame):
projected = [pd.DataFrame(p, columns=item.columns) for p in projected]
elif isinstance(item, np.ndarray):
projected = [np.column_stack([p[d.name] for d in element.dimensions()])
for p in projected]
return element.clone(projected, crs=self.p.projection)
class project_shape(_project_operation):
"""
Projects Shape Element from the source coordinate reference system
to the supplied projection.
"""
supported_types = [Shape]
def _process_element(self, element):
if not len(element):
return element.clone(crs=self.p.projection)
geom = element.geom()
vertices = geom_to_array(geom)
if isinstance(geom, (MultiPolygon, Polygon)):
obj = Polygons([vertices])
else:
obj = Path([vertices])
source coordinate reference system to the supplied projection.
"""
projection = param.ClassSelector(default=ccrs.GOOGLE_MERCATOR,
class_=ccrs.Projection,
instantiate=False, doc="""
Projection the shape type is projected to.""")
# Defines the types of elements supported by the operation
supported_types = []
def _process(self, element, key=None):
return element.map(self._process_element, self.supported_types)
class project_path(_project_operation):
"""
Projects Polygons and Path Elements from their source coordinate
reference system to the supplied projection.
"""
supported_types = [Polygons, Path, Contours, EdgePaths]
def _process_element(self, element):
if not bool(element):
return element.clone(crs=self.p.projection)
crs = element.crs
proj = self.p.projection
if (isinstance(crs, ccrs.PlateCarree) and not isinstance(proj, ccrs.PlateCarree)
and crs.proj4_params['lon_0'] != 0):
element = self.instance(projection=ccrs.PlateCarree())(element)