Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SEDONA-476] Expose kepler & pydeck as extras #1229

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions docs/tutorial/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,9 @@ Spatial query results can be visualized in a Jupyter lab/notebook environment us
SedonaPyDeck exposes APIs to create interactive map visualizations using [pydeck](https://pydeck.gl/index.html#) based on [deck.gl](https://deck.gl/)

!!!Note
To use SedonaPyDeck, GeoPandas and PyDeck must be installed. We recommend the following installation commands:
To use SedonaPyDeck, install sedona with the `pydeck-map` extra:
```
pip install 'pandas<=1.3.5'
pip install 'geopandas<=0.10.2'
pip install pydeck==0.8.0
pip install sedona[pydeck-map]
```

The following tutorial showcases the various maps that can be created using SedonaPyDeck, the datasets used to create these maps are publicly available.
Expand Down Expand Up @@ -658,11 +656,9 @@ Spatial query results can be visualized in a Jupyter lab/notebook environment us
SedonaKepler exposes APIs to create interactive and customizable map visualizations using [KeplerGl](https://kepler.gl/).

!!!Note
To use SedonaKepler, GeoPandas and KeplerGL must be installed. We recommend the following installation commands:
To use SedonaKepler, install sedona with the `kepler-map` extra:
```
pip install 'pandas<=1.3.5'
pip install 'geopandas<=0.10.2'
pip install keplergl==0.3.2
pip install sedona[kepler-map]
```

This tutorial showcases how simple it is to instantly visualize geospatial data using SedonaKepler.
Expand Down
8 changes: 7 additions & 1 deletion python/sedona/maps/SedonaKepler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# specific language governing permissions and limitations
# under the License.

from keplergl import KeplerGl
from sedona.maps.SedonaMapUtils import SedonaMapUtils


Expand All @@ -30,6 +29,13 @@ def create_map(cls, df=None, name="unnamed", config=None):
dataframe, if a df is passed with no name, a default name of 'unnamed' is set for it.
param config: [Optional] A map config to be applied to the rendered map :return: A map object
"""

try:
from keplergl import KeplerGl
except ImportError:
msg = "Install sedona[kepler-map] to convert sedona dataframes to kepler maps."
raise ImportError(msg) from None

kepler_map = KeplerGl()
if df is not None:
SedonaKepler.add_df(kepler_map, df, name)
Expand Down
20 changes: 19 additions & 1 deletion python/sedona/maps/SedonaPyDeck.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

import pydeck as pdk
from types import ModuleType
from sedona.maps.SedonaMapUtils import SedonaMapUtils


Expand All @@ -37,6 +37,7 @@ def create_choropleth_map(cls, df, fill_color=None, plot_col=None, initial_view_
:param map_provider:
:return: A pydeck Map object with choropleth layer added:
"""
pdk = _try_import_pydeck()

if initial_view_state is None:
gdf = SedonaPyDeck._prepare_df_(df, add_coords=True)
Expand Down Expand Up @@ -79,6 +80,8 @@ def create_geometry_map(cls, df, fill_color="[85, 183, 177, 255]", line_color="[
:param map_provider: optional map_provider of the pydeck map
:return: A pydeck map with a GeoJsonLayer map added
"""
pdk = _try_import_pydeck()

geometry_col = SedonaMapUtils.__get_geometry_col__(df)
gdf = SedonaPyDeck._prepare_df_(df, geometry_col=geometry_col)
geom_type = gdf[geometry_col][0].geom_type
Expand Down Expand Up @@ -116,6 +119,8 @@ def create_scatterplot_map(cls, df, fill_color="[255, 140, 0]", radius_col=1, ra
:param map_provider: optional map_provider to be added to the pydeck map
:return: A pydeck map object with a scatterplot layer added
"""
pdk = _try_import_pydeck()

gdf = SedonaPyDeck._prepare_df_(df, add_coords=True)
layer = pdk.Layer(
"ScatterplotLayer",
Expand Down Expand Up @@ -152,6 +157,7 @@ def create_heatmap(cls, df, color_range=None, weight=1, aggregation="SUM", initi
:param map_provider: Optional map_provider for the pydeck map
:return: A pydeck map with a heatmap layer added
"""
pdk = _try_import_pydeck()

gdf = SedonaPyDeck._prepare_df_(df, add_coords=True)

Expand Down Expand Up @@ -239,6 +245,7 @@ def _create_coord_column_(cls, gdf, geometry_col, add_points=False):

@classmethod
def _create_fat_layer_(cls, gdf, fill_color, line_color, elevation_col):
pdk = _try_import_pydeck()
layer = pdk.Layer(
'GeoJsonLayer', # `type` positional argument is here
data=gdf,
Expand All @@ -254,3 +261,14 @@ def _create_fat_layer_(cls, gdf, fill_color, line_color, elevation_col):
)

return layer


def _try_import_pydeck() -> ModuleType:
try:
import pydeck as pdk

except ImportError:
msg = "Install sedona[pydeck-map] to convert sedona dataframes to pydeck maps."
raise ImportError(msg) from None

return pdk
9 changes: 1 addition & 8 deletions python/sedona/spark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,4 @@
from sedona.register import SedonaRegistrator
from sedona.spark.SedonaContext import SedonaContext
from sedona.raster_utils.SedonaUtils import SedonaUtils
try:
from sedona.maps.SedonaKepler import SedonaKepler
except:
print('Skipping SedonaKepler import, verify if keplergl is installed')
try:
from sedona.maps.SedonaPyDeck import SedonaPyDeck
except:
print('Skipping SedonaPyDeck import, verify if pydeck is installed')
from sedona.maps import SedonaKepler, SedonaPyDeck
7 changes: 6 additions & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@
long_description_content_type="text/markdown",
python_requires='>=3.6',
install_requires=['attrs', "shapely>=1.7.0"],
extras_require={"spark": ['pyspark>=2.3.0']},
extras_require={
"spark": ["pyspark>=2.3.0"],
"pydeck-map": ["pandas<=1.3.5", "geopandas<=0.10.2", "pydeck==0.8.0"],
"kepler-map": ["pandas<=1.3.5", "geopandas<=0.10.2", "keplergl==0.3.2"],
"all": ["pyspark>=2.3.0", "pandas<=1.3.5", "geopandas<=0.10.2","pydeck==0.8.0", "keplergl==0.3.2"],
},
project_urls={
'Documentation': 'https://sedona.apache.org',
'Source code': 'https://github.com/apache/sedona',
Expand Down