1
1
# This file is **symlinked** across the APIs to ensure they are exactly the same.
2
+ from typing import Literal
2
3
3
4
from ... import _cy
4
5
from ..._h3shape import (
5
- ContainmentMode ,
6
6
H3Shape ,
7
7
LatLngPoly ,
8
8
LatLngMultiPoly ,
@@ -468,6 +468,13 @@ def uncompact_cells(cells, res):
468
468
return _out_collection (hu )
469
469
470
470
471
+ def polygon_to_cells (h3shape , res ):
472
+ """
473
+ Alias for ``h3shape_to_cells``.
474
+ """
475
+ return h3shape_to_cells (h3shape , res )
476
+
477
+
471
478
def h3shape_to_cells (h3shape , res ):
472
479
"""
473
480
Return the collection of H3 cells at a given resolution whose center points
@@ -519,25 +526,46 @@ def h3shape_to_cells(h3shape, res):
519
526
return _out_collection (mv )
520
527
521
528
522
- def polygon_to_cells (h3shape , res ):
529
+ def polygon_to_cells_experimental (
530
+ h3shape : H3Shape ,
531
+ res : int ,
532
+ contain : Literal ['center' , 'full' , 'overlap' , 'bbox_overlap' ] = 'center' ,
533
+ ):
523
534
"""
524
- Alias for ``h3shape_to_cells ``.
535
+ Alias for ``h3shape_to_cells_experimental ``.
525
536
"""
526
- return h3shape_to_cells (h3shape , res )
537
+ return h3shape_to_cells_experimental (h3shape , res , contain )
527
538
528
539
529
- def h3shape_to_cells_experimental (h3shape , res , flags = 0 ):
540
+ def h3shape_to_cells_experimental (
541
+ h3shape : H3Shape ,
542
+ res : int ,
543
+ contain : Literal ['center' , 'full' , 'overlap' , 'bbox_overlap' ] = 'center' ,
544
+ ):
530
545
"""
531
- Return the collection of H3 cells at a given resolution whose center points
532
- are contained within an ``LatLngPoly`` or ``LatLngMultiPoly``.
546
+ Experimental function similar to ``h3shape_to_cells``, but with support for
547
+ multiple cell containment modes.
548
+
549
+ Using ``contain='center'`` should give identical behavior as
550
+ ``h3shape_to_cells``.
551
+
552
+ Note that this function is **experimental** and has no API stability gaurantees
553
+ across versions, so it may change in the future.
554
+
533
555
534
556
Parameters
535
557
----------
536
558
h3shape : ``H3Shape``
537
559
res : int
538
560
Resolution of the output cells
539
- flags : ``ContainmentMode``, int, or string
540
- Containment mode flags
561
+ contain : {'center', 'full', 'overlap', 'bbox_overlap'}, optional
562
+ Specifies the containment condition.
563
+ - 'center': Cell center is contained in shape
564
+ - 'full': Cell is fully contained in shape
565
+ - 'overlap': Cell is partially contained in shape
566
+ - 'bbox_overlap': Cell bounding box is partially contained in shape
567
+
568
+ Default is 'center'.
541
569
542
570
Returns
543
571
-------
@@ -550,7 +578,7 @@ def h3shape_to_cells_experimental(h3shape, res, flags=0):
550
578
... [(37.68, -122.54), (37.68, -122.34), (37.82, -122.34),
551
579
... (37.82, -122.54)],
552
580
... )
553
- >>> h3.h3shape_to_cells_experimental(poly, 6, h3.ContainmentMode.containment_center )
581
+ >>> h3.h3shape_to_cells_experimental(poly, 6, 'center' )
554
582
['862830807ffffff',
555
583
'862830827ffffff',
556
584
'86283082fffffff',
@@ -564,30 +592,27 @@ def h3shape_to_cells_experimental(h3shape, res, flags=0):
564
592
There is currently no guaranteed order of the output cells.
565
593
"""
566
594
567
- if isinstance (flags , str ):
568
- try :
569
- flags = ContainmentMode [flags ]
570
- except KeyError as e :
571
- raise ValueError ('Unrecognized flags: ' + flags ) from e
572
- if isinstance (flags , ContainmentMode ):
573
- flags = int (flags )
574
- if not isinstance (flags , int ):
575
- raise ValueError (
576
- 'Flags should be ContainmentMode, str, or int, but got: ' + str (type (flags ))
577
- )
595
+ contain_modes = {
596
+ 'center' : 0 ,
597
+ 'full' : 1 ,
598
+ 'overlap' : 2 ,
599
+ 'bbox_overlap' : 3 ,
600
+ }
601
+
602
+ flag = contain_modes [contain ]
578
603
579
604
# todo: not sure if i want this dispatch logic here. maybe in the objects?
580
605
if isinstance (h3shape , LatLngPoly ):
581
606
poly = h3shape
582
607
mv = _cy .polygon_to_cells_experimental (
583
608
poly .outer ,
584
- res = res ,
585
- holes = poly .holes ,
586
- flags = flags
609
+ res = res ,
610
+ holes = poly .holes ,
611
+ flag = flag ,
587
612
)
588
613
elif isinstance (h3shape , LatLngMultiPoly ):
589
614
mpoly = h3shape
590
- mv = _cy .polygons_to_cells_experimental (mpoly .polys , res = res , flags = flags )
615
+ mv = _cy .polygons_to_cells_experimental (mpoly .polys , res = res , flag = flag )
591
616
elif isinstance (h3shape , H3Shape ):
592
617
raise ValueError ('Unrecognized H3Shape: ' + str (h3shape ))
593
618
else :
@@ -596,13 +621,6 @@ def h3shape_to_cells_experimental(h3shape, res, flags=0):
596
621
return _out_collection (mv )
597
622
598
623
599
- def polygon_to_cells_experimental (h3shape , res , flags = 0 ):
600
- """
601
- Alias for ``h3shape_to_cells_experimental``.
602
- """
603
- return h3shape_to_cells_experimental (h3shape , res , flags = flags )
604
-
605
-
606
624
def cells_to_h3shape (cells , * , tight = True ):
607
625
"""
608
626
Return an ``H3Shape`` describing the area covered by a collection of H3 cells.
0 commit comments