Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. -- Function: public.update_visibility_graph(double precision, double precision)
  2.  
  3. -- DROP FUNCTION public.create_visibility_graph(text, text, double precision, double precision);
  4.  
  5. CREATE OR REPLACE FUNCTION public.create_visibility_graph(
  6.  
  7. IN barriers text,
  8.  
  9. IN sites text,
  10.  
  11. IN max_distance double precision DEFAULT 40000,
  12.  
  13. IN buffer_distance double precision DEFAULT 100,
  14.  
  15. OUT result text)
  16.  
  17. RETURNS text AS
  18.  
  19. $BODY$
  20.  
  21. DECLARE
  22.  
  23. querystring text;
  24.  
  25. BEGIN
  26.  
  27. drop table if exists public.visibility_graph;
  28.  
  29. drop table if exists public.allpts;
  30.  
  31. -- create a table for the points
  32.  
  33. create temporary table allpts (
  34.  
  35. gid serial primary key,
  36.  
  37. id integer,
  38.  
  39. the_geom geometry(Point,3857) not null
  40.  
  41. );
  42.  
  43. -- populate the temp table with convex coastal points
  44.  
  45. querystring = $$
  46.  
  47. insert into allpts (the_geom)
  48.  
  49. select
  50.  
  51. distinct the_geom
  52.  
  53. from (
  54.  
  55. select the_geom,
  56.  
  57. (angle > 180) or (angle between -180 and 0) as concave
  58.  
  59. from (
  60.  
  61. select pt as the_geom,
  62.  
  63. degrees(atan2((st_y(ptn) - st_y(pt)), (st_x(ptn) - st_x(pt)))) - degrees(atan2((st_y(ptp) - st_y(pt)), (st_x(ptp) - st_x(pt)))) as angle
  64.  
  65. from (
  66.  
  67. select
  68.  
  69. pt, feat, ptnum,
  70.  
  71. lag(pt,1) over (partition by feat) as ptp, -- the previous point, or the last if null lag(pt,-1,pt2) over (partition by feat) as ptn -- the next point or null if it is the last
  72.  
  73. from (
  74.  
  75. select
  76.  
  77. (dmp).path[1] as feat,
  78.  
  79. (st_dumppoints((dmp).geom)).geom as pt,
  80.  
  81. (st_dumppoints((dmp).geom)).path[2] as ptnum, st_pointn(ST_ExteriorRing((dmp).geom),2) as pt2
  82.  
  83. from (
  84.  
  85. select
  86.  
  87. st_dump(
  88.  
  89. st_simplifypreservetopology(
  90.  
  91. st_buffer(
  92.  
  93. st_intersection(barriers.geom, sitebuffer.geom), $$||buffer_distance||$$, 1) ,$$||buffer_distance||$$)
  94.  
  95. ) as dmp from (
  96.  
  97. -- get the barrier polygon layer
  98.  
  99. select st_transform(st_union(geom),3857) as geom
  100.  
  101. from $$||barriers||$$) barriers
  102.  
  103. join (
  104.  
  105. -- create a buffer of the sites
  106.  
  107. select st_union(st_buffer(st_transform(geom,3857), $$||max_distance||$$)) as geom from $$||sites||$$) sitebuffer on true
  108.  
  109. ) d
  110.  
  111. ) foo
  112.  
  113. ) bar where ptp is not null -- exclude the first point of each polygon (same as last) ) foobar
  114.  
  115. join m50nad17n b on not st_intersects(b.geom, the_geom)
  116.  
  117. ) foofoobar where not concave$$;
  118.  
  119. execute querystring;
  120.  
  121. -- add in the site locations
  122.  
  123. querystring := $$insert into allpts (id, the_geom)
  124.  
  125. select s.gid, st_transform(s.geom, 3857)
  126.  
  127. from $$||sites||$$ s$$;
  128.  
  129. execute querystring;
  130.  
  131. -- join all nearby point pairs that have a clear line of sight
  132.  
  133. querystring:=$$create table visibility_graph as
  134.  
  135. select
  136.  
  137. row_number() over () as id,
  138.  
  139. p1.gid as source,
  140.  
  141. p2.gid as target,
  142.  
  143. p1.id as sid, p2.id as tid,
  144.  
  145. st_makeline(p1.the_geom, p2.the_geom)::geometry(LINESTRING,3857) as the_geom
  146.  
  147. from allpts p1
  148.  
  149. join allpts p2 on p1.gid < p2.gid
  150.  
  151. and st_distance(p1.the_geom, p2.the_geom) < max_distance
  152.  
  153. join $$||barriers||$$ l2 on true -- obstacle layer
  154.  
  155. where not st_intersects(l2.geom, st_makeline(p1.the_geom, p2.the_geom))$$;
  156.  
  157. execute querystring;
  158.  
  159. -- done with the temp table drop table allpts;
  160.  
  161. -- build routing table
  162.  
  163. select into result pgr_createTopology('visibility_graph',0.0001, clean:=true);
  164.  
  165. END
  166.  
  167. $BODY$
  168.  
  169. LANGUAGE
  170.  
  171. plpgsql VOLATILE
  172.  
  173. COST 100;
  174.  
  175.  
  176.  
  177.  
  178.  
  179. SET search_path TO public;
  180.  
  181. create_visibility_graph(m50nad17n, sites2, 40000, 100);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement