Created
May 15, 2017 14:02
-
-
Save jsanz/37ce84faed051c8c32245b253d1bb960 to your computer and use it in GitHub Desktop.
SQL: Get the angles of segment breaks of a line table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with | |
-- dump the geometries | |
dumps as ( | |
select cartodb_id, | |
st_dumppoints(the_geom) as dp | |
from ne_50m_rivers_lake_centerlines | |
), | |
-- get the points | |
points as ( | |
select cartodb_id, | |
(dumps.dp).geom point , | |
(dumps.dp).path | |
from dumps | |
order by cartodb_id, (dumps.dp).path | |
), | |
-- get the azimuths of a point against the previous and the next point | |
-- you may also want to get the distance between them | |
azimuths as ( | |
select | |
cartodb_id, | |
path, | |
st_azimuth( | |
lag(point) over (partition by cartodb_id order by path), | |
point | |
) az_lag, | |
st_azimuth( | |
point, | |
lead(point) over (partition by cartodb_id order by path) | |
) az_lead | |
from points | |
) | |
-- difference the azimuths, here you can start filtering by a threshold in radians or whatever | |
select cartodb_id, | |
path, | |
case when az_lead is not null and az_lag is not null | |
then az_lead - az_lag | |
else null | |
end as az_diff | |
from azimuths | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment