【发布时间】: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 的不透明度,但没有任何效果。
在此先感谢您抽出宝贵的时间,很抱歉发了这么长的帖子。
最好的问候
【问题讨论】: