Picking up data in pairs in sequential lines

Asked

Viewed 81 times

0

I’m reading a spreadsheet csv with pandas across pd.read_csv().

The spreadsheet contains vehicle location data every moment. For example:

Place 1: Latitude a, Longitude a
Place 2: Latitude b, Longitude b
Place 3: Latitude c, Longitude c
Place 4: Latitude d, Longitude d
Place 5: Latitude e, Longitude e
Place 6: Latitude f, Longitude f

I need to show a distance history by performing the calculation by the Google Maps API via:

Location 1 to 2: xKM
Location 2 to 3: yKM
etc...

I tried using a apply() within a for, but it didn’t work out.

  • 1

    You can put the code you tried to use, and at least a chunk of code where we have your variables with the data, how you are doing to import and use the google api, and so on?

  • 1

    Note that without this information, you are not "taking a doubt" - who would replace would not only make the program for you, inclusive remaking the parts that already work, as well as create sample data to be able to make any test.

1 answer

0

You can use the Haversine’s formula to calculate the distance between two geographical coordinates of your samples:

inserir a descrição da imagem aqui

Here is an (tested) example of how to solve your problem:

from math import radians, cos, sin, asin, sqrt

# Formula de Haversine
def haversine( latA, lonA, latB, lonB ):
    # Raio da Terra em Metros
    r = 6371000

    # Converte coordenadas de graus para radianos
    lon1, lat1, lon2, lat2 = map(radians, [ lonA, latA, lonB, latB ] )

    # Formula de Haversine
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    hav = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    d = 2 * r * asin( sqrt(hav) )

    return d


dist = 0;
lat0 = 0;
lon0 = 0;

with open("path.csv") as arq:

    for i, amostra in enumerate(arq):

        lat,lon = amostra.strip('\n').split(',')
        lat, lon = float(lat), float(lon)

        if( i > 0 ):
            dist = haversine( lat, lon, lat0, lon0 );
            print( "[Amostra %d] Latitude: %f, Longitude: %f, Distancia: %f Metros" % (i, lat, lon, dist ) )
        else:
            print( "[Amostra %d] Latitude: %f, Longitude: %f" % (i, lat, lon ) )

        lat0 = lat;
        lon0 = lon;

CSV input file (path.csv):

-23.55172772,-46.63113769
-23.54896501,-46.64119576
-23.55247579,-46.63805426
-23.54635555,-46.63022746
-23.55371538,-46.63583097
-23.55013902,-46.62514466
-23.54576266,-46.64009231
-23.55461708,-46.64011483
-23.54577566,-46.63642792
-23.54339516,-46.6348212

Coordinates on the Map:

inserir a descrição da imagem aqui

Exit:

[Amostra 0] Latitude: -23.551728, Longitude: -46.631138
[Amostra 1] Latitude: -23.548965, Longitude: -46.641196, Distancia: 1070.287937 Metros
[Amostra 2] Latitude: -23.552476, Longitude: -46.638054, Distancia: 504.915839 Metros
[Amostra 3] Latitude: -23.546356, Longitude: -46.630227, Distancia: 1048.641060 Metros
[Amostra 4] Latitude: -23.553715, Longitude: -46.635831, Distancia: 997.994340 Metros
[Amostra 5] Latitude: -23.550139, Longitude: -46.625145, Distancia: 1159.600298 Metros
[Amostra 6] Latitude: -23.545763, Longitude: -46.640092, Distancia: 1599.514940 Metros
[Amostra 7] Latitude: -23.554617, Longitude: -46.640115, Distancia: 984.569258 Metros
[Amostra 8] Latitude: -23.545776, Longitude: -46.636428, Distancia: 1052.505353 Metros
[Amostra 9] Latitude: -23.543395, Longitude: -46.634821, Distancia: 311.274120 Metros

Browser other questions tagged

You are not signed in. Login or sign up in order to post.