Skip to content

rivabar.geometry_utils

rivabar.geometry_utils

convert_to_utm(x, y, left_utm_x, upper_utm_y, delta_x, delta_y)

Convert coordinates from pixel coordinates to UTM (Universal Transverse Mercator) coordinates.

Args: x (array): The x-coordinates in the original projection system. y (array): The y-coordinates in the original projection system. left_utm_x (float): The x-coordinate of the leftmost point in the UTM coordinate system. upper_utm_y (float): The y-coordinate of the uppermost point in the UTM coordinate system. delta_x (float): The change in x-coordinate per unit distance in the UTM coordinate system. delta_y (float): The change in y-coordinate per unit distance in the UTM coordinate system.

Returns: x_utm: the UTM x-coordinates corresponding to the input x-coordinates. y_utm: the UTM y-coordinates corresponding to the input y-coordinates.

Source code in rivabar/geometry_utils.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def convert_to_utm(x, y, left_utm_x, upper_utm_y, delta_x, delta_y):
    """
    Convert coordinates from pixel coordinates to UTM (Universal Transverse Mercator) coordinates.

    Args:
        x (array): The x-coordinates in the original projection system.
        y (array): The y-coordinates in the original projection system.
        left_utm_x (float): The x-coordinate of the leftmost point in the UTM coordinate system.
        upper_utm_y (float): The y-coordinate of the uppermost point in the UTM coordinate system.
        delta_x (float): The change in x-coordinate per unit distance in the UTM coordinate system.
        delta_y (float): The change in y-coordinate per unit distance in the UTM coordinate system.

    Returns:
        x_utm: the UTM x-coordinates corresponding to the input x-coordinates.
        y_utm: the UTM y-coordinates corresponding to the input y-coordinates.
    """
    x_utm = left_utm_x + 0.5*delta_x + x*delta_x 
    y_utm = upper_utm_y + 0.5*delta_y + y*delta_y 
    return x_utm, y_utm

convert_geographic_proj_to_utm(dirname, fname, dstCrs)

Converts a geographic projection raster to UTM projection.

Parameters:

Name Type Description Default
dirname str

Directory name where the source raster file is located.

required
fname str

Filename of the source raster file.

required
dstCrs dict or str

The destination coordinate reference system (CRS) for the UTM projection.

required

Returns:

Type Description
None

The function saves the reprojected raster to a new file with '_UTM.tif' suffix in the same directory.

Source code in rivabar/geometry_utils.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def convert_geographic_proj_to_utm(dirname, fname, dstCrs):
    """
    Converts a geographic projection raster to UTM projection.

    Parameters
    ----------
    dirname : str
        Directory name where the source raster file is located.
    fname : str
        Filename of the source raster file.
    dstCrs : dict or str
        The destination coordinate reference system (CRS) for the UTM projection.

    Returns
    -------
    None
        The function saves the reprojected raster to a new file with '_UTM.tif' suffix in the same directory.
    """
    #open source raster
    srcRst = rasterio.open(dirname+fname)
    #calculate transform array and shape of reprojected raster
    transform, width, height = calculate_default_transform(
            srcRst.crs, dstCrs, srcRst.width, srcRst.height, *srcRst.bounds)
    #working of the meta for the destination raster
    kwargs = srcRst.meta.copy()
    kwargs.update({
            'crs': dstCrs,
            'transform': transform,
            'width': width,
            'height': height
        })
    #open destination raster
    dstRst = rasterio.open(dirname+fname[:-4]+'_UTM.tif', 'w', **kwargs)
    #reproject and save raster band data
    for i in range(1, srcRst.count + 1):
        reproject(
            source=rasterio.band(srcRst, i),
            destination=rasterio.band(dstRst, i),
            src_crs=srcRst.crs,
            dst_crs=dstCrs,
            resampling=Resampling.nearest)
    #close destination raster
    dstRst.close()

closest_point_on_segment(p, a, b)

Calculate the closest point on a line segment to a given point.

Parameters:

Name Type Description Default
p ndarray

The point from which the closest point on the segment is to be found.

required
a ndarray

The starting point of the line segment.

required
b ndarray

The ending point of the line segment.

required

Returns:

Type Description
ndarray

The closest point on the line segment to the point p.

Notes

This function assumes that p, a, and b are numpy arrays of the same dimension.

Source code in rivabar/geometry_utils.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def closest_point_on_segment(p, a, b):
    """
    Calculate the closest point on a line segment to a given point.

    Parameters
    ----------
    p : numpy.ndarray
        The point from which the closest point on the segment is to be found.
    a : numpy.ndarray
        The starting point of the line segment.
    b : numpy.ndarray
        The ending point of the line segment.

    Returns
    -------
    numpy.ndarray
        The closest point on the line segment to the point `p`.

    Notes
    -----
    This function assumes that `p`, `a`, and `b` are numpy arrays of the same dimension.
    """
    ap = p - a
    ab = b - a
    t = np.dot(ap, ab) / np.dot(ab, ab)
    if t < 0.0:
        return a
    elif t > 1.0:
        return b
    return a + t * ab

closest_segment(line, x, y)

Finds the segment from a list of points that is closest to the midpoint of a given line.

Parameters:

Name Type Description Default
line list of numpy.ndarray

A list containing two numpy arrays representing the endpoints of the line.

required
x list of float

A list of x-coordinates of the points.

required
y list of float

A list of y-coordinates of the points.

required

Returns:

Type Description
tuple of numpy.ndarray

A tuple containing two numpy arrays representing the endpoints of the closest segment.

Source code in rivabar/geometry_utils.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def closest_segment(line, x, y):
    """
    Finds the segment from a list of points that is closest to the midpoint of a given line.

    Parameters
    ----------
    line : list of numpy.ndarray
        A list containing two numpy arrays representing the endpoints of the line.
    x : list of float
        A list of x-coordinates of the points.
    y : list of float
        A list of y-coordinates of the points.

    Returns
    -------
    tuple of numpy.ndarray
        A tuple containing two numpy arrays representing the endpoints of the closest segment.
    """
    mid_point = (line[0].flatten() + line[1].flatten()) / 2
    min_dist = float('inf')
    closest_seg = None
    for i in range(len(x) - 1):
        point_a = np.array([x[i], y[i]])
        point_b = np.array([x[i+1], y[i+1]])
        point_on_segment = closest_point_on_segment(mid_point, point_a, point_b)
        dist = np.linalg.norm(mid_point - point_on_segment)
        if dist < min_dist:
            min_dist = dist
            closest_seg = (point_a, point_b)
    return closest_seg

angle_between(v1, v2)

Calculate the angle between two vectors.

Parameters:

Name Type Description Default
v1 array_like

First input vector.

required
v2 array_like

Second input vector.

required

Returns:

Type Description
float

The angle between the two vectors in radians.

Notes

The angle is calculated using the dot product and the norms of the vectors. The result is clipped to the range [-1, 1] to avoid numerical issues with arccos. If either vector has zero length, the angle is undefined and np.nan is returned.

Examples:

>>> import numpy as np
>>> v1 = np.array([1, 0, 0])
>>> v2 = np.array([0, 1, 0])
>>> angle_between(v1, v2)
1.5707963267948966  # π/2 radians or 90 degrees
Source code in rivabar/geometry_utils.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def angle_between(v1, v2):
    """
    Calculate the angle between two vectors.

    Parameters
    ----------
    v1 : array_like
        First input vector.
    v2 : array_like
        Second input vector.

    Returns
    -------
    float
        The angle between the two vectors in radians.

    Notes
    -----
    The angle is calculated using the dot product and the norms of the vectors.
    The result is clipped to the range [-1, 1] to avoid numerical issues with arccos.
    If either vector has zero length, the angle is undefined and np.nan is returned.

    Examples
    --------
    >>> import numpy as np
    >>> v1 = np.array([1, 0, 0])
    >>> v2 = np.array([0, 1, 0])
    >>> angle_between(v1, v2)
    1.5707963267948966  # π/2 radians or 90 degrees
    """
    norm1 = np.linalg.norm(v1)
    norm2 = np.linalg.norm(v2)
    if norm1 == 0 or norm2 == 0:
        return np.nan  # or: raise ValueError("One or both vectors have zero length.")
    cos_theta = np.dot(v1, v2) / (norm1 * norm2)
    return np.arccos(np.clip(cos_theta, -1, 1))  # In radians

