1616import pyproj
1717import shapely
1818from 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
2119from polars import col as c
2220
2321from ._utils import validate_cmap_input , validate_width_and_radius_input
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" )
4873class 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 ,
0 commit comments