Skip to content

Commit

Permalink
[SEDONA-622] Improve SedonaPyDeck behavior (#1515)
Browse files Browse the repository at this point in the history
* feat: throw an error when column is not numeric and add stroked parameter

* fix: tests
  • Loading branch information
furqaankhan committed Jul 9, 2024
1 parent 9a774cd commit 9c3c67d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
8 changes: 6 additions & 2 deletions docs/api/sql/Visualization_SedonaPyDeck.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Following are details on all the APIs exposed via SedonaPyDeck:
```python
def create_geometry_map(df, fill_color="[85, 183, 177, 255]", line_color="[85, 183, 177, 255]",
elevation_col=0, initial_view_state=None,
map_style=None, map_provider=None, api_keys=None):
map_style=None, map_provider=None, api_keys=None, stroked=True):
```

The parameter `fill_color` can be given a list of RGB/RGBA values, or a string that contains RGB/RGBA values based on a column, and is used to color polygons or point geometries in the map
Expand All @@ -33,18 +33,22 @@ The parameter `line_color` can be given a list of RGB/RGBA values, or a string t

The parameter `elevation_col` can be given a static elevation or elevation based on column values like `fill_color`, this only works for the polygon geometries in the map.

The parameter `stroked` determines whether to draw an outline around polygons and points, accepts a boolean value. For more information, please refer to this [documentation of deck.gl](https://deck.gl/docs/api-reference/layers/geojson-layer#:~:text=%27circle%27.-,stroked,-(boolean%2C%20optional)).

Optionally, parameters `initial_view_state`, `map_style`, `map_provider`, `api_keys` can be passed to configure the map as per user's liking.
More details on the parameters and their default values can be found on the PyDeck website as well by deck.gl [here](https://github.com/visgl/deck.gl/blob/8.9-release/docs/api-reference/layers/geojson-layer.md)

### **Choropleth Map**

```python
def create_choropleth_map(df, fill_color=None, plot_col=None, initial_view_state=None, map_style=None,
map_provider=None, api_keys=None, elevation_col=0)
map_provider=None, api_keys=None, elevation_col=0, stroked=True)
```

The parameter `fill_color` can be given a list of RGB/RGBA values, or a string that contains RGB/RGBA values based on a column.

The parameter `stroked` determines whether to draw an outline around polygons and points, accepts a boolean value. For more information please refer to this [documentation of deck.gl](https://deck.gl/docs/api-reference/layers/geojson-layer#:~:text=%27circle%27.-,stroked,-(boolean%2C%20optional)).

For example, all these are valid values of fill_color:

```python
Expand Down
23 changes: 17 additions & 6 deletions python/sedona/maps/SedonaPyDeck.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# under the License.

from types import ModuleType

from pyspark.sql.types import FloatType, DoubleType, IntegerType, LongType, DecimalType, ShortType, ByteType

from sedona.maps.SedonaMapUtils import SedonaMapUtils


Expand All @@ -24,7 +27,7 @@ class SedonaPyDeck:
# User Facing APIs
@classmethod
def create_choropleth_map(cls, df, fill_color=None, plot_col=None, initial_view_state=None, map_style=None,
map_provider=None, elevation_col=0, api_keys=None):
map_provider=None, elevation_col=0, api_keys=None, stroked=True):
"""
Create a pydeck map with a choropleth layer added
:param elevation_col: Optional elevation for the polygons
Expand All @@ -38,6 +41,14 @@ def create_choropleth_map(cls, df, fill_color=None, plot_col=None, initial_view_
:param api_keys: Optional dictionary of API keys for Map providers
:return: A pydeck Map object with choropleth layer added:
"""

for field in df.schema.fields:
if field.name == plot_col and field.dataType not in [IntegerType(), FloatType(), DoubleType(), LongType(),
DecimalType(), ShortType(), ByteType()]:
message = (f"'{field.name}' must be of numeric type. \nIf you are importing data from csv set "
f"'inferSchema' option as true")
raise TypeError(message) from None

pdk = _try_import_pydeck()

if initial_view_state is None:
Expand All @@ -56,7 +67,7 @@ def create_choropleth_map(cls, df, fill_color=None, plot_col=None, initial_view_
get_fill_color=fill_color,
opacity=1.0,
get_elevation=elevation_col,
stroked=False,
stroked=stroked,
extruded=True,
wireframe=True,
pickable=True
Expand All @@ -68,7 +79,7 @@ def create_choropleth_map(cls, df, fill_color=None, plot_col=None, initial_view_
@classmethod
def create_geometry_map(cls, df, fill_color="[85, 183, 177, 255]", line_color="[85, 183, 177, 255]",
elevation_col=0, initial_view_state=None,
map_style=None, map_provider=None, api_keys=None):
map_style=None, map_provider=None, api_keys=None, stroked=True):
"""
Create a pydeck map with a GeoJsonLayer added for plotting given geometries.
:param line_color:
Expand All @@ -92,7 +103,7 @@ def create_geometry_map(cls, df, fill_color="[85, 183, 177, 255]", line_color="[
# line_color = "[237, 119, 79]"

layer = SedonaPyDeck._create_fat_layer_(gdf, fill_color=fill_color, elevation_col=elevation_col,
line_color=line_color)
line_color=line_color, stroked=stroked)

if initial_view_state is None:
initial_view_state = pdk.data_utils.compute_view(gdf['coordinate_array_sedona'])
Expand Down Expand Up @@ -229,15 +240,15 @@ def _create_coord_column_(cls, gdf, geometry_col, add_points=False):
lambda val: list(SedonaMapUtils.__extract_coordinate__(val[geometry_col], type_list)), axis=1)

@classmethod
def _create_fat_layer_(cls, gdf, fill_color, line_color, elevation_col):
def _create_fat_layer_(cls, gdf, fill_color, line_color, elevation_col, stroked):
pdk = _try_import_pydeck()
layer = pdk.Layer(
'GeoJsonLayer', # `type` positional argument is here
data=gdf,
auto_highlight=True,
get_fill_color=fill_color,
opacity=0.4,
stroked=False,
stroked=stroked,
extruded=True,
get_elevation=elevation_col,
get_line_color=line_color,
Expand Down
5 changes: 3 additions & 2 deletions python/tests/maps/test_sedonapydeck.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def testChoroplethMap(self):
buildings_csv_df = self.spark.read.format("csv"). \
option("delimiter", ","). \
option("header", "true"). \
option("inferSchema", "true"). \
load(google_buildings_input_location)
buildings_csv_df.createOrReplaceTempView("buildings_table")
buildings_df = self.spark.sql(
Expand All @@ -43,7 +44,7 @@ def testChoroplethMap(self):
auto_highlight=True,
get_fill_color=fill_color,
opacity=1.0,
stroked=False,
stroked=True,
extruded=True,
wireframe=True,
get_elevation=0,
Expand All @@ -70,7 +71,7 @@ def testPolygonMap(self):
auto_highlight=True,
get_fill_color="[85, 183, 177, 255]",
opacity=0.4,
stroked=False,
stroked=True,
extruded=True,
get_elevation='confidence * 10',
pickable=True,
Expand Down

0 comments on commit 9c3c67d

Please sign in to comment.