3 -- clname(): Returnerer navnet på det nærmeste veipunktet i wayp.
4 CREATE OR REPLACE FUNCTION clname(point) RETURNS text -- {{{
12 WHERE ($1 <-> coor) < 0.05
19 -- cldist(): Returnerer avstanden (i grader) til det nærmeste veipunktet i wayp.
20 CREATE OR REPLACE FUNCTION cldist(point) RETURNS numeric -- {{{
22 SELECT round(avs::numeric, 5) FROM (
27 WHERE ($1 <-> coor) < 0.05
31 $$ LANGUAGE SQL; -- }}}
33 -- findpos(): Beregn koordinater for et tidspunkt som ligger mellom to trackpunkter.
34 CREATE OR REPLACE FUNCTION findpos(currtime timestamptz) RETURNS point AS $$ -- {{{
36 firstdate timestamptz;
38 firsttime timestamptz;
45 -- RAISE NOTICE '-----------------------------------';
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
78 -- RAISE NOTICE 'currtime = %', currtime;
79 -- RAISE NOTICE 'firsttime = %, firstcoor = %', firsttime, firstcoor;
80 -- RAISE NOTICE 'lasttime = %, lastcoor = %', lasttime, lastcoor;
82 IF firsttime = lasttime THEN
86 currlat = firstcoor[0] +
89 lastcoor[0]-firstcoor[0]
93 EXTRACT(EPOCH FROM currtime)-EXTRACT(EPOCH FROM firsttime)
97 EXTRACT(EPOCH FROM lasttime)-EXTRACT(EPOCH FROM firsttime)
101 currlon = firstcoor[1] +
104 lastcoor[1]-firstcoor[1]
108 EXTRACT(EPOCH FROM currtime)-EXTRACT(EPOCH FROM firsttime)
112 EXTRACT(EPOCH FROM lasttime)-EXTRACT(EPOCH FROM firsttime)
116 -- RAISE NOTICE 'currcoor = (%,%)', currlat, currlon;
117 RETURN (currlat,currlon);
119 $$ LANGUAGE plpgsql; -- }}}
121 -- wherepos(): Returnerer en streng med dato, posisjon, nærmeste navn og avstand til nærmeste punkt.
122 CREATE OR REPLACE FUNCTION wherepos(currtime timestamptz) RETURNS text AS $$ -- {{{
127 currlat numeric(9, 6);
128 currlon numeric(9, 6);
130 currpos = findpos(currtime);
131 currlat = currpos[0];
132 currlon = currpos[1];
133 currsted = clname(currpos);
134 currdist = cldist(currpos);
135 RETURN(currtime || ' - ' || currlat::text || ' ' || currlon::text || ' - ' || currsted || ' - ' || currdist);
137 $$ LANGUAGE plpgsql; -- }}}
139 -- loop_wayp_new(): Loop gjennom alle entryene i wayp_new og legg dem inn i systemet.
140 CREATE OR REPLACE FUNCTION loop_wayp_new() RETURNS void AS $$ -- {{{
145 -- FOR curr IN SELECT * FROM wayp_new LOOP
149 curr_id = first_wayp_new();
150 IF curr_id IS NOT NULL THEN
151 RAISE NOTICE 'curr_id er ikke null: %', curr_id;
152 currpoint = (SELECT coor FROM wayp_new WHERE id = curr_id);
153 IF (SELECT coor FROM wayp WHERE coor[0] = currpoint[0] AND coor[1] = currpoint[1]) IS NOT NULL THEN
154 RAISE NOTICE '% finnes allerede i wayp', currpoint;
155 INSERT INTO wayp_rej SELECT * FROM wayp_new WHERE id = curr_id;
157 RAISE NOTICE '% er ikke i wayp', currpoint;
158 INSERT INTO wayp SELECT * FROM wayp_new WHERE id = curr_id;
159 PERFORM update_trackpoint(currpoint);
161 DELETE FROM wayp_new WHERE id = curr_id;
162 -- COPY (SELECT name FROM wayp WHERE coor::varchar = currpoint::varchar)
165 RAISE NOTICE 'currpoint er null';
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 -- Avstanden hjemmefra.
177 UPDATE logg SET avst = '(60.42543,5.29959)'::point <-> $1
178 WHERE ($1 <-> coor) < 0.05;
179 RAISE NOTICE 'avst ferdig';
181 UPDATE logg SET sted = clname(coor), dist = cldist(coor)
182 WHERE ($1 <-> coor) < 0.05;
183 RAISE NOTICE 'update_trackpoint(%) er ferdig', currpoint;
188 -- first_wayp_new(): Returnerer id for den eldste i wayp_new.
189 CREATE OR REPLACE FUNCTION first_wayp_new() RETURNS integer AS $$ -- {{{
191 RETURN (SELECT id FROM wayp_new ORDER BY id LIMIT 1);
196 -- secmidnight(): Returnerer antall sekunder sia midnatt for en dato.
197 CREATE OR REPLACE FUNCTION secmidnight(timestamptz) RETURNS double precision -- {{{
199 SELECT extract(hour from $1) * 3600 + extract(minute from $1) * 60 + extract(second from $1);
200 $$ LANGUAGE SQL; -- }}}