Coverage for src/sensai/geoanalytics/geopandas/coordinates.py: 46%
28 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-13 22:17 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-13 22:17 +0000
1import geopandas as gp
2import numpy as np
3from abc import ABC, abstractmethod
4from shapely.geometry import MultiPoint
5from typing import Union
7from ...clustering import EuclideanClusterer
9TCoordinates = Union[np.ndarray, MultiPoint, gp.GeoDataFrame, EuclideanClusterer.Cluster]
12def validate_coordinates(coordinates: np.ndarray):
13 # for the moment we only support 2-dim coordinates. We can adjust it in the future when needed
14 if not len(coordinates.shape) == 2 or coordinates.shape[1] != 2:
15 raise Exception(f"Coordinates must be of shape (n, 2), instead got: {coordinates.shape}")
18def extract_coordinates_array(coordinates: TCoordinates) -> np.ndarray:
19 """
20 Extract coordinates as numpy array
21 """
22 if isinstance(coordinates, gp.GeoDataFrame):
23 try:
24 coordinates = np.array(MultiPoint(list(coordinates.geometry)))
25 except Exception:
26 raise ValueError(f"Could not extract coordinates from GeoDataFrame. "
27 f"Is the geometry column a sequence of Points?")
28 elif isinstance(coordinates, MultiPoint):
29 coordinates = np.array(coordinates)
30 elif isinstance(coordinates, EuclideanClusterer.Cluster):
31 coordinates = coordinates.datapoints
32 validate_coordinates(coordinates)
33 return coordinates
36class GeoDataFrameWrapper(ABC):
37 @abstractmethod
38 def to_geodf(self, *args, **kwargs) -> gp.GeoDataFrame:
39 pass
41 def plot(self, *args, **kwargs):
42 self.to_geodf().plot(*args, **kwargs)