Hey guys,
I wonder whether it's possible for DivIcon
to have an automatic size.
The context:
On my map, hovering over a country will trigger a tooltip with the country's name.
I also have several DivIcon
s, with the following code:
folium.Marker(
location=[lat, lon],
icon=folium.DivIcon(
html='''
<div style="background-color: red; display: inline-block;">
<span>
{region_name}:
<br />{region_info}
</span>
</div>
''',
icon_size=(100, 100),
icon_anchor=(0, 0),
)
).add_to(map)
Because of the variation in region_name
s and region_info
s, the width of the content varies between 50 and 90 pixels. I want my text to be displayed on two lines and the div
to fit its content: the display: inline-block
in the html allows me to do this, even if my icon_size
is constant.
But the problem is that the size of the DivIcon
affects the tooltips: I can have a 70px-wide block with content, but an area of 30px next to it which doesn't display as a block but still counts as that block. So if I hover out of the visible block, instead of the tooltip appearing with some country's name, nothing happens. This is especially problematic when this area covers a bunch of small countries.
What I'd like to happen is for the DivIcon to fit to the html within, or to have some kind of dynamic behaviour. I have tried doing icon_size=(10, 10)
or removing icon_size
, but the block just shrinks to fit to the longest word in the content. I have tried to go through the code for folium.DivIcon
, branca.MacroElement
, branca.Figure
, etc., but wasn't able to get control of this behaviour.
Any suggestions welcome.
gpd.__version__
'0.6.1'
folium.__version__
'0.11.0'
gdf.crs # gdf created via gpd.read_file()
{'init': 'epsg:4326'}
folium.GeoJson(gdf, name='gdf test').add_to(test_map)
RuntimeError Traceback (most recent call last)
<ipython-input-24-268ce60b1d50> in <module>
35
---> 36 folium.GeoJson(gdf, name='gdf test').add_to(test_map)
37
. . .
S:\MDP\Programs\Anaconda3\lib\site-packages\pyproj\__init__.py in __new__(self, projparams, preserve_units, **kwargs)
360 # on case-insensitive filesystems).
361 projstring = projstring.replace('EPSG','epsg')
--> 362 return _proj.Proj.__new__(self, projstring)
363
364 def __call__(self, *args, **kw):
_proj.pyx in _proj.Proj.__cinit__()
RuntimeError: b'no arguments in initialization list'
fg = folium.FeatureGroup(name="tiles")
with open(geojson_file) as geojson:
tileDic = json.load(geojson)
for feature in tileDic['features']:
coords = [[y,x] for i in feature['geometry']['coordinates'] for x,y in i ]
tileId = feature['properties']['tile_id']
print(coords)
print(tileId)
fg.add_child(folium.Polygon(locations=coords,fill=True,popup=(folium.Popup(tileId))))
I would like to publish information from an ArcGIS MapServer to Folium. I had some It looks like that can be done if a given api is configured to create tiles, but many are not. My first question is, does anyone know if this is possible?
Some ArcGIS MapServer services are published in the WMS format but these are relative few. I tried to configure the api listed below as a Folium “wms_layer” but I am getting a driver error (also listed below). Anyone know if this is even possible?
m.add_wms_layer(wms_name="Census Current (2020) WMS",wms_url="https://tigerweb.geo.census.gov/arcgis/services/TIGERweb/tigerWMS_Current/MapServer/WMSServer",
wms_format="image/png",
wms_layers='2010 Census Public Use Microdata Areas Labels')
DriverError: D:\Dev\APS\aps\data\satskred\AvalDet_20190101_051134_ref_20181226_trno_095_VV\AvalDet_20190101_051134_ref_20181226_trno_095_VV.shp\AvalDet_20190101_051134_ref_20181226_trno_095_VV.shp: No such file or directory
import folium
from folium import features
m = folium.Map([ 45.75345246,12.34117475], zoom_start=7)
points_ = {'type': 'FeatureCollection',
'features': [{'type': 'Feature',
'properties': {'Codice': 500732, 'Categoria': 'D1', 'Cluster': 3},
'geometry': {'type': 'Point', 'coordinates': [12.34117475, 45.75345246]},
'id': '0'},
{'type': 'Feature',
'properties': {'Codice': 500732, 'Categoria': 'A2', 'Cluster': 3,'iconColor':'red'},
'geometry': {'type': 'Point', 'coordinates': [14.34117475, 46.75345246]},
'id': '1'}]}
pp = folium.Popup("hello",fields=['Codice'])
ic = features.Icon(iconColor="red")
gj = folium.GeoJson(points_)#, tooltip=tooltip)
gj.add_child(ic)
gj.add_child(pp)
m.add_child(gj).add_child(pp).add_child(ic)
m
Hi is there a way to add a symbol/marker to the center of the widget?
I wand to mark the center for a moving map application.
Could you use the centroid? I loaded data into a GeoDataFrame and then it's easy to determine the center geocode:
for index, row in df.iterrows():
centroid = row.geometry.centroid
Hi everyone. I've joined this Gitter in the hope of finding some active community that uses Folium. I just started using it this month.
Can anyone share guidance on how to create 'labels' for folium.features.GeoJson? I have a lot of small polygons that I want to permanently label and the tooltip/popups are not working well for my use-case.
I've been driving myself insane trying to come up with a solution. The main challenge I have is that all the labels resize with the map. I do not want this; I want a fixed size in meters.
1) Tried using HTML in the DivIcon of a Marker. This works and I can create labels but they resize on zoom.
2) I've tried permanent popups (permanent=True) on folium.GeoJsonTooltip. Same issue, the tooltip is resizing.
3) I've even created custom text images using Python Image Library (PIL) to create PNG files that I add in using ImageOverlay. This is not resizing the images (yay) but several implementation issues: (i) the images are pixelated, the quality is poor, (ii) I am generating a lot of images (several hundred), and (iii) I want to size the labels depending on the polygon size; I want every label to stay inside its polygon but be as large as possible.
The only element I can find in Folium that isn't resizing is folium.Circle.
Thanks for anyone who can help me out. I can't help but feel I am missing obvious as labeling feels like it should be a basic requirement.
Cheers!
Hi
I am trying to plot some data using folium maps.
Below is my python code and a screenshot(1st image).
india_coord = [lat,long]
my_map = folium.Map(location = india_coord,zoom_start=5,tiles=None,overlay=False)
feature_group = folium.FeatureGroup(name='Central Region',overlay=True)
feature_group2 = folium.FeatureGroup(name='South Region',overlay=True)
feature_group3 = folium.FeatureGroup(name='East Region',overlay=True)
feature_group4 = folium.FeatureGroup(name='West Region',overlay=True)
feature_group5 = folium.FeatureGroup(name='North Region',overlay=True)
feature_group.add_to(my_map)
feature_group2.add_to(my_map)
feature_group3.add_to(my_map)
feature_group4.add_to(my_map)
feature_group5.add_to(my_map)
c1 = folium.Choropleth(
geo_data = india,
name = 'India',
legend_name = 'India',
fill_color = 'orange',
fill_opacity = 0.3,
highlight = True).add_to(my_map)
folium.TileLayer('openstreetmap',overlay=False,name = f'SUMMARY {summ_date}').add_to(my_map)
folium.LayerControl().add_to(my_map)
folium.Marker(location,tooltip = data1,popup = popup, icon=folium.Icon(color='cadetblue',icon = 'fa-industry', prefix='fa')).add_to(feature_group)
...
...
...
folium.Marker(location,tooltip = data1,popup = popup, icon=folium.Icon(color='cadetblue',icon = 'fa-industry', prefix='fa')).add_to(feature_group5)
my_map.add_child(feature_group)
my_map.add_child(feature_group2)
my_map.add_child(feature_group3)
my_map.add_child(feature_group4)
my_map.add_child(feature_group5)
With this code I get the map above but I need to make another layer control option for another summary.
For ex. if I add another
folium.LayerControl().add_to(my_map)
to my code I get the following(2nd Image):
But this layer control has the same options as the above summary.
Is there a way in folium to create different layer controls with different data on the same map so that If I am selecting the first summary in the first layer control I get the data like in image1 but if I choose the below layer control the data is displayed according to that particular layer control.
How do I link different data with different layer control to view them on the same map.
Thank you
import pandas as pd
import folium
from folium.plugins import MarkerCluster
import shapely.wkt
data = pd.read_csv('crops.csv', index_col='UniqueID')
field = data.iloc[50, :]
pgon = shapely.wkt.loads(field['geometry'])
coords = [[j,i] for i,j,k in list(pgon.exterior.coords)]
mmap = folium.Map(location = coords[0], zoom_start = 20)
folium.PolyLine(locations=coords,
line_opacity=0.5).add_to(mmap)
MarkerCluster(coords).add_to(mmap)
display(mmap)
Hi community. I have question related to selecting a feature object after using a search. I want to highlight the searched feature, so that it is selected. Here is my code.
'''
from folium.plugins import Search
search_link = Search(layer=pct_feature_group, geom_type="LineString", placeholders = "Search for Link ID",
collapsed="True", search_label = 'id', search_zoom = 17, position='topleft',
).add_to(pct_m)
step_pct.add_to(pct_m)
pct_m
'''
How should I insert the highlight_function?