【发布时间】:2021-12-22 07:03:57
【问题描述】:
我有一个 GPS 点“poyline_gdf”的地理数据框:
| geometry |
| -------- |
| POINT (7.30161 52.56024) |
| POINT (7.30007 52.55877) |
| ... |
我想从这些点制作一个线串并将它们绘制在地图上。对于可视化,我使用了 folium(找到了一些教程)。我用这段代码画了点:
def add_markers(mapobj, gdf):
coords = []
for i, row in gdf.iterrows():
coords.append([row.geometry.y, row.geometry.x])
for coord in coords:
folium.CircleMarker(location = coord,
radius = 2.5,
fill = True,
fill_color = '#F50057',
fill_opacity = 0.75,
color = 'whitesmoke',
weight = 0.5).add_to(mapobj)
return mapobj
f = folium.Figure(height = 400)
m = folium.Map([52.303333,8.02], zoom_start = 15, tiles='Cartodb dark_matter')
m.add_to(f)
add_markers(m, poyline_gdf)
然后,我找到了从这些点制作线串的方法:
def make_lines(gdf, df_out, i, geometry = 'geometry'):
geom0 = gdf.loc[i][geometry]
geom1 = gdf.loc[i + 1][geometry]
start, end = [(geom0.x, geom0.y), (geom1.x, geom1.y)]
line = LineString([start, end])
# Create a DataFrame to hold record
data = {'id': i,
'geometry': [line]}
df_line = pd.DataFrame(data, columns = ['id', 'geometry'])
# Add record DataFrame of compiled records
df_out = pd.concat([df_out, df_line])
return df_out
df = pd.DataFrame(columns = ['id', 'geometry'])
# Loop through each row of the input point GeoDataFrame
x = 1
while x < len(polyline_gdf) - 1:
df = make_lines(polyline_gdf, df, x)
x = x + 1
crs = {'init': 'epsg:4326'}
gdf_line = GeoDataFrame(df, crs=crs)
gdf_line = gdf_line.reset_index().drop(['index','id'],axis=1)
我得到了 Linestrings "gdf_line" 的这个地理数据框:
| geometry|
| -------- |
| LINESTRING (7.30007 52.55877, 7.29891 52.55521) |
| LINESTRING (7.29891 52.55521, 7.29502 52.55436) |
| ... |
我可以在地图上显示它:
folium.GeoJson(gdf_line).add_to(m)
m
我在地图上看到了这条线: LineStrings on the map
我做了一些计算并向数据框添加了新列 - “coverage”,其中包含 0 到 1 之间的数值。现在,我的数据框如下所示:
| geometry| coverage|
| -------- | ---------|
| LINESTRING (7.30007 52.55877, 7.29891 52.55521) | 0.86|
| LINESTRING (7.29891 52.55521, 7.29502 52.55436) | 0.32|
|...|...|
我想对 LineStrings 进行与上图相同的可视化,但每个 LineString 的颜色应根据“coverage”列中的值而变化。例如,如果值超过 0.5 接近 1 (0.5-1),它们将从浅蓝色变为深蓝色。并且,如果值小于 0.5 接近 0 (0.5-0),它们将从深红色变为浅红色。任何帮助将不胜感激。如果有另一个可视化库的代码,那也适用于我。
【问题讨论】: