3 -- Returnerer navnet på det nærmeste veipunktet i wayp.
4 CREATE OR REPLACE FUNCTION clname(point) RETURNS text
12 WHERE ($1 <-> coor) < 0.05
18 -- Returnerer avstanden (i grader) til det nærmeste veipunktet i wayp.
19 CREATE OR REPLACE FUNCTION cldist(point) RETURNS numeric
21 SELECT round(avs::numeric, 5) FROM (
26 WHERE ($1 <-> coor) < 0.05
32 -- Beregn koordinater for et tidspunkt som ligger mellom to
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);
121 CREATE OR REPLACE FUNCTION wherepos(currtime timestamptz) RETURNS text AS $$
126 currlat numeric(9, 6);
127 currlon numeric(9, 6);
129 currpos = findpos(currtime);
130 currlat = currpos[0];
131 currlon = currpos[1];
132 currsted = clname(currpos);
133 currdist = cldist(currpos);
134 RETURN(currtime || ' - ' || currlat::text || ' ' || currlon::text || ' - ' || currsted || ' - ' || currdist);
138 -- Returnerer antall sekunder sia midnatt for en dato.
139 CREATE OR REPLACE FUNCTION secmidnight(timestamptz) RETURNS double precision
141 SELECT extract(hour from $1) * 3600 + extract(minute from $1) * 60 + extract(second from $1);