I want to change the icon of the marker in .explore to a house icon.
I have read the documentation of geopandas.GeoDataFrame.explore and folium, still not able to understand it.
JavaScript
x
9
1
geo_df.explore(m=m
2
,column='pop'
3
,tooltip={"name","pop"}
4
,cmap='summer'
5
,style_kwds=dict(stroke=True,weight=1,color='black', opacity=0.5, fillOpacity=0.9)
6
,marker_kwds=dict(radius=5,icon=folium.Icon(icon='house-blank'))
7
,name="Residental"
8
)
9
The icon represents the position of the house and I set colormap base on an integer value.
Also, is there a way to make a radius size don’t change when zoom?
Advertisement
Answer
There is a feature in folium GeoJson Marker and a feature in geopandas flexible style function that can be used to meet your requirement
- make your code a MWE using cities dataset
- define
marker_type
andmarker_kwds
so that a DivIcon is used - define a style function so that marker is formatted as you require. Actually using column created by geopandas from
column
andcmap
parameters - the size of markers does not change on zoom as font-size has been set in pixels
JavaScript
1
29
29
1
import geopandas as gpd
2
import folium
3
import numpy as np
4
5
geo_df = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
6
geo_df["pop"] = np.random.randint(1, 5, len(geo_df))
7
m = None
8
9
m = geo_df.explore(
10
m=m,
11
column="pop",
12
tooltip={"name", "pop"},
13
cmap="summer",
14
style_kwds=dict(
15
style_function=lambda x: {
16
"html": f"""<span class="fa fa-home"
17
style="color:{x["properties"]["__folium_color"]};
18
font-size:14px"></span>"""
19
},
20
),
21
marker_type="marker",
22
marker_kwds=dict(icon=folium.DivIcon()),
23
name="Residental",
24
height=300,
25
width=300,
26
)
27
28
m
29
output
responsive and icon borders
using this as inspiration Injecting CSS and JavaScript into folium
JavaScript
1
57
57
1
import geopandas as gpd
2
import folium
3
import numpy as np
4
5
geo_df = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
6
geo_df["pop"] = np.random.randint(1, 5, len(geo_df))
7
m = None
8
9
m = geo_df.explore(
10
m=m,
11
column="pop",
12
tooltip={"name", "pop"},
13
cmap="summer",
14
style_kwds=dict(
15
style_function=lambda x: {
16
"html": f"""<span class="fa fa-home"
17
style="color:{x["properties"]["__folium_color"]};">
18
</span>"""
19
},
20
),
21
marker_type="marker",
22
marker_kwds=dict(icon=folium.DivIcon(class_name="mapIcon")),
23
name="Residental",
24
height=300,
25
width=500,
26
)
27
28
# inject html into the map html
29
m.get_root().html.add_child(
30
folium.Element(
31
"""
32
<style>
33
.mapIcon {
34
font-size:large;
35
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
36
}
37
</style>
38
<script type="text/javascript">
39
window.onload = function(){
40
var sizeFromZoom = function(z){return z<3 ? "small" : (0.5*z)+"em"}
41
var updateIconSizes = function(){
42
var mapZoom = {mapObj}.getZoom();
43
var txtSize = sizeFromZoom(mapZoom);
44
$(".mapIcon").css("font-size", txtSize);
45
}
46
updateIconSizes();
47
{mapObj}.on("zoomend", updateIconSizes);
48
}
49
</script>
50
""".replace(
51
"{mapObj}", m.get_name()
52
)
53
)
54
)
55
56
m
57