utilities module¶
check_file_path(file_path, make_dirs=True)
¶
Gets the absolute file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
The path to the file. |
required |
make_dirs |
bool
|
Whether to create the directory if it does not exist. Defaults to True. |
True
|
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the directory could not be found. |
TypeError
|
If the input directory path is not a string. |
Returns:
Name | Type | Description |
---|---|---|
str | The absolute path to the file. |
Source code in lidar/common.py
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 |
|
check_install(package)
¶
Checks whether a package is installed. If not, it will install the package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package |
str
|
The name of the package to check. |
required |
Source code in lidar/common.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
|
clip_image(image, mask, output)
¶
Clip an image by mask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
str
|
Path to the image file in GeoTIFF format. |
required |
mask |
str | list | dict
|
The mask used to extract the image. It can be a path to vector datasets (e.g., GeoJSON, Shapefile), a list of coordinates, or m.user_roi. |
required |
output |
str
|
Path to the output file. |
required |
Raises:
Type | Description |
---|---|
ImportError
|
If the fiona or rasterio package is not installed. |
FileNotFoundError
|
If the image is not found. |
ValueError
|
If the mask is not a valid GeoJSON or raster file. |
FileNotFoundError
|
If the mask file is not found. |
Source code in lidar/common.py
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 667 668 669 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 |
|
clone_repo(out_dir='.', unzip=True)
¶
Clones the lidar GitHub repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_dir |
str
|
Output folder for the repo. Defaults to '.'. |
'.'
|
unzip |
bool
|
Whether to unzip the repository. Defaults to True. |
True
|
Source code in lidar/common.py
315 316 317 318 319 320 321 322 323 324 |
|
csv_points_to_shp(in_csv, out_shp, latitude='latitude', longitude='longitude')
¶
Converts a csv file containing points (latitude, longitude) into a shapefile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_csv |
str
|
File path or HTTP URL to the input csv file. For example, https://raw.githubusercontent.com/giswqs/data/main/world/world_cities.csv |
required |
out_shp |
str
|
File path to the output shapefile. |
required |
latitude |
str
|
Column name for the latitude column. Defaults to 'latitude'. |
'latitude'
|
longitude |
str
|
Column name for the longitude column. Defaults to 'longitude'. |
'longitude'
|
Source code in lidar/common.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
csv_to_shp(in_csv, out_shp, latitude='latitude', longitude='longitude')
¶
Converts a csv file with latlon info to a point shapefile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_csv |
str
|
The input csv file containing longitude and latitude columns. |
required |
out_shp |
str
|
The file path to the output shapefile. |
required |
latitude |
str
|
The column name of the latitude column. Defaults to 'latitude'. |
'latitude'
|
longitude |
str
|
The column name of the longitude column. Defaults to 'longitude'. |
'longitude'
|
Source code in lidar/common.py
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 305 306 307 308 309 310 311 312 |
|
download_file(url=None, output=None, quiet=False, proxy=None, speed=None, use_cookies=True, verify=True, id=None, fuzzy=False, resume=False, unzip=True, overwrite=False)
¶
Download a file from URL, including Google Drive shared URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
Google Drive URL is also supported. Defaults to None. |
None
|
output |
str
|
Output filename. Default is basename of URL. |
None
|
quiet |
bool
|
Suppress terminal output. Default is False. |
False
|
proxy |
str
|
Proxy. Defaults to None. |
None
|
speed |
float
|
Download byte size per second (e.g., 256KB/s = 256 * 1024). Defaults to None. |
None
|
use_cookies |
bool
|
Flag to use cookies. Defaults to True. |
True
|
verify |
bool | str
|
Either a bool, in which case it controls whether the server's TLS certificate is verified, or a string, in which case it must be a path to a CA bundle to use. Default is True.. Defaults to True. |
True
|
id |
str
|
Google Drive's file ID. Defaults to None. |
None
|
fuzzy |
bool
|
Fuzzy extraction of Google Drive's file Id. Defaults to False. |
False
|
resume |
bool
|
Resume the download from existing tmp file if possible. Defaults to False. |
False
|
unzip |
bool
|
Unzip the file. Defaults to True. |
True
|
overwrite |
bool
|
Overwrite the file if it already exists. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
str | The output file path. |
Source code in lidar/common.py
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 71 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 |
|
download_folder(url=None, id=None, output=None, quiet=False, proxy=None, speed=None, use_cookies=True, remaining_ok=False)
¶
Downloads the entire folder from URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
URL of the Google Drive folder. Must be of the format 'https://drive.google.com/drive/folders/{url}'. Defaults to None. |
None
|
id |
str
|
Google Drive's folder ID. Defaults to None. |
None
|
output |
str
|
String containing the path of the output folder. Defaults to current working directory. |
None
|
quiet |
bool
|
Suppress terminal output. Defaults to False. |
False
|
proxy |
str
|
Proxy. Defaults to None. |
None
|
speed |
float
|
Download byte size per second (e.g., 256KB/s = 256 * 1024). Defaults to None. |
None
|
use_cookies |
bool
|
Flag to use cookies. Defaults to True. |
True
|
resume |
bool
|
Resume the download from existing tmp file if possible. Defaults to False. |
required |
Returns:
Name | Type | Description |
---|---|---|
list | List of files downloaded, or None if failed. |
Source code in lidar/common.py
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 124 125 126 127 128 129 |
|
download_from_gdrive(gfile_url, file_name, out_dir='.', unzip=True, verbose=True)
¶
Download a file shared via Google Drive (e.g., https://drive.google.com/file/d/18SUo_HcDGltuWYZs1s7PpOmOq_FvFn04/view?usp=sharing)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gfile_url |
str
|
The Google Drive shared file URL |
required |
file_name |
str
|
The output file name to use. |
required |
out_dir |
str
|
The output directory. Defaults to '.'. |
'.'
|
unzip |
bool
|
Whether to unzip the output file if it is a zip file. Defaults to True. |
True
|
verbose |
bool
|
Whether to display or not the output of the function |
True
|
Source code in lidar/common.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
download_from_url(url, out_file_name=None, out_dir='.', unzip=True, verbose=True)
¶
Download a file from a URL (e.g., https://github.com/giswqs/whitebox/raw/master/examples/testdata.zip)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The HTTP URL to download. |
required |
out_file_name |
str
|
The output file name to use. Defaults to None. |
None
|
out_dir |
str
|
The output directory to use. Defaults to '.'. |
'.'
|
unzip |
bool
|
Whether to unzip the downloaded file if it is a zip file. Defaults to True. |
True
|
verbose |
bool
|
Whether to display or not the output of the function |
True
|
Source code in lidar/common.py
132 133 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 170 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 |
|
download_ned_by_bbox(bbox, datasets=None, out_dir=None, return_url=False, download_args={}, **kwargs)
¶
Download the US National Elevation Datasets (NED) for a bounding box. See https://apps.nationalmap.gov/tnmaccess/#/ for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bbox |
list
|
The bounding box in the form [xmin, ymin, xmax, ymax]. |
required |
huc_type |
str
|
The HUC type, e.g., huc2, huc4, huc8. Defaults to "huc8". |
required |
datasets |
str
|
Comma-delimited list of valid dataset tag names. The commonly used datasets include: Digital Elevation Model (DEM) 1 meter National Elevation Dataset (NED) 1/3 arc-second Current National Elevation Dataset (NED) 1/9 arc-second Current National Elevation Dataset (NED) 1 arc-second Current For more information, see https://apps.nationalmap.gov/tnmaccess/#/product Defaults to None, which will be the (NED) 1/3 arc-second |
None
|
out_dir |
str
|
The output directory. Defaults to None, which will use the current working directory. |
None
|
return_url |
bool
|
If True, the URL will be returned instead of downloading the data. Defaults to False. |
False
|
download_args |
dict
|
The download arguments to be passed to the download_file function. Defaults to {}. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
list | The list of downloaded files. |
Source code in lidar/common.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 |
|
download_ned_by_huc(huc_id, huc_type='huc8', datasets=None, out_dir=None, return_url=False, download_args={}, **kwargs)
¶
Download the US National Elevation Datasets (NED) for a Hydrologic Unit region. See https://apps.nationalmap.gov/tnmaccess/#/ for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
huc_id |
str
|
The HUC ID, for example, "01010002" |
required |
huc_type |
str
|
The HUC type, e.g., huc2, huc4, huc8. Defaults to "huc8". |
'huc8'
|
datasets |
str
|
Comma-delimited list of valid dataset tag names. The commonly used datasets include: Digital Elevation Model (DEM) 1 meter National Elevation Dataset (NED) 1/3 arc-second Current National Elevation Dataset (NED) 1/9 arc-second Current National Elevation Dataset (NED) 1 arc-second Current For more information, see https://apps.nationalmap.gov/tnmaccess/#/product Defaults to None, which will be the (NED) 1/3 arc-second |
None
|
out_dir |
str
|
The output directory. Defaults to None, which will use the current working directory. |
None
|
return_url |
bool
|
If True, the URL will be returned instead of downloading the data. Defaults to False. |
False
|
download_args |
dict
|
The download arguments to be passed to the download_file function. Defaults to {}. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
list | The list of downloaded files. |
Source code in lidar/common.py
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 |
|
geometry_bounds(geometry, decimals=4)
¶
Returns the bounds of a geometry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geometry |
dict
|
A GeoJSON geometry. |
required |
decimals |
int
|
The number of decimal places to round the bounds to. Defaults to 4. |
4
|
Returns:
Name | Type | Description |
---|---|---|
list | A list of bounds in the form of [minx, miny, maxx, maxy]. |
Source code in lidar/common.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
|
github_raw_url(url)
¶
Get the raw URL for a GitHub file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The GitHub URL. |
required |
Returns:
Name | Type | Description |
---|---|---|
str | The raw URL. |
Source code in lidar/common.py
416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
in_colab_shell()
¶
Tests if the code is being executed within Google Colab.
Source code in lidar/common.py
10 11 12 13 14 15 16 17 |
|
is_drive_mounted()
¶
Checks whether Google Drive is mounted in Google Colab.
Returns:
Name | Type | Description |
---|---|---|
bool | Returns True if Google Drive is mounted, False otherwise. |
Source code in lidar/common.py
20 21 22 23 24 25 26 27 28 29 30 |
|
is_tool(name)
¶
Check whether name
is on PATH and marked as executable.
Source code in lidar/common.py
391 392 393 394 395 396 |
|
join_tables(in_shp, in_csv, out_shp)
¶
Joins a CSV table to a shapefile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_shp |
str
|
Path to the input shapefile. |
required |
in_csv |
str
|
Path to the input CSV file. |
required |
out_shp |
str
|
Path to the output shapefile. |
required |
Source code in lidar/common.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 |
|
mosaic(images, output, ext='.tif', merge_args={}, verbose=True, **kwargs)
¶
Mosaics a list of images into a single image. Inspired by https://bit.ly/3A6roDK.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images |
str | list
|
An input directory containing images or a list of images. |
required |
output |
str
|
The output image filepath. |
required |
ext |
str
|
The image file extension. Defaults to '.tif'. |
'.tif'
|
merge_args |
dict
|
A dictionary of arguments to pass to the rasterio.merge function. Defaults to {}. |
{}
|
verbose |
bool
|
Whether to print progress. Defaults to True. |
True
|
Source code in lidar/common.py
431 432 433 434 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 488 489 490 491 |
|
random_string(string_length=3)
¶
Generates a random string of fixed length.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string_length |
int
|
Fixed length. Defaults to 3. |
3
|
Returns:
Name | Type | Description |
---|---|---|
str | A random string |
Source code in lidar/common.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
|
reproject_image(image, output, dst_crs='EPSG:4326', resampling='nearest', **kwargs)
¶
Reprojects an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
str
|
The input image filepath. |
required |
output |
str
|
The output image filepath. |
required |
dst_crs |
str
|
The destination CRS. Defaults to "EPSG:4326". |
'EPSG:4326'
|
resampling |
Resampling
|
The resampling method. Defaults to "nearest". |
'nearest'
|
**kwargs |
Additional keyword arguments to pass to rasterio.open. |
{}
|
Source code in lidar/common.py
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 566 567 568 569 570 571 |
|
resample(src, dst, resolution, **kwargs)
¶
Resample a raster to a new resolution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src |
str
|
The source raster. |
required |
dst |
str
|
The destination raster. |
required |
resolution |
float
|
The new resolution. |
required |
Source code in lidar/common.py
866 867 868 869 870 871 872 873 874 875 876 |
|
temp_file_path(extension)
¶
Returns a temporary file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extension |
str
|
The file extension. |
required |
Returns:
Name | Type | Description |
---|---|---|
str | The temporary file path. |
Source code in lidar/common.py
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
|
update_package()
¶
Updates the lidar package from the lidar GitHub repository without the need to use pip or conda. In this way, I don't have to keep updating pypi and conda-forge with every minor update of the package.
Source code in lidar/common.py
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 |
|