-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
GeoJsonTooltip fails to show pop-up on non-connected areas (e.g. city with islands) #929
Comments
Hey @mjkvaak! Glad you're enjoying it. Are you sure that the polygons in question have a |
I now digged deeper into the "bug" and found out that the problems with pop-ups' labels are encountered when the geometry types of non-connected areas equaled to shapely's GeometryCollection. After changing the types into shapely Multipolygon (by running .apply(lambda x: Multipolygon(x)) on the geometry column) the pop-ups worked as they should. GeoJson understood both GeometryCollection and Multipolygon -types in the sense that they both produced the correct areas on the map, but on the former I couldn't get the pop-ups working with the non-connected areas. |
Ok, good to know. Did GeoPandas One thing I did notice when developing the tool is that when |
I'm not sure if I fully understood, but after applying the .to_json() on the GDF the GeometryCollection objects remain as GeometryCollection objects, i.e. are not converted into MultiPolygons. It may be more convenient for you to download the json-file I am using from GitHub: https://github.com/tomimick/mapcolorizer/blob/master/data-finland/data/kuntarajat.geojson (Borders of Finnish Cities) and playing with it yourself. The below example demonstrates the problem I'm having with the pop-up -tool. Ps. I forgot to mention earlier, but I'm running on folium version 0.5.0+152.g05f8e81, win 7 and Chrome 68.0.3440.106 (Official build, 64-bit).
|
Ok, thanks for the info! I tested it and see what's happening. Did a little more research and found this and this. It seems like the best practice right now is to use the Multi-geometries when possible instead of of the GeometryCollections - most use cases for GeometryCollection, I think could/should warrant their own Leaflet's I think this could possibly be accounted for in our tool, but I'm not sure how common it is to encounter these Geometry Collections in practice or if it is more of a unicorn. @Conengmo, @ocefpaf, /anyone else, have you guys come across these? Thanks for pointing out this bug and bringing to our attention, @mjkvaak! |
Good that you already dived into this problem. I see how GeometryCollection should be rare, but may be misused more often as we see here. Is it even possible for us to fix this, or are we dependent on Leaflet? You're description suggests we cannot. Maybe unless we define a separate tooltip case for Geometry collection. Maybe the best/easiest fix is to do an assertion on the type when someone is adding tooltips. I think it's not unreasonable to have tooltips not working when someone wants to use a rare type. |
I did a little testing on it and was able to work around the limitation using the Check it out here: jsfiddle Adapting to this (it seems like?) would preclude the usage of our separate class templating structure in folium, though, since I don't think we can access This makes me think of something else though: toggling This could be a good solution for map labeling. I think |
@mjkvaak what is your problem, but thanks a lot for Btw, if you post your code here, it can be easily formatted using
E.g.: fmap= folium.Map(
location=[65,25],
tiles='cartodbpositron',
zoom_start=5,
control_scale=True
)
folium.GeoJson(
gdf[['name', 'geometry']].to_json(),
name='Finnish cities',
show=True,
style_function=lambda feature: {
'fillColor': 'green',
'color': 'black',
'weight': 1,
'dashArray': '5, 5',
'fillOpacity':0.5
},
highlight_function=lambda x: {'weight':3,
'color':'black',
'fillOpacity':1
},
tooltip=folium.features.GeoJsonTooltip(
fields=['name'],
aliases=['City name:'],
),
).add_to(fmap) can anyone add the above to the examples?... |
@Demetrio92 I agree that it can take some effort to understand how the GeoJson tooltips work and it was very difficult to find examples in the interwebs. Glad to hear that the above code could help you! |
@jtbaker I like the idea of having For this issue to me it seems we have three options:
I'm not a fan of 1, because it may introduce new problems. 2 seems the least risky. I would be okay with 3, since though this issue is a bug, things are not broken, just not as expected. @jtbaker and @mjkvaak, could you chime in on what action we should take? I want to work towards closing this issue. |
@Conengmo I'd go for option 2. Also option 3 sounds OK, since both the issue and the solution have been covered here. |
@mjkvaak thanks for chiming in. Do you want to open a PR for this? I'm now thinking we could check if there is a If you don't have the time that's fine as well, we'll go with option 3 :) |
@Conengmo Thanks for the offer, but I'm afraid I am not experienced enough coder to commit changes to this package. What you suggested about the warning msg sounds good, thinking that there might be people for whom the unexpected behaviour goes otherwise unnoticed (which can be a bit nasty in some situations). That is, better to give a friendly warning suggesting to use something else than GeometryCollection. |
@Conengmo I took a stab at some QA on this. The problem is, if we place this in the
the ValueError doesn't display because the render function is overwriting the content in the out cell in Jupyter. I could rework it as an assertion, but that would break execution and we just want it to be a warning. |
Looks good what you're doing. I don't really understand what the problem is, if you raise a |
I don't think it should be a breaking QC check though - just give the user a warning that the |
Good point. How about a
|
Hi there!
First of all, thank you so much for the excellent work you have done so far with folium!
I played with the folium GeoJson and noticed a problem with the GeoJsonTooltip -function, namely it fails to show a pop-up when moving mouse over an area that is not connected, that is consisting of two (or more) different areas (Polygons). You can see the unwanted behavior in the screen cap I attached with cities having island. The names of the cities consisting of only one connected area (only one Polygon) work as they should. I guess that when generating the pop up the function tries to look for a polygon but fails when it finds a list of them instead.
The text was updated successfully, but these errors were encountered: