Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Scripts for Postgres/Postgis
- --
- -- Create a layer with fields of near neighbors from other layer
- -- .Layer 'a' = Target
- -- .Layer 'b' = Source of neighbors
- -- In select statement you can choose the fields in both tables 'a' and 'b'
- -- This script can use in 'Custon DB Query' plugin for QGIS
- select a.id as gid, a.the_geom, b.id as gid_b, ST_Distance(a.the_geom, b.the_geom) as min_dist_a_b
- from
- a,b,
- (
- -- ID of the near neighbors
- SELECT m.id as a_id, m.nn[1] as b_id
- FROM
- (SELECT a.id, a.the_geom,
- ARRAY(SELECT b.id
- FROM b
- --WHERE ST_DWithin(a.the_geom, b.the_geom, 5000) -- limit of distance
- ORDER BY ST_Distance(a.the_geom, b.the_geom)
- LIMIT 1) As nn
- FROM a ) m
- ) nn
- where
- nn.a_id = a.id and nn.b_id = b.id
Advertisement
Add Comment
Please, Sign In to add comment