util
¶
The Util sub-module contains a number of generally helpful utility functions, classes, and constants.
Functions:
-
KernelProcessor–A decorator which automates the production of kernel processors for use
-
compare_geoms–Compare two lists of geometries and return a list of booleans indicating whether each pair of geometries are equal.
-
drawImage–Draw a matrix as an image on a matplotlib canvas.
-
isRaster–Test if loadRaster fails for the given input.
-
isVector–Test if loadVector fails for the given input.
-
quickRaster–GeoKit internal for quickly creating a raster datasource.
-
quickVector–GeoKit internal for quickly creating a vector datasource.
-
scaleMatrix–Scale a 2-dimensional matrix. For example, a 2x2 matrix, with a scale of 2,
KernelProcessor
¶
KernelProcessor(
size, edgeValue=0, outputType=None, passIndex=False
)
A decorator which automates the production of kernel processors for use in mutateRaster (although it could really used for processing any matrix).
Parameters:
-
(size¶int) –The number of pixels to expand around a center pixel * A 'size' of 0 would make a processing matrix with size 1x1. As in, just the value at each point. This would be silly to call... * A 'size' of 1 would make a processing matrix of size 3x3. As in, one pixel around the center pixel in all directions * Processed matrix size is equal to 2*size+1
-
(edgeValue¶numeric; optional, default:0) –The value to apply to the edges of the matrix before applying the kernel * Will be factored into the kernelling when processing near the edges
-
(outputType¶np.dtype; optional, default:None) –The datatype of the processed values * Only useful if the output type of the kerneling step is different from the matrix input type
-
(passIndex¶bool, default:False) –Whether or not to pass the x and y index to the processing function * If True, the decorated function must accept an input called 'xi' and 'yi' in addition to the matrix * The xi and yi correspond to the index of the center pixel in the original matrix
Returns:
-
function–
Example:
- Say we want to make a processor which calculates the average of pixels which are within a distance of 2 indices. In other words, we want the average of a 5x5 matrix centered around each pixel.
- Assume that we can use the value -9999 as a no data value
@KernelProcessor(2, edgeValue=-9999) def getMean( mat ): # Get only good values goodValues = mat[mat!=-9999]
# Return the mean return goodValues.mean()
Source code in geokit/core/util.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 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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
compare_geoms
¶
Compare two lists of geometries and return a list of booleans indicating whether each pair of geometries are equal. The order of the lists is important, as the first geometry in the first list will be compared to the first geometry in the second list, and so on.
Returns:
-
list–A list of booleans indicating whether each pair of geometries are equal.
Source code in geokit/core/util.py
drawImage
¶
drawImage(
matrix: ndarray,
ax: Axis | None = None,
xlim: tuple[float | float] | None = None,
ylim: tuple[float | float] | None = None,
yAtTop: bool = True,
scaling: float = 1,
fontsize: int = 16,
hideAxis=False,
figsize: tuple[float, float] = (12, 12),
cbar: bool = True,
cbarPadding=0.01,
cbarTitle=None,
vmin=None,
vmax=None,
cmap="viridis",
cbax=None,
cbargs=None,
leftMargin=0,
rightMargin=0,
topMargin=0,
bottomMargin=0,
**kwargs,
) -> AxHands
Draw a matrix as an image on a matplotlib canvas.
Parameters:
-
(matrix¶ndarray) –The matrix data to draw
-
(ax¶matplotlib.axis.Axis; optional, default:None) –The axis to draw the geometries on * If not given, a new axis is generated and returned
-
(xlim¶tuple[float, float]; optional, default:None) –The x-axis limits to draw the matrix on
-
(ylim¶tuple[float, float]; optional, default:None) –The y-axis limits to draw the matrix on
-
(yAtTop¶bool; optional, default:True) –If True, the first row of data should be plotted at the top of the image
-
(scaling¶numeric; optional, default:1) –An integer factor by which to scale the matrix before plotting
-
(figsize¶tuple[float, float]; optional, default:(12, 12)) –The figure size to create when generating a new axis * If resulting figure looks weird, altering the figure size is your best bet to make it look nicer
-
(fontsize¶int; optional, default:16) –A base font size to apply to tick marks which appear * Titles and labels are given a size of 'fontsize' + 2
-
(cbar¶bool; optional, default:True) –If True, a color bar will be drawn
-
(cbarPadding¶float; optional, default:0.01) –The spacing padding to add between the generated axis and the generated colorbar axis * Only useful when generating a new axis * Only useful when 'colorBy' is given
-
(cbarTitle¶str; optional, default:None) –The title to give to the generated colorbar * If not given, but 'colorBy' is given, the same string for 'colorBy' is used * Only useful when 'colorBy' is given
-
(vmin¶float; optional, default:None) –The minimum value to color
-
(vmax¶float; optional, default:None) –The maximum value to color
-
(cmap¶str or matplotlib ColorMap; optional, default:'viridis') –The colormap to use when coloring
-
(cbax¶matplotlib axis; optional, default:None) –An explicitly given axis to use for drawing the colorbar
-
(cbargs¶dict; optional, default:None) – -
(leftMargin¶float; optional, default:0) –Additional margin to add to the left of the figure * Before using this, try adjusting the 'figsize'
-
(rightMargin¶float; optional, default:0) –Additional margin to add to the left of the figure * Before using this, try adjusting the 'figsize'
-
(topMargin¶float; optional, default:0) –Additional margin to add to the left of the figure * Before using this, try adjusting the 'figsize'
-
(bottomMargin¶float; optional, default:0) –Additional margin to add to the left of the figure * Before using this, try adjusting the 'figsize'
Returns:
-
A namedtuple containing:–'ax' -> The map axis 'handles' -> All geometry handles which were created in the order they were drawn 'cbar' -> The colorbar handle if it was drawn
Source code in geokit/core/util.py
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 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 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 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 | |
isRaster
¶
isRaster(source)
Test if loadRaster fails for the given input.
Parameters:
-
(source¶str) –The path to the raster file to load
Returns:
-
bool -> True if the given input is a raster–
Source code in geokit/core/util.py
isVector
¶
isVector(source)
Test if loadVector fails for the given input.
Parameters:
-
(source¶str) –The path to the vector file to load
Returns:
-
bool -> True if the given input is a vector–
Source code in geokit/core/util.py
quickRaster
¶
quickRaster(
bounds: tuple[numeric, numeric, numeric, numeric],
srs: SpatialReference | None,
dx: numeric,
dy: numeric,
dtype: geokit_c_data_types_literal | None = None,
noData: None | numeric | bool = None,
data: ndarray | None = None,
scale: numeric | None = None,
offset: numeric | None = None,
) -> Dataset
GeoKit internal for quickly creating a raster datasource.
Parameters:
-
(bounds¶tuple[numeric, numeric, numeric, numeric]) –The bounds to create the raster for
-
(srs¶SpatialReference | None) –The SRS to assign to the raster. Please be aware that some operations may not work correctly if no SRS is given.
-
(dx¶numeric) –The pixel width in x direction.
-
(dy¶numeric) –The pixel height in y direction.
-
(dtype¶geokit_c_data_types_literal | None, default:None) –The GDAL C datatype to use for the raster band. by default None
-
(noData¶None | numeric | bool, default:None) –The value to use for no data in the raster band, by default None
-
(data¶ndarray | None, default:None) –The data array to write to the raster band, by default None
-
(scale¶numeric | None, default:None) –The scale factor to apply to the raster band, by default None
-
(offset¶numeric | None, default:None) –The offset to apply to the raster band, by default None
Returns:
-
A raster dataset as gdal.Dataset–
Source code in geokit/core/util.py
424 425 426 427 428 429 430 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 492 493 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 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
quickVector
¶
GeoKit internal for quickly creating a vector datasource.
Source code in geokit/core/util.py
scaleMatrix
¶
Scale a 2-dimensional matrix. For example, a 2x2 matrix, with a scale of 2, will become a 4x4 matrix. Or scaling a 24x24 matrix with a scale of -3 will produce an 8x8 matrix.
- Scaling UP (positive) results in a dimensionally larger matrix where each value is repeated scale^2 times
- scaling DOWN (negative) results in a dimensionally smaller matrix where each value is the average of the associated 'up-scaled' block
Returns:
-
ndarray–
Examples:
INPUT Scaling Factor Output¶
| 1 2 | 2 | 1 1 2 2 | | 3 4 | | 1 1 2 2 | | 3 3 4 4 | | 3 3 4 4 |
| 1 1 1 1 | -2 | 1.5 2.0 | | 2 2 3 3 | | 5.25 6.75| | 4 4 5 5 | | 6 7 8 9 |
| 1 1 1 1 | -3 | 2.55 3.0 | | 2 2 3 3 | * strict=False | 7.0 9 | | 4 4 5 5 |
| 6 7 8 9 | padded
| 1 1 1 1 0 0 |
| 2 2 3 3 0 0 |
| 4 4 5 5 0 0 |
| 6 7 8 9 0 0 |
| 0 0 0 0 0 0 |
| 0 0 0 0 0 0 |
Source code in geokit/core/util.py
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 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 248 | |