Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:25:41+00:00 2026-06-13T05:25:41+00:00

I am working with an array created from a list of geographical coordinates describing

  • 0

I am working with an array created from a list of geographical coordinates describing a GPS trajectory. The data is like this:

[[-51.203018 -29.996149]
 [-51.203018 -29.99625 ]
 [-51.20266  -29.996229]
 ..., 
 [-51.64315  -29.717896]
 [-51.643112 -29.717737]
 [-51.642937 -29.717709]]

I want to calculate the geographic distances between rows (with the special condition that the first element is always zero, at the starting point). This would give me either a list of distances with len(distances) == coord_array.shape[1], or maybe a third column in the same array.

It is important to note that I have already have a function that returns a distance between two points (two coordinate pairs), but I don’t know how to apply it with a single array operation instead of looping through row pairs.

Currently I am doing the below to calculate segment distances in one new column, and cumulative distances in another new column (latlonarray is already shown above and distance(p1, p2) is already defined):

    dists = [0.0]
    for n in xrange(len(lonlat)-1):
        dists.append(distance(lonlat[n+1], lonlat[n]))

    lonlatarray = numpy.array(lonlat).reshape((-1,2))
    distsarray = numpy.array(dists).reshape((-1,1))
    cumdistsarray = numpy.cumsum(distsarray).reshape((-1,1))

    print numpy.hstack((lonlatarray, distsarray, cumdistsarray))

[[   -51.203018      -29.996149        0.              0.        ]
 [   -51.203018      -29.99625         7.04461338      7.04461338]
 [   -51.20266       -29.996229       39.87928578     46.92389917]
 ..., 
 [   -51.64315       -29.717896       11.11669769  92529.72742791]
 [   -51.643112      -29.717737       11.77016407  92541.49759198]
 [   -51.642937      -29.717709       19.57670066  92561.07429263]]

My main question is: “How could I perform the distance function (which takes a pair of rows as argument) like an array operation instead of a loop?” (that is, how could I properly vectorize it)

Other on-topic questions would be:

  • If I decide to use Pandas, is ther some clever trick to accomplish this?
  • Is there a way to put scipy.spatial.distance to “work for me” using geographic distance (haversine, great-circle distance)?

Also, I would appreciate some tips if I am doing anything unnecessarily complicated.

Thank you all, very much, for your interest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:25:43+00:00Added an answer on June 13, 2026 at 5:25 am

    It sounds like you need to have your original data lonlat represented as a pair of numpy arrays, then pass these arrays to a version of the function distance which accepts arrays.

    For example, looking up the definition of haversine distance, you can fairly easily turn it into a vectorised formula as follows:

    def haversine_pairwise(phi, lam):
        
        dphi = phi[1:]-phi[:-1]
        dlam = lam[1:]-lam[:-1]
        
        # r is assumed to be a known constant
        return r*(0.5*(1-cos(dphi)) + cos(phi[1:])*cos(phi[:-1])*0.5*(1-cos(dlam)))
    

    I’m not familiar with these formulas myself, but hopefully this shows you how you can do it for whichever formula you want. You would then use cumsum as you have already done. The array slicing syntax which I have used is documented here in case it’s not clear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with an array and need some help. I would like to
Im working on a template class which represents a managed Array. E (*data)[]; data
I'm working with two-dimensional array-values that should be inserted into a ArrayList. But this
Im working on the notepad tutorial from the android website. I have created a
Possible Duplicate: Python List vs. Array - when to use? I'm working on a
I have created a dropdown list, which is working well in separate page: however,
I'm trying to create a iterator like this one , for a list of
I have created an app which retrieves the data from a url using xml
I've having trouble with nested while loops. I've created this function to list three
I have the session created and it stores the data, that is working 100%;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.