[Qgis-user] Snap points to nearest line

Nicklas Avén nicklas.aven at jordogskog.no
Tue Nov 15 02:08:33 PST 2011


Hallo

If you have your data stored in PostGIS you can do it directly in the
database.

To get a new table with the snapped points and the line attribudes you
can run:

CREATE TABLE line_attribute_point AS
SELECT distinct on (linetable.id) linetable.*,
ST_ClosestPoint(linetable.geom, pointtable.geom) as snapped_point 
FROM
linetable,pointtable
ORDER BY ST_Distance(linetable.geom, pointtable.geom);

Something like that. 

But this will be slow if you have many points and polygons. Then you can
use ST_Dwithin to speed it up if you know how far away the line and
point can be that should be snapped. Say you don't want to snap longer
than 100 meters then:

CREATE TABLE line_attribute_point AS
SELECT distinct on (linetable.id) linetable.*,
ST_ClosestPoint(linetable.geom, pointtable.geom) as snapped_point 
FROM
linetable INNER JOIN pointtable on ST_Dwithin(linetable.geom,
pointtable.geom, 100)
ORDER BY ST_Distance(linetable.geom, pointtable.geom);

Something like that.


HTH
Nicklas








On Tue, 2011-11-15 at 10:49 +0100, Andreas Neumann wrote:
> Hi Cameron,
> 
>  Snapping works fine, but automatically getting the attributes from an 
>  object it snaps to is not possible.
> 
>  Andreas
> 
>  On Mon, 14 Nov 2011 17:59:58 -0800 (PST), Cameron Munro wrote:
> > Hi,
> >
> > I have two layers - points and lines. I'd like to snap the points to
> > the nearest line and take the attributes of that line. Is this
> > possible within QGIS?
> >
> > Thanks,
> >
> > Cameron.
> 





More information about the Qgis-user mailing list