0
# Measurements
1
2
Compute geometric properties and metrics of geometries including area, length, distance, and bounds. These functions provide quantitative analysis of spatial objects.
3
4
## Capabilities
5
6
### Area and Length
7
8
Compute area and perimeter measurements.
9
10
```python { .api }
11
def area(geometry, **kwargs):
12
"""
13
Compute area of polygon geometries.
14
15
Parameters:
16
- geometry: input geometry or array of geometries
17
18
Returns:
19
float or ndarray: area (0 for non-polygonal geometries)
20
"""
21
22
def length(geometry, **kwargs):
23
"""
24
Compute length of linear geometries or perimeter of polygonal geometries.
25
26
Parameters:
27
- geometry: input geometry or array of geometries
28
29
Returns:
30
float or ndarray: length/perimeter
31
"""
32
```
33
34
**Usage Example:**
35
36
```python
37
import shapely
38
from shapely.geometry import Point, LineString, Polygon
39
40
# Area measurements
41
square = Polygon([(0, 0), (2, 0), (2, 2), (0, 2)])
42
print(f"Square area: {shapely.area(square)}") # 4.0
43
44
circle_approx = Point(0, 0).buffer(1.0)
45
print(f"Circle area: {shapely.area(circle_approx):.2f}") # ~3.14
46
47
# Length measurements
48
line = LineString([(0, 0), (3, 4)])
49
print(f"Line length: {shapely.length(line)}") # 5.0
50
51
print(f"Square perimeter: {shapely.length(square)}") # 8.0
52
```
53
54
### Distance Measurements
55
56
Compute distances between geometries.
57
58
```python { .api }
59
def distance(a, b, **kwargs):
60
"""
61
Compute minimum Cartesian distance between geometries.
62
63
Parameters:
64
- a: first geometry or array of geometries
65
- b: second geometry or array of geometries
66
67
Returns:
68
float or ndarray: minimum distance (0 if geometries touch/overlap)
69
"""
70
71
def hausdorff_distance(a, b, densify=None, **kwargs):
72
"""
73
Compute Hausdorff distance between geometries.
74
75
Parameters:
76
- a: first geometry or array of geometries
77
- b: second geometry or array of geometries
78
- densify: optional densification fraction
79
80
Returns:
81
float or ndarray: Hausdorff distance
82
"""
83
84
def frechet_distance(a, b, densify=None, **kwargs):
85
"""
86
Compute Fréchet distance between geometries.
87
88
Parameters:
89
- a: first geometry or array of geometries
90
- b: second geometry or array of geometries
91
- densify: optional densification fraction
92
93
Returns:
94
float or ndarray: Fréchet distance
95
"""
96
```
97
98
**Usage Example:**
99
100
```python
101
import shapely
102
from shapely.geometry import Point, LineString
103
104
# Simple distance
105
point1 = Point(0, 0)
106
point2 = Point(3, 4)
107
print(f"Point distance: {shapely.distance(point1, point2)}") # 5.0
108
109
# Distance to polygon
110
polygon = shapely.box(5, 5, 7, 7)
111
print(f"Distance to polygon: {shapely.distance(point1, polygon):.2f}")
112
113
# Advanced distance metrics
114
line1 = LineString([(0, 0), (1, 1)])
115
line2 = LineString([(0, 1), (1, 0)])
116
hausdorff = shapely.hausdorff_distance(line1, line2)
117
frechet = shapely.frechet_distance(line1, line2)
118
print(f"Hausdorff distance: {hausdorff:.2f}")
119
print(f"Fréchet distance: {frechet:.2f}")
120
```
121
122
### Bounds and Extents
123
124
Get bounding box information and spatial extents.
125
126
```python { .api }
127
def bounds(geometry, **kwargs):
128
"""
129
Compute bounding box of geometry.
130
131
Parameters:
132
- geometry: input geometry or array of geometries
133
134
Returns:
135
ndarray: bounding box as [minx, miny, maxx, maxy] or array of boxes
136
"""
137
138
def total_bounds(geometry, **kwargs):
139
"""
140
Compute total bounds of geometry array.
141
142
Parameters:
143
- geometry: array of geometries
144
145
Returns:
146
ndarray: total bounding box as [minx, miny, maxx, maxy]
147
"""
148
```
149
150
**Usage Example:**
151
152
```python
153
import shapely
154
import numpy as np
155
156
# Single geometry bounds
157
polygon = shapely.Polygon([(1, 2), (5, 2), (5, 6), (1, 6)])
158
bounds = shapely.bounds(polygon)
159
print(f"Bounds: {bounds}") # [1, 2, 5, 6]
160
161
# Multiple geometries
162
geometries = [
163
shapely.Point(0, 0),
164
shapely.Point(10, 10),
165
shapely.Polygon([(2, 2), (8, 2), (8, 8), (2, 8)])
166
]
167
all_bounds = shapely.bounds(geometries)
168
print(f"Individual bounds shape: {all_bounds.shape}") # (3, 4)
169
170
total_bounds = shapely.total_bounds(geometries)
171
print(f"Total bounds: {total_bounds}") # [0, 0, 10, 10]
172
```
173
174
### Specialized Measurements
175
176
Advanced geometric measurements and analysis.
177
178
```python { .api }
179
def minimum_clearance(geometry, **kwargs):
180
"""
181
Compute minimum clearance distance (robustness measure).
182
183
Parameters:
184
- geometry: input geometry or array of geometries
185
186
Returns:
187
float or ndarray: minimum clearance distance
188
"""
189
190
def minimum_bounding_radius(geometry, **kwargs):
191
"""
192
Compute radius of minimum bounding circle.
193
194
Parameters:
195
- geometry: input geometry or array of geometries
196
197
Returns:
198
float or ndarray: minimum bounding circle radius
199
"""
200
```
201
202
**Usage Example:**
203
204
```python
205
import shapely
206
207
# Create polygon with narrow feature
208
polygon = shapely.Polygon([
209
(0, 0), (10, 0), (10, 1), (5, 1), (5, 0.1), (4.9, 0.1), (4.9, 1), (0, 1)
210
])
211
212
clearance = shapely.minimum_clearance(polygon)
213
print(f"Minimum clearance: {clearance}") # Shows narrowest gap
214
215
# Bounding circle radius
216
circle_polygon = shapely.Point(0, 0).buffer(5.0)
217
radius = shapely.minimum_bounding_radius(circle_polygon)
218
print(f"Bounding radius: {radius:.1f}") # ~5.0
219
```
220
221
### Array Operations
222
223
Efficient measurements on geometry arrays.
224
225
**Usage Example:**
226
227
```python
228
import shapely
229
import numpy as np
230
231
# Create array of random polygons
232
np.random.seed(42)
233
centers = np.random.rand(100, 2) * 10
234
radii = np.random.rand(100) * 0.5 + 0.1
235
236
polygons = [shapely.Point(x, y).buffer(r) for (x, y), r in zip(centers, radii)]
237
238
# Vectorized measurements
239
areas = shapely.area(polygons)
240
perimeters = shapely.length(polygons)
241
bounds_array = shapely.bounds(polygons)
242
243
print(f"Total area: {np.sum(areas):.2f}")
244
print(f"Average perimeter: {np.mean(perimeters):.2f}")
245
print(f"Overall bounds: {shapely.total_bounds(polygons)}")
246
247
# Distance matrix between first 5 polygons
248
first_five = polygons[:5]
249
distances = np.array([
250
[shapely.distance(a, b) for b in first_five]
251
for a in first_five
252
])
253
print(f"Distance matrix shape: {distances.shape}")
254
```