0
# Linear Operations
1
2
Operations specialized for working with linear geometries like LineStrings and LinearRings. These functions provide capabilities for interpolation, location, merging, and analysis of linear features.
3
4
## Capabilities
5
6
### Line Interpolation and Location
7
8
Find points along lines and locate points on lines.
9
10
```python { .api }
11
def line_interpolate_point(line, distance, normalized=False, **kwargs):
12
"""
13
Interpolate a point along a line at specified distance.
14
15
Parameters:
16
- line: input LineString geometry or array of LineStrings
17
- distance: distance along line (or fraction if normalized=True)
18
- normalized: if True, distance is fraction of total length (0.0 to 1.0)
19
20
Returns:
21
Point geometry or array of Points
22
"""
23
24
def line_locate_point(line, point, normalized=False, **kwargs):
25
"""
26
Locate the distance of a point along a line.
27
28
Parameters:
29
- line: input LineString geometry or array of LineStrings
30
- point: Point geometry to locate on line
31
- normalized: if True, return fraction of total length
32
33
Returns:
34
float or ndarray: distance along line
35
"""
36
```
37
38
**Usage Example:**
39
40
```python
41
import shapely
42
from shapely.geometry import LineString, Point
43
44
# Create a line
45
line = LineString([(0, 0), (10, 0), (10, 10)])
46
47
# Interpolate points along line
48
point_at_5 = shapely.line_interpolate_point(line, 5.0)
49
point_at_half = shapely.line_interpolate_point(line, 0.5, normalized=True)
50
51
print(f"Point at distance 5: {point_at_5}")
52
print(f"Point at 50% of length: {point_at_half}")
53
54
# Locate existing point on line
55
test_point = Point(10, 5)
56
distance = shapely.line_locate_point(line, test_point)
57
fraction = shapely.line_locate_point(line, test_point, normalized=True)
58
59
print(f"Test point distance: {distance}")
60
print(f"Test point fraction: {fraction:.2f}")
61
```
62
63
### Line Merging
64
65
Combine connected linear features into longer lines.
66
67
```python { .api }
68
def line_merge(geometry, directed=False, **kwargs):
69
"""
70
Merge connected LineStrings into longer LineStrings.
71
72
Parameters:
73
- geometry: LineString, MultiLineString, or array of linear geometries
74
- directed: if True, only merge lines with same direction
75
76
Returns:
77
LineString, MultiLineString, or array: merged line geometry
78
"""
79
```
80
81
**Usage Example:**
82
83
```python
84
import shapely
85
from shapely.geometry import LineString, MultiLineString
86
87
# Create connected line segments
88
line1 = LineString([(0, 0), (1, 1)])
89
line2 = LineString([(1, 1), (2, 0)])
90
line3 = LineString([(2, 0), (3, 1)])
91
92
# Combine into MultiLineString
93
multi_line = MultiLineString([line1, line2, line3])
94
95
# Merge connected segments
96
merged = shapely.line_merge(multi_line)
97
print(f"Original segments: {len(multi_line.geoms)}")
98
print(f"Merged geometry type: {merged.geom_type}")
99
print(f"Merged coordinates: {list(merged.coords)}")
100
```
101
102
### Shared Paths
103
104
Find common segments between linear geometries.
105
106
```python { .api }
107
def shared_paths(a, b, **kwargs):
108
"""
109
Find shared paths between two linear geometries.
110
111
Parameters:
112
- a: first linear geometry
113
- b: second linear geometry
114
115
Returns:
116
GeometryCollection: shared path segments
117
"""
118
```
119
120
### Shortest Line
121
122
Find shortest connecting line between geometries.
123
124
```python { .api }
125
def shortest_line(a, b, **kwargs):
126
"""
127
Find shortest line connecting two geometries.
128
129
Parameters:
130
- a: first geometry
131
- b: second geometry
132
133
Returns:
134
LineString: shortest connecting line
135
"""
136
```
137
138
**Usage Example:**
139
140
```python
141
import shapely
142
from shapely.geometry import Point, Polygon
143
144
# Find shortest line between geometries
145
point = Point(0, 0)
146
polygon = Polygon([(5, 2), (8, 2), (8, 5), (5, 5)])
147
148
shortest = shapely.shortest_line(point, polygon)
149
print(f"Shortest line length: {shortest.length:.2f}")
150
print(f"Shortest line coords: {list(shortest.coords)}")
151
152
# Verify this matches the distance
153
distance = shapely.distance(point, polygon)
154
print(f"Distance matches line length: {abs(shortest.length - distance) < 1e-10}")
155
```