Tuesday, 16 February 2016

qgis - Calculate polygon field based on neigboring polygon attribute


I have a vector shape with many different sized polygons each possessing an own class name (for instance "houses", "houses with offices", etc ...)


I want to assign a new field to each polygon containing the name of the majority (higher proportion) of adjacent polygons attribute name. Ergo some kind of polygon neighborhood analysis.


enter image description here



The query should look to the attribute of each bordering polygon and change another fields value to the name which occurs the most. For instance the beige polygon in the middle of the above image is surrounded by polygons with the attribute "houses" and therefore it should get the attribute "houses". (Best would be if i can somehow display and save the proportion of different attributed polygons).




Does anyone have an idea or a tool how to accomplish this task? I am thankful for every partial or full solution. Any good way to solve this problem is appreciated!


I intend to use QGIS, but i can also switch to other tools (Arcgis 9, Grass, Saga, R, ...)



Answer



Quantum GIS has excellent support for PostGIS (which I guess you can use at home since it's free software), so if you are familiar with it, you could script this procedure using SQL with something like this:


UPDATE poly_layer p
SET neighbors_class = (
SELECT class FROM (
SELECT class, count(0)

FROM poly_layer n
WHERE ST_Intersects(n.geom, p.geom)
GROUP BY class
ORDER BY count(0) DESC
) AS foo LIMIT 1
);

(more or less :) )


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...