I am trying to make a spatial join to calculate the mean value of many variables (100+ columns) of census tracts by city. In order to do that, I would, in QGIS Desktop, run a spatial join (join attributes by location). I did it and it worked. However, I need to automate the process and use the command line "processing" commands ("processing.runalg...").
Even tough it works in QGIS and should work in command line with the following line: processing.runalg("qgis:joinattributesbylocation", neighborsPath, censusPath, "['intersects']",1,"mean", 0, layerOutput) The process works, but gives an empty shapefile (1kb size).
As suggested in many posts (Why does "Join attributes by location" fail to compute sums when used in a model?) and (QGIS Join Attributes by Location not working from Python script), I downgraded processing to v.2.2.0, and used the joinbylocation, the result only gives me stats for the first column of my joined shapefile and not for the 100+ columns I have. This is the line that I am using: processing.runalg("qgis:joinbylocation", neighborsPath, censusPath, 1, 'mean', 0,1, layerOutput)
I don't know if one of you encountered this problem and successfully fixed it. (p.s. I tried all versions of Processing) I hope someone can help me! (This is my first question on stackexchange, I hope it's well formulated enough)
Also, qgis:extractbylocation also gives empty shapefiles, for an unknown reason... So the cause might be the same in both cases.
The files are all in WGS84 CRS
Here is the code I am using:
import os import sys from qgis.core import * from PyQt4.QtGui import * from os import walk app = QApplication() QgsApplication.setPrefixPath("C:/OSGEO4~1/apps/qgis", True) QgsApplication.initQgis() sys.path.append(r'C:\OSGeo4W64\apps\qgis\python\plugins') ogr2ogr = r"C:\OSGeo4W64\bin\ogr2ogr.exe" import processing from processing.core.Processing import Processing Processing.initialize() Processing.updateAlgsList() neighborsPath = r"C:\Users\JoeNavut\Desktop\tests\wgs84\corrected_geom\calgary.shp" #PATH TO NEIGHBOR FILE censusPath = r"C:\Users\JoeNavut\Desktop\tests\wgs84\corrected_geom\censusData.shp" #PATH TO CENSUS FILE layerOutput = neighborsPath.replace(".shp", "_Criteria.shp") layerInput = QgsVectorLayer(neighborsPath, "file_to_calculate", "ogr") censusInput = QgsVectorLayer(censusPath, "file_for_calc", "ogr") print layerInput.isValid() print censusInput.isValid() try: processing.runalg("qgis:joinattributesbylocation", neighborsPath, censusPath, "['intersects']",1,"mean", 0, layerOutput) print "Criteria calculations complete" except: print "Criteria calculations failed" QgsApplication.exitQgis() QApplication.exit()