extract_coords(geometry)

Extract coordinates from a geometry object.

This function extracts x and y coordinates from a geometry object, handling both LineString and MultiLineString geometries. For MultiLineString geometries, it adds NaN values between line segments to facilitate plotting.

Parameters:

Name Type Description Default
geometry geometry

The geometry object from which to extract coordinates. Can be LineString, MultiLineString, or other geometry types.

required

Returns:

Name Type Description
x_all list

List of x-coordinates extracted from the geometry. For MultiLineString, includes NaN values between segments.

y_all list

List of y-coordinates extracted from the geometry. For MultiLineString, includes NaN values between segments.

Source code in rivabar/geometry_utils.py
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
def extract_coords(geometry):
    """
    Extract coordinates from a geometry object.

    This function extracts x and y coordinates from a geometry object,
    handling both LineString and MultiLineString geometries. For MultiLineString
    geometries, it adds NaN values between line segments to facilitate plotting.

    Parameters
    ----------
    geometry : shapely.geometry
        The geometry object from which to extract coordinates.
        Can be LineString, MultiLineString, or other geometry types.

    Returns
    -------
    x_all : list
        List of x-coordinates extracted from the geometry.
        For MultiLineString, includes NaN values between segments.
    y_all : list
        List of y-coordinates extracted from the geometry.
        For MultiLineString, includes NaN values between segments.
    """
    if geometry.is_empty:
        return [], []

    if geometry.geom_type == 'LineString':
        x, y = geometry.xy
        return list(x), list(y)
    elif geometry.geom_type == 'MultiLineString':
        x_all, y_all = [], []
        for line in geometry.geoms:
            x, y = line.xy
            x_all.extend(list(x))
            y_all.extend(list(y))
            # Add NaN to separate line segments when plotting
            x_all.append(float('nan'))
            y_all.append(float('nan'))
        return x_all, y_all
    else:
        return [], []

find_matching_indices(x1, y1, x2, y2)

Find the indices of coordinates in the first set that match coordinates in the second set.

Parameters:

Name Type Description Default
x1 list or array - like

The x-coordinates of the first set.

required
y1 list or array - like

The y-coordinates of the first set.

required
x2 list or array - like

The x-coordinates of the second set.

required
y2 list or array - like

The y-coordinates of the second set.

required

Returns:

Name Type Description
matching_indices list

A list of indices from the first set where the coordinates match those in the second set.

Source code in rivabar/geometry_utils.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
def find_matching_indices(x1, y1, x2, y2):
    """
    Find the indices of coordinates in the first set that match coordinates in the second set.

    Parameters
    ----------
    x1 : list or array-like
        The x-coordinates of the first set.
    y1 : list or array-like
        The y-coordinates of the first set.
    x2 : list or array-like
        The x-coordinates of the second set.
    y2 : list or array-like
        The y-coordinates of the second set.

    Returns
    -------
    matching_indices : list
        A list of indices from the first set where the coordinates match those in the second set.
    """
    # Convert the coordinates into NumPy arrays
    coords1 = np.array(list(zip(x1, y1)))
    coords2 = np.array(list(zip(x2, y2)))
    # Create an empty list to store matching indices
    matching_indices = []
    # Use a dictionary for faster lookup of coordinates in the second set
    coords2_set = {tuple(coord): i for i, coord in enumerate(coords2)}
    # Iterate over the first set of coordinates
    for i, coord in enumerate(coords1):
        if tuple(coord) in coords2_set:
            # If the coordinate is in the second set, add the index to the list
            matching_indices.append(i)
    return matching_indices

