This blog post has the answer I think you are looking for.
In this case it looks at neighboring polygons; lists their ids, and sums up a defined attribute of them.
You might want to make a copy of the original data and then run the code in the python console of QGIS.
NOTE:
I noticed an error in his code on line 52
here is the corrected if statement:
# these conditions. So if a feature is not disjoint, it is a neighbor.
if (f != intersecting_f and not intersecting_f.geometry().disjoint(geom)):
neighbors.append(intersecting_f[_NAME_FIELD])
neighbors_sum += intersecting_f[_SUM_FIELD]
Sample Output:
#UPDATE#
I've updated to reflect your comment.
If you load the layer into QGIS , edit the variables and output file locations, and run the following, you will get:
- A layer for each shared edge
(showing: id, touches, length, sumcolumn)
- A table with a row for each polygon (stats summed)
(showing: id, length of edges summed, sumcolumn summed)
Sample Output:
1.Shared Edges:
2.Sums per polygon:
'''
FOR EACH POYGON OF INPUT:
SUM the length of touching sides
SUM a specified column of touching polygons
'''
import itertools
import math
from PyQt4.QtCore import QVariant
#SPECIFY VARIABLES
polycode = 'ID'
polysum = 'SumCol'
#SPECIFY EPSG
epsg = '28355'
#SPECIFY OUTPUTS
outputedges = r"C://temp//DELETEME//t_line_2.shp"
outputedges_sum = r"C://temp//DELETEME//t_line_2_sum.shp"
#USE TJHE LOADED LAYER:
layer = iface.activeLayer()
#CREATE LINE MEMORY LAYER
vlline = QgsVectorLayer("LineString?crs=EPSG:%s" % epsg, "temporary_lines", "memory")
prline = vlline.dataProvider()
# add fields
prline.addAttributes([QgsField('ID', QVariant.String), QgsField('Touches', QVariant.String), QgsField('length', QVariant.Double), QgsField('tosum', QVariant.Double)])
vlline.updateFields() # tell the vector layer to fetch changes from the provid
#LIST OF EXTERIOR RINGS, ALONG WITH PECIFIED COLUMNS
rings = [(QgsGeometry.fromPolyline(elem.geometry().asPolygon()[0]), elem[polycode], elem[polysum])for elem in layer.getFeatures()]
#CREATE TABLE FOR EACH EDGE (SHOW: id, touches, length, sumcolumn)
for i in itertools.permutations(rings, 2):
print 'Working on ID: '+ str(i[0][1])
if i[0][0].intersects(i[1][0]):
len = i[0][0].intersection(i[1][0]).length()
#print len
fetline = QgsFeature()
fetline.setGeometry(i[0][0].intersection(i[1][0]))
fetline.setAttributes([str(i[0][1]),str(i[1][1]),len,(i[1][2])])
prline.addFeatures([fetline])
vlline.updateExtents()
#WRITE EDGES TABLES
QgsVectorFileWriter.writeAsVectorFormat(vlline,outputedges,"utf-8",None,"ESRI Shapefile")
iface.addVectorLayer(outputedges, "hello_edges", "ogr")
#CREATE Vlayer of summed data (grouped by ID)
vlayer = QgsVectorLayer( "?query=SELECT ID, SUM(length) sum_length, SUM(tosum) sum_tosum FROM hello_edges GROUP BY ID ", "vlayer", "virtual" )
querylayer = vlayer.dataProvider()
QgsVectorFileWriter.writeAsVectorFormat(vlayer,outputedges_sum,"utf-8",None,"ESRI Shapefile")
iface.addVectorLayer(outputedges_sum, "hello_sums", "ogr")
iface.showAttributeTable(iface.activeLayer());
Geometry calculations and operations (area, length, buffer) are done based on the CRS of the layer. So, if your layer is in WGS84, your area is measured in square-degree. Not really useful.
Either save your layer using any projected CRS, or you run something like area(transform($geometry,'current EPSG','projected EPSG'))
.
Best 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:
(more or less :) )