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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
python-version: ["3.10", "3.11"]

steps:
- name: Checkout repository
Expand Down
22 changes: 9 additions & 13 deletions Open Raster at Multi-Point.ipynb

Large diffs are not rendered by default.

7 changes: 6 additions & 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.6.0"
version = "1.7.0"
description = "raster processing toolkit"
readme = "README.md"
authors = [
Expand Down Expand Up @@ -52,3 +52,8 @@ rasters = ["*.txt"]

[project.urls]
"Homepage" = "https://github.com/python-rasters/rasters"

[tool.pytest.ini_options]
filterwarnings = [
"ignore:The 'shapely.geos' module is deprecated:DeprecationWarning:geopandas._compat",
]
61 changes: 45 additions & 16 deletions rasters/coordinate_array.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from __future__ import annotations

from typing import Union, TYPE_CHECKING

import numpy as np
from pyproj import Transformer
import warnings # Import the warnings module

from .CRS import CRS, WGS84
from .point import Point
from .spatial_geometry import SpatialGeometry

if TYPE_CHECKING:
from .bbox import BBox

class CoordinateArray(SpatialGeometry):
"""
DEPRECATED: This class is deprecated and will be removed in a future version.
Please use the `MultiPoint` class instead, which now includes all the
functionality previously provided by `CoordinateArray`, along with
Shapely-based geometric operations.

A class representing an array of coordinates with a coordinate reference system (CRS).

Attributes:
Expand All @@ -22,16 +25,24 @@ class CoordinateArray(SpatialGeometry):
crs (CRS): The coordinate reference system. Defaults to WGS84.
"""
def __init__(self, x: np.ndarray, y: np.ndarray, crs: Union[CRS, str] = WGS84, **kwargs):
"""
Initializes a new CoordinateArray object.

Args:
x (np.ndarray): The x-coordinates.
y (np.ndarray): The y-coordinates.
crs (Union[CRS, str]): The coordinate reference system. Defaults to WGS84.
**kwargs: Additional keyword arguments passed to the parent class constructor.
"""
warnings.warn(
"The `CoordinateArray` class is deprecated and will be removed in a future version. "
"Please use the `MultiPoint` class instead, which now includes all `CoordinateArray` functionality.",
DeprecationWarning,
stacklevel=2 # Points to the user's code, not this internal method
)
super(CoordinateArray, self).__init__(crs=crs, **kwargs)

if not isinstance(x, np.ndarray):
x = np.asarray(x)
if not isinstance(y, np.ndarray):
y = np.asarray(y)

if x.shape != y.shape:
raise ValueError("x and y arrays must have the same shape.")
if x.ndim != 1:
raise ValueError("x and y arrays must be 1-dimensional.")

self.x = x
self.y = y

Expand All @@ -43,18 +54,24 @@ def bbox(self) -> BBox:
BBox: The bounding box of the coordinate array.
"""
from .bbox import BBox # Import here to avoid circular dependency
if self.x.size == 0:
return BBox(crs=self.crs) # Return empty BBox for empty CoordinateArray
return BBox.from_points(self.x, self.y, crs=self.crs)

def centroid(self) -> Point:
def centroid(self) -> "Point": # Assuming Point is defined elsewhere and is callable
"""
Calculates the centroid of the coordinate array.

Returns:
Point: The centroid of the coordinate array.
"""
# Ensure Point is imported or type-hinted if it's in a separate module
from .point import Point
if self.x.size == 0:
return Point(np.nan, np.nan, crs=self.crs)
return Point(np.nanmean(self.x), np.nanmean(self.y), crs=self.crs)

def to_crs(self, crs: CRS | str) -> SpatialGeometry:
def to_crs(self, crs: Union[CRS, str]) -> "CoordinateArray":
"""
Transforms the coordinate array to a new CRS.

Expand All @@ -64,14 +81,26 @@ def to_crs(self, crs: CRS | str) -> SpatialGeometry:
Returns:
CoordinateArray: A new CoordinateArray with the transformed coordinates.
"""
transformer = Transformer.from_crs(self.crs, crs, always_xy=True)
from pyproj import Transformer # Import here to avoid circular dependency if not already at top

if isinstance(crs, str):
crs = CRS(crs)

if self.crs.equals(crs):
return self

if self.x.size == 0:
return CoordinateArray(np.array([]), np.array([]), crs=crs)

# Assuming your CRS class has a method to_pyproj() that returns a pyproj.CRS object
transformer = Transformer.from_crs(self.crs.to_pyproj(), crs.to_pyproj(), always_xy=True)
x, y = transformer.transform(self.x, self.y)
result = CoordinateArray(x, y, crs=crs)

return result

@property
def latlon(self) -> CoordinateArray:
def latlon(self) -> "CoordinateArray":
"""
Returns the coordinate array in the WGS84 (latitude/longitude) CRS.

Expand Down
Loading