|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import traitlets as t |
| 6 | + |
| 7 | +from lonboard._utils import auto_downcast as _auto_downcast |
| 8 | + |
| 9 | +# Important to import from ._polygon to avoid circular imports |
| 10 | +from lonboard.layer._polygon import PolygonLayer |
| 11 | +from lonboard.traits import ArrowTableTrait, TextAccessor |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + import sys |
| 15 | + |
| 16 | + import pandas as pd |
| 17 | + from arro3.core.types import ArrowStreamExportable |
| 18 | + |
| 19 | + from lonboard.types.layer import GeohashLayerKwargs, TextAccessorInput |
| 20 | + |
| 21 | + if sys.version_info >= (3, 11): |
| 22 | + from typing import Self |
| 23 | + else: |
| 24 | + from typing_extensions import Self |
| 25 | + |
| 26 | + if sys.version_info >= (3, 12): |
| 27 | + from typing import Unpack |
| 28 | + else: |
| 29 | + from typing_extensions import Unpack |
| 30 | + |
| 31 | + |
| 32 | +class GeohashLayer(PolygonLayer): |
| 33 | + """The `GeohashLayer` renders filled and/or stroked polygons based on the [Geohash](https://en.wikipedia.org/wiki/Geohash) geospatial indexing system. |
| 34 | +
|
| 35 | + !!! warning |
| 36 | + This layer does not currently support auto-centering the map view based on the |
| 37 | + extent of the input. |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + table: ArrowStreamExportable, |
| 43 | + *, |
| 44 | + get_geohash: TextAccessorInput, |
| 45 | + _rows_per_chunk: int | None = None, |
| 46 | + **kwargs: Unpack[GeohashLayerKwargs], |
| 47 | + ) -> None: |
| 48 | + """Create a new GeohashLayer. |
| 49 | +
|
| 50 | + Args: |
| 51 | + table: An Arrow table with properties to associate with the geohashes. |
| 52 | +
|
| 53 | + Keyword Args: |
| 54 | + get_geohash: The identifier of each geohash. |
| 55 | + kwargs: Extra args passed down as GeohashLayer attributes. |
| 56 | +
|
| 57 | + """ |
| 58 | + super().__init__( |
| 59 | + table=table, |
| 60 | + get_geohash=get_geohash, |
| 61 | + _rows_per_chunk=_rows_per_chunk, |
| 62 | + **kwargs, |
| 63 | + ) |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def from_pandas( |
| 67 | + cls, |
| 68 | + df: pd.DataFrame, |
| 69 | + *, |
| 70 | + get_geohash: TextAccessorInput, |
| 71 | + auto_downcast: bool = True, |
| 72 | + **kwargs: Unpack[GeohashLayerKwargs], |
| 73 | + ) -> Self: |
| 74 | + """Create a new GeohashLayer from a pandas DataFrame. |
| 75 | +
|
| 76 | + Args: |
| 77 | + df: a Pandas DataFrame with properties to associate with geohashes. |
| 78 | +
|
| 79 | + Keyword Args: |
| 80 | + get_geohash: geohash identifiers. |
| 81 | + auto_downcast: Whether to save memory on input by casting to smaller types. Defaults to True. |
| 82 | + kwargs: Extra args passed down as GeohashLayer attributes. |
| 83 | +
|
| 84 | + """ |
| 85 | + try: |
| 86 | + import pyarrow as pa |
| 87 | + except ImportError as e: |
| 88 | + raise ImportError( |
| 89 | + "pyarrow required for converting GeoPandas to arrow.\n" |
| 90 | + "Run `pip install pyarrow`.", |
| 91 | + ) from e |
| 92 | + |
| 93 | + if auto_downcast: |
| 94 | + # Note: we don't deep copy because we don't need to clone geometries |
| 95 | + df = _auto_downcast(df.copy()) # type: ignore |
| 96 | + |
| 97 | + table = pa.Table.from_pandas(df) |
| 98 | + return cls(table, get_geohash=get_geohash, **kwargs) |
| 99 | + |
| 100 | + _layer_type = t.Unicode("geohash").tag(sync=True) |
| 101 | + |
| 102 | + table = ArrowTableTrait(geometry_required=False) |
| 103 | + """An Arrow table with properties to associate with the geohashes. |
| 104 | +
|
| 105 | + If you have a Pandas `DataFrame`, use |
| 106 | + [`from_pandas`][lonboard.GeohashLayer.from_pandas] instead. |
| 107 | + """ |
| 108 | + |
| 109 | + get_geohash = TextAccessor() |
| 110 | + """The cell identifier of each geohash. |
| 111 | +
|
| 112 | + Accepts either an array of strings or uint64 integers representing geohash IDs. |
| 113 | +
|
| 114 | + - Type: [TextAccessor][lonboard.traits.TextAccessor] |
| 115 | + """ |
0 commit comments