Skip to content

Commit dd59b21

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

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Shapely geometry classes with CRS information attached.
142142
chop_along_antimeridian
143143
clip_lon180
144144
common_crs
145+
count_coordinates
145146
densify
146147
intersects
147148
lonlat_bounds

odc/geo/geom.py

Lines changed: 31 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,36 @@ 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+
:param g:
1481+
Geometry, iterable of geometries, or bounding box to count coordinates for
1482+
:return:
1483+
Number of coordinates in the input geometry(ies)
1484+
1485+
Examples:
1486+
>>> point = geom.point(10, 20, crs="EPSG:4326")
1487+
>>> count_coordinates(point)
1488+
1
1489+
>>> line = geom.line([(0, 0), (1, 1), (2, 2)], crs="EPSG:4326")
1490+
>>> count_coordinates(line)
1491+
3
1492+
>>> bbox = geom.BoundingBox(0, 0, 10, 10, crs="EPSG:4326")
1493+
>>> count_coordinates(bbox)
1494+
4
1495+
>>> geometries = [point, line]
1496+
>>> count_coordinates(geometries)
1497+
4
1498+
"""
1499+
if isinstance(g, Geometry):
1500+
return shapely.count_coordinates(g.geom)
1501+
if isinstance(g, BoundingBox):
1502+
return 4
1503+
return shapely.count_coordinates([_g.geom for _g in g])
1504+
1505+
14751506
def _auto_resolution(g: Geometry) -> float:
14761507
# aim for ~100 points per side of a square
14771508
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)