I had a geodataframe of gps points "poyline_gdf":
| geometry |
| -------- |
| POINT (7.30161 52.56024) |
| POINT (7.30007 52.55877) |
| ... |
I wanted to make a linestring from these points and draw them on the map. For visualization, I used folium (found some tutorial). I draw the points by this code:
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)
Then, I found method to make linestrings from these points:
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)
And I got this geodataframe of Linestrings "gdf_line":
| geometry|
| -------- |
| LINESTRING (7.30007 52.55877, 7.29891 52.55521) |
| LINESTRING (7.29891 52.55521, 7.29502 52.55436) |
| ... |
And I could show it on the map:
folium.GeoJson(gdf_line).add_to(m)
m
I got this line on the map: LineStrings on the map
I did some my calculations and added new column to dataframe - "coverage", which contains numeric values between 0 and 1. And now, my dataframe looks like this:
| geometry| coverage|
| -------- | ---------|
| LINESTRING (7.30007 52.55877, 7.29891 52.55521) | 0.86|
| LINESTRING (7.29891 52.55521, 7.29502 52.55436) | 0.32|
|...|...|
I want to make the same visualization of LineStrings as in picture above, but the colors of each LineString should change based on values in "coverage" column. For instance, if values are more than 0.5 closer to 1 (0.5-1), they will change from light blue to dark blue. And, if values are less than 0.5 closer to 0 (0.5-0), they will change from dark red to light red. Any help will be appreciated. If there is a code for another visualization library, that will also work for me.