首页 > 解决方案 > Python:加载设施名称时地图未加载

问题描述

我正在制作一张交互式地图,以绘制全国各地的各种医疗设施。我在网上找到了一个合适的形状/数据库文件(连同 GeoJson 和一个 CSV),可以用于这个项目。

我设法让位置显示在地图上,但设施名称没有出现。

根据我打印的 sf.fields ,名称在字段 8 中,我可以将这些名称打印为record.record[8],它们将出现在控制台中。

但是,当我将弹出窗口设置为popup=record.record[8].title()地图时将不再加载。它在控制台中编译并且不返回错误。我在这里想念什么?

这是我到目前为止所得到的:

import folium
import pandas
import shapefile
import csv

with open('VA_Facilities.csv', 'r') as data_file:
    csv_data = csv.reader(data_file)

myshp = open('data/va_facilities_1.shp', "rb")
mydbf = open('data/va_facilities_1.dbf', "rb")
sf = shapefile.Reader(shp=myshp, dbf=mydbf)
records = sf.shapeRecords()

# Prints the number of records, type of the shape, and fields for the 
dataset
print (len(records))
print (sf.shapes()[0].shapeType)
print (sf.fields)

# Prints the first three records for verification reasons
for record in records[:3]:
    print (record.record[0], record.shape.points[0], record.record[8])


map=folium.Map(location=[47.1164, -101.2996],zoom_start=4,tiles='CartoDB 
positron')
for record in records:
    lat, lng = (record.shape.points[0][1],record.shape.points[0][0])

    folium.RegularPolygonMarker(
    [lat, lng],
    popup=record.record[8].title(),
    fill_color='#EE1C25',
    number_of_sides=5,
    radius=5
    ).add_to(map)


map.save(outfile='Healthcare_Facilities.html')

将值更改record.record[0]回零将照常加载地图,但没有名称。

标签: pythonshapefilefolium

解决方案


弄清楚了。最新版本的 Folium 依赖于原始 HTML 输入,因此parse_html=True必须添加到popup=字段中才能获得所需的结果。

谢谢您的帮助!


推荐阅读