Skip to content

Commit 6b4073b

Browse files
authored
arro3 followups (#596)
### Change list - Rename `PyarrowTableTrait` to `ArrowTableTrait` - Clearer `ImportError` exception messages for code paths that still use pyarrow - Use Arrow exportable types from arro3, because those types are [better documented online](https://kylebarron.dev/arro3/latest/api/core/types/) - Add arro3 for docs hyperlinking
1 parent 10fcd05 commit 6b4073b

File tree

13 files changed

+66
-54
lines changed

13 files changed

+66
-54
lines changed

docs/api/traits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
::: lonboard.traits.FilterValueAccessor
77
::: lonboard.traits.NormalAccessor
88
::: lonboard.traits.PointAccessor
9-
::: lonboard.traits.PyarrowTableTrait
9+
::: lonboard.traits.ArrowTableTrait

lonboard/_cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Dict, List, Optional
66

77
import click
8-
import pyarrow.parquet as pq
98
from arro3.core import Table
109
from pyproj import CRS
1110

@@ -65,6 +64,14 @@ def read_geoparquet(path: Path) -> Table:
6564
Args:
6665
path: Path to GeoParquet file
6766
"""
67+
try:
68+
import pyarrow.parquet as pq
69+
except ImportError as e:
70+
raise ImportError(
71+
"pyarrow currently required for reading GeoParquet files.\n"
72+
"Run `pip install pyarrow`."
73+
) from e
74+
6875
file = pq.ParquetFile(path)
6976
geo_meta = file.metadata.metadata.get(b"geo")
7077
if not geo_meta:

lonboard/_geoarrow/geopandas_interop.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ def geopandas_to_geoarrow(
1616
columns: Optional[List[str]] = None,
1717
preserve_index: Optional[bool] = None,
1818
) -> Table:
19-
import pyarrow
19+
try:
20+
import pyarrow
21+
except ImportError as e:
22+
raise ImportError(
23+
"pyarrow required for converting GeoPandas to arrow.\n"
24+
"Run `pip install pyarrow`."
25+
) from e
2026

2127
df_attr = gdf.drop(columns=[gdf._geometry_column_name])
2228

lonboard/_layer.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import ipywidgets
2525
import traitlets
2626
from arro3.core import Table
27+
from arro3.core.types import ArrowStreamExportable
2728

2829
from lonboard._base import BaseExtension, BaseWidget
2930
from lonboard._constants import EXTENSION_NAME, OGC_84
@@ -38,12 +39,11 @@
3839
from lonboard._utils import auto_downcast as _auto_downcast
3940
from lonboard._utils import get_geometry_column_index, remove_extension_kwargs
4041
from lonboard.traits import (
42+
ArrowTableTrait,
4143
ColorAccessor,
4244
FloatAccessor,
4345
NormalAccessor,
44-
PyarrowTableTrait,
4546
)
46-
from lonboard.types.arrow import ArrowStreamExportable
4747

4848
if TYPE_CHECKING:
4949
import geopandas as gpd
@@ -283,7 +283,7 @@ class BaseArrowLayer(BaseLayer):
283283

284284
# The following traitlets **are** serialized to JS
285285

286-
table: traitlets.TraitType
286+
table: ArrowTableTrait
287287

288288
def __init__(
289289
self,
@@ -714,7 +714,7 @@ def from_duckdb(
714714

715715
_layer_type = traitlets.Unicode("column").tag(sync=True)
716716

717-
table = PyarrowTableTrait(allowed_geometry_types={EXTENSION_NAME.POINT})
717+
table = ArrowTableTrait(allowed_geometry_types={EXTENSION_NAME.POINT})
718718
"""A GeoArrow table with a Point or MultiPoint column.
719719
720720
This is the fastest way to plot data from an existing GeoArrow source, such as
@@ -1001,7 +1001,7 @@ def from_duckdb(
10011001

10021002
_layer_type = traitlets.Unicode("polygon").tag(sync=True)
10031003

1004-
table = PyarrowTableTrait(
1004+
table = ArrowTableTrait(
10051005
allowed_geometry_types={EXTENSION_NAME.POLYGON, EXTENSION_NAME.MULTIPOLYGON}
10061006
)
10071007
"""A GeoArrow table with a Polygon or MultiPolygon column.
@@ -1246,7 +1246,7 @@ def from_duckdb(
12461246

12471247
_layer_type = traitlets.Unicode("scatterplot").tag(sync=True)
12481248

1249-
table = PyarrowTableTrait(
1249+
table = ArrowTableTrait(
12501250
allowed_geometry_types={EXTENSION_NAME.POINT, EXTENSION_NAME.MULTIPOINT}
12511251
)
12521252
"""A GeoArrow table with a Point or MultiPoint column.
@@ -1488,7 +1488,7 @@ def from_duckdb(
14881488

14891489
_layer_type = traitlets.Unicode("path").tag(sync=True)
14901490

1491-
table = PyarrowTableTrait(
1491+
table = ArrowTableTrait(
14921492
allowed_geometry_types={
14931493
EXTENSION_NAME.LINESTRING,
14941494
EXTENSION_NAME.MULTILINESTRING,
@@ -1659,7 +1659,7 @@ def from_duckdb(
16591659

16601660
_layer_type = traitlets.Unicode("point-cloud").tag(sync=True)
16611661

1662-
table = PyarrowTableTrait(
1662+
table = ArrowTableTrait(
16631663
allowed_geometry_types={EXTENSION_NAME.POINT}, allowed_dimensions={3}
16641664
)
16651665
"""A GeoArrow table with a Point column.
@@ -1792,7 +1792,7 @@ def from_duckdb(
17921792

17931793
_layer_type = traitlets.Unicode("solid-polygon").tag(sync=True)
17941794

1795-
table = PyarrowTableTrait(
1795+
table = ArrowTableTrait(
17961796
allowed_geometry_types={EXTENSION_NAME.POLYGON, EXTENSION_NAME.MULTIPOLYGON}
17971797
)
17981798
"""A GeoArrow table with a Polygon or MultiPolygon column.
@@ -1971,7 +1971,7 @@ def from_duckdb(
19711971

19721972
_layer_type = traitlets.Unicode("heatmap").tag(sync=True)
19731973

1974-
table = PyarrowTableTrait(allowed_geometry_types={EXTENSION_NAME.POINT})
1974+
table = ArrowTableTrait(allowed_geometry_types={EXTENSION_NAME.POINT})
19751975
"""A GeoArrow table with a Point column.
19761976
19771977
This is the fastest way to plot data from an existing GeoArrow source, such as

lonboard/_testing.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

lonboard/_viz.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
import pyarrow
3838
import shapely.geometry
3939
import shapely.geometry.base
40+
from arro3.core.types import ArrowArrayExportable, ArrowStreamExportable
4041
from numpy.typing import NDArray
4142

42-
from lonboard.types.arrow import ArrowArrayExportable, ArrowStreamExportable
4343
from lonboard.types.layer import (
4444
PathLayerKwargs,
4545
PolygonLayerKwargs,
@@ -372,8 +372,21 @@ def _viz_shapely_array(
372372
def _viz_geo_interface(
373373
data: dict, **kwargs
374374
) -> List[Union[ScatterplotLayer, PathLayer, PolygonLayer]]:
375-
import pyarrow as pa
376-
import shapely
375+
try:
376+
import pyarrow as pa
377+
except ImportError as e:
378+
raise ImportError(
379+
"pyarrow required for visualizing __geo_interface__ objects.\n"
380+
"Run `pip install pyarrow`."
381+
) from e
382+
383+
try:
384+
import shapely
385+
except ImportError as e:
386+
raise ImportError(
387+
"shapely required for visualizing __geo_interface__ objects.\n"
388+
"Run `pip install shapely`."
389+
) from e
377390

378391
if data["type"] in [
379392
"Point",

lonboard/colormap.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,14 @@ def apply_categorical_cmap(
153153
dimension will have a length of either `3` if `alpha` is `None`, or `4` is
154154
each color has an alpha value.
155155
"""
156-
import pyarrow as pa
157-
import pyarrow.compute as pc
156+
try:
157+
import pyarrow as pa
158+
import pyarrow.compute as pc
159+
except ImportError as e:
160+
raise ImportError(
161+
"pyarrow required for apply_categorical_cmap.\n"
162+
"Run `pip install pyarrow`."
163+
) from e
158164

159165
# Import from PyCapsule interface
160166
if hasattr(values, "__arrow_c_array__"):

lonboard/experimental/_layer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from lonboard._constants import EXTENSION_NAME
1111
from lonboard._layer import BaseArrowLayer
1212
from lonboard.traits import (
13+
ArrowTableTrait,
1314
ColorAccessor,
1415
FloatAccessor,
1516
PointAccessor,
16-
PyarrowTableTrait,
1717
TextAccessor,
1818
)
1919

@@ -23,7 +23,7 @@ class ArcLayer(BaseArrowLayer):
2323

2424
_layer_type = traitlets.Unicode("arc").tag(sync=True)
2525

26-
table = PyarrowTableTrait()
26+
table = ArrowTableTrait()
2727
"""A GeoArrow table.
2828
2929
This is the fastest way to plot data from an existing GeoArrow source, such as
@@ -128,7 +128,7 @@ class TextLayer(BaseArrowLayer):
128128

129129
_layer_type = traitlets.Unicode("text").tag(sync=True)
130130

131-
table = PyarrowTableTrait(allowed_geometry_types={EXTENSION_NAME.POINT})
131+
table = ArrowTableTrait(allowed_geometry_types={EXTENSION_NAME.POINT})
132132
"""A GeoArrow table with a Point or MultiPoint column.
133133
134134
This is the fastest way to plot data from an existing GeoArrow source, such as

lonboard/traits.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def error(
137137
raise TraitError(e)
138138

139139

140-
class PyarrowTableTrait(FixedErrorTraitType):
140+
class ArrowTableTrait(FixedErrorTraitType):
141141
"""A trait to validate input for a geospatial Arrow-backed table
142142
143143
Allowed input includes:
@@ -152,7 +152,10 @@ class from
152152
"""
153153

154154
default_value = None
155-
info_text = "a pyarrow or GeoArrow Table"
155+
info_text = (
156+
"a table-like Arrow object, such as a pyarrow or arro3 Table or "
157+
"RecordBatchReader"
158+
)
156159

157160
def __init__(
158161
self: TraitType,

lonboard/types/arrow.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)