API Reference
main
create_bboxes_sam(table, name_field, crs=25831, geometry_column='geom')
Generation of Bounding Boxes to be used as prompts for SAM. They will be stored in CRS EPSG:4326.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table
|
DuckDBPyRelation
|
Table containing the geometries and the tiles (original or virtual) they are associated with. |
required |
name_field
|
str
|
Name field containing the data for the element. |
required |
crs
|
int
|
Input CRS as EPSG code. Defaults to 25831. |
25831
|
geometry_column
|
str
|
Name for the geometry column. Defaults to 'geom'. |
'geom'
|
Returns:
Name | Type | Description |
---|---|---|
tile_names |
list
|
Paths to the output tiles |
boxes |
list[list]
|
list of lists of the kind [minx, miny, maxx, maxy] for each polygon included in each tile |
Source code in apb_spatial_computer_vision/main.py
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 283 284 285 286 |
|
create_file_from_sql(table, column, name, file_name, crs)
Creates a GeoJSON file from an SQL projection ("SELECT" operation into a column)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table
|
DuckDBPyRelation
|
Table to which the select operation will be applied to |
required |
column
|
str
|
The name of the column to filter by. |
required |
name
|
str
|
The value to filter the column by. |
required |
file_name
|
str
|
Output file name without extension. It will be stored as GeoJSON |
required |
crs
|
str
|
CRS as EPSG code. |
required |
Source code in apb_spatial_computer_vision/main.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
|
create_files_from_sql(tab, column, tile_names, file_names, crs=25831)
Creates multiple GeoJSON files from an SQL projection ("SELECT" operation into a column) for each tile name. Args: tab (duckdb.DuckDBPyRelation): Table to which the select operation will be applied to column (str): The name of the column to filter by. tile_names (Iterable): Paths for each tile file_names (Iterable): Output file names without extension. They will be stored as GeoJSON crs (int, optional): CRS as EPSG code. Defaults to 25831.
Source code in apb_spatial_computer_vision/main.py
242 243 244 245 246 247 248 249 250 251 252 |
|
create_first_iteration(result, segmentation_name, input_image, sam)
Create a first segmentation with the best available contained bounding boxes in the tiles
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result
|
list[dict]
|
Contains the names for the tiles and the arrays (minx,miny,maxx,maxy) for the boxes grouped by name of tile |
required |
segmentation_name
|
str
|
The name for the segmentation object. Should be defaulted to '' |
required |
input_image
|
_type_
|
description |
required |
sam
|
SamGeo_apb
|
The SAM model |
required |
Returns:
Name | Type | Description |
---|---|---|
_type_ |
description |
Source code in apb_spatial_computer_vision/main.py
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 499 500 501 502 503 504 505 506 507 508 509 |
|
create_geojson_mass(table, name_field, output_directory, crs=25831, geometry_column='geom')
Mass generation of GeoJSON files
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table
|
DuckDBPyRelation
|
Table containing the geometries and the tiles (original or virtual) they are associated with. |
required |
name_field
|
str
|
Name field containing the data for the element |
required |
output_directory
|
str
|
Path to output directory. |
required |
crs
|
int
|
CRS as EPSG code. Defaults to 25831. |
25831
|
geometry_column
|
str
|
Name of the geometry column. Defaults to 'geom'. |
'geom'
|
Returns:
Name | Type | Description |
---|---|---|
tiles_names |
list
|
Paths to the intersected tiles |
files_names |
list
|
Paths to the output GeoJSON files |
Source code in apb_spatial_computer_vision/main.py
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 313 314 315 316 |
|
create_level_dirs(results_dir, depth)
Generates the directories for contained and limit dirs in a given pyramid level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results_dir
|
str
|
Original dir in which all the layers are to be placed. |
required |
depth
|
int
|
The level of the pyramid to be built. |
required |
Returns:
Name | Type | Description |
---|---|---|
contained_dir |
str
|
Path to the dir to host the contained entities' GeoJSON files. |
limit_dir |
str
|
Path to the dir to host the limit entities' GeoJSON files. |
Source code in apb_spatial_computer_vision/main.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
create_random_points(extents_list, tile_extents)
Generates random points along a set of boxes and also around their tiles. Stores them in table random_points in the DUCKDB connection
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extents_list
|
Iterable
|
DUCKDB extent list (format achieved using ST_EXTENT()) |
required |
tile_extents
|
Iterable
|
Paths to the files where the random points are to be placed |
required |
Source code in apb_spatial_computer_vision/main.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
|
create_sam_dirs(sam_out_dir, results, depth, contained_sam_out_images=[], limit_sam_out_images=[])
Generate the dirs for the batch SAM prediction and finds the available files to be found.
Meant for recursion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sam_out_dir
|
str
|
Path to the general dir in which the SAM predictions are desired |
required |
depth
|
int
|
Level of depth in the image pyramid desired |
required |
contained_sam_out_images
|
list
|
Names of the images with prompts totally contained into the tiles. Defaults to []. |
[]
|
limit_sam_out_images
|
list
|
Name of the images whose prompts are within several tiles and are thus saved in GDAL VRT virtual layers. Defaults to []. |
[]
|
Returns:
Type | Description |
---|---|
contained_sam_out_images, contained_sam_out_images: Returns new added file paths into original arrays. Meant for recursive use |
Source code in apb_spatial_computer_vision/main.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|
create_second_iteration(input_image, low_resolution_geometries_duckdb, segmentation_name, sam, min_expected_element_area=0.5, lowest_pixel_size=1024, contained_sam_out_images=[])
Improves an existing segmentation by applying point prompting, then compares it to the input for a cleaner refined iteration
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_image
|
Ortophoto
|
Aerial image to be segmented |
required |
low_resolution_geometries_duckdb
|
DuckDBPyRelation
|
Previous iteration or read file using the read_file() function in this modules Available formats: see ST_READ drivers in DuckDB (run DUCKDB.sql('SELECT * FROM ST_Drivers();')). |
required |
segmentation_name
|
str
|
The name for the segmentation object. |
required |
sam
|
SamGeo_apb
|
The SAM model |
required |
min_expected_element_area
|
float
|
Area of the smallest element to be expected. Defaults to 0.5. |
0.5
|
lowest_pixel_size
|
int
|
Pixel size for each tile of the pyramid. Defaults to 1024. |
1024
|
contained_sam_out_images
|
list
|
Paths to the files to be created. Defaults to []. If not declared, it will be stored in a subfolder of the image folder |
[]
|
Source code in apb_spatial_computer_vision/main.py
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 795 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 |
|
duckdb_2_gdf(tab, geometry_column, crs=25831)
Generates a GeoDataFrame from DuckDB
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tab
|
DuckDBPyRelation
|
Geospatial able to export from DUCKDB |
required |
geometry_column
|
str
|
Name for the geometry column |
required |
crs
|
int
|
CRS as EPSG code. Defaults to 25831. |
25831
|
Returns:
Type | Description |
---|---|
gpd.GeoDataFrame: Returngs the same data wiht |
Source code in apb_spatial_computer_vision/main.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
file_pyramid_sam_apply(image_path, geospatial_prompt_file_path, geometry_column, segmentation_name, sam, min_expected_element_area=0.5, lowest_pixel_size=1024)
Iteratively generate SAM segmentations
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
Aerial image to be segmented |
required |
geospatial_prompt_file_path
|
str
|
Path to any geospatial file. Available formats: see ST_READ drivers in DuckDB (run DUCKDB.sql('SELECT * FROM ST_Drivers();')). |
required |
geometry_column
|
str
|
Geometry column in the geospatial prompt file |
required |
segmentation_name
|
str
|
The name for the segmentation object. Should be defaulted to '' |
required |
sam
|
SamGeo_apb
|
The SAM model |
required |
min_expected_element_area
|
float
|
Area of the smallest element to be expected. Defaults to 0.5. |
0.5
|
lowest_pixel_size
|
int
|
Pixel size for all tiles, which will have different resolutions. Defaults to 1024. |
1024
|
Source code in apb_spatial_computer_vision/main.py
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 |
|
filter_level(detections, pyramid_dir, depths, geometry_column, segmentation_name)
Finds which elements are contained and limiting to each tile, prompting the creation of virtual layers
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detections
|
DuckDBPyRelation
|
Table with the polygon geometries to be used as box prompts |
required |
pyramid_dir
|
str
|
Path to the image pyramid |
required |
depth
|
int
|
The level of the pyramid to be built. |
required |
geometry_column
|
str
|
: Name for the geometry column. |
required |
Returns:
Type | Description |
---|---|
duckdb.DuckDBPyRelation: Contained geometries and their respective tile names |
|
duckdb.DuckDBPyRelation: Limit geometries and their respective virtual raster layer (GDAL VRT) tile names |
Source code in apb_spatial_computer_vision/main.py
95 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 124 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 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
point_prompt_based_sam(image_path, geospatial_prompt_file_path, segmentation_name, sam, min_expected_element_area=0.5, lowest_pixel_size=1024, contained_sam_out_images=[])
Reads geospatial prompt files to improve segmentation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
Path to the image to be segmented |
required |
geospatial_prompt_file_path
|
str
|
Geospatial prompt file to be called |
required |
segmentation_name
|
str
|
The name for the segmentation object |
required |
sam
|
SamGeo_apb
|
The SAM model |
required |
min_expected_element_area
|
float
|
Area of the smallest element to be expected. Defaults to 0.5. |
0.5
|
lowest_pixel_size
|
int
|
Pixel size for each tile of the pyramid. Defaults to 1024. |
1024
|
contained_sam_out_images
|
list
|
Paths to the files to be created. Defaults to []. |
[]
|
Source code in apb_spatial_computer_vision/main.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
|
post_processing(depths, pyramid_dir, detections, geometry_column='geom', segmentation_name='')
Function to store in arrays files the detections into several different elements in order to find out what happened inside the process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
depth
|
int
|
The level of the pyramid to be built. |
required |
pyramid_dir
|
str
|
Path to the image pyramid |
required |
detections
|
DuckDBPyRelation
|
Table with the polygon geometries to be used as box prompts |
required |
Returns:
Type | Description |
---|---|
list[dict]: Contains the names for the tiles and the arrays (minx,miny,maxx,maxy) for the boxes grouped by name of tile |
Source code in apb_spatial_computer_vision/main.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
|
post_processing_geojson(depths, pyramid_dir, detections, output_dir, geometry_column='geom', segmentation_name='')
Function to store in GeoJSON files the detections into several different elements in order to find out what happened inside the process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
depth
|
int
|
The level of the pyramid to be built. |
required |
pyramid_dir
|
str
|
Path to the image pyramid |
required |
detections
|
DuckDBPyRelation
|
Table with the polygon geometries to be used as box prompts |
required |
output_dir
|
str
|
Output directory |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Contains the names for the tiles and the arrays for the boxes |
Source code in apb_spatial_computer_vision/main.py
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 |
|
predict_tile(image_path, boxes, out_name, sam)
Apply SAM using BBOX.
Args:
image_path (str | np.array): Path to the tile or numpy array stemming from GDAL ReadAsArray(), or in this package tems, Ortophoto.raster.ReadAsArray()
boxes (Iterable | str [GeoJSON]): Nested list of bounds (X_min,Y_min,X_max,Y_max), or the path to a GeoJSON path containing Polygon geometries.
out_name (str): Name for the output file, can be either vector (GeoJSON) or Raster (GeoTIFF)
sam (SamGeo_apb): The SAM model
Source code in apb_spatial_computer_vision/main.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
|
predict_tile_points(image_path, boxes, out_name, point_coords, point_labels, sam)
Calls SAM predict using point prompts
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
Image path |
required |
boxes
|
Iterable
|
Nested list of bounds (X_min,Y_min,X_max,Y_max), or the path to a GeoJSON path containing Polygon geometries. |
required |
out_name
|
str
|
Name of the output file. |
required |
point_coords
|
Iterable
|
[(x1,y1),...,(xn,yn)] with the points |
required |
point_labels
|
Iterable
|
0 (negative) or 1 (positive) labeling of the points |
required |
sam
|
SamGeo_apb
|
The SAM model |
required |
Returns:
Name | Type | Description |
---|---|---|
_type_ |
description |
Source code in apb_spatial_computer_vision/main.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
|
prediction_to_bbox(gdf, crs=25831)
Generates the oriented envelope of a geodataframe
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf
|
GeoDataFrame
|
Polygon geometry |
required |
crs
|
int
|
CRS as EPSG code. Defaults to 25831. |
25831
|
Returns:
Type | Description |
---|---|
gpd.GeoDataFrame: Rotated envelope geometry |
Source code in apb_spatial_computer_vision/main.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
pyramid_sam_apply(input_image, detections, geometry_column, segmentation_name, sam, min_expected_element_area=0.5, lowest_pixel_size=1024)
Iteratively generate SAM segmentations using image pyramid algorithms. First through bounding box and then refined using point prompts
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_image
|
Ortophoto
|
The complete image, whose pyramid will be dynamically created if not already loaded into the object |
required |
detections
|
DuckDBPyRelation | str | Iterable
|
Polygon geometry with a detection to be better segmented Available formats: see ST_READ drivers in DuckDB (run DUCKDB.sql('SELECT * FROM ST_Drivers();')). |
required |
geometry_column
|
str
|
Geometry column in the geospatial prompt file |
required |
segmentation_name
|
str
|
The name for the segmentation object. Should be defaulted to '' |
required |
sam
|
SamGeo_apb
|
The SAM model |
required |
lowest_pixel_size
|
int
|
Pixel size for all tiles, which will have different resolutions. Defaults to 1024. |
1024
|
min_expected_element_area
|
float
|
Area of the smallest element to be expected. Defaults to 0.5. |
0.5
|
Source code in apb_spatial_computer_vision/main.py
561 562 563 564 565 566 567 568 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 |
|
pyramid_sam_apply_geojson(image_path, geospatial_prompt_file_path, geometry_column, segmentation_name, sam, lowest_pixel_size=1024, min_expected_element_area=0.5, results_dir=None)
Generate a pyramid with intermediate GeoJSON files. Slower performance but allows for repeatability
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
str
|
Aerial image to be segmented |
required |
geospatial_prompt_file_path
|
str
|
Path to any geospatial file. Available formats: see ST_READ drivers in DuckDB (run DUCKDB.sql('SELECT * FROM ST_Drivers();')). |
required |
geometry_column
|
str
|
Geometry column in the geospatial prompt file |
required |
segmentation_name
|
str
|
The name for the segmentation object. Should be defaulted to '' |
required |
sam
|
SamGeo_apb
|
The SAM model |
required |
lowest_pixel_size
|
int
|
Pixel size for all tiles, which will have different resolutions. Defaults to 1024. |
1024
|
min_expected_element_area
|
float
|
Area of the smallest element to be expected. Defaults to 0.5. |
0.5
|
results_dir
|
str
|
Where to store the GeoJSON files. Defaults to None, thus storing in the project folder using under "GeoJSON_bboxes_{segmentation_name}" |
None
|
Source code in apb_spatial_computer_vision/main.py
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 |
|
read_file(paths, DUCKDB=DUCKDB, layer=None)
Geospatial data reader for DuckDB
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paths
|
Iterable | str
|
Path/s to the file/s |
required |
DUCKDB
|
DuckDBPyConnection
|
Defaults to DUCKDB (environment variable). |
DUCKDB
|
Returns:
Type | Description |
---|---|
duckdb.DuckDBPyRelation: Table containing the elements |
Source code in apb_spatial_computer_vision/main.py
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 |
|
text_to_bbox_lowres_complete(input_image, text_prompt, output=None, sam=None)
Generate bouding boxes from a text prompt. Calls the Grounding DINO parts of LangSAM excluding SAM use, speeding up the prediction of this task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_image
|
Ortophoto
|
Aerial image where the element should be identified |
required |
text_prompt
|
str
|
Prompt for Grounding DINO. |
required |
output
|
str
|
Path of where to store the bounding box geometries. Defaults to None |
None
|
Returns:
Name | Type | Description |
---|---|---|
bboxes_duckdb |
DuckdbPyRelation
|
Spatial database table with the bounding boxes found by Grounding DINO. |
Source code in apb_spatial_computer_vision/main.py
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 |
|