getExtrapolatedLine(p1, p2, ratio)

Creates a line extrapolated in the p1->p2 direction.

Parameters:

Name Type Description Default
p1 tuple of float

The starting point of the line (x1, y1).

required
p2 tuple of float

The ending point of the line (x2, y2).

required
ratio float

The ratio by which to extrapolate the line, relative to the distance between P1 and p2

required

Returns:

Type Description
tuple of tuple of float

A tuple containing the starting point a and the extrapolated point b.

Source code in rivabar/geometry_utils.py
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
def getExtrapolatedLine(p1, p2, ratio):
    """
    Creates a line extrapolated in the p1->p2 direction.

    Parameters
    ----------
    p1 : tuple of float
        The starting point of the line (x1, y1).
    p2 : tuple of float
        The ending point of the line (x2, y2).
    ratio : float
        The ratio by which to extrapolate the line, relative to the distance
        between P1 and p2

    Returns
    -------
    tuple of tuple of float
        A tuple containing the starting point `a` and the extrapolated point `b`.
    """
    a = p1
    b = (p1[0]+ratio*(p2[0]-p1[0]), p1[1]+ratio*(p2[1]-p1[1]))
    return a, b

extend_line(x, y, ratio)

Extend a line by extrapolating its endpoints.

Parameters:

Name Type Description Default
x array - like

The x-coordinates of the points defining the line.

required
y array - like

The y-coordinates of the points defining the line.

required
ratio float

The ratio by which to extend the line at both ends.

required

Returns:

Name Type Description
line LineString

A LineString object representing the extended line.

Examples:

>>> import numpy as np
>>> x = np.array([1, 2, 3])
>>> y = np.array([1, 2, 3])
>>> extended_line = extend_line(x, y, 0.5)
Source code in rivabar/geometry_utils.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def extend_line(x, y, ratio):
    """
    Extend a line by extrapolating its endpoints.

    Parameters
    ----------
    x : array-like
        The x-coordinates of the points defining the line.
    y : array-like
        The y-coordinates of the points defining the line.
    ratio : float
        The ratio by which to extend the line at both ends.

    Returns
    -------
    line : LineString
        A LineString object representing the extended line.

    Examples
    --------
    >>> import numpy as np
    >>> x = np.array([1, 2, 3])
    >>> y = np.array([1, 2, 3])
    >>> extended_line = extend_line(x, y, 0.5)
    """
    p1 = (x[0], y[0])
    p2 = (x[1], y[1])
    a, b = getExtrapolatedLine(p1, p2, -ratio)
    p1 = (x[-2], y[-2])
    p2 = (x[-1], y[-1])
    c, d = getExtrapolatedLine(p1, p2, 1 + ratio)
    x_new = [b[0]] + list(x) + [d[0]]
    y_new = [b[1]] + list(y) + [d[1]]
    line = LineString(list(zip(x_new, y_new)))
    return line

find_closest_point(x1, y1, other_points)

Find the index of the closest point to a given point from a list of other points.

Parameters:

Name Type Description Default
x1 float

The x-coordinate of the given point.

required
y1 float

The y-coordinate of the given point.

required
other_points list of tuples or list of lists

A list of points where each point is represented as a tuple or list of two floats (x, y).

required

Returns:

Type Description
int

The index of the closest point in the other_points list.

Source code in rivabar/geometry_utils.py
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
def find_closest_point(x1, y1, other_points):
    """
    Find the index of the closest point to a given point from a list of other points.

    Parameters
    ----------
    x1 : float
        The x-coordinate of the given point.
    y1 : float
        The y-coordinate of the given point.
    other_points : list of tuples or list of lists
        A list of points where each point is represented as a tuple or list of two floats (x, y).

    Returns
    -------
    int
        The index of the closest point in the `other_points` list.
    """
    point = np.array([x1, y1])
    other_points = np.array(other_points)
    distances = np.linalg.norm(other_points - point, axis=1)
    min_index = np.argmin(distances)
    return min_index

find_longer_segment_coords(polygon, i1, i2, xs, ys)

