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 | |
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 | |
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 |
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 | |
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 | |
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 | |
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 | |
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 | |
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 |
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 | |
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 | |
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 |
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 | |
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 | |
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 | |
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 | |