Difference between revisions of "Useful Queries"

From ecology
Jump to: navigation, search
(How to separate birds or tag deployments when querying data where the same GPS logger has been used more than once or the same individual has been tracked with more than on logger)
(How to separate birds or tag deployments when querying data where the same GPS logger has been used more than once or the same individual has been tracked with more than one logger)
Line 104: Line 104:
  
 
== How to separate birds or tag deployments when querying data where the same GPS logger has been used more than once or the same individual has been tracked with more than one logger ==
 
== How to separate birds or tag deployments when querying data where the same GPS logger has been used more than once or the same individual has been tracked with more than one logger ==
NOTE: The current query does not work yet and can only be implemented in a testing environment - we expect to have this ready by December 2014
+
NOTE: The current query does not work yet and can only be implemented in a testing environment - we expect to have this ready by January 2015
  
 
In this query you will select the tracking data related to each deployment of a GPS logger. This query is slightly more complex than the query above. If you have used the same logger on more than one individual bird, or have tracked the same individual with more than one logger, this query will enable you to make that distinction, by showing both GPS logger id (device_info_serial) and a unique identifier for your individual (eg ring_number or colour_ring). Furthermore, as above, this will only select tracking data from the start and end of a particular track session.
 
In this query you will select the tracking data related to each deployment of a GPS logger. This query is slightly more complex than the query above. If you have used the same logger on more than one individual bird, or have tracked the same individual with more than one logger, this query will enable you to make that distinction, by showing both GPS logger id (device_info_serial) and a unique identifier for your individual (eg ring_number or colour_ring). Furthermore, as above, this will only select tracking data from the start and end of a particular track session.
  
 
   SELECT t.device_info_serial, t.date_time, t.latitude, t.longitude, t.altitude, t.speed, i.colour_ring, s.track_session_id, s.key_name
 
   SELECT t.device_info_serial, t.date_time, t.latitude, t.longitude, t.altitude, t.speed, i.colour_ring, s.track_session_id, s.key_name
   FROM webapp.ee_track_session_limited s
+
   FROM gps.ee_track_session_limited s
   JOIN webapp.ee_individual_limited i
+
   JOIN gps.ee_individual_limited i
 
     ON s.individual_id = i.individual_id
 
     ON s.individual_id = i.individual_id
   JOIN webapp.ee_tracking_speed_limited t
+
   JOIN gps.ee_tracking_speed_limited t
 
     ON t.device_info_serial = s.device_info_serial
 
     ON t.device_info_serial = s.device_info_serial
 
   AND t.date_time BETWEEN s.start_date AND s.end_date
 
   AND t.date_time BETWEEN s.start_date AND s.end_date

Revision as of 11:23, 9 January 2015

UvAGPS functions

get_uvagps_track_speed

Compute distance (meters), time-interval (time stamp hh:mm:ss), speed (meters/second) and heading (degrees) for a series of subsequent tracking entries for a specific device_info_serial. For the first entry in the sequence these values will always be missing because they are computed as the difference between subsequent rows.

Usage examples :

select * from gps.get_uvagps_track_speed(119, '2008-01-01 00:00:00', '2010-01-01 00:00:00');

select * from gps.get_uvagps_track_speed(119);

select t.*, a.distance, a.interval, a.speed, a.direction 
from gps.get_uvagps_track_speed(119, '2008-01-01 00:00:00', '2010-01-01 00:00:00') a 
join gps.uva_tracking t using(device_info_serial, date_time);

Distances and heading are calculated with spherical correction with the Haversine formula as described here. The heading is the initial heading. PostGIS does not calculate the heading, so therefore we don't use PostGIS.

In these functions the records that have been flagged as invalid by setting the userflag to 1 are not taken into account and discarded. If speed calculations are required for all records an additional boolean argument get raw data can be given. i.e.

select * from gps.get_uvagps_track_speed(119, '2008-01-01 00:00:00', '2010-01-01 00:00:00', true);

select * from gps.get_uvagps_track_speed(119, true);

get_uvagps_track_distance

Compute distance (meters) for UvAGPS tracks to arbitrary point in lat/long or geometry location

Example usage :

select * from gps.get_uvagps_track_distance(119, 52.1897017, 6.1996606); 

select * from gps.get_uvagps_track_distance(119, (select location from gps.uva_tracking where device_info_serial = 119 and date_time = '2009-05-07 19:55:08')); 

Here the distance is calculated with the PostGIS distance_sphere function which uses spherical correction.

get_uvagps_track_distance_direction

Compute distance (meters) and direction (degrees) for UvAGPS tracks from arbitrary point in lat/long or geometry location

Example usage:

select * from gps.get_uvagps_track_distance_direction(119, 52.1897017, 6.1996606); 

Distances and direction are calculated with spherical correction with the Haversine formula as described here. The direction is the direction from the given point.

In this function the records where the userflag has been set are discarded. To see these records a additional boolean argument for show raw data should be added like :

select * from gps.get_uvagps_track_distance_direction(119, 52.1897017, 6.1996606, true); 

Calculating summary statistics

Total distance

A query to determine the total distance a bird has flown is :

select sum(distance)/1000 as total_distance
from gps.get_uvagps_track_speed(<ID>);

or for a fixed time period :

select sum(distance)/1000 as total_distance
from gps.get_uvagps_track_speed(<ID>, '2010-01-01 00:00:00', '2011-01-01 00:00:00');

Total number of measurements per tag

Count the total number of measurements for each device using the GROUP BY clause:

select device_info_serial, count(*) 
from gps.ee_tracking_speed_limited
group by device_info_serial
ORDER BY device_info_serial ASC; 


If you want to count the number of measurements with valid GPS fixes you can add a where clause :

select device_info_serial, count(*) 
from gps.ee_tracking_speed_limited
where latitude is not null 
group by device_info_serial
ORDER BY device_info_serial ASC;

Select tracking data from the beginning of a track session

Select only those points that were collected during a valid track session (i.e. data that begins from the start_date of a track session) including 2D GPS speed. Filter our points where no GPS fix is returned (latitude is not null) and where a user has flagged the data as inappropriate (user_flag <> 1):

SELECT t.device_info_serial, t.date_time, t.latitude, t.longitude, t.altitude, t.temperature, t.speed, t.userflag
FROM gps.ee_tracking_speed_limited t, gps.ee_track_session_limited s
WHERE t.device_info_serial = s.device_info_serial AND t.date_time >= s.start_date AND t.device_info_serial = <ID>
  AND t.latitude IS  NOT NULL AND t.userflag <> 1

How to separate birds or tag deployments when querying data where the same GPS logger has been used more than once or the same individual has been tracked with more than one logger

NOTE: The current query does not work yet and can only be implemented in a testing environment - we expect to have this ready by January 2015

In this query you will select the tracking data related to each deployment of a GPS logger. This query is slightly more complex than the query above. If you have used the same logger on more than one individual bird, or have tracked the same individual with more than one logger, this query will enable you to make that distinction, by showing both GPS logger id (device_info_serial) and a unique identifier for your individual (eg ring_number or colour_ring). Furthermore, as above, this will only select tracking data from the start and end of a particular track session.

 SELECT t.device_info_serial, t.date_time, t.latitude, t.longitude, t.altitude, t.speed, i.colour_ring, s.track_session_id, s.key_name
 FROM gps.ee_track_session_limited s
 JOIN gps.ee_individual_limited i
   ON s.individual_id = i.individual_id
 JOIN gps.ee_tracking_speed_limited t
   ON t.device_info_serial = s.device_info_serial
 AND t.date_time BETWEEN s.start_date AND s.end_date
 WHERE s.key_name = '<Project name>' AND t.userflag = 0
 ORDER BY t.device_info_serial, t.date_time

HINT: WHERE s.key_name = 'HG_TEXEL'