添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
爱看球的蟠桃  ·  linux 海康 sdk ...·  1 月前    · 
闯红灯的柿子  ·  两张图疏通Spring ...·  1 月前    · 
礼貌的炒饭  ·  手机版·  4 月前    · 

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

Hi all,

I can plot markers using GeoJson or Marker . I like using GeoJson since I can plot them all at once without a loop, and also I love GeoJson.Tooltip , which looks great. Please see below for an example.

Unfortunately, I can't figure out how to change the icon, or other properties of the markers when using GeoJson . If I switch to looping and using Marker , I have more control over the markers but the tooltips don't look as good when specifying a list of values (I get a string representation).

Is there a way to change properties of the markers using the GeoJson method?

data={'type': 'FeatureCollection',
 'features': [{'id': '0',
  'type': 'Feature',
  'properties': {'SCHOOL_NAME': 'Ascension Elementary School',
   'SCHOOL_CODE': 'ACSN',
   'ADDRESS': '5205 New Street',
   'CITY': 'Burlington',
   'PROV': 'ON',
   'POST': 'L7L 1V3',
   'COUNTRY': 'Canada',
   'style': {'markerColor': '#00000000', 'color': '#00000000'},
   'highlight': {}},
  'geometry': {'type': 'Point', 'coordinates': [-79.7491672, 43.371468]}},
 {'id': '2',
  'type': 'Feature',
  'properties': {'SCHOOL_NAME': 'Bishop P. F. Reding Secondary School',
   'SCHOOL_CODE': 'BHRD',
   'ADDRESS': '1120 Main Street East',
   'CITY': 'Milton',
   'PROV': 'ON',
   'POST': 'L9T 6H7',
   'COUNTRY': 'Canada',
   'style': {'markerColor': '#00000000', 'color': '#00000000'},
   'highlight': {}},
  'geometry': {'type': 'Point',
   'coordinates': [-79.86306090000001, 43.530966799999995]}}]}
m = folium.Map(location=[43.342094, -79.803047], zoom_start=12)
folium.GeoJson(data, name='Schools', tooltip=folium.features.GeoJsonTooltip(fields=['SCHOOL_NAME', 'ADDRESS'], localize=True)).add_to(m)

Thanks so much for this incredible package.

There's nothing better than a fun job! I appreciate you getting back to me. I will stay tuned for the update.

Take care,

Hey @Alcampopiano, glad you're enjoying the package.

I've got a PR in for this issue that I have been needing to finish up in #957. Just been quite busy working on a lot of really fun stuff at a new job. I'll try to get it wrapped up soon.

@jtbaker any word?

@Damorck @CriticalWill Not exactly what OP is looking for, but here's my workaround until the PR drops.

Load geojson as normal, loop through and make a new Marker for each point feature.

    # this is the layer that eventually gets added to Map
    layer = folium.FeatureGroup(name='Your layer with Markers', show=False)
    # load GEOJSON, but don't add it to anything
    gj = folium.GeoJson('path/to/some/point_layer.geojson')
    # iterate over GEOJSON features, pull out point coordinates, make Markers and add to layer
    for feature in gj.data['features']:
        if feature['geometry']['type'] == 'Point':
            folium.Marker(location=list(reversed(feature['geometry']['coordinates'])),
                          icon=folium.Icon(
                              icon_color='#ff033e',
                              icon='certificate',
                              prefix='fa')
                          ).add_to(layer)
    layer.add_to(m)
          

This is a bit hacky but here's the same code with tooltips added for all attributes. I leave styling up to you.

    # this is the layer that eventually gets added to Map
    layer = folium.FeatureGroup(name='Your layer with Markers', show=False)
    # load GEOJSON, but don't add it to anything
    gj = folium.GeoJson('path/to/some/point_layer.geojson')
    # iterate over GEOJSON features, pull out point coordinates, make Markers and add to layer
    for feature in gj.data['features']:
        if feature['geometry']['type'] == 'Point':
            tool_tip_text = '<pre>\n'
            for p in feature['properties']:
                tool_tip_text = tool_tip_text + str(p) + ': ' + str(feature['properties'][p]) + '\n'
            tool_tip_text = tool_tip_text + '\n</pre>'
            tool_tip = folium.Tooltip(tool_tip_text)
            folium.Marker(location=list(reversed(feature['geometry']['coordinates'])),
                          icon=folium.Icon(
                              icon_color='#ff033e',
                              icon='certificate',
                              prefix='fa'),
                          tooltip=tool_tip
                          ).add_to(layer)
    layer.add_to(m)

Edit: Also note that using this method may substantially increase the size of your output index.html file depending on the size of your data. the GeoJson classes actually use the underlying data, while my method relies on replicating everything.

Hi, I need help here. I tried all solutions above according to my dataset. Unfortunately, I failed to replicate them using MarkerCluster in order to show the tooltip.

This is my code :

alerts_gj = alerts2geojson(alerts_df) #convert the dataframe to geojson
map = folium.Map(location = [alerts_df['latitude'].mean(), alerts_df['longitude'].mean()] ,zoom_start=10)
jam_cluster = plugins.MarkerCluster(control=False)
mc1= plugins.FeatureGroupSubGroup(jam_cluster, name='Jam2')
for f in alerts_gj['features']:
    popup = """
    Location : <b>%s</b><br>
    """ % (f['properties']['city'])
    if f['properties']['type'] == 'JAM' or f['properties']['type'] == 'JAM2':
        folium.Marker(location=f['geometry']['coordinates'],
                            tooltip=popup,
                            icon=folium.Icon(
                              icon_color='#ff033e',
                              icon='certificate',
                              prefix='fa')
                          ).add_to(jam_cluster)
map.add_child(jam_cluster)
map.add_child(mc1)
mc1.add_child(folium.GeoJson(alerts_gj, embed=False))
folium.LayerControl(collapsed=False).add_to(map)

alerts_df is a dataframe.
alerts_gj is a geojson.

Current output (I removed out the street name):

Also, for some reason, I needed to tick and untick Jam2 in order to display the cluster. Any way I can improve this? Perhaps not using FeatureGroupSubGroup ( I assume this was caused of the tick/untick issue)?

EDIT :
alerts2geojson function:

def alerts2geojson(df):
    features = []
    insert_features = lambda X: features.append(
            geojson.Feature(geometry=geojson.Point((X["longitude"],
                                                    X["latitude"])),
                            properties=dict(
                                            time=str(X["startTime"]),
                                            country=X['country'],
                                            type=X['type'],
                                            city=X['city'])))
    df.apply(insert_features, axis=1)
    alerts_gj = geojson.FeatureCollection(features)
    return alerts_gj #features

Thank you.

@mong220394 I haven't used the MarkerCluster class, so I won't comment on that. But it looks like you need to change for f in alerts_gj['features']: to for f in alerts_gj.data['features']:.

You don't provide code for the alerts2geojson function. But alerts_gj must be a folium.GeoJson object.

Also, you may have better luck using folium.Tooltip() class instead of just passing a string.

Also it looks like Markers get added to FeatureGroupSubGroup and not the MarkerCluster. See docs

@mong220394 It looks like your alerts2geojson function is the issue here. You need to read in your source data using the folium.GeoJson class and then my above code samples can be modified for your use case.

As an alternative, you could use the coordinates/attributes in your existing dataframe to create the folium Markers. Creating geojson in the strict sense might be an extra step here if you're just plotting points.

See the GeoPandas examples here for plotting data from a geopandas dataframe. The example pages in general will get you a long way.

Also check out the folium Gitter - help questions might reach a wider audience.

Hey @Alcampopiano, glad you're enjoying the package.

I've got a PR in for this issue that I have been needing to finish up in #957. Just been quite busy working on a lot of really fun stuff at a new job. I'll try to get it wrapped up soon.

Hi Thanks for the branch, it hasn't be merged yet. I've download your branch, but I cannot find a way to modify marker color from geoJson. Could you provide an example with your branch?
Thank you!