制图。在Python中使用色标在地图上快速移动速度 [英] Cartography. Vizualizing speed of movement with color scale on map in Python

查看:195
本文介绍了制图。在Python中使用色标在地图上快速移动速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在csv中有3个主要值(经度,纬度和速度)。使用Folium库,我可以使用lon和lat degree映射位置。我当前的代码是:

I have 3 main values (longitude, latitude and speed) in csv. Using Folium library i can map the location with lon and lat degree.my current code is:

import pandas as pd
from geopy.geocoders import Nominatim
import folium
from pandas import *
import numpy as np
from matplotlib import cm
import folium

df = DataFrame({'lon': [9.899193, 9.899236, 9.899292, 9.899331, 9.899372, 9.899411, 9.89946, 9.899518, 9.899393, 9.899409, 9.899456, 9.899498, 9.89952, 9.899558, 9.8996, 9.899625, 9.899646, 9.899659, 9.899678, 9.899707, 9.899745, 9.899778], 'lat': [48.849231, 48.849201, 48.849163, 48.849128, 48.84909, 48.849056, 48.84901, 48.848968, 48.849018, 48.849014, 48.848979, 48.848949, 48.848926, 48.84888, 48.848831, 48.848789,48.848762, 48.848735, 48.848712, 48.848686,48.848655, 48.848632 ], 'speed' : [15, 25, 32, 45, 50, 75, 64, 32, 13, 23, 42, 13, 23, 42, 64, 32, 13, 23, 42, 13, 23, 42]})
ave_lt = sum(df['lat'])/len(df)
ave_lg = sum(df['lon'])/len(df)
points = zip(df['lat'], df['lon'])
points = list(points)
my_map = folium.Map(location=[ave_lt, ave_lg], zoom_start=14) 
folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)
my_map

直到这里它运作良好,并给我这样的输出:
,但现在我还希望将csv的speed属性生成颜色刻度(基于速度值) )在当前地图上。例如,如果速度在0-20之间,则该行的部分为红色;如果速度在20-60之间,则为黄色;如果速度高于60,则该行为绿色。有可能在python中做到吗?有人可以帮我吗?我想得到这样的输出:

till here it works well and gives me output like this: but now i want also put the speed attribute from csv to generate color scale (based on speed values) on current map. for example if the speed is between 0-20 the that part of line is red, if speed is between 20-60 the yellow, if the speed is higher than 60 then the line is green. is it possible to do it in python? Can anybody help me with this?I would like to get output like this :

很抱歉这个问题,但是我是python新手,我真的需要这样做,谢谢!!非常感谢您的帮助!

Im sorry for this question but im new in python and i really need to do this, Thanks in advance!! I'd really appreciate your help!

推荐答案

尝试一下:

首先,编写以下两个函数

First, write the following two functions

def draw_polylines(points, speeds, map):
    colors = [speed_color(x) for x in speeds]
    n = len(colors)
    # Need to have a corresponding color for each point
    if n != len(points):
        raise ValueError
    i = 0
    j = 1
    curr = colors[0]
    while i < n and j < n:
        if colors[i] != colors[j]:
            line = folium.PolyLine(points[i:j], color=curr, weight=2.5, opacity=1)
            line.add_to(map)
            curr = colors[j]
            i = j
        j += 1
    if i < j:
        folium.PolyLine(points[i:j], color=curr, weight=2.5, opacity=1).add_to(map)


def speed_color(speed):
    if speed < 0:
        raise ValueError
    elif speed >= 0 and speed < 20:
        return 'red'
    elif speed >= 20 and speed < 60:
        return 'yellow'
    else:
        return 'green'

然后,在该行之后

my_map = folium.Map(location=[ave_lt, ave_lg], zoom_start=14) 

拨打电话

draw_polylines(points, df['speed'], my_map)

根据您给定的数据,它似乎可以工作。

I tried it on your given data and it seemed to work. Definitely review it for yourself.

本质上,线段颜色是逐点确定的,每个点的速度都相应。如果每个点都没有速度,或者速度为负(编写更广泛的错误测试;数据通常不干净!),则此代码将引发错误。

Essentially, the line segment colors are decided point by point, using the corresponding speed for each one. If there isn't a speed for every point, or there is a negative speed (write more extensive error testing; data is usually not clean!) this code will throw an error.

这篇关于制图。在Python中使用色标在地图上快速移动速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