2 -- File ID: 1a0fae52-fafb-11dd-b392-000475e441b9
4 -- clname(): Return the name of the closest waypoint in wayp.
5 CREATE OR REPLACE FUNCTION clname(point) RETURNS text -- {{{
13 WHERE ($1 <-> coor) < 0.05
20 -- cldist(): Return the distance (in degrees) to the closest waypoint in wayp.
21 CREATE OR REPLACE FUNCTION cldist(point) RETURNS numeric -- {{{
23 SELECT round(avs::numeric, 5) FROM (
28 WHERE ($1 <-> coor) < 0.05
32 $$ LANGUAGE SQL; -- }}}
34 -- findpos(): Calculate coordinates for a specific point in time that’s between two track points.
35 CREATE OR REPLACE FUNCTION findpos(currtime timestamp) RETURNS point AS $$ -- {{{
46 SELECT INTO firstdate date
50 SELECT INTO lastdate date
54 IF currtime < firstdate OR currtime > lastdate THEN
58 SELECT INTO firsttime date
60 WHERE date <= currtime
63 SELECT INTO firstcoor coor
65 WHERE date <= currtime
68 SELECT INTO lasttime date
70 WHERE date >= currtime
73 SELECT INTO lastcoor coor
75 WHERE date >= currtime
79 IF firsttime = lasttime THEN
83 currlat = firstcoor[0] +
86 lastcoor[0] - firstcoor[0]
90 extract(EPOCH FROM currtime) - extract(EPOCH FROM firsttime)
94 extract(EPOCH FROM lasttime) - extract(EPOCH FROM firsttime)
98 currlon = firstcoor[1] +
101 lastcoor[1] - firstcoor[1]
105 extract(EPOCH FROM currtime) - extract(EPOCH FROM firsttime)
109 extract(EPOCH FROM lasttime) - extract(EPOCH FROM firsttime)
113 RETURN(currlat, currlon);
115 $$ LANGUAGE plpgsql; -- }}}
117 -- wherepos(): Return a string with date, position, closest name and distance to the nearest point.
118 CREATE OR REPLACE FUNCTION wherepos(currtime timestamp) RETURNS text AS $$ -- {{{
123 currlat numeric(9, 6);
124 currlon numeric(9, 6);
126 currpos = findpos(currtime);
127 currlat = currpos[0];
128 currlon = currpos[1];
129 currname = clname(currpos);
130 currdist = cldist(currpos);
131 RETURN(currtime || ' - ' || currlat::text || ' ' || currlon::text || ' - ' || currname || ' - ' || currdist);
133 $$ LANGUAGE plpgsql; -- }}}
135 -- loop_wayp_new(): Loop through all the entries in wayp_new and add them to wayp.
136 CREATE OR REPLACE FUNCTION loop_wayp_new() RETURNS void AS $$ -- {{{
141 UPDATE wayp_new SET coor = point(
142 round(coor[0]::numeric, 6),
143 round(coor[1]::numeric, 6)
146 curr_id = (SELECT id FROM wayp_new ORDER BY id LIMIT 1);
147 IF curr_id IS NOT NULL THEN
148 currpoint = (SELECT coor FROM wayp_new WHERE id = curr_id);
149 IF (SELECT coor FROM wayp WHERE coor[0] = currpoint[0] AND coor[1] = currpoint[1] LIMIT 1) IS NOT NULL THEN
150 RAISE NOTICE '% already exists in wayp', currpoint;
151 INSERT INTO wayp_rej SELECT * FROM wayp_new WHERE id = curr_id;
153 INSERT INTO wayp SELECT * FROM wayp_new WHERE id = curr_id;
155 DELETE FROM wayp_new WHERE id = curr_id;
161 $$ LANGUAGE plpgsql; -- }}}
163 CREATE OR REPLACE FUNCTION numpoints(place varchar) RETURNS integer AS $$ -- {{{
165 RETURN((SELECT count(*) from logg where name = place));
167 $$ LANGUAGE plpgsql; -- }}}
169 -- update_trackpoint(): Update all fields in a fixed radius from the specified coordinate.
170 CREATE OR REPLACE FUNCTION update_trackpoint(currpoint point) RETURNS void AS $$ -- {{{
172 RAISE NOTICE 'starting update_trackpoint(%), %', currpoint, clname(currpoint);
173 UPDATE logg SET name = clname(coor), dist = cldist(coor)
174 WHERE ($1 <-> coor) < 0.05;
175 RAISE NOTICE 'update_trackpoint(%) is finished', currpoint;
180 -- secmidnight(): Return number of seconds since midnight for a specified date.
181 CREATE OR REPLACE FUNCTION secmidnight(timestamp) RETURNS double precision -- {{{
183 SELECT extract(HOUR FROM $1) * 3600 + extract(MINUTE FROM $1) * 60 + extract(SECOND FROM $1);
184 $$ LANGUAGE SQL; -- }}}
186 -- nullable(): Make it possible to use SELECT with NULL string.
187 CREATE OR REPLACE FUNCTION nullable(t text) RETURNS text -- {{{
196 $$ LANGUAGE plpgsql; -- }}}