0

仕事のプロジェクトのコードを書くのに助けが必要です。pandas を使用して Excel ファイルを読み取るスクリプトを作成しました。各行を反復処理し、Excel ファイルの緯度/経度データをマップ (Folium、Open Street Map) に追加する while ループを記述しました。

私が遭遇した問題は、GPS データに関係しています。車両座標を含む CVS ファイルをダウンロードします。私が追跡している一部の車両では、何らかの理由で GPS が信号を失い、何百マイルもオンラインに戻りません。これにより、ライン プロットを使用してマップ上の車両の動きを追跡しているときに問題が発生します。Folium は、車両がオフラインになる前の最後の GPS 座標と、車両がオンラインに戻ったときに利用可能な次の GPS 座標を接続しようとするため、都市を横切る長い直線が得られます。これは、ここに示すように数百マイル離れている可能性があります. スクリプトが GPS 座標のギャップを見つけるたびに、基本的にまったく新しいライン プロットを開始して既存のマップに追加する新しいループを生成できると思います。このようにして、車両ルート全体が地図上に表示されますが、長い線が壊れたポイントを接続しようとすることはありません.

私の考えは、経度データの各反復間の絶対値の差をスクリプトに計算させることです。各ポイントの差が 0.01 より大きい場合、プログラムでループを終了し、新しいループを開始する必要があります。この新しいループには、新しい変数 init が必要です。各車両で GPS がオフライン/オンラインになる回数を予測する方法がないため、新しいループをいくつ作成する必要があるかはわかりません。

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

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) Long/Lat を 10 進度に変換します

3) 初期位置1

4) while ループを実行して座標を追加する

5) abs(j) >= 0.01 の場合、ループを中断します。

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

7) new while i < len(lat): loop using location(2,3,...) を生成

9) i < len(lat) の間、ステップ 5-7 を繰り返します (abs(j) >= 0.01 のインスタンスが存在する回数だけ繰り返します)。

10) ロケーションの変数ごとに (c1, c2, c3,...) = folium.MultiPolyLine(locations=[location], color='blue', 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")
4

1 に答える 1

0

あなたのwhileループは不安定に見えます。ループ外で j を 1 回だけ設定します。また、線分のリストが必要だと思います。このようなものが欲しかったですか。

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), ..... ]
于 2016-09-22T19:22:46.793 に答える