Wednesday, 1 January 2020

polygon - Adding to attributes based on shared line in QGIS?


using 2.18.16 QGIS Trying to attribute shared poly lines. for example would like to calc the adjoining polygon name to a attribute filed Called Adjoin_Po.


enter image description here


so for P01 it would be 'P02,P08' for P10 it would be 'P03,P11,P17,P09'


enter image description here



for the above image I am looking to query out 4,8,6,2. The green ones and not the red ones 1,7,9,3.



Answer



I would suggest a Virtual Layer.


(1) Go to Layer | Add Layer | Add/Edit Virtual Layer


(2) Import your grid layer (assumed Grid in the following example).


(3) Give syntax as below: (if your unique id field is not fid, please change .fid to your id field).


SELECT t1.fid AS id1, t2.fid AS id2
FROM Grid AS t1, Grid AS t2
WHERE t1.fid != t2.fid
AND st_length(st_intersection(t1.geometry, t2.geometry)) > 0


enter image description here


This will create a table above. To be honest I prefer this table.


(4) As required, another syntax to create Adjoin_Po field to store all adjacent polygons. You will notice the middle part (inside FROM( )) is exactly same as the first syntax.


SELECT t0.id1 AS id, Group_Concat(t0.id2, ',') AS Adjoin_Po
FROM(
SELECT t1.fid AS id1, t2.fid AS id2
FROM Grid AS t1, Grid AS t2
WHERE t1.fid != t2.fid
AND st_length(st_intersection(t1.geometry, t2.geometry)) > 0

) AS t0
GROUP BY t0.id1

enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...