Skip to content

Commit a679600

Browse files
committed
un-require optional packages
1 parent b541827 commit a679600

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "spatial-polars"
3-
version = "0.2.1" # dont forget __init__.py and uv lock
3+
version = "0.2.2" # dont forget __init__.py and uv lock
44
description = "A package that extends polars for working with geospatial data."
55
readme = "README.md"
66
authors = [

src/spatial_polars/_utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@
44

55
import numpy as np
66
import polars as pl
7-
from matplotlib.colors import Colormap
8-
from palettable.palette import Palette
97
from polars import selectors as cs
108

9+
try:
10+
from matplotlib.colors import Colormap
11+
12+
HAS_MATPLOTLIB = True
13+
except ModuleNotFoundError:
14+
HAS_MATPLOTLIB = False
15+
16+
try:
17+
from palettable.palette import Palette
18+
19+
HAS_PALETTABLE = True
20+
except ModuleNotFoundError:
21+
HAS_PALETTABLE = False
22+
1123

1224
def validate_col_exists_in_df(df: pl.DataFrame, col: str) -> None:
1325
if col not in df.columns:
@@ -138,7 +150,9 @@ def validate_cmap_input(
138150
raise ValueError(msg)
139151

140152
if cmap_type == "continuous":
141-
if isinstance(cmap, (Palette, Colormap)) is False:
153+
if (HAS_MATPLOTLIB and isinstance(cmap, Colormap) is False) and (
154+
HAS_PALETTABLE and isinstance(cmap, Palette) is False
155+
):
142156
# verify continuous cmaps get a cmap or appropriate type.
143157
msg = (
144158
f"`{cmap_type=}` is `continuous`, but {cmap=}. Please provide a",

src/spatial_polars/spatialframe.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import pyproj
1717
import shapely
1818
from geoarrow.pyarrow import io as gaio
19-
from lonboard import PathLayer, PolygonLayer, ScatterplotLayer, viz
20-
from lonboard.colormap import apply_categorical_cmap, apply_continuous_cmap
2119
from polars import col as c
2220

2321
from ._utils import validate_cmap_input, validate_width_and_radius_input
@@ -43,6 +41,33 @@
4341
"SpatialFrame",
4442
]
4543

44+
try:
45+
from lonboard import PathLayer, PolygonLayer, ScatterplotLayer, viz
46+
from lonboard.colormap import apply_categorical_cmap, apply_continuous_cmap
47+
48+
HAS_LONBOARD = True
49+
except ModuleNotFoundError:
50+
HAS_LONBOARD = False
51+
52+
try:
53+
from scipy.spatial import KDTree
54+
55+
HAS_SCIPY = True
56+
except ModuleNotFoundError:
57+
HAS_SCIPY = False
58+
59+
60+
def check_lonboard() -> None:
61+
if HAS_LONBOARD is False:
62+
err = "Lonboard not installed, to use this function, please install lonboard."
63+
raise ModuleNotFoundError(err)
64+
65+
66+
def check_scipy() -> None:
67+
if HAS_SCIPY is False:
68+
err = "Scipy not installed, to use this function, please install scipy."
69+
raise ModuleNotFoundError(err)
70+
4671

4772
@pl.api.register_dataframe_namespace("spatial")
4873
class SpatialFrame:
@@ -691,8 +716,6 @@ def centroid_knn_join(
691716
This method relies on scipy.spatial's KDTree to find the neighbors.
692717
693718
"""
694-
from scipy.spatial import KDTree # NOQA:PLC0415
695-
696719
if left_on is None:
697720
left_on = on
698721
if right_on is None:
@@ -771,6 +794,7 @@ def viz(
771794
>>> df.spatial.viz()
772795
773796
""" # NOQA:E501
797+
check_lonboard()
774798
geoarrow_table = self.to_geoarrow(geometry_name)
775799

776800
return viz(
@@ -790,6 +814,7 @@ def _make_color_array(
790814
*,
791815
normalize_cmap_col: bool,
792816
) -> NDArray | None:
817+
check_lonboard()
793818
color = None
794819
if cmap_col is not None:
795820
if cmap_type == "continuous":
@@ -976,6 +1001,7 @@ def to_scatterplotlayer(
9761001
Implementation varies slightly from Lonboard for the setting of color and width to make it easy to use from the SpatialFrame.
9771002
9781003
""" # NOQA:E501
1004+
check_lonboard()
9791005
validate_cmap_input(
9801006
self._df,
9811007
fill_cmap_col,
@@ -1145,6 +1171,7 @@ def to_pathlayer(
11451171
Implementation varies slightly from Lonboard for the setting of color and width to make it easy to use from the SpatialFrame.
11461172
11471173
""" # NOQA:E501
1174+
check_lonboard()
11481175
validate_cmap_input(
11491176
self._df,
11501177
cmap_col,
@@ -1329,6 +1356,7 @@ def to_polygonlayer(
13291356
Implementation varies slightly from Lonboard for the setting of color and width to make it easy to use from the SpatialFrame.
13301357
13311358
""" # NOQA:E501
1359+
check_lonboard()
13321360
validate_cmap_input(
13331361
self._df,
13341362
fill_cmap_col,

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)