首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助编写能自动编写更多代码的代码吗?

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

Stack Overflow用户
提问于 2016-09-22 18:07:54
回答 1查看 784关注 0票数 0

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

我遇到的问题与GPS数据有关。我下载了一个带有车辆坐标的CVS文件。在我跟踪的一些车辆上,GPS无论出于什么原因都会失去信号,而且在数百英里内也不会恢复正常运行。这会引起问题,当我使用线图来跟踪车辆在地图上的运动。最后,我在城市之间找到了一条长长的直线,因为Folium试图在车辆离线之前连接上一个全球定位系统坐标,而下一个全球定位系统坐标在车辆重新上线后就可用了,这可能是数百英里外的在这里显示。我认为,如果每次脚本在GPS和弦中发现一个缺口,我就可以生成一个新的循环,它基本上会启动一个全新的线条图,并将其附加到现有的地图中。这样,我仍然可以看到地图上的整个车辆路线,但没有试图将断点连接在一起的长队。

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

https://gist.github.com/tapanojum/81460dd89cb079296fee0c48a3d625a7

代码语言:javascript
复制
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)将Lat/Lat转换为十进制

3) Init location1

4)运行while循环来追加和弦。

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

6) Init地点(2,3,.)

)生成新的,而i< len(lat):循环使用位置(2,3,.)

9)在我< len(lat)时重复步骤5-7 (重复次数为abs(j) >= 0.01的实例)

10) Creats (c1,c2,c3,.)= folium.MultiPolyLine(locations=location,color='blue',weight=1.5,opacity=0.5)。

11)为每个c1.add_to、c2、c3.上表

12) mapa.save

任何帮助都将不胜感激!

更新:工作解决方案

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

发布于 2016-09-22 19:22:46

你的时间循环看起来很不稳定。在循环之外,只设置j一次。另外,我想你想要一个线段的列表。你想要这样的东西吗?

代码语言:javascript
复制
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将是一个片段列表,例如;

代码语言:javascript
复制
 [ segment0, segment1, ..... ]

每一段将是一份要点清单,例如;

代码语言:javascript
复制
 [ (lat,lon), (lan,lon), ..... ]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39646016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档