Skip to content

Instantly share code, notes, and snippets.

@stuartlynn
Created June 3, 2016 20:07
Show Gist options
  • Select an option

  • Save stuartlynn/3d2d1c4f264a6e480d4074f49cce673c to your computer and use it in GitHub Desktop.

Select an option

Save stuartlynn/3d2d1c4f264a6e480d4074f49cce673c to your computer and use it in GitHub Desktop.
Snap To Closest
with nearest as (
SELECT DISTINCT ON(b.cartodb_id) b.cartodb_id as id_point, a.cartodb_id as id_road, a.the_geom
FROM stuartlynn.primaryandsecondaryroads a INNER JOIN stuartlynn.taret b
ON ST_DWithin(CDB_LATLNG(b.dest_lat, b.dest_lng), a.the_geom, 1000)
ORDER BY b.cartodb_id, ST_Distance(a.the_geom, b.the_geom)
),
snap as (
select ST_SNAP(CDB_LATLNG(b.origin_lat, b.origin_lng), a.the_geom ,1.01 ) as the_geom,
id_point as cartodb_id
from nearest,
stuartlynn.target b,
stuartlynn.primaryandsecondaryroads a
where
b.cartodb_id = id_point and
a.cartodb_id = id_road
)
update stuartlynn.target
set dest_lat_snap = ST_Y(snap.the_geom),dest_lng_snap = ST_X(snap.the_geom)
from snap
where snap.cartodb_id = stuartlynn.target.cartodb_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment