NetworkRoutingComputer
A class to compute network routes based on a specified mode of transportation.
This class provides methods to find the closest source and target nodes, compute routes, and manage the graph structure for routing.
Attributes:
Name | Type | Description |
---|---|---|
mode |
str
|
The mode of transportation ('walk', 'bike', 'drive'). |
G_ig |
Graph
|
The graph representing the transportation network. |
v_coordinates |
list
|
List of vertex coordinates in the graph. |
A |
ndarray
|
Array of vertex coordinates. |
tree |
cKDTree
|
KDTree for spatial indexing of vertex coordinates. |
names |
list
|
List of vertex names. |
weights |
list
|
List of edge weights in the graph. |
route_cache |
dict
|
Cache to store computed routes. |
speed_factor |
int
|
Factor to adjust travel speed based on the mode of transportation. |
Methods:
Name | Description |
---|---|
get_closest_source_target |
Find the closest source and target nodes in the graph for the given coordinates. |
path_to_linestring |
Convert a path in the graph to a LineString. |
compute_route |
Compute the route and travel time between source and target coordinates. |
Source code in tripsender\routing.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
|
TransitMatrixComputer
Source code in tripsender\routing.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
|
__init__(hdf5_path=hdf5_path, origins_gdf=origins_gdf)
A class to compute and manage a transit matrix, providing travel times between origin-destination pairs.
This class preprocesses data from an HDF5 file and uses a KDTree for fast lookup of travel times. It also provides methods to query travel times and compute routes between coordinates.
Attributes:
Name | Type | Description |
---|---|---|
hdf5_path |
str
|
Path to the HDF5 file containing the transit data. |
travel_times_dict |
dict
|
Dictionary to store travel times between origin-destination pairs. |
origins_gdf |
GeoDataFrame
|
GeoDataFrame containing origin points. |
points |
ndarray
|
Array of coordinates extracted from the origins_gdf. |
tree |
KDTree
|
KDTree for spatial indexing of origin points. |
ids |
ndarray
|
Array of IDs corresponding to the origin points. |
Functions:
Name | Description |
---|---|
preprocess_data |
Preprocess the data from the HDF5 file and store it in a dictionary for fast lookups. |
get_closest_id_tree |
Get the closest origin ID to the given latitude and longitude using the KDTree. |
query_travel_time |
Query the travel time between two coordinates. |
compute_route |
Compute the route and travel time between source and target coordinates. |
Source code in tripsender\routing.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
|
preprocess_data()
Preprocess the data from the HDF5 file and store it in a dictionary for fast lookups.
Source code in tripsender\routing.py
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
|
compute_edge_weights(coords, edge_length, dem_raster_path, landuse_vector, convex_hull)
Compute edge weights for the graph based on edge length, slope, and proximity to natural features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coords |
list
|
List of coordinates for each edge. |
required |
edge_length |
list
|
List of edge lengths. |
required |
dem_raster_path |
str
|
Path to the DEM raster file. |
required |
landuse_vector |
GeoDataFrame
|
Land use vector data. |
required |
convex_hull |
Polygon
|
Convex hull for clipping. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
Computed edge weights. |
Source code in tripsender\routing.py
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 |
|
compute_routes_for_all_buildings(mode='walk', parallel=False)
Compute routes for all buildings in the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode |
str
|
Mode of transportation ('walk', 'bike', 'drive'). Defaults to 'walk'. |
'walk'
|
parallel |
bool
|
Whether to process buildings in parallel. Defaults to False. |
False
|
Source code in tripsender\routing.py
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 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
|
create_gradient(clipped_vector_landuse, out_shape, out_transform, max_distance=200.0)
Create a gradient effect of natural features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clipped_vector_landuse |
GeoDataFrame
|
Clipped land use vector data. |
required |
out_shape |
tuple
|
Shape of the output raster. |
required |
out_transform |
Affine
|
Transform of the output raster. |
required |
max_distance |
float
|
Maximum distance for gradient scaling. Defaults to 200.0. |
200.0
|
Returns:
Name | Type | Description |
---|---|---|
ndarray |
Gradient raster. |
Source code in tripsender\routing.py
209 210 211 212 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 246 247 |
|
find_closest_source_target(tree, G_ig, source_coord, targets_coords)
Find the closest source and target nodes in the graph using a KDTree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree |
cKDTree
|
The KDTree for spatial indexing. |
required |
G_ig |
Graph
|
The input graph. |
required |
source_coord |
Point
|
The source coordinate. |
required |
targets_coords |
list
|
The target coordinates. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
Closest source and target nodes. |
Source code in tripsender\routing.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
get_edge_coords(G_ig)
Get the coordinates of edges in the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G_ig |
Graph
|
The input graph. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
A list of tuples representing edge coordinates. |
Source code in tripsender\routing.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
get_edge_lengths(G_ig)
Compute the lengths of geometries for each edge in the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G_ig |
Graph
|
The input graph. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
A list of edge lengths. |
Source code in tripsender\routing.py
55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
get_edge_travel_time(G_ig)
Compute the travel times for each edge in the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G_ig |
Graph
|
The input graph. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
A list of travel times. |
Source code in tripsender\routing.py
69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
get_vertex_coords(G_ig)
Get the coordinates of vertices in the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G_ig |
Graph
|
The input graph. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
A list of tuples representing vertex coordinates. |
Source code in tripsender\routing.py
83 84 85 86 87 88 89 90 91 92 93 94 |
|
normalise_list(input_list)
Normalize a list of values to a 0-1 range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_list |
list
|
List of values. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
Normalized list of values. |
Source code in tripsender\routing.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
|
path_to_linestring(path_indices, G_ig, s, t)
Convert a path in the graph to a LineString.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_indices |
list
|
List of vertex indices representing the path. |
required |
G_ig |
Graph
|
The input graph. |
required |
s |
str
|
Source node identifier. |
required |
t |
str
|
Target node identifier. |
required |
Returns:
Type | Description |
---|---|
LineString or Point: The LineString representing the path or a Point if the path is a single point. |
Source code in tripsender\routing.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
|
plot_gradient(gradient, transform, title='Gradient Effect of Natural Features', cmap='viridis', figsize=(10, 5))
Plot the gradient effect of natural features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gradient |
ndarray
|
Gradient raster. |
required |
transform |
Affine
|
Transform of the raster. |
required |
title |
str
|
Title of the plot. Defaults to 'Gradient Effect of Natural Features'. |
'Gradient Effect of Natural Features'
|
cmap |
str
|
Colormap for the plot. Defaults to 'viridis'. |
'viridis'
|
figsize |
tuple
|
Figure size. Defaults to (10, 5). |
(10, 5)
|
Source code in tripsender\routing.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
|
process_building(building, tree, A, G_ig, route_cache, mode)
Process a single building to compute routes to preferred locations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
building |
Building
|
The building to process. |
required |
tree |
cKDTree
|
KDTree for spatial indexing. |
required |
A |
ndarray
|
Array of vertex coordinates. |
required |
G_ig |
Graph
|
The input graph. |
required |
route_cache |
dict
|
Cache to store computed routes. |
required |
mode |
str
|
Mode of transportation ('walk', 'car', 'bike'). |
required |
Returns:
Name | Type | Description |
---|---|---|
Building |
The updated building with computed routes. |
Source code in tripsender\routing.py
435 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 |
|
process_building_batch(building_batch, tree, A, G_ig, route_cache, mode)
Process a batch of buildings to compute routes in parallel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
building_batch |
list
|
List of buildings to process. |
required |
tree |
cKDTree
|
KDTree for spatial indexing. |
required |
A |
ndarray
|
Array of vertex coordinates. |
required |
G_ig |
Graph
|
The input graph. |
required |
route_cache |
dict
|
Cache to store computed routes. |
required |
mode |
str
|
Mode of transportation ('walk', 'car', 'bike'). |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
List of updated buildings with computed routes. |
Source code in tripsender\routing.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
|
process_landuse_data(landuse_vector, convex_hull, dem_raster_crs='EPSG:3006')
Process land use data by clipping and dissolving geometries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
landuse_vector |
GeoDataFrame
|
The land use vector data. |
required |
convex_hull |
Polygon
|
The convex hull for clipping. |
required |
dem_raster_crs |
str
|
The CRS of the DEM raster. Defaults to 'EPSG:3006'. |
'EPSG:3006'
|
Returns:
Name | Type | Description |
---|---|---|
GeoDataFrame |
Processed and clipped land use vector data. |
Source code in tripsender\routing.py
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 |
|
raster_value_at_line_ends(raster, transform, start_coord, end_coord, value_type='average')
Retrieve raster values at the ends of a line segment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster |
ndarray
|
The raster data. |
required |
transform |
Affine
|
Affine transform of the raster. |
required |
start_coord |
tuple
|
Start coordinate. |
required |
end_coord |
tuple
|
End coordinate. |
required |
value_type |
str
|
Type of value to return ('average' or 'absolute_difference'). |
'average'
|
Returns:
Name | Type | Description |
---|---|---|
float |
The calculated value. |
Source code in tripsender\routing.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
read_and_clip_raster(dem_raster_path, convex_hull)
Read and clip a DEM raster using a convex hull.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dem_raster_path |
str
|
Path to the DEM raster file. |
required |
convex_hull |
Polygon
|
The convex hull for clipping. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
Clipped raster image, transform, and metadata. |
Source code in tripsender\routing.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
rowcol(transform, x, y)
Convert real-world coordinates to pixel coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transform |
Affine
|
Affine transform. |
required |
x |
float
|
X-coordinate. |
required |
y |
float
|
Y-coordinate. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
Row and column indices. |
Source code in tripsender\routing.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|