grid
This module provides functionality for generating a grid based on the INSPIRE grid system specification.
InspireGridGenerator
A class used to generate a grid based on the INSPIRE 100m grid system specification.
Source code in multimno/core/grid.py
17 18 19 20 21 22 23 24 25 26 27 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 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 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 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 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 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 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 400 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 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 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 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 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 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 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 |
|
cover_extent_with_grid_centroids(extent, n_origin=None, e_origin=None)
Covers the given extent with grid centroids. It takes an extent expressed in EPSG:4326 and covers it
with grid centroid point geometries in EPSG:3035, returning a DataFrame with these geometries, the internal
4-byte grid ID and the origin used to define the 4-byte ID. If both n_origin
and e_origin
are provided,
they are used as the origin of the ID; if not, the origin is taken from the provided extent.
It is desirable to define the origin using n_origin
and e_origin
when one wants to cover several extents
sharing the same origin, i.e. using the 4-byte grid ID defined in the same way for all of them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extent
|
list[float]
|
The extent in lat/lon (EPSG:4326) to cover with grid centroids. Ordering must be [lon_min, lat_min, lon_max, lat_max]. |
required |
n_origin
|
int
|
northing origin to be used for the 4-byte grid ID, in EPSG:3035 (metres). Defaults to None. |
None
|
e_origin
|
int
|
easting origin to be used for the 4-byte grid ID, in EPSG:3035 (metres). Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The DataFrame representing the grid centroids covering the extent, with their grid ID and origin columns. |
Source code in multimno/core/grid.py
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 400 401 402 403 404 405 406 407 |
|
cover_extent_with_grid_tiles(extent, n_origin=None, e_origin=None)
Covers the given extent with grid tiles. It takes an extent expressed in EPSG:4326 and covers it
with grid tile polygon geometries in EPSG:3035, returning a DataFrame with these geometries, the internal
4-byte grid ID and the origin used to define the 4-byte ID. If both n_origin
and e_origin
are provided,
they are used as the origin of the ID; if not, the origin is taken from the provided extent.
It is desirable to define the origin using n_origin
and e_origin
when one wants to cover several extents
sharing the same origin, i.e. using the 4-byte grid ID defined in the same way for all of them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extent
|
list[float]
|
The extent in lat/lon (EPSG:4326) to cover with grid tiles. Ordering must be [lon_min, lat_min, lon_max, lat_max]. |
required |
n_origin
|
int
|
northing origin to be used for the 4-byte grid ID, in EPSG:3035 (metres). Defaults to None. |
None
|
e_origin
|
int
|
easting origin to be used for the 4-byte grid ID, in EPSG:3035 (metres). Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
Tuple[DataFrame, List[float]]
|
The DataFrame representing the grid tiles covering the extent, with their grid ID and origin columns. |
Source code in multimno/core/grid.py
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 |
|
cover_polygon_with_grid_centroids(polygon_sdf, n_origin=None, e_origin=None)
Covers the given polygon with grid centroids. It takes an polygon expressed in EPSG:4326 and covers it
with grid centroid point geometries in EPSG:3035, returning a DataFrame with these geometries, the internal
4-byte grid ID and the origin used to define the 4-byte ID. If both n_origin
and e_origin
are provided,
they are used as the origin of the ID; if not, the origin is taken from the extent covering the provided
polygon.
It is desirable to define the origin using n_origin
and e_origin
when one wants to cover several polygons
sharing the same origin, i.e. using the 4-byte grid ID defined in the same way for all of them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polygon_sdf
|
DataFrame
|
DataFrame containing a single row with a polygon in EPSG:4326 in a column named
|
required |
n_origin
|
int
|
northing origin to be used for the 4-byte grid ID, in EPSG:3035 (metres). Defaults to None. |
None
|
e_origin
|
int
|
easting origin to be used for the 4-byte grid ID, in EPSG:3035 (metres). Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The DataFrame representing the grid centroids covering the polygon, with their grid ID and origin columns. |
Source code in multimno/core/grid.py
409 410 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 |
|
cover_polygon_with_grid_tiles(polygon_sdf, n_origin, e_origin)
Covers the given polygon with grid tiles. It takes an polygon expressed in EPSG:4326 and covers it
with grid tile polygon geometries in EPSG:3035, returning a DataFrame with these geometries, the internal
4-byte grid ID and the origin used to define the 4-byte ID. If both n_origin
and e_origin
are provided,
they are used as the origin of the ID; if not, the origin is taken from the polygon covering the provided
polygon.
It is desirable to define the origin using n_origin
and e_origin
when one wants to cover several polygons
sharing the same origin, i.e. using the 4-byte grid ID defined in the same way for all of them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polygon_sdf
|
DataFrame
|
DataFrame containing a single row with a polygon in EPSG:4326 in a column named
|
required |
n_origin
|
int
|
northing origin to be used for the 4-byte grid ID, in EPSG:3035 (metres). Defaults to None. |
required |
e_origin
|
int
|
easting origin to be used for the 4-byte grid ID, in EPSG:3035 (metres). Defaults to None. |
required |
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The DataFrame representing the grid tiles covering the polygon, with their grid ID and origin columns. |
Source code in multimno/core/grid.py
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 |
|
grid_id_from_coarser_resolution(sdf, coarse_resolution, coarse_grid_id_col, new_grid_id_col=None)
This function takes a DataFrame that contains the grid ID representation of grid tiles in a resolution coarser than 100m, and transforms it back into 100m.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdf
|
DataFrame
|
DataFrame with grid IDs in a coarser resolution. |
required |
coarse_resolution
|
int
|
coarser resolution of the grid IDs of the provided DataFrame. Must be a multiple
of |
required |
coarse_grid_id_col
|
str
|
column that currently holds the IDs of the grid tiles in the coarser resolution. |
required |
new_grid_id_col
|
str
|
column that will hold the IDs of the grid tiles in the 100m resolution.
If None, it will be set (and possible replace an existing column) as |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
DataFrame with the coarser grid IDs. |
Source code in multimno/core/grid.py
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 |
|
grid_id_to_coarser_resolution(sdf, coarse_resolution, coarse_grid_id_col=None)
This function takes a DataFrame that contains the grid ID representation of 100m grid tiles, and transforms
it into a coarser resolution. It is always expected that the provided DataFrame has a grid ID that represents
100m grid tiles (in the self.grid_id_col column
), and not a different resolution.
Notice that this method does not take into account the origin of the 4-byte grid IDs. Thus, the coarser grids need not be compatible with the INSPIRE definition of a resolution coarser than 100m.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdf
|
DataFrame
|
DataFrame for which a coarser resolution grid ID will be computed |
required |
coarse_resolution
|
int
|
coarser resolution to compute. Must be a multiple of |
required |
coarse_grid_id_col
|
str
|
column that will hold the IDs of the grid tiles in the coarser resolution. If None, it will replace the original grid ID column. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
DataFrame with the coarser grid IDs. |
Source code in multimno/core/grid.py
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 |
|
grid_id_to_inspire_id(sdf, inspire_resolution, grid_id_col=None, origin=None)
Function that takes a DataFrame containing 4-byte grid IDs and returns it with a new column containing the official INSPIRE grid ID string. Only accepted INSPIRE grid resolutions are 100m and 1km.
It is expected that the grid ID column contains the internal representation for 100m grid tiles, and not for a coarser resolution. If the 100m INSPIRE grid ID was requested, the ID corresponding to the 100m grid tile represented by the internal grid ID is constructed. If the 1km INSPIRE grid ID was requested, the ID corresponding to the 1km grid tile containing the internal grid ID is constructed.
By default, the function will use a ColNames.origin column of sdf
. Only if the origin
parameter is passed,
the existence of this column will not be checked, and origin
will be used as the origin of the 4-byte grid ID
definition even if the column exists. This origin will be treated as an 8-byte integer, where the first (most
significant) 4 bytes hold the easting origin divided by 100 and the last (least significant) 4 bytes hold the
northing origin divided by 100. That is, taking the first 4 bytes and multiplying by 100 gets the easting
value in metres (analogous for northing).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdf
|
DataFrame
|
DataFrame containing the grid ID column, and a |
required |
inspire_resolution
|
int
|
resolution for the INSPIRE grid ID. Currently accepts two value: |
required |
grid_id_col
|
str
|
Name of the column containing the internal 4-byte grid ID. If None, the value
|
None
|
origin
|
int
|
If provided, it will be used as the origin of the definition of the 4-byte grid ID.
It will ignore the ColNames.origin column even if it exists. If not provided, it is expected that
|
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
DataFrame with a new column, |
Raises:
Type | Description |
---|---|
ValueError
|
If the |
ValueError
|
If the |
ValueError
|
If the |
Source code in multimno/core/grid.py
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 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 |
|
grid_ids_to_grid_centroids(sdf, grid_resolution, grid_id_col=None, geometry_col=None, origin=None)
Function that takes a DataFrame containing internal 4-byte grid IDs and returns it with point geometries of the centroids of the corresponding grid tiles.
By default, the function will use a ColNames.origin column of sdf
. Only if the origin
parameter is passed,
the existence of this column will not be checked, and origin
will be used as the origin of the 4-byte grid ID
definition even if the column exists. This origin will be treated as an 8-byte integer, where the first (most
significant) 4 bytes hold the easting origin divided by 100 and the last (least significant) 4 bytes hold the
northing origin divided by 100. That is, taking the first 4 bytes and multiplying by 100 gets the easting
value in metres (analogous for northing).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdf
|
DataFrame
|
DataFrame containing the internal 4-byte grid IDs. |
required |
grid_resolution
|
int
|
resolution, in metres, of the current grid as represented by the internal 4-byte
grid IDs. Must be a multiple of |
required |
grid_id_col
|
str
|
column that holds the internal grid IDs. If None, it is set to
|
None
|
geometry_col
|
str
|
column that will hold the grid centroid geometries. If None, it is set to
|
None
|
origin
|
int
|
If provided, it will be used as the origin of the definition of the 4-byte grid ID.
It will ignore the ColNames.origin column even if it exists. If not provided, it is expected that
|
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
DataFrame with the grid centroid geometries |
Source code in multimno/core/grid.py
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 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 |
|
grid_ids_to_grid_tiles(sdf, grid_resolution, geometry_col=None, origin=None)
Function that takes a DataFrame containing internal 4-byte grid IDs and returns it with polygon geometries of the corresponding grid tiles.
By default, the function will use a ColNames.origin column of sdf
. Only if the origin
parameter is passed,
the existence of this column will not be checked, and origin
will be used as the origin of the 4-byte grid ID
definition even if the column exists. This origin will be treated as an 8-byte integer, where the first (most
significant) 4 bytes hold the easting origin divided by 100 and the last (least significant) 4 bytes hold the
northing origin divided by 100. That is, taking the first 4 bytes and multiplying by 100 gets the easting
value in metres (analogous for northing).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdf
|
DataFrame
|
DataFrame containing the internal 4-byte grid IDs. |
required |
grid_resolution
|
int
|
resolution, in metres, of the current grid as represented by the internal 4-byte
grid IDs. Must be a multiple of |
required |
geometry_col
|
str
|
column that will hold the grid tile geometries. If None, it is set to
|
None
|
origin
|
int
|
If provided, it will be used as the origin of the definition of the 4-byte grid ID.
It will ignore the ColNames.origin column even if it exists. If not provided, it is expected that
|
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
DataFrame with the grid centroid geometries |
Source code in multimno/core/grid.py
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 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 |
|
inspire_id_to_grid_centroids(sdf, inspire_id_col=None, geometry_col=None)
Function that takes a DataFrame containing INSPIRE grid ID strings and returns it with point geometries of the centroids of the corresponding grid tiles. It extracts the units and grid size from the first element of the DataFrame and uses it to construct the necessary geometries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdf
|
DataFrame
|
DataFrame containing the INSPIRE grid ID strings. |
required |
inspire_id_col
|
str
|
name of the column holding the INSPIRE grid IDs. If None, it is set to
|
None
|
geometry_col
|
str
|
column that will hold the grid centroid geometries. If None, it is set to
|
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
DataFrame with the grid centroid geometries |
Source code in multimno/core/grid.py
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 |
|
inspire_id_to_grid_tiles(sdf, inspire_id_col=None, geometry_col=None)
Function that takes a DataFrame containing INSPIRE grid ID strings and returns it with polygon geometries of the corresponding grid tiles. It extracts the units and grid size from the first element of the DataFrame and uses it to construct the necessary geometries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdf
|
DataFrame
|
DataFrame containing the INSPIRE grid ID strings. |
required |
inspire_id_col
|
str
|
name of the column holding the INSPIRE grid IDs. If None, it is set to
|
None
|
geometry_col
|
str
|
column that will hold the grid tile geometries. If None, it is set to
|
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
DataFrame with the grid centroid geometries |
Source code in multimno/core/grid.py
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 |
|
process_latlon_extent(extent)
Takes an extent expressed in latitude and longitude (EPSG 4326), projects it into EPSG 3035, creates bounding box, snaps to grid, and extends it some extra tiles in each direction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extent
|
list[float]
|
The extent in lat/lon to process. Ordering is [lon_min, lat_min, lon_max, lat_max]. |
required |
Returns:
Name | Type | Description |
---|---|---|
extent |
list[float]
|
Coordinates of the rectangle/bounding box that covers the projected and extended extent. Order is [n_min, e_min, n_max, e_max] (bottom-left and top-right corners) |
raster_bounds |
list[float]
|
Appropriate raster bounds |
Source code in multimno/core/grid.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|