Find the longer segment between two points on a Shapely polygon.

Parameters:

Name Type Description Default
polygon Polygon

The polygon from which to find the segments.

required
i1 int

The index of the first point on the polygon's exterior.

required
i2 int

The index of the second point on the polygon's exterior.

required
xs int or array - like

If an integer, it represents a specific point index. If array-like, it represents x-coordinates of points.

required
ys array - like

The y-coordinates of points, only used if xs is array-like.

required

Returns:

Type Description
tuple of array-like

The x and y coordinates of the longer segment. The coordinates are returned in the order that minimizes the distance to the provided points if xs is array-like.

Source code in rivabar/geometry_utils.py
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
def find_longer_segment_coords(polygon, i1, i2, xs, ys):
    """
    Find the longer segment between two points on a Shapely polygon.

    Parameters
    ----------
    polygon : shapely.geometry.Polygon
        The polygon from which to find the segments.
    i1 : int
        The index of the first point on the polygon's exterior.
    i2 : int
        The index of the second point on the polygon's exterior.
    xs : int or array-like
        If an integer, it represents a specific point index. If array-like, it represents x-coordinates of points.
    ys : array-like
        The y-coordinates of points, only used if xs is array-like.

    Returns
    -------
    tuple of array-like
        The x and y coordinates of the longer segment. The coordinates are returned in the order that minimizes the distance to the provided points if xs is array-like.
    """
    if type(xs) == np.int_:
        xs0 = xs

    points = list(polygon.exterior.coords)

    # Ensure i1 < i2 for simplicity
    if i1 > i2:
        i1, i2 = i2, i1

    segment1_coords = points[i1:i2 + 1]
    segment2_coords = points[i2:] + points[:i1 + 1]

    # Handle cases where segments have only 1 point
    if len(segment1_coords) == 1 and len(segment2_coords) == 1:
        # Both segments have only 1 point - return the first one
        coord = segment1_coords[0]
        return [coord[0]], [coord[1]]

    elif len(segment1_coords) == 1:
        # segment1 has only 1 point, so segment2 is longer
        if len(segment2_coords) >= 2:
            segment2 = LineString(segment2_coords)
            return _get_oriented_coords(segment2, xs, ys, xs0 if type(xs) == np.int_ else None, points)
        else:
            # This shouldn't happen, but handle gracefully
            coord = segment2_coords[0]
            return [coord[0]], [coord[1]]

    elif len(segment2_coords) == 1:
        # segment2 has only 1 point, so segment1 is longer
        if len(segment1_coords) >= 2:
            segment1 = LineString(segment1_coords)
            return _get_oriented_coords(segment1, xs, ys, xs0 if type(xs) == np.int_ else None, points)
        else:
            # This shouldn't happen, but handle gracefully
            coord = segment1_coords[0]
            return [coord[0]], [coord[1]]

    else:
        # Both segments have 2+ points - use original logic
        segment1 = LineString(segment1_coords)
        segment2 = LineString(segment2_coords)

        # Compare simplified lengths to determine longer segment
        if len(segment1.simplify(100).xy[0]) > len(segment2.simplify(100).xy[0]):
            return _get_oriented_coords(segment1, xs, ys, xs0 if type(xs) == np.int_ else None, points)
        else:
            return _get_oriented_coords(segment2, xs, ys, xs0 if type(xs) == np.int_ else None, points)

find_graph_edges_close_to_start_and_end_points(graph, start_x, start_y, end_x, end_y, left_utm_x, upper_utm_y, delta_x, delta_y)

Find graph edges that are closest to specified start and end points.

This function identifies the edges in a graph that are closest to given start and end points in UTM coordinates. It converts pixel coordinates to UTM, builds a KD-tree for efficient nearest neighbor search, and returns the node indices of the edges closest to the input points.

Parameters:

Name Type Description Default
graph Graph

The graph containing edges with 'pts' attributes. Comes from skeletonization.

required
start_x float

The x-coordinate (UTM) of the start point.

required
start_y float

The y-coordinate (UTM) of the start point.

required
end_x float

The x-coordinate (UTM) of the end point.

required
end_y float

The y-coordinate (UTM) of the end point.

required
left_utm_x float

The UTM x-coordinate of the left edge of the raster.

required
upper_utm_y float

The UTM y-coordinate of the upper edge of the raster.

required
delta_x float

The pixel width in UTM coordinates.

required
delta_y float

The pixel height in UTM coordinates.

required

Returns:

Name Type Description
start_ind1 int

The source node index of the edge closest to the start point.

end_ind1 int

The target node index of the edge closest to the start point.

start_ind2 int

The source node index of the edge closest to the end point.

end_ind2 int

The target node index of the edge closest to the end point.

Source code in rivabar/geometry_utils.py
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
def find_graph_edges_close_to_start_and_end_points(graph, start_x, start_y, end_x, end_y, left_utm_x, upper_utm_y, delta_x, delta_y):
    """
    Find graph edges that are closest to specified start and end points.

    This function identifies the edges in a graph that are closest to given start and end points
    in UTM coordinates. It converts pixel coordinates to UTM, builds a KD-tree for efficient
    nearest neighbor search, and returns the node indices of the edges closest to the input points.

    Parameters
    ----------
    graph : networkx.Graph
        The graph containing edges with 'pts' attributes. Comes from skeletonization.
    start_x : float
        The x-coordinate (UTM) of the start point.
    start_y : float
        The y-coordinate (UTM) of the start point.
    end_x : float
        The x-coordinate (UTM) of the end point.
    end_y : float
        The y-coordinate (UTM) of the end point.
    left_utm_x : float
        The UTM x-coordinate of the left edge of the raster.
    upper_utm_y : float
        The UTM y-coordinate of the upper edge of the raster.
    delta_x : float
        The pixel width in UTM coordinates.
    delta_y : float
        The pixel height in UTM coordinates.

    Returns
    -------
    start_ind1 : int
        The source node index of the edge closest to the start point.
    end_ind1 : int
        The target node index of the edge closest to the start point.
    start_ind2 : int
        The source node index of the edge closest to the end point.
    end_ind2 : int
        The target node index of the edge closest to the end point.
    """
    # find reasonable start and end points on graph edges
    edge_utm_xs = []
    edge_utm_ys = []
    ss = []
    es = []    
    for s, e, d in graph.edges:
        x = np.array(list(graph[s][e][0]['pts'][:, 1]))
        y = np.array(list(graph[s][e][0]['pts'][:, 0]))
        x, y = convert_to_utm(x, y, left_utm_x, upper_utm_y, delta_x, delta_y)
        edge_utm_xs += list(x)
        edge_utm_ys += list(y)
        ss += [s] * len(x)
        es += [e] * len(x)
    tree = KDTree(np.vstack((edge_utm_xs, edge_utm_ys)).T)
    start_ind = tree.query(np.reshape([start_x, start_y], (1, -1)))[1][0][0]
    end_ind = tree.query(np.reshape([end_x, end_y], (1, -1)))[1][0][0]
    # start_dist = tree.query(np.reshape([start_x, start_y], (1, -1)))[0][0][0]
    # end_dist = tree.query(np.reshape([end_x, end_y], (1, -1)))[0][0][0]
    start_ind1 = ss[start_ind]
    end_ind1 = es[start_ind]
    start_ind2 = ss[end_ind]
    end_ind2 = es[end_ind]
    return start_ind1, end_ind1, start_ind2, end_ind2

insert_node(graph, start_ind, end_ind, left_utm_x, upper_utm_y, delta_x, delta_y, start_x, start_y)

Inserts a new node into a graph, connecting two existing nodes with an edge. The new node will be located on an existing edge and will be as close as possible to (start_x, start_y). Updates the coordinates of the new node based on the given pixel coordinates and converts them to UTM coordinates.

Parameters: - graph (networkx.Graph): The graph object to insert the new node into. - start_ind (int): The index of the starting node. - end_ind (int): The index of the ending node. - left_utm_x (float): The x-coordinate of the leftmost point in the UTM coordinate system. - upper_utm_y (float): The y-coordinate of the uppermost point in the UTM coordinate system. - delta_x (float): The change in x-coordinate per unit distance in the UTM coordinate system. - delta_y (float): The change in y-coordinate per unit distance in the UTM coordinate system. - start_x (float): The x-coordinate of the starting point in pixel coordinates. - start_y (float): The y-coordinate of the starting point in pixel coordinates.

Returns: - graph (networkx.Graph): The updated graph with the new node and edges. - node (int): The index of the newly inserted node.

Source code in rivabar/geometry_utils.py
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
def insert_node(graph, start_ind, end_ind, left_utm_x, upper_utm_y, delta_x, delta_y, start_x, start_y):
    """
    Inserts a new node into a graph, connecting two existing nodes with an edge.
    The new node will be located on an existing edge and will be as close as possible to (start_x, start_y).
    Updates the coordinates of the new node based on the given pixel coordinates and converts them to UTM coordinates.

    Parameters:
    - graph (networkx.Graph): The graph object to insert the new node into.
    - start_ind (int): The index of the starting node.
    - end_ind (int): The index of the ending node.
    - left_utm_x (float): The x-coordinate of the leftmost point in the UTM coordinate system.
    - upper_utm_y (float): The y-coordinate of the uppermost point in the UTM coordinate system.
    - delta_x (float): The change in x-coordinate per unit distance in the UTM coordinate system.
    - delta_y (float): The change in y-coordinate per unit distance in the UTM coordinate system.
    - start_x (float): The x-coordinate of the starting point in pixel coordinates.
    - start_y (float): The y-coordinate of the starting point in pixel coordinates.

    Returns:
    - graph (networkx.Graph): The updated graph with the new node and edges.
    - node (int): The index of the newly inserted node.
    """
    x = np.array(list(graph[start_ind][end_ind][0]['pts'][:, 1])) # x pixel coordinates of the edge
    y = np.array(list(graph[start_ind][end_ind][0]['pts'][:, 0])) # y pixel coordinates of the edge
    x, y = convert_to_utm(x, y, left_utm_x, upper_utm_y, delta_x, delta_y)
    tree = KDTree(np.vstack((x, y)).T)
    # index of point in edge geometry that is closest to (start_x, start_y):
    edge_ind = tree.query(np.reshape([start_x, start_y], (1, -1)))[1][0][0] 
    node = max(list(graph.nodes)) + 1 # index for new node
    # add the new node:
    graph.add_node(node, pts = graph[start_ind][end_ind][0]['pts'][edge_ind],
               o = graph[start_ind][end_ind][0]['pts'][edge_ind].astype('float'))
    # now we add the new edges:
    x = list(graph[start_ind][end_ind][0]['pts'][:, 1]) # x pixel coordinates of the old edge
    y = list(graph[start_ind][end_ind][0]['pts'][:, 0]) # y pixel coordinates of the old edge
    point1 = np.array([x[0], y[0]]) # coordinates of the first point on the old edge
    point2 = np.array([x[-1], y[-1]]) # coordinates of the last point on the old edge
    # coordinates of the start node of the old edge:
    point3 = np.array([graph.nodes()[start_ind]['o'][1], graph.nodes()[start_ind]['o'][0]])
    # if the start node is closer to the first point of the edge:
    if np.linalg.norm(point1 - point3) < np.linalg.norm(point2 - point3):
        graph.add_edge(start_ind, node, pts = graph[start_ind][end_ind][0]['pts'][:edge_ind])
        graph.add_edge(node, end_ind, pts = graph[start_ind][end_ind][0]['pts'][edge_ind:])
    else:
        graph.add_edge(start_ind, node, pts = graph[start_ind][end_ind][0]['pts'][edge_ind:])
        graph.add_edge(node, end_ind, pts = graph[start_ind][end_ind][0]['pts'][:edge_ind])
    graph.remove_edge(start_ind, end_ind) # remove the original edge
    return graph, node