Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 218 additions & 0 deletions Open Raster at Multi-Point.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Open Raster at Point.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "rasters"
version = "1.5.5"
version = "1.6.0"
description = "raster processing toolkit"
readme = "README.md"
authors = [
Expand Down
7 changes: 5 additions & 2 deletions rasters/mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
from .raster import Raster
from .raster_geometry import RasterGeometry

def mosaic(images: Iterator[Union[Raster, str]], geometry: RasterGeometry) -> Raster:
def mosaic(
images: Iterator[Union[Raster, str]],
geometry: RasterGeometry,
resampling: str = "nearest") -> Raster:
"""
Creates a mosaic from a sequence of Raster images.

Expand Down Expand Up @@ -44,7 +47,7 @@ def mosaic(images: Iterator[Union[Raster, str]], geometry: RasterGeometry) -> Ra
# Overlay the image onto the mosaic using the 'where' function
with warnings.catch_warnings():
warnings.simplefilter("ignore")
mosaic = where(np.isnan(mosaic), image.to_geometry(geometry), mosaic)
mosaic = where(np.isnan(mosaic), image.to_geometry(geometry, resampling=resampling), mosaic)

mosaic = mosaic.astype(dtype) # Set the data type of the mosaic
mosaic = Raster(mosaic, geometry=geometry, nodata=nodata, metadata=metadata) # Create the final Raster
Expand Down
72 changes: 72 additions & 0 deletions rasters/multi_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import Union

import numpy as np
import shapely

from .CRS import CRS, WGS84
Expand Down Expand Up @@ -39,3 +40,74 @@ def __init__(self, points, crs: Union[CRS, str] = WGS84):
MultiVectorGeometry.__init__(self, crs=crs)

self.geometry = geometry

@property
def x(self) -> np.ndarray:
"""
Returns the x-coordinates of the points in the multi-point geometry.

Returns:
np.ndarray: An array of x-coordinates.
"""
return np.array([point.x for point in self.geometry.geoms])

@property
def y(self) -> np.ndarray:
"""
Returns the y-coordinates of the points in the multi-point geometry.

Returns:
np.ndarray: An array of y-coordinates.
"""
return np.array([point.y for point in self.geometry.geoms])

@property
def xmin(self) -> float:
"""
Returns the minimum x-coordinate of the points in the multi-point geometry.

Returns:
float: The minimum x-coordinate.
"""
return np.nanmin(self.x)

@property
def ymin(self) -> float:
"""
Returns the minimum y-coordinate of the points in the multi-point geometry.

Returns:
float: The minimum y-coordinate.
"""
return np.nanmin(self.y)

@property
def xmax(self) -> float:
"""
Returns the maximum x-coordinate of the points in the multi-point geometry.

Returns:
float: The maximum x-coordinate.
"""
return np.nanmax(self.x)

@property
def ymax(self) -> float:
"""
Returns the maximum y-coordinate of the points in the multi-point geometry.

Returns:
float: The maximum y-coordinate.
"""
return np.nanmax(self.y)

@property
def bbox(self) -> "BBox":
"""
Returns the bounding box of the multi-point geometry.

Returns:
BBox: The bounding box of the multi-point geometry.
"""
from .bbox import BBox
return BBox(self.xmin, self.ymin, self.xmax, self.ymax, crs=self.crs)
31 changes: 29 additions & 2 deletions rasters/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from .bbox import BBox
from .vector_geometry import VectorGeometry, SingleVectorGeometry
from .point import Point
from .multi_point import MultiPoint
from .polygon import Polygon
from .kdtree import KDTree
from .multi_raster import MultiRaster
Expand Down Expand Up @@ -409,13 +410,39 @@ def open(
filename: str,
nodata=None,
remove=None,
geometry: Union[RasterGeometry, Point] = None,
geometry: Union[RasterGeometry, Point, MultiPoint] = None,
buffer: int = None,
window: Window = None,
resampling: str = None,
cmap: Union[Colormap, str] = None,
**kwargs) -> Raster:
**kwargs) -> Union[Raster, np.ndarray]:
from .point import Point
from .multi_point import MultiPoint

if isinstance(geometry, MultiPoint):
values = []

for point in geometry:
value = cls.open(
filename=filename,
nodata=nodata,
remove=remove,
geometry=point,
buffer=buffer,
window=window,
resampling=resampling,
cmap=cmap,
**kwargs
)

if isinstance(value, Raster):
value = value.to_point(point)

values.append(value)

values = np.ndarray(values)

return values

target_geometry = geometry

Expand Down
8 changes: 7 additions & 1 deletion rasters/vector_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from .CRS import CRS, WGS84
from .spatial_geometry import SpatialGeometry


class VectorGeometry(SpatialGeometry):
"""
Base class for representing vector geometries.
Expand Down Expand Up @@ -161,6 +160,13 @@ def __getitem__(self, index):

return self.geometry[index]

# need to be able to iterate over geometries and apply self.contain to each one
def __iter__(self):
from .wrap_geometry import wrap_geometry

for geometry in self.geoms:
yield wrap_geometry(geometry)

# @property
# def geoms(self) -> list[VectorGeometry]:
# """
Expand Down
2 changes: 1 addition & 1 deletion rasters/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.5
1.6.0