需要帮助编写能够自动编写更多代码的代码吗?

时间:2016-09-22 18:07:55

标签: python automation openstreetmap folium

我需要帮助编写工作项目的代码。我编写了一个使用pandas来读取excel文件的脚本。我编写了一个while循环来迭代每一行,并将excel文件中的纬度/经度数据附加到地图上(Folium,Open Street Map)

我遇到的问题与GPS数据有关。我下载了带有车辆坐标的CVS文件。在我跟踪的一些车辆上,GPS因任何原因都会丢失信号,并且不会在数百英里的情况下重新上线。当我使用线图跟踪地图上的车辆移动时,这会导致问题。因为Folium试图在车辆下线之前连接最后一个GPS坐标,所以我最终得到了很长的直线穿越城市,一旦车辆重新上线,下一个GPS坐标可用,这可能是数百英里之外{{3 }}。我想如果每次脚本在GPS坐标中找到间隙,我都会生成一个新的循环,基本上会启动一个全新的线图并将其附加到现有的地图上。这样我仍然可以在地图上看到整个车辆路线,但没有长线试图将断点连接在一起。

我的想法是让我的脚本计算每次经度数据迭代之间的绝对值差异。如果每个点之间的差异大于0.01,我希望我的程序结束循环并开始一个新的循环。然后,这个新循环需要有新的变量init。我不知道需要创建多少个新循环,因为无法预测GPS在每辆车中离线/上线的次数。

shown here

import folium
import pandas as pd

#  Pulls CSV file from this location and adds headers to the columns
df = pd.read_csv('Example.CSV',names=['Longitude', 'Latitude',])

lat = (df.Latitude / 10 ** 7)  # Converting Lat/Lon into decimal degrees
lon = (df.Longitude / 10 ** 7)

zoom_start = 17  # Zoom level and starting location when map is opened
mapa = folium.Map(location=[lat[1], lon[1]], zoom_start=zoom_start)

i = 0
j = (lat[i] - lat[i - 1])
location = []
while i < len(lat):
if abs(j) < 0.01:
    location.append((lat[i], lon[i]))
    i += 1
else:
    break

# This section is where additional loops would ideally be generated

# Line plot settings
c1 = folium.MultiPolyLine(locations=[location], color='blue', weight=1.5,         opacity=0.5)
c1.add_to(mapa)

mapa.save(outfile="Example.html")

这是我想要实现这个目标的伪代码。

1)Python读取csv

2)将长/纬度转换为十进制度

3)初始位置1

4)在循环中运行以附加坐标

5)如果abs(j)> = 0.01,则断开循环

6)初始位置(2,3,...)

7)生成新的,而i&lt; len(lat):使用location(2,3,...)循环

9)重复步骤5-7而i <1。 len(lat)(重复次数 abs(j)的实例> = 0.01))

10)每个变量的Creats(c1,c2,c3,...)= folium.MultiPolyLine(locations = [location],color =&#39; blue&#39;,weight = 1.5,opacity = 0.5)位置

11)为上面列出的每个c1,c2,c3 ......创建c1.add_to(mapa)

12)mapa.save

非常感谢任何帮助!

更新 工作解决方案

import folium
import pandas as pd

#  Pulls CSV file from this location and adds headers to the columns
df = pd.read_csv(EXAMPLE.CSV',names=['Longitude', 'Latitude'])

lat = (df.Latitude / 10 ** 7)  # Converting Lat/Lon into decimal degrees
lon = (df.Longitude / 10 ** 7)

zoom_start = 17  # Zoom level and starting location when map is opened
mapa = folium.Map(location=[lat[1], lon[1]], zoom_start=zoom_start)

i = 1
location = []
while i < (len(lat)-1):
location.append((lat[i], lon[i]))
i += 1
j = (lat[i] - lat[i - 1])
 if abs(j) > 0.01:
    c1 = folium.MultiPolyLine(locations=[location], color='blue',    weight=1.5, opacity=0.5)
    c1.add_to(mapa)
    location = []

mapa.save(outfile="Example.html")

1 个答案:

答案 0 :(得分:0)

你的while循环看起来很糟糕。您只在循环外设置一次j。另外,我想你想要一个线段列表。你想要这样的东西吗?

i = 0
segment = 0
locations = []
while i < len(lat):
    locations[segment] = []  # start a new segment

    # add points to the current segment until all are 
    # consumed or a disconnect is detected
    while i < len(lat):
        locations[segment].append((lat[i], lon[i]))
        i += 1
        j = (lat[i] - lat[i - 1])
        if abs(j) > 0.01:
            break
    segment += 1

完成此操作后,locations将是一个段列表,例如;

 [ segment0, segment1, ..... ]

每个段都是一个点列表,例如;

 [ (lat,lon), (lan,lon), ..... ]