Skip to content
New issue

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

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

Already on GitHub? # to your account

Integrate mapclassify #136

Open
kylebarron opened this issue Oct 18, 2023 · 9 comments
Open

Integrate mapclassify #136

kylebarron opened this issue Oct 18, 2023 · 9 comments

Comments

@kylebarron
Copy link
Member

From the geopandas.plot docstring

Name of a choropleth classification scheme (requires mapclassify). A mapclassify.MapClassifier object will be used under the hood. Supported are all schemes provided by mapclassify (e.g. ‘BoxPlot’, ‘EqualInterval’, ‘FisherJenks’, ‘FisherJenksSampled’, ‘HeadTailBreaks’, ‘JenksCaspall’, ‘JenksCaspallForced’, ‘JenksCaspallSampled’, ‘MaxP’, ‘MaximumBreaks’, ‘NaturalBreaks’, ‘Quantiles’, ‘Percentiles’, ‘StdMean’, ‘UserDefined’). Arguments can be passed in classification_kwds.

@davidbailey
Copy link

davidbailey commented Jan 4, 2024

The following worked for me:


import numpy

from lonboard import Map, SolidPolygonLayer
from mapclassify import JenksCaspall

layer = SolidPolygonLayer.from_geopandas(zones_interpolated, opacity=0.2)
jc = JenksCaspall(
    zones_interpolated["ProductionAttractionSum Density"].fillna(0),
    k=20
)
colors = []
for color in jc.yb:
    colors.append(Viridis_20.colors[color])
layer.get_fill_color = numpy.uint8(colors)

Map(layers=[layer])

@kylebarron
Copy link
Member Author

Thanks! By the way, I'm not sure where Viridis_20 comes from, but doing an array lookup would be much faster than a loop. You should probably be able to do something like

Viridis_20.colors[jc.yb]

@knaaptime
Copy link

since this is still open, i've cooked up a proof of concept that uses pandas's extension API to create a geopandas explore method that uses lonboard as a backend. The code is here and an example notebook here. The functions are fairly small and dont do much besides handle classification schemes and categorical variables using mapclassify/matplotlib and expose a few more styling args for convenience and parity with the existing explore

works well for my use cases and I'm happy to keep it in geosnap if it's out of scope. That said, I suspect it might be more useful/visible in a central location like lonboard itself so also more than happy to contribute it if anyone has interest

@knaaptime
Copy link

heres a little notebook that updates the color scheme dynamically using jupyterwidgets (note, not all classifiers take advantage of all k)

https://gist.github.com/knaaptime/cc6863d99a4a08a9825191dc27b3a5f1

Image

@kylebarron
Copy link
Member Author

kylebarron commented Jan 7, 2025

Thanks @knaaptime, that's very cool.

I don't think I want to add mapclassify as a required dependency of Lonboard, but I'm open to having a GeoPandas integration like it's mentioned that hvplot does. So then we could have

from lonboard.geopandas import register_plot_methods

register_plot_methods()

Then

gdf = gpd.GeoDataFrame(..)
gdf.plot()
# or maybe
gdf.lb.explore()
# or even have a way to override .explore

Would you be open to making the PR?

Then we could have the mapclassify code be self-contained within the GeoPandas integration.

@knaaptime
Copy link

cool, that's exactly what i was thinking (agree, no desire to make mapclassify a hard dependency). Happy to put together the PR

@knaaptime
Copy link

i'll start the PR so the code is more visible, but for some reason I cant import the local version of lonboard because it cant find /lonboard/lonboard/static/index.css and .js

@knaaptime
Copy link

(nvm, i just grabbed those files from the tarball on pypi; looks like maybe they never got committed or something)

@kylebarron
Copy link
Member Author

https://github.com/developmentseed/lonboard/blob/main/DEVELOP.md

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants