Skip to content

Commit cc497d4

Browse files
committed
Expose shapely.count_coordinates
1 parent 4cf8d47 commit cc497d4

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

odc/geo/geom.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
)
2727

2828
import numpy
29+
import shapely
2930
from affine import Affine
3031
from pyproj.aoi import AreaOfInterest
3132
from shapely import geometry, ops
@@ -1472,6 +1473,17 @@ def mid_longitude(geom: Geometry) -> float:
14721473
return lon
14731474

14741475

1476+
def count_coordinates(g: Geometry | Iterable[Geometry] | BoundingBox) -> int:
1477+
"""
1478+
Count the number of coordinates in a geometry.
1479+
"""
1480+
if isinstance(g, Geometry):
1481+
return shapely.count_coordinates(g.geom)
1482+
if isinstance(g, BoundingBox):
1483+
return 4
1484+
return shapely.count_coordinates([_g.geom for _g in g])
1485+
1486+
14751487
def _auto_resolution(g: Geometry) -> float:
14761488
# aim for ~100 points per side of a square
14771489
return math.sqrt(g.area) * 4 / 100

tests/test_geom.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from odc.geo.geom import (
1717
chop_along_antimeridian,
1818
clip_lon180,
19+
count_coordinates,
1920
densify,
2021
force_2d,
2122
multigeom,
@@ -1055,3 +1056,49 @@ def test_explore_geom(geom_json) -> None:
10551056
geometry.explore(map=m_external)
10561057
assert isinstance(m_external, Map)
10571058
assert any(isinstance(child, GeoJson) for child in m_external._children.values())
1059+
1060+
1061+
def test_count_coordinates() -> None:
1062+
"""Test count_coordinates function with various input types."""
1063+
crs = epsg4326
1064+
1065+
# Test with Geometry (Point)
1066+
point = geom.point(10, 20, crs)
1067+
assert count_coordinates(point) == 1
1068+
1069+
# Test with Geometry (LineString)
1070+
line = geom.line([(0, 0), (1, 1), (2, 2)], crs)
1071+
assert count_coordinates(line) == 3
1072+
1073+
# Test with Geometry (Polygon)
1074+
polygon = geom.polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], crs)
1075+
assert count_coordinates(polygon) == 5
1076+
1077+
# Test with Geometry (MultiPoint)
1078+
multi_point = geom.multipoint([(0, 0), (1, 1), (2, 2)], crs)
1079+
assert count_coordinates(multi_point) == 3
1080+
1081+
# Test with BoundingBox
1082+
bbox = geom.BoundingBox(0, 0, 10, 10, crs)
1083+
assert count_coordinates(bbox) == 4
1084+
1085+
# Test with Iterable[Geometry]
1086+
geometries = [point, line, polygon]
1087+
assert count_coordinates(geometries) == 9 # 1 + 3 + 5
1088+
1089+
# Test with empty list
1090+
assert count_coordinates([]) == 0
1091+
1092+
# Test with single geometry in list
1093+
assert count_coordinates([point]) == 1
1094+
1095+
# Test with complex geometry (MultiPolygon)
1096+
multi_polygon = geom.multipolygon(
1097+
[
1098+
[[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]],
1099+
[[(2, 2), (3, 2), (3, 3), (2, 3), (2, 2)]],
1100+
],
1101+
crs,
1102+
)
1103+
# MultiPolygon with 2 polygons, each with 5 coordinates
1104+
assert count_coordinates(multi_polygon) == 10

0 commit comments

Comments
 (0)