0
# Geometry Classes
1
2
The fundamental geometry types in Shapely that represent spatial objects. All geometries inherit from the base `Geometry` class and provide both object-oriented methods and functional array-based operations.
3
4
## Capabilities
5
6
### Point Geometry
7
8
Represents a single point in 2D or 3D space.
9
10
```python { .api }
11
class Point(Geometry):
12
def __init__(self, x=None, y=None, z=None):
13
"""
14
Create a Point geometry.
15
16
Parameters:
17
- x (float): X-coordinate
18
- y (float): Y-coordinate
19
- z (float, optional): Z-coordinate for 3D points
20
"""
21
22
@property
23
def x(self) -> float:
24
"""X-coordinate of the point."""
25
26
@property
27
def y(self) -> float:
28
"""Y-coordinate of the point."""
29
30
@property
31
def z(self) -> float:
32
"""Z-coordinate of the point (0 if 2D)."""
33
```
34
35
**Usage Example:**
36
37
```python
38
from shapely.geometry import Point
39
40
# Create 2D point
41
point_2d = Point(1.5, 2.5)
42
print(f"X: {point_2d.x}, Y: {point_2d.y}")
43
44
# Create 3D point
45
point_3d = Point(1.5, 2.5, 3.5)
46
print(f"X: {point_3d.x}, Y: {point_3d.y}, Z: {point_3d.z}")
47
```
48
49
### LineString Geometry
50
51
Represents a sequence of points connected by straight line segments.
52
53
```python { .api }
54
class LineString(Geometry):
55
def __init__(self, coordinates=None):
56
"""
57
Create a LineString geometry.
58
59
Parameters:
60
- coordinates: sequence of (x, y[, z]) coordinate tuples
61
"""
62
63
@property
64
def coords(self):
65
"""Coordinate sequence of the linestring."""
66
67
@property
68
def is_closed(self) -> bool:
69
"""True if linestring is closed (first and last points are the same)."""
70
71
@property
72
def is_simple(self) -> bool:
73
"""True if linestring does not cross itself."""
74
```
75
76
**Usage Example:**
77
78
```python
79
from shapely.geometry import LineString
80
81
# Create a simple line
82
line = LineString([(0, 0), (1, 1), (2, 0)])
83
print(f"Length: {line.length}")
84
print(f"Is closed: {line.is_closed}")
85
86
# Create a 3D line
87
line_3d = LineString([(0, 0, 0), (1, 1, 1), (2, 0, 2)])
88
```
89
90
### LinearRing Geometry
91
92
A closed LineString that forms the boundary of a polygon.
93
94
```python { .api }
95
class LinearRing(Geometry):
96
def __init__(self, coordinates=None):
97
"""
98
Create a LinearRing geometry (closed linestring).
99
100
Parameters:
101
- coordinates: sequence of (x, y[, z]) coordinate tuples
102
First and last points will be automatically connected
103
"""
104
105
@property
106
def coords(self):
107
"""Coordinate sequence of the ring."""
108
109
@property
110
def is_ccw(self) -> bool:
111
"""True if ring is oriented counter-clockwise."""
112
```
113
114
### Polygon Geometry
115
116
Represents a filled area bounded by a LinearRing exterior with optional holes.
117
118
```python { .api }
119
class Polygon(Geometry):
120
def __init__(self, shell=None, holes=None):
121
"""
122
Create a Polygon geometry.
123
124
Parameters:
125
- shell: exterior boundary as coordinate sequence or LinearRing
126
- holes: sequence of interior holes as coordinate sequences or LinearRings
127
"""
128
129
@property
130
def exterior(self) -> LinearRing:
131
"""Exterior boundary of the polygon."""
132
133
@property
134
def interiors(self):
135
"""Sequence of interior holes."""
136
137
@property
138
def coords(self):
139
"""Coordinate sequence of exterior boundary."""
140
```
141
142
**Usage Example:**
143
144
```python
145
from shapely.geometry import Polygon
146
147
# Simple rectangle
148
rect = Polygon([(0, 0), (2, 0), (2, 2), (0, 2)])
149
print(f"Area: {rect.area}")
150
151
# Polygon with hole
152
exterior = [(0, 0), (4, 0), (4, 4), (0, 4)]
153
hole = [(1, 1), (3, 1), (3, 3), (1, 3)]
154
poly_with_hole = Polygon(exterior, [hole])
155
print(f"Area: {poly_with_hole.area}") # 16 - 4 = 12
156
```
157
158
### MultiPoint Geometry
159
160
Collection of Point geometries.
161
162
```python { .api }
163
class MultiPoint(Geometry):
164
def __init__(self, points=None):
165
"""
166
Create a MultiPoint geometry.
167
168
Parameters:
169
- points: sequence of Point objects or coordinate tuples
170
"""
171
172
@property
173
def geoms(self):
174
"""Sequence of Point geometries."""
175
176
def __len__(self) -> int:
177
"""Number of points in the collection."""
178
179
def __getitem__(self, index) -> Point:
180
"""Get point by index."""
181
```
182
183
### MultiLineString Geometry
184
185
Collection of LineString geometries.
186
187
```python { .api }
188
class MultiLineString(Geometry):
189
def __init__(self, linestrings=None):
190
"""
191
Create a MultiLineString geometry.
192
193
Parameters:
194
- linestrings: sequence of LineString objects or coordinate sequences
195
"""
196
197
@property
198
def geoms(self):
199
"""Sequence of LineString geometries."""
200
201
def __len__(self) -> int:
202
"""Number of linestrings in the collection."""
203
204
def __getitem__(self, index) -> LineString:
205
"""Get linestring by index."""
206
```
207
208
### MultiPolygon Geometry
209
210
Collection of Polygon geometries.
211
212
```python { .api }
213
class MultiPolygon(Geometry):
214
def __init__(self, polygons=None, context_type='polygon'):
215
"""
216
Create a MultiPolygon geometry.
217
218
Parameters:
219
- polygons: sequence of Polygon objects or coordinate sequences
220
- context_type: context type for coordinate interpretation
221
"""
222
223
@property
224
def geoms(self):
225
"""Sequence of Polygon geometries."""
226
227
def __len__(self) -> int:
228
"""Number of polygons in the collection."""
229
230
def __getitem__(self, index) -> Polygon:
231
"""Get polygon by index."""
232
```
233
234
### GeometryCollection
235
236
Collection of mixed geometry types.
237
238
```python { .api }
239
class GeometryCollection(Geometry):
240
def __init__(self, geometries=None):
241
"""
242
Create a GeometryCollection.
243
244
Parameters:
245
- geometries: sequence of any Geometry objects
246
"""
247
248
@property
249
def geoms(self):
250
"""Sequence of geometries in the collection."""
251
252
def __len__(self) -> int:
253
"""Number of geometries in the collection."""
254
255
def __getitem__(self, index) -> Geometry:
256
"""Get geometry by index."""
257
```
258
259
**Usage Example:**
260
261
```python
262
from shapely.geometry import Point, LineString, Polygon, GeometryCollection
263
264
# Create mixed collection
265
point = Point(0, 0)
266
line = LineString([(1, 1), (2, 2)])
267
poly = Polygon([(3, 3), (4, 3), (4, 4), (3, 4)])
268
269
collection = GeometryCollection([point, line, poly])
270
print(f"Number of geometries: {len(collection)}")
271
272
# Iterate through geometries
273
for geom in collection.geoms:
274
print(f"Geometry type: {geom.geom_type}")
275
```
276
277
## Common Properties and Methods
278
279
All geometry classes inherit common properties and methods from the base `Geometry` class:
280
281
```python { .api }
282
class Geometry:
283
@property
284
def area(self) -> float:
285
"""Area of the geometry (0 for non-areal geometries)."""
286
287
@property
288
def bounds(self) -> tuple:
289
"""Bounding box as (minx, miny, maxx, maxy)."""
290
291
@property
292
def length(self) -> float:
293
"""Length/perimeter of the geometry."""
294
295
@property
296
def geom_type(self) -> str:
297
"""Geometry type name."""
298
299
@property
300
def is_empty(self) -> bool:
301
"""True if geometry is empty."""
302
303
@property
304
def is_valid(self) -> bool:
305
"""True if geometry is topologically valid."""
306
307
def buffer(self, distance: float, **kwargs) -> 'Geometry':
308
"""Create buffer around geometry."""
309
310
def contains(self, other: 'Geometry') -> bool:
311
"""Test if this geometry contains other."""
312
313
def intersects(self, other: 'Geometry') -> bool:
314
"""Test if this geometry intersects other."""
315
316
def within(self, other: 'Geometry') -> bool:
317
"""Test if this geometry is within other."""
318
319
def touches(self, other: 'Geometry') -> bool:
320
"""Test if this geometry touches other."""
321
322
def crosses(self, other: 'Geometry') -> bool:
323
"""Test if this geometry crosses other."""
324
325
def overlaps(self, other: 'Geometry') -> bool:
326
"""Test if this geometry overlaps other."""
327
328
def equals(self, other: 'Geometry') -> bool:
329
"""Test if this geometry equals other spatially."""
330
331
def distance(self, other: 'Geometry') -> float:
332
"""Compute minimum distance to other geometry."""
333
334
def intersection(self, other: 'Geometry') -> 'Geometry':
335
"""Compute intersection with other geometry."""
336
337
def union(self, other: 'Geometry') -> 'Geometry':
338
"""Compute union with other geometry."""
339
340
def difference(self, other: 'Geometry') -> 'Geometry':
341
"""Compute difference from other geometry."""
342
343
def symmetric_difference(self, other: 'Geometry') -> 'Geometry':
344
"""Compute symmetric difference with other geometry."""
345
```