Skip to content

Instantly share code, notes, and snippets.

@dfwarden
Created January 17, 2016 21:35
Show Gist options
  • Save dfwarden/2cfcff6e2356e5fc83e9 to your computer and use it in GitHub Desktop.
Save dfwarden/2cfcff6e2356e5fc83e9 to your computer and use it in GitHub Desktop.
Get sum of change in degrees between the 2 line segments in every trigram of points in a LinestringZ as proxy for curvature.
select
ogc_fid,
sum(degrees(atan2(opposite, adjacent))) as delta_degrees
from
(select
ogc_fid,
point_idx,
st_3ddistance(unit_tangent_endpoint, unit_vector_endpoint) as opposite,
st_3ddistance(common_point, unit_vector_endpoint) as adjacent
from
(select
ogc_fid,
point_idx,
point_2 as common_point,
st_setsrid(
st_makepoint(
st_x(point_2) + ((st_x(point_2) - st_x(point_1)) / st_3ddistance(point_1, point_2)),
st_y(point_2) + ((st_y(point_2) - st_y(point_1)) / st_3ddistance(point_1, point_2)),
st_z(point_2) + ((st_z(point_2) - st_z(point_1)) / st_3ddistance(point_1, point_2))
), 2852
) as unit_tangent_endpoint,
st_setsrid(
st_makepoint(
st_x(point_2) + ((st_x(point_3) - st_x(point_2)) / st_3ddistance(point_2, point_3)),
st_y(point_2) + ((st_y(point_3) - st_y(point_2)) / st_3ddistance(point_2, point_3)),
st_z(point_2) + ((st_z(point_3) - st_z(point_3)) / st_3ddistance(point_2, point_3))
), 2852
) as unit_vector_endpoint
from
(select ogc_fid, point_idx, point_count,
st_transform(st_pointn(geom, point_idx), 2852) as point_1,
st_transform(st_pointn(geom, point_idx+1), 2852) as point_2,
st_transform(st_pointn(geom, point_idx+2), 2852) as point_3
from
(select ogc_fid, st_numpoints(wkb_geometry) as point_count,
generate_series(1, st_numpoints(wkb_geometry)-2) as point_idx, wkb_geometry as geom
from vermont.roads
where
tnmfrc < 4
and st_numpoints(wkb_geometry) >= 3
) as points
) as point_3tuple
where st_3ddistance(point_1, point_2) > 0 and st_3ddistance(point_2, point_3) > 0
) as unit_vector_endpoints
) as tan_components
group by ogc_fid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment