3

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()

image

And in QGIS, we see that the table has been created, but is empty... enter image description here

  • Welcome to gis.stackexchange! This text currently reads like two questions. Please note that the format of this Q&A site demands that each thread focuses on only one question. Therefore, please separate the two issues so they can be answered separately. (ad the extractbylocation issue: include information about the input CRS. screenshots would help too) – underdark Sep 28 '15 at 19:27
3

Try changing your "['intersects']" from your processing.runalg line to u'intersects' instead:

processing.runalg("qgis:joinattributesbylocation", neighborsPath, censusPath, u'intersects', 1, "mean", 0, layerOutput)

I had this problem before, tested this and works with QGIS 2.10.1-Pisa; Processing version 2.10.2.

Hope this helps!

  • @JonathanGaudreau - Awesome buddy! Glad you got it working :) – Joseph Oct 1 '15 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.