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

start geopandas extension #728

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
616 changes: 309 additions & 307 deletions examples/explore.ipynb

Large diffs are not rendered by default.

42 changes: 25 additions & 17 deletions lonboard/geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import numpy as np
import pandas as pd

from . import basemap
from . import viz
from . import basemap, viz
from .colormap import apply_categorical_cmap, apply_continuous_cmap

__all__ = ["LonboardAccessor"]
Expand Down Expand Up @@ -33,7 +32,7 @@ def explore(
alpha=1,
layer_kwargs=None,
map_kwargs=None,
classify_kwargs=None,
classification_kwds=None,
nan_color=[255, 255, 255, 255],
color=None,
wireframe=False,
Expand Down Expand Up @@ -75,7 +74,7 @@ def explore(
map_kwargs : dict, optional
additional keyword arguments passed to lonboard.viz map_kwargs, by default
None
classify_kwargs : dict, optional
classification_kwds : dict, optional
additional keyword arguments passed to `mapclassify.classify`, by default
None
nan_color : list-like, optional
Expand Down Expand Up @@ -111,7 +110,7 @@ def explore(
alpha,
layer_kwargs,
map_kwargs,
classify_kwargs,
classification_kwds,
nan_color,
color,
wireframe,
Expand All @@ -133,7 +132,7 @@ def _dexplore(
alpha=1,
layer_kwargs=None,
map_kwargs=None,
classify_kwargs=None,
classification_kwds=None,
nan_color=[255, 255, 255, 255],
color=None,
wireframe=False,
Expand Down Expand Up @@ -174,7 +173,7 @@ def _dexplore(
geometry type), by default None
map_kwargs : dict, optional
additional keyword arguments passed to lonboard.viz map_kwargs, by default None
classify_kwargs : dict, optional
classification_kwds : dict, optional
additional keyword arguments passed to `mapclassify.classify`, by default None
nan_color : list-like, optional
color used to shade NaN observations formatted as an RGBA list, by
Expand All @@ -194,13 +193,6 @@ def _dexplore(
a lonboard map with geodataframe included as a Layer object.

"""
try:
from mapclassify.util import get_color_array
from matplotlib import colormaps
except ImportError as e:
raise ImportError(
"you must have the `mapclassify` package installed to use this function"
) from e

providers = {
"CartoDB Positron": basemap.CartoBasemap.Positron,
Expand All @@ -213,8 +205,8 @@ def _dexplore(

if map_kwargs is None:
map_kwargs = dict()
if classify_kwargs is None:
classify_kwargs = dict()
if classification_kwds is None:
classification_kwds = dict()
if layer_kwargs is None:
layer_kwargs = dict()
if isinstance(elevation, str):
Expand Down Expand Up @@ -252,6 +244,13 @@ def _dexplore(
else:
layer_kwargs["get_fill_color"] = color
if column is not None:
try:
from matplotlib import colormaps
except ImportError as e:
raise ImportError(
"you must have matplotlib installed to style by a column"
) from e

if column not in gdf.columns:
raise ValueError(f"the designated column {column} is not in the dataframe")
if gdf[column].dtype in ["O", "category"]:
Expand All @@ -273,14 +272,23 @@ def _dexplore(
values=transformed, cmap=colormaps[cmap], alpha=alpha
)
else:
try:
from mapclassify.util import get_color_array
except ImportError as e:
raise ImportError(
"you must have the `mapclassify` package installed to use this function"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe more clear?

"you must have the `mapclassify` package installed if you pass `classification_kwds`"

) from e
if k is not None and 'k' in classification_kwds:
# k passed directly takes precedence
classification_kwds.pop('k')
color_array = get_color_array(
knaaptime marked this conversation as resolved.
Show resolved Hide resolved
gdf[column],
scheme=scheme,
k=k,
cmap=cmap,
alpha=alpha,
nan_color=nan_color,
**classify_kwargs,
**classification_kwds,
)

if LINE:
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ classifiers = [

[project.optional-dependencies]
cli = ["click>=8.1.7", "pyogrio>=0.8", "shapely>=2"]
geopandas = ["geopandas>=0.13", "pandas>=2", "shapely>=2"]
geopandas = ["geopandas>=0.13", "mapclassify>=2.8.1", "pandas>=2", "shapely>=2"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can take out the dependency altogether from here. If someone hits the error they'll be instructed to install it.

movingpandas = ["movingpandas>=0.17"]
explore = ["mapclassify>=2.8.1"]


[project.urls]
Expand Down
Loading