Skip to content

Instantly share code, notes, and snippets.

@Komzpa
Created December 2, 2017 12:35
Show Gist options
  • Save Komzpa/9b73a051e54e9aa872fb01cce31748a0 to your computer and use it in GitHub Desktop.
Save Komzpa/9b73a051e54e9aa872fb01cce31748a0 to your computer and use it in GitHub Desktop.
with params as (
select 'SRID=4326;POINT(0 0)' :: gemetry as geom
)
select
a.altitude,
a.distance,
b.altitude, -- may be null, then do not interpolate
b.distance -- may be null, then do not interpolate
from (-- closest linestring
select
c.*,
+ST_Distance(c.geom :: geography, p.geom :: geography) as distance
from el_contour_1to5m c, params p
order by c.geom <-> p.geom
limit 1
) a
-- left join, as we can be in peak or in lowest area
left join lateral (
select
c.*,
ST_Distance(c.geom :: geography, p.geom :: geography) as distance
from el_contour_1to5m c, params p
where not exists(
select
from el_contour_1to5m j
where j.altitude = a.altitude
and ST_Intersects(
j.geom,
ST_ShortestLine(p.geom, c.geom) -- may not work on non-convex isolines :(
)
)
order by c.geom <-> p.geom
limit 1
) b on true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment