Repository navigation
Description
Ok, I've been wanting to extend the functionality in GeoJsonTooltip to Popups for a while now with a GeoJsonPopup class. This shouldn't be too hard at the Leaflet GeoJson .bindTooltip / .bindPopup method level - they operate at the same level.
It would be pretty easy to inherit/__init__ args from GeoJsonTooltip and copy over the GeoJsonTooltip template method and swap out the method name.
However, one functionality I'd like to add is to add a vegalite kwarg to the class and be able to pass a 'base' VegaLite and a 'join key' for both the VegaLite data source, and the GeoJson.
Right now, in the folium.map.Popup architecture, folium.features.VegaLite objects are added as 'children' via .add_to, and the render method on each child is invoked in the constructor, and vegaEmbed occurs in the render method of folium.features.VegaLite.
folium.features.GeoJsonPopup functionality would have to work a little differently since it's returning a JS function instead of a string. So I think that vegalite should be added as an argument, instead of a child.
The base chart spec can be added as a variable.
A deep copy is created using JSON.parse(JSON.stringify(spec)), and its values are mutated each time the .bindPopup function is invoked using a click, passing a key from properties to filter on and building a chart spec only for the subset of features. vegaEmbed passes the mutated chart and embeds to the function scope div element:
var key1 = spec.data.name
var speccopy = JSON.parse(JSON.stringify(spec))
geo_json_62d64f3ada804c0db5906054f1b52522.bindPopup(function(layer){
geovar = layer.feature.properties.NAME
vegavar = 'state'
var d = document.createElement('div')
speccopy.datasets[key1]=spec.datasets[key1].filter(record=>record[vegavar]==geovar)
vegaEmbed(d, speccopy)
return d
},{'maxWidth':900}
)
Source: git
Here is a rough in version what it would look like (not polished chart/map, just proof of concept).
What I'd like some feedback is some feedback on how to design such a functionality in folium working with our existing architecture and good coding practices. I'd like inherit parts from both GeoJsonTooltip and Popup but not sure the best way to go about such a task.
Activity
jtbaker commentedon Sep 17, 2018
The
VegaLitefunctionality could be a functionality that people might see use for inGeoJsonTooltipandTooltipas well. Is that something worth trying to tackle?Issues:
Vegalitespec objects, which have the data sources as a 1-level deep nested object, and are thus unreachable withObject.assign(). This may further compound our Jupyter Notebook breakdown issues when working with large datasets.I reached out about how to best do this on vega slack and was directed to immutable JavaScript libraries like Ramda and ClojureScript. Supposedly Leaflet is a CLJS-included libary, but I'm not super familiar with how to use it.
Any ideas/thoughts?
davidolmo commentedon Oct 29, 2018
@jtbaker , I wish I could help you, but I'm pretty new to folium, and lack of enough knowledge to be able to support in this issue. I just wanted to let you know that I've been doing extensive research around the topic of adding complex plotly graph as HTML or Vega graphs as geoJson popup, and didn't find any way to accomplish this. I'm finally exporting vega as png and including popup as static image
jtbaker commentedon Oct 31, 2018
@davidolmo all good, the learning is part of the process!
So in Leaflet, binding a Popup to geojsons can take either a string or a function(that returns a string or some html object as a result). The function can pass arguments from the mouse hover event (so you can get the details of the feature thats being hovered, and pass those values to other functions (like building a chart).
A way I've figured out to embed these more complex types of popups, with API calls etc, is to create a new HTML
<div>element withdocument.createElement("div")and then adding content to that with theinnerHTMLattribute. If that element has an htmlidthen you should be able to target that element to build javascript content (with something like VegaEmbed).Sorry for the horribly complex spaghetti of code (I was teaching myself JS in the process of building this and started adding functionality incrementally in the project), but a working example I have is at this jsfiddle. The most important section for the popups in regard to this is lines 105-112 of the javascript section. It creates the div element, returns the empty element, and the content in the div is updated after the API call has finished executing.
davidolmo commentedon Feb 12, 2019
@jtbaker please check my last response on https://github.com/python-visualization/folium/issues/904#issuecomment-462764695 as I think it is a workaround on this issue? It iterates over all GeoJson to create one geoJson per record, but it doesn't seem like it impacts on more weight on the map
jtbaker commentedon Feb 17, 2019
Hey @davidolmo! Yes, this is a serviceable workaround for the time being.
Once #1023 moves forward, however,
GeoJsonPopupwill have the same (and more functionality) asGeoJsonTooltip.jtbaker commentedon Feb 19, 2019
closing this issue - any further discussion can take place in #1023 .