Guest User

Untitled

a guest
Oct 16th, 2012
208
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /* select osm_id,highway, st_length(way) as len, max_degree(way), st_numpoints(way) from
  3.    planet_osm_line where highway is not null and oneway <> 'no' and junction is null
  4.    and st_isring(way) and st_length(way) < 1000  and
  5.    highway in ('primary','secondary','tertiary','trunk','residential','unclassified');
  6. */
  7.  
  8. DROP FUNCTION IF EXISTS max_degree(way geometry);
  9. CREATE OR REPLACE FUNCTION max_degree(way geometry)
  10.   RETURNS numeric
  11.   AS
  12.   $$
  13.   DECLARE
  14.      i              numeric;
  15.      result         float;
  16.      curAngle       float;
  17.      angle          float;
  18.      azimuth1       float;
  19.      azimuth2       float;
  20.      intNumPoints   numeric;
  21.      point1         geometry;
  22.      point2         geometry;
  23.      point3         geometry;  
  24. BEGIN
  25.  
  26.    IF geometrytype(way) <> 'LINESTRING' THEN
  27.      RETURN 9999;
  28.    END IF;
  29.  
  30.    result := 0;
  31.  
  32.    intNumPoints := ST_NumPoints(way);
  33.  
  34.    FOR i in 2..intNumPoints LOOP
  35.       point1 := ST_PointN(way, i-1);
  36.       point2 := ST_PointN(way, i);
  37.       IF i + 1 <= intNumPoints THEN
  38.          point3 := ST_PointN(way, i+1);
  39.       ELSE
  40.          point3 := ST_PointN(way, 1);
  41.       END IF;
  42.  
  43.       azimuth1 := degrees(ST_Azimuth(point1, point2));
  44.       azimuth2 := degrees(ST_Azimuth(point2, point3));
  45.  
  46.       angle := abs(round((azimuth1 - azimuth2)::decimal, 2));
  47.  
  48.       IF angle < (360 - angle) THEN
  49.          curAngle := angle;
  50.       ELSE
  51.          curAngle := 360 - angle;
  52.       END IF;
  53.  
  54.       IF curAngle > result THEN
  55.         result := curAngle;
  56.       END IF;
  57.  
  58.    END LOOP;
  59.  
  60.    RETURN result;
  61.  
  62. END
  63. $$ LANGUAGE 'plpgsql' IMMUTABLE;
  64.  
  65.  
  66. /* RAISE NOTICE 'Index %, Angle %, A1 %, A2 %', i, curAngle, azimuth1, azimuth2; */
RAW Paste Data