mortie.convert
Address-space conversions between geographic coordinates, packed morton words,
UNIQ cell numbers and HEALPix NESTED ids — plus mort2bbox / mort2polygon,
which turn a word into a bounding box or a ring. Split out of mortie.tools by
domain (issue #159) so the Python surface mirrors the Rust tree
(geo2mort.rs, morton.rs, cell_geom.rs); the names stay flat on the package
(mortie.geo2mort, mortie.mort2polygon).
Address-space conversions between geographic, morton, UNIQ and HEALPix.
The X2Y family: :func:geo2mort / :func:mort2geo and :func:geo2uniq /
:func:uniq2geo across the geographic boundary, :func:norm2mort /
:func:mort2norm and :func:norm2uniq / :func:unique2parent across the
normalized-address boundary, and :func:mort2healpix out to NESTED cell ids.
:func:mort2bbox and :func:mort2polygon belong here too: from the caller's
side they turn a word into a bounding box or a ring, which is a conversion --
even though their kernels live in src_rust/src/cell_geom.rs rather than in
geo2mort.rs / morton.rs with the rest of this module's twins.
Split out of mortie.tools (issue #159) so the Python surface mirrors the
Rust tree's own decomposition. The names stay flat on the package
(mortie.geo2mort, mortie.mort2polygon): this module is where they live,
not how they are spelled.
geo2mort(lats, lons, order=None, points=None)
Compute morton indices from geographic coordinates.
The entire pipeline runs in Rust via the healpix crate — no
Python HEALPix backend is needed.
lat/lon inputs are treated as points by default (indeterminate
resolution, encoded at max precision), so a bare geo2mort(lats, lons)
returns order-29 Kind::Point words. Passing an explicit order asks
for an area cell at that resolution instead (points inferred
False). The two flags resolve as:
order=None, points=None(bare call) -> order-29 point words;- an explicit
orderwithpointsunset -> area cell atorder; points=True-> order-29 point words (order-29-only; an explicitorder != 29raisesValueError, matching :meth:MortonIndexArray.from_latlon);points=False-> area cell atorder(order=None-> 29).
Non-finite lat/lon encode to the reserved empty word 0 (base
cell 0 is the null sentinel) on both the area and point routes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lats
|
array - like
|
Latitude(s) in degrees. |
required |
lons
|
array - like
|
Longitude(s) in degrees. |
required |
order
|
int
|
HEALPix order (0-29). Defaults to 29. An explicit value implies an area
cell unless |
None
|
points
|
bool
|
Encode |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Packed |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in mortie/convert.py
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 | |
mort2geo(morton)
Convert morton index to lat/lon of pixel center.
This is the inverse of geo2mort, returning the center coordinates of the HEALPix cell identified by the morton index.
Mixed-order arrays are supported (issue #116): elements are grouped by
order (:func:orders_of), each group runs the uniform kernel, and the
results scatter back to input positions. Point words (spec §4) are order
29 by definition and group with order 29 — a point's location is exactly
what mort2geo returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morton
|
int or array - like
|
Morton index (mixed orders allowed). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
lat |
float or array
|
Latitude in degrees |
lon |
float or array
|
Longitude in degrees |
Source code in mortie/convert.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 510 511 512 513 514 515 516 517 518 519 | |
mort2bbox(morton)
Convert morton index to bounding box of the pixel.
For pixels touching the antimeridian, vertex longitudes at ±180° are normalized to use consistent representation based on hemisphere voting, preventing bbox misinterpretation as spanning the entire globe.
Mixed-order arrays are supported (issue #116): elements are grouped by
order (:func:orders_of), each group runs the uniform kernel, and the
results scatter back to input positions. Point words (spec §4) are order
29 by definition and group with order 29 — a point yields the bounding box
of its containing order-29 cell (the cell that contains the point), which
is exactly the bbox of the order-29 area word at the same location. A
group of points therefore covers a well-defined area, element by element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morton
|
int or array - like
|
Morton index (mixed orders allowed). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bbox |
dict or list of dicts
|
Bounding box in format suitable for STAC/CMR: {"west": min_lon, "south": min_lat, "east": max_lon, "north": max_lat} |
Source code in mortie/convert.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 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 | |
mort2polygon(morton, step=1)
Convert morton index to polygon representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morton
|
int or array - like
|
Morton index. |
required |
step
|
int
|
Points per side for the cell boundary (default 1 = 4 corners). Use step=32 for 128 boundary points that accurately trace curved cell edges, important for polar cells where 4-corner polygons poorly approximate the true HEALPix boundary. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
polygon |
list or list of lists
|
Polygon coordinates as [[lat, lon], ...] in standard geographic order. The polygon is closed (first point repeated at end). Note: Returns [lat, lon] pairs, NOT [lon, lat]. This is the standard geographic coordinate order used by most spatial analysis libraries. |
Notes
Polygons that touch the antimeridian (±180° longitude) are automatically normalized to use consistent longitude representation (-180 or +180) based on which hemisphere contains the majority of vertices. This prevents spatial libraries from misinterpreting touching polygons as crossing polygons.
Mixed-order arrays are supported (issue #116): elements are grouped by
order (:func:orders_of), each group runs the uniform kernel, and the
results scatter back to input positions (rings are 4step+1 vertices at
every order, so mixed orders do not change the output shape). Point words
(spec §4) are order 29 by definition and group with order 29 — a point
yields the polygon ring of its containing order-29 cell, exactly the ring
of the order-29 area* word at the same location.
Source code in mortie/convert.py
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 | |
mort2healpix(morton)
Convert morton index to HEALPix cell ID and order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morton
|
int or array - like
|
Morton index. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
cell_ids |
int or ndarray
|
HEALPix cell ID(s) in NESTED scheme |
order |
int
|
HEALPix order (resolution level) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the words are at mixed orders (propagated from :func: |
Notes
The function converts morton indices to HEALPix NESTED scheme cell IDs. All input morton indices must be at the same order.
Examples:
>>> import mortie
>>> m = mortie.geo2mort(-80.0, 120.0, order=6)[0]
>>> cell_id, order = mortie.mort2healpix(m)
>>> print(f"HEALPix cell {cell_id} at order {order}")
HEALPix cell 37010 at order 6
Source code in mortie/convert.py
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 | |
mort2norm(morton)
Convert morton index back to normalized address and parent cell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morton
|
int or array - like
|
Packed morton word(s) ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
normed |
int or array
|
Normalized HEALPix address |
parent |
int or array
|
Parent base cell (0-11) |
order |
int or array
|
HEALPix order inferred from morton index |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the words are at mixed orders — the return contract carries a single
scalar order, so use :func: |
Notes
Empty input returns two empty int64 arrays and order == 0.
Source code in mortie/convert.py
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 | |
norm2mort(normed, parent, order)
Convert a normalized HEALPix address + base cell to a packed morton word.
The exact inverse of :func:mort2norm: mort2norm(norm2mort(n, p, o))
returns (n, p, o). Born order-29-native (issue #48) — there is no order
cap beyond the kernel's MAX_ORDER of 29. The returned uint64 is the
packed decimal_morton word (issue #58; the prefix is base+1, so bit 63
is set — a large unsigned value — for base cells 7-11), not the retired
decimal encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normed
|
int or array
|
Normalized HEALPix address (the in-base z-order, |
required |
parent
|
int or array
|
Parent base cell (0-11). |
required |
order
|
int
|
HEALPix order (0-29). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
morton |
uint64 or ndarray
|
Packed morton word(s). |
Source code in mortie/convert.py
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 | |
!!! note "Not yet documented here"
The UNIQ helpers (`geo2uniq`, `norm2uniq`, `uniq2geo`, `unique2parent`) are
omitted while their signatures are in flux — see
[issue #136](https://github.com/espg/mortie/issues/136). `heal_norm` is
omitted because it is being removed under
[PR #130](https://github.com/espg/mortie/pull/130).