Which gps library would you recommend for python?

前端 未结 6 1316
不思量自难忘°
不思量自难忘° 2021-02-01 11:05

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

6条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 11:19

    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 :)

提交回复
热议问题