在密度图上叠加Shapefile数据点

2024-04-20 04:58:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉python中的shapefile和映射,所以我希望能得到一些帮助,将shapefile中的数据点叠加到密度图上。你知道吗

老实说,我是一个初学者与地图和阅读的形状文件,所以我到目前为止没有多少。你知道吗

我已经开始使用pyshp,但如果有更好的软件包可以做到这一点,那么我会喜欢任何反馈。你知道吗

以下代码用于创建洛杉矶地区的底图:

def get_base_map(rides_clean):
    return folium.Map(locations=[rides_clean.start_lat.mean(),                                     
                                 rides_clean.start_lon.mean()],
                      zoom_start = 20, tiles = 'cartodbpositron')

以下代码用于创建密度/热量贴图:

from folium import plugins
stationArr = rides_clean[['start_lat', 'start_lon']][:40000].as_matrix()
get_base_map(rides_clean).add_child(plugins.HeatMap(stationArr, 
                                    radius=40, max_val=300))

以下代码与热图相同,但添加了路线:

(draw_route_lines(get_base_map(rides_clean), 
        routedf_vol)).add_child(plugins.HeatMap(stationArr, radius=40, 
        max_val=300))

enter image description here

我想看到数据点从shapefile上显示为标记顶部的密度图。你知道吗


Tags: 数据代码cleanmapbasegetpluginsmean
1条回答
网友
1楼 · 发布于 2024-04-20 04:58:28

用pyshp可以做到这一点。我只使用Matplotlib在地图上绘制shapefile点,但是这个方法将创建两个数组,它们将是要绘制的每个点的x和y坐标。如果shapefile中有多个形状,则使用第一个代码段;如果只有一个形状,则可以使用第二个代码段。你知道吗

import shapefile
import numpy as np
sf = shapefile.Reader('/path/to/shapefile')
point_list = []

for shape in sf:
    temp = shape.points()
    point_list.append(temp)

point_list = np.array(point_list)
x = point_list[:,0]
y = point_list[:,1]

对于只有一个形状的shapefile:

import shapefile
import numpy as np
sf = shapefile.Reader('/path/to/shapefile')

point_list = np.array(sf.shape(0).points)
x = point_list[:,0]
y = point_list[:,1]

您可以使用sf.shapes()判断shapefile中有多少个形状,它将打印一个详细列出所有不同形状的列表。从你的问题看来,你是想把它绘制成点上的标记,而不是线,抱歉,如果不是这样的情况。你知道吗

相关问题 更多 >