Mapping markers for charter schools across the United States has worked. I'm now trying to apply a cluster method to this. The code below runs without an error. When I add a cell and run 'mappo' to see if the cluster effect has worked. I get the error mentioned in the last part of this post.
#MAIN METHOD THAT RETURNS CHOROPLETH MAP WITH POINTS
charterdropped.crs = sdshape.crs
charterdropped = charterdropped.to_crs(epsg='4326')
cjson = charterdropped.to_json()
mappo = folium.Map([37.7556, -122.4399], zoom_start = 7)
mappo.choropleth(
geo_data=districtchanged,
data=districtchanged,
columns=['FIPS', '% Total Population: White Alone'],
key_on='feature.properties.FIPS',
legend_name='White people',
fill_color='YlGn',
fill_opacity=0.4,
highlight=True)
points = folium.features.GeoJson(cjson)
mappo.add_child(points)
#Cluster Method
marker_cluster = MarkerCluster().add_to(mappo)
for each in charterdropped[0:len(charterdropped)].iterrows():
folium.Marker(
location=[each[1]['latitude'], each[1]['longitude']],
popup=folium.Popup(
folium.IFrame(
html=each[1]['SCH_NAME'])
).add_to(marker_cluster))
assert isinstance(figure, Figure), ('You cannot render this Element ' 'if it is not in a Figure.')
Can you reduce it to an SSCCE? Without the data I cannot reproduce your error. The creation of the marker_cluster looks OK at a first glance.
Seems you misplaced a parenthesis. You're adding the popup to the markercluster instead of the marker.
# Wrong
folium.Marker(
location=[each[1]['latitude'], each[1]['longitude']],
popup=folium.Popup(
folium.IFrame(
html=each[1]['SCH_NAME'])
).add_to(marker_cluster))
# Right
folium.Marker(
location=[each[1]['latitude'], each[1]['longitude']],
popup=folium.Popup(
folium.IFrame(
html=each[1]['SCH_NAME'])
)).add_to(marker_cluster)
Easier to see if you line out the parentheses:
# Wrong:
folium.Marker(
location=[each[1]['latitude'], each[1]['longitude']],
popup=folium.Popup(
folium.IFrame(html=each[1]['SCH_NAME'])
).add_to(marker_cluster)
)
# Right:
folium.Marker(
location=[each[1]['latitude'], each[1]['longitude']],
popup=folium.Popup(
folium.IFrame(html=each[1]['SCH_NAME'])
)
).add_to(marker_cluster)
Did that solve your problem?
@Conengmo
Adding the parentheses returned a blank map of U.S. with no markers.
charterdropped.crs = sdshape.crs
charterdropped = charterdropped.to_crs(epsg='4326')
cjson = charterdropped.to_json() #contains coordinates in correct format
mapclus = folium.Map([37.7556, -122.4399], zoom_start = 7)
marker_cluster = MarkerCluster().add_to(mapclus)
for each in charterdropped[0:len(charterdropped)].iterrows():
folium.Marker(
location=[each[1]['latitude'], each[1]['longitude']],
popup=folium.Popup(
folium.IFrame(html=each[1]['SCH_NAME'])
)
).add_to(marker_cluster)
mapclus.save('clustercharterschoolswithdistricts.html')
Method below returned the map with markers but the cluster method still is not reflected. There is something I am not linking correctly.
charterdropped.crs = sdshape.crs
charterdropped = charterdropped.to_crs(epsg='4326')
cjson = charterdropped.to_json()
mapclus = folium.Map([37.7556, -122.4399], zoom_start = 7)
locations = list(zip(charterdropped.latitude, charterdropped.longitude))
icons = [folium.Icon(icon="ok-sign", prefix="fa") for _ in range(len(locations))]
cluster = MarkerCluster(locations=locations, icons=icons)
mapclus.add_child(cluster)
mapclus.save('clustercharterschoolswithdistricts.html')
If your map stays blank there probably is an issue happening in the browser, that wasn't detected in Python.
You can use your browser developer tools to check the console output. If there is an error in the Javascript it will be displayed there.
For example, I'm on Firefox so I press F12 to open the developer tools and then on the 'Console' tab I can see Javascript logs.
Thanks for your suggestions @Conengmo. I figured out a solution. Something that was important for me to do was switch the ordering of latitude and longitude. (x,y) --> (y,x). I believe perhaps maybe they were mislabeled in data handling.
locations = charterdroppedlol[['longitude','latitude']]
locationlist = locations.values.tolist()
locationlist
mapclus = folium.Map(location=[37.7556, -122.4399], zoom_start=7)
marker_cluster = MarkerCluster().add_to(mapclus)
for point in range(0, len(locationlist)):
folium.Marker(locationlist[point], popup="hi").add_to(marker_cluster)
mapclus.save('clustercalifornia.html')
Good to hear your problem is solved!
Most helpful comment
Good to hear your problem is solved!