views:

2078

answers:

4

I'm looking for a free library for python that can calculate your direction and your speed from GPS coordinates and maybe can calculate if you are in some boundaries or things like this.

Are there Libraries that you know and that worked well for you?

Edit: Ok it seems I have to specify this a little bit. We are using python on a linux machine and getting the data from the gpsd. So I hope there is no need for a speciel library for only talking to the device. What I'm looking for is python code doing some basic calculations with the data. Such as comparing the last positions and calculate speed and direction.

+3  A: 

I'm not sure I understand your exact requirements, but, depending on your device &c, there seem to be many possible candidates, such as:

If what you mean is that you've already obtained the GPS' unit output and just need to parse it &c, I suspect that one or more of the above examples (which unfortunately I have not tried) will contain well-isolated modules for that task, which, depending on licensing conditions, you could repurpose; this SO question might also help if that's what you're doing.

Alex Martelli
I found the geo_helper script on http://gagravarr.org/code/geo_helper.py and it seems it is a little bit what I'm looking for. I will give it a try
Janusz
Nice script but doesn't seem to do speed. According to http://www.perrygeo.net/wordpress/?p=13 gpsd comes with gps.py which appears to do much of what you require (not the check if you're within boundaries, but depending on boundary format that could be computer separately).
Alex Martelli
A: 

The book "Beginning Python Visualization" includes just such an example - parsing GPS data and inferring speed and location from it. Its source code is available online at http://www.apress.com/

Eli Bendersky
A: 

The GPS sentences that you receive from a GPS device are pretty easy to decode, and this looks like a fun project. I'm not sure what you get from gspd, but if it's these sentences, I actually had to do something like this for school a few weeks ago (but in LabView):

$GPGGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx

hhmmss.ss = UTC of position 
llll.ll = latitude of position
a = N or S
yyyyy.yy = Longitude of position
a = E or W 
x = GPS Quality indicator (0=no fix, 1=GPS fix, 2=Dif. GPS fix) 
xx = number of satellites in use 
x.x = horizontal dilution of precision 
x.x = Antenna altitude above mean-sea-level
M = units of antenna altitude, meters 
x.x = Geoidal separation
M = units of geoidal separation, meters 
x.x = Age of Differential GPS data (seconds) 
xxxx = Differential reference station ID

So you could just do a gpsstring.split(',') and you'll get an array of all elements which you can then parse. To see more about these sentences (there are others I think for speed and direction), click here.

For example, to get the approximate distance between two points you can use the Haversine Formula:

distance=R*2*asin(sqrt((sin((lat1-lat2)/2))**2
               +cos(lat1)*cos(lat2)*(sin((long1-long2)/2))**2))

Where R is the radius of the Earth in the unit of measurement you want to get your result in (for example R=6372km). That actual line is taken from the LabView program I had lying around, but the syntax is pretty similar to Python's (maybe check the division operator, you might want to do "from future import division".

Also, lat1, lat2, long1 and long2 have to be expressed in radians. The format you're getting them in is pretty strange (hhmm.ff, where ff are fractions of minutes, so they go from 0 to 99 instead of 0 to 59 (as seconds)).

The code I used for that is:

h=floor(x/100);
m=floor(x-(h*100));
s=(x-floor(x))*60;
deg=sgn*(h+(m/60)+(s/3600));
rad=deg*pi/180;

Where sign is 1 for North and East, and -1 for South and East. Again, watch out for division.

Checking boundaries is I think the easiest part. If you've already got the positions in radians or degrees, you can just check if latitude is between two lat boundaries, and do the same for longitude.

Write a wrapper around everything, and you've got your GPS library :)

Adrian Mester
+1  A: 

Apparently the python module that comes with gpsd is the best module to go with for us. For a start look here

The gps module coming with the gpsd has some very useful functions. The first one is getting the data from gpsd and transforming those data in a usable data structure. Then the moduls give you access to your speed, and your current heading relative to north. Also included is a function for calculating the distance between two coordinates on the earth taking the spherical nature of earth into account.

The functions that are missing for our special case are:

Calculating the heading between to points. Means I am at point a facing north to which degree do I have to turn to face the point I want to navigate to.

Taking the data of the first function and our current heading to calculate a turn in degrees that we have to do to face a desired point (not a big deal because it is mostly only a subtraction)

The biggest problem for working with this library is that it is mostly a wrapper for the gpsd so if you are programming on a different OS then you gpscode should work on like Windows or MacOS you are not able to run the code or to install the module.

Janusz
For more algorithms about gps in python look at this page: http://www.deanandara.com/Argonaut/Sensors/Gps/GettingData.html
Janusz