【问题标题】:folium:: make GeoJson search marker transparentfolium:: 使 GeoJson 搜索标记透明
【发布时间】:2019-05-01 18:47:15
【问题描述】:

我有一个基本的 folium 热图,将位置显示为 CircleMarker,顶部有一个 HeatMap 图层,如下所示。

我想在我的地图中添加搜索功能,所以我将我的 pandas 数据框转换为 GeoJson 格式,以便我可以传递它。

类 folium.plugins.Search(layer, search_label=None, search_zoom=None, geom_type='Point', position='topleft', placeholder='Search', collapsed=False, **kwargs) 基础: branca.element.MacroElement

为您的地图添加搜索工具。

参数:layer(GeoJson, TopoJson, FeatureGroup, MarkerCluster 类对象。)–要在其中索引的地图层

我能够使用以下代码将我的 Pandas DataFrame 转换为 GeoJson。

df_json = pd.read_csv("C:\\py\\folium\\NE Task 1\\json.csv").dropna(how="any")

# convert lat-long to floats and change address from ALL CAPS to Regular Capitalization
df_json['latitude'] = df_json['latitude'].astype(float)
df_json['longitude'] = df_json['longitude'].astype(float)
df_json['Site Name'] = df_json['Site Name'].str.title()


# we don't need all those columns - only keep useful ones
useful_cols = ['Site ID', 'Site Name', 'latitude', 'longitude']
df_subset = df_json[useful_cols]

# drop any rows that lack lat/long data
df_geo = df_subset.dropna(subset=['latitude', 'longitude'], axis=0, inplace=False)


def df_to_geojson(df_json, properties, lat='latitude', lon='longitude'):

    geojson = {'type': 'FeatureCollection', 'features': []}

    # loop through each row in the dataframe and convert each row to geojson format
    for _, row in df_json.iterrows():
        # create a feature template to fill in
        feature = {'type': 'Feature',
                   'properties': {},
                   'geometry': {'type': 'Point', 'coordinates': []}}


        # fill in the coordinates
        feature['geometry']['coordinates'] = [row[lon], row[lat]]

        # for each column, get the value and add it as a new feature property
        for prop in properties:
            feature['properties'][prop] = row[prop]

        # add this feature (aka, converted dataframe row) to the list of features inside our dict
        geojson['features'].append(feature)

    return geojson


geojson_dict = df_to_geojson(df_geo, properties=useful_cols)
geojson_str = json.dumps(geojson_dict, indent=2)


folium.plugins.Search(data=geojson_dict, geom_type='Point',
                      search_zoom=14, search_label='Site ID').add_to(map)

完成后,搜索功能按我的意愿正常工作,但顶部显示了一个标记,我无法像下面那样隐藏。

请帮助我指导如何隐藏此标记并保持 GeoJson 完整,以便我可以将其用于搜索功能。我试图使其透明,通过我在 stackOverflow 上找到的解决方案更改 GeoJson 的不透明度,但没有任何效果。

在此先感谢您抽出宝贵的时间,很抱歉发了这么长的帖子。

最好的问候

【问题讨论】:

    标签: python geojson folium


    【解决方案1】:

    一般来说,

    如果您将folium.LayerControl().add_to(map) 添加到地图,则它提供了显示或隐藏您的 Geojson 标记的功能。然后您可以使用show=False 或地图右上角的“图层”图标隐藏或显示标记(见下图)

    例如:

    # creating folium GeoJson objects from out GeoDataFrames
    pointgeo = folium.GeoJson(gdf,name='group on map', show=False,
                          tooltip=folium.GeoJsonTooltip(fields=['Name', 'Relation', 'City'], aliases=['Name','Relation', 'City'],
                            localize=True)).add_to(map)
    
    # To Add a LayerControl add below line
    folium.LayerControl().add_to(map)
    
    map
    

    就像在这些图像中一样,通过从右上角的“层”控制层来显示或隐藏标记 - 1) 带有标记 2) 隐藏标记

    您可以参考此链接以获取更多示例: Folium_search

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 2020-02-14
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多