Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在使用Folium向一张地图添加多个Geojson传单时,如何正确使用样式功能?_Python_Pandas_Geojson_Folium - Fatal编程技术网

Python 在使用Folium向一张地图添加多个Geojson传单时,如何正确使用样式功能?

Python 在使用Folium向一张地图添加多个Geojson传单时,如何正确使用样式功能?,python,pandas,geojson,folium,Python,Pandas,Geojson,Folium,我试图在一张城市地图上添加多个行政区的Geojson传单,根据人口密度进行绘制,但我无法理解style_函数的正确用法 我的代码: colorscale=branca.colormap.linear.YlGnBu_09.scale(vmin=df_BoroughsSP.Density.min(),vmax=df_BoroughsSP.Density.max()) def style_function(feature): density = df_BoroughsSP.Density[i

我试图在一张城市地图上添加多个行政区的Geojson传单,根据人口密度进行绘制,但我无法理解style_函数的正确用法

我的代码:

colorscale=branca.colormap.linear.YlGnBu_09.scale(vmin=df_BoroughsSP.Density.min(),vmax=df_BoroughsSP.Density.max())

def style_function(feature):
    density = df_BoroughsSP.Density[i]
    return {
        'fillOpacity': 0.5,
        'weight': 0.5,
        'fillColor': colorscale(density)
    }

map_SP = folium.Map(location=[latitudeSP, longitudeSP], zoom_start=10)

for i in range(len(df_BoroughsSP.Density)):
 jsonurl='http://polygons.openstreetmap.fr/get_geojson.py?id='+str(df_BoroughsSP.RelationID[i])+'&params=0'
 with urllib.request.urlopen(jsonurl) as url:
    data = json.loads(url.read().decode())
 folium.GeoJson(
    data,
    name='geojson',
    style_function=style_function
 ).add_to(map_SP)

map_SP

我得到的
dict'对象是不可调用的
输出。 我尝试使用预先计算的值列表代替style_函数,但得到了相同的结果

有人知道怎么解决这个问题吗

我知道使用Cloropleth会更容易,但我没有将整个城市的Geojson文件分为几个区


p、 我是编程新手,所以任何类型的建议都会对那些遇到类似问题的人非常有帮助。我发现创建一个复合Geojson,然后使用Chloroleth方法来显示它比尝试在地图上用循环应用单独的补丁要容易得多

我的最终代码:

# df_BoroughsSP is a Pandas dataframe with density and OpenStreetMap RelationID of each city borough


# Preparing a composite Geojson file from files of individual boroughs
features = []

for index in range(len(df_BoroughsSP.index)):
 jsonurl='http://polygons.openstreetmap.fr/get_geojson.py?id='+str(df_BoroughsSP.RelationID[index])+'&params=0'
 with urllib.request.urlopen(jsonurl) as url:
    Geometry=json.loads(url.read().decode())
    features.append(Feature(geometry=Geometry, properties={"name": df_BoroughsSP.Borough[index],"density":df_BoroughsSP.Density[index]}))

feature_collection = FeatureCollection(features)
with open('myfile.geojson', 'w') as f:
   dump(feature_collection, f)
with open('myfile.geojson', 'r') as f:    
   GeodataSP=json.load(f) #Geojson file with city boundaries, broken down to individual boroughs

请提供完整的代码和数据。谢谢