Calculate distances in a file with latitude and longitude

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculate distances in a file with latitude and longitude

Roberts, Michael-2

Hi List,

 

I would like some help with calculating distances between hospitals based on their lat & long, which I have.  I have a list of about 250 locations and need to be able to get the distances from each hospital to every other hospital.  The data are in a text file and I have SPSS ver. 17.0.3, so I can use the Python extendedtransforms package.  The end result would have to be a matrix of 250 by 250 having distances from every hospital to every other hospital, or something of the sort, I think!

 

Eg:

 

               Hosp1 hosp2 hosp3…

Hosp      x              y              z

Hosp2   a              b             c

Hosp3   l               m            n

 

I used to know how to do loops and vectors but have been exclusively working in SQL and the rust is flaking off terribly J  Hope this makes sense to everyone.

 

Would appreciate any help with this.

 

TIA

Mike

Reply | Threaded
Open this post in threaded view
|

Re: Calculate distances in a file with latitude and longitude

Jon K Peck
Since you have an input table consisting, I presume of data like this,
hosp1, lat, long
hosp2, lat, long
...

the easiest thing to do is to read the text file directly and write a new file in the format
hosp1 dist11 dist12 dist13 ...
hosp2 dist21 dist22 dist23 ...

and then read that as needed.  Of course, the distance from a hospital to itself should be zero.

Here is a tiny Python program to do that.
Note 1: extendedTransforms has both a spherical distance function, sphDist, and an ellipsoidal distance function, ellipseDist.
I used the latter here, which is a little more accurate, but it probably makes little difference unless these hospitals are scattered around the world.
Note 2: the distance function can take input in radians or degrees.  I have assumed radians here.
Note 3: Since the email often mangles indentation, I've written dashes at the start of each indented line to make the indentation structure clear.
========== code starts ============
# read lat/long table and write all distances
import csv
from extendedTransforms import ellipseDist

table = csv.reader(open("c:/users/mcroberts/latlong.csv", "rb"))
output = csv.writer(open("c:/users/mcroberts/latlongout.csv", "wb"))

data = []

for row in table:
----data.append(row)
   
for item in data:
----hosp, lat, long = item[0].split()
----dist = [hosp]
----for item2 in data:
--------hosp2, lat2, long2 = item2[0].split()
--------dist.append(str(ellipseDist(float(lat), float(long), float(lat2), float(long2), inradians=True)))
----output.writerow(dist)
============ code ends =============

   


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        "Roberts, Michael" <[hidden email]>
To:        [hidden email]
Date:        02/20/2012 01:53 PM
Subject:        [SPSSX-L] Calculate distances in a file with latitude and              longitude
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi List,
 
I would like some help with calculating distances between hospitals based on their lat & long, which I have.  I have a list of about 250 locations and need to be able to get the distances from each hospital to every other hospital.  The data are in a text file and I have SPSS ver. 17.0.3, so I can use the Python extendedtransforms package.  The end result would have to be a matrix of 250 by 250 having distances from every hospital to every other hospital, or something of the sort, I think!
 
Eg:
 
               Hosp1 hosp2 hosp3…
Hosp      x              y              z
Hosp2   a              b             c
Hosp3   l               m            n

 
I used to know how to do loops and vectors but have been exclusively working in SQL and the rust is flaking off terribly J  Hope this makes sense to everyone.
 
Would appreciate any help with this.
 
TIA

Mike

Reply | Threaded
Open this post in threaded view
|

Re: Calculate distances in a file with latitude and longitude

Boreak Silk
Alternatively, you can map those XY coordinates by using one of GIS applications (ArcMap or MapInfo) and then
calculate straight lines between hospitals. If you have roam map layer you can calculate road distances between
those hospitals.
 
 
Good luck,
 
Boreak Silk


 


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Jon K Peck
Sent: Tuesday, 21 February 2012 9:27 AM
To: [hidden email]
Subject: Re: Calculate distances in a file with latitude and longitude

Since you have an input table consisting, I presume of data like this,
hosp1, lat, long
hosp2, lat, long
...

the easiest thing to do is to read the text file directly and write a new file in the format
hosp1 dist11 dist12 dist13 ...
hosp2 dist21 dist22 dist23 ...

and then read that as needed.  Of course, the distance from a hospital to itself should be zero.

Here is a tiny Python program to do that.
Note 1: extendedTransforms has both a spherical distance function, sphDist, and an ellipsoidal distance function, ellipseDist.
I used the latter here, which is a little more accurate, but it probably makes little difference unless these hospitals are scattered around the world.
Note 2: the distance function can take input in radians or degrees.  I have assumed radians here.
Note 3: Since the email often mangles indentation, I've written dashes at the start of each indented line to make the indentation structure clear.
========== code starts ============
# read lat/long table and write all distances
import csv
from extendedTransforms import ellipseDist

table = csv.reader(open("c:/users/mcroberts/latlong.csv", "rb"))
output = csv.writer(open("c:/users/mcroberts/latlongout.csv", "wb"))

data = []

for row in table:
----data.append(row)
   
for item in data:
----hosp, lat, long = item[0].split()
----dist = [hosp]
----for item2 in data:
--------hosp2, lat2, long2 = item2[0].split()
--------dist.append(str(ellipseDist(float(lat), float(long), float(lat2), float(long2), inradians=True)))
----output.writerow(dist)
============ code ends =============

   


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        "Roberts, Michael" <[hidden email]>
To:        [hidden email]
Date:        02/20/2012 01:53 PM
Subject:        [SPSSX-L] Calculate distances in a file with latitude and              longitude
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi List,
 
I would like some help with calculating distances between hospitals based on their lat & long, which I have.  I have a list of about 250 locations and need to be able to get the distances from each hospital to every other hospital.  The data are in a text file and I have SPSS ver. 17.0.3, so I can use the Python extendedtransforms package.  The end result would have to be a matrix of 250 by 250 having distances from every hospital to every other hospital, or something of the sort, I think!
 
Eg:
 
               Hosp1 hosp2 hosp3…
Hosp      x              y              z
Hosp2   a              b             c
Hosp3   l               m            n

 
I used to know how to do loops and vectors but have been exclusively working in SQL and the rust is flaking off terribly J  Hope this makes sense to everyone.
 
Would appreciate any help with this.
 
TIA

Mike

This email is intended solely for the named addressee.
If you are not the addressee indicated please delete it immediately.
Reply | Threaded
Open this post in threaded view
|

Re: Calculate distances in a file with latitude and longitude

Roberts, Michael-2

Boreak Silk,

 

Thank you for the suggestion.  Unfortunately I don’t have immediate access to the GIS apps.  We do have access to licenses, but there is a time delay that is not feasible. The solution provided by Jon has worked very well though.  I have version 17.0.3 and after downloading and installing the extendedtransforms.py file, everything fell into place – thanks to Jon!

 

 

Mike

 

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Boreak Silk
Sent: Tuesday, February 21, 2012 6:08 PM
To: [hidden email]
Subject: Re: Calculate distances in a file with latitude and longitude

 

Alternatively, you can map those XY coordinates by using one of GIS applications (ArcMap or MapInfo) and then

calculate straight lines between hospitals. If you have roam map layer you can calculate road distances between

those hospitals.

 

 

Good luck,

 

Boreak Silk


 


From: SPSSX(r) Discussion [[hidden email]] On Behalf Of Jon K Peck
Sent: Tuesday, 21 February 2012 9:27 AM
To: [hidden email]
Subject: Re: Calculate distances in a file with latitude and longitude

Since you have an input table consisting, I presume of data like this,
hosp1, lat, long
hosp2, lat, long
...

the easiest thing to do is to read the text file directly and write a new file in the format
hosp1 dist11 dist12 dist13 ...
hosp2 dist21 dist22 dist23 ...

and then read that as needed.  Of course, the distance from a hospital to itself should be zero.

Here is a tiny Python program to do that.
Note 1: extendedTransforms has both a spherical distance function, sphDist, and an ellipsoidal distance function, ellipseDist.
I used the latter here, which is a little more accurate, but it probably makes little difference unless these hospitals are scattered around the world.
Note 2: the distance function can take input in radians or degrees.  I have assumed radians here.
Note 3: Since the email often mangles indentation, I've written dashes at the start of each indented line to make the indentation structure clear.
========== code starts ============
# read lat/long table and write all distances
import csv
from extendedTransforms import ellipseDist

table = csv.reader(open("c:/users/mcroberts/latlong.csv", "rb"))
output = csv.writer(open("c:/users/mcroberts/latlongout.csv", "wb"))

data = []

for row in table:
----data.append(row)
   
for item in data:
----hosp, lat, long = item[0].split()
----dist = [hosp]
----for item2 in data:
--------hosp2, lat2, long2 = item2[0].split()
--------dist.append(str(ellipseDist(float(lat), float(long), float(lat2), float(long2), inradians=True)))
----output.writerow(dist)
============ code ends =============

   


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        "Roberts, Michael" <[hidden email]>
To:        [hidden email]
Date:        02/20/2012 01:53 PM
Subject:        [SPSSX-L] Calculate distances in a file with latitude and              longitude
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





Hi List,
 
I would like some help with calculating distances between hospitals based on their lat & long, which I have.  I have a list of about 250 locations and need to be able to get the distances from each hospital to every other hospital.  The data are in a text file and I have SPSS ver. 17.0.3, so I can use the Python extendedtransforms package.  The end result would have to be a matrix of 250 by 250 having distances from every hospital to every other hospital, or something of the sort, I think!
 
Eg:
 
               Hosp1 hosp2 hosp3…
Hosp      x              y              z
Hosp2   a              b             c
Hosp3   l               m            n

 
I used to know how to do loops and vectors but have been exclusively working in SQL and the rust is flaking off terribly J  Hope this makes sense to everyone.
 
Would appreciate any help with this.
 
TIA

Mike

This email is intended solely for the named addressee.
If you are not the addressee indicated please delete it immediately.