2 -- File ID: 1a0fae52-fafb-11dd-b392-000475e441b9
4 -- clname(): Returnerer navnet på det nærmeste veipunktet i wayp.
5 CREATE OR REPLACE FUNCTION clname(point) RETURNS text -- {{{
13 WHERE ($1 <-> coor) < 0.05
20 -- cldist(): Returnerer avstanden (i grader) til det nærmeste veipunktet i 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(): Beregn koordinater for et tidspunkt som ligger mellom to trackpunkter.
35 CREATE OR REPLACE FUNCTION findpos(currtime timestamptz) RETURNS point AS $$ -- {{{
37 firstdate timestamptz;
39 firsttime timestamptz;
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(): Returnerer en streng med dato, posisjon, nærmeste navn og avstand til nærmeste punkt.
118 CREATE OR REPLACE FUNCTION wherepos(currtime timestamptz) 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 gjennom alle entryene i wayp_new og legg dem inn i systemet.
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 RAISE NOTICE 'curr_id er ikke null: %', curr_id;
149 currpoint = (SELECT coor FROM wayp_new WHERE id = curr_id);
150 IF (SELECT coor FROM wayp WHERE coor[0] = currpoint[0] AND coor[1] = currpoint[1] LIMIT 1) IS NOT NULL THEN
151 RAISE NOTICE '% finnes allerede i wayp', currpoint;
152 INSERT INTO wayp_rej SELECT * FROM wayp_new WHERE id = curr_id;
154 RAISE NOTICE '% er ikke i wayp', currpoint;
155 INSERT INTO wayp SELECT * FROM wayp_new WHERE id = curr_id;
157 DELETE FROM wayp_new WHERE id = curr_id;
159 RAISE NOTICE 'curr_id er null';
164 $$ LANGUAGE plpgsql; -- }}}
166 CREATE OR REPLACE FUNCTION numpoints(place varchar) RETURNS integer AS $$ -- {{{
168 RETURN((SELECT count(*) from logg where name = place));
170 $$ LANGUAGE plpgsql; -- }}}
172 -- update_trackpoint(): Oppdater alle feltene i en viss omkrets av punktet som spesifiseres.
173 CREATE OR REPLACE FUNCTION update_trackpoint(currpoint point) RETURNS void AS $$ -- {{{
175 RAISE NOTICE 'starter update_trackpoint(%), %', currpoint, clname(currpoint);
176 UPDATE logg SET name = clname(coor), dist = cldist(coor)
177 WHERE ($1 <-> coor) < 0.05;
178 RAISE NOTICE 'update_trackpoint(%) er ferdig', currpoint;
183 -- secmidnight(): Returnerer antall sekunder sia midnatt for en dato.
184 CREATE OR REPLACE FUNCTION secmidnight(timestamptz) RETURNS double precision -- {{{
186 SELECT extract(HOUR FROM $1) * 3600 + extract(MINUTE FROM $1) * 60 + extract(SECOND FROM $1);
187 $$ LANGUAGE SQL; -- }}}