2

I’ve been working on a standalone Python application using the QGIS Processing Framework. For my application, I am supposed to determine average values of a field from a polygonal vector layer along lines in another vector layer.

To accomplish this, I have been using the “qgis:joinattributesbylocation” algorithm with general.runalg() as follows:

newLayerp = general.runalg("qgis:joinattributesbylocation", line_layer, polygon_region, u'intersects', 2, 1, "mean,max", 1, None)
newLayer = processing.getObject(newLayerp['OUTPUT'])

When I visualize this layer in QGIS, some lines have been assigned NULL values. I am not having this issue when using Vector->Data Management Tools->Join Attributes by Location.

How can I get the same results from the standalone application as I seem to be getting from the QGIS GUI?

EDIT

I have tried performing an intersection operation between the line layer and the polygon layer, and then performing the same join attributes by location with the intersection layer and the line layer. I don't get any NULL values anymore, but I'm getting strange values (it is saying the average field value along the length of the line is in the thousands, when all values are less than ten).

Background Information:

The polygonal vector layer’s field consists of values between 0 and 9. The following is the vector layer, colour-coded according to the field I’m using:

enter image description here

The following are the arguments I’m using for the Join Attributes by Location tool in the QGIS GUI:

enter image description here

The following is the result that I get from running the algorithm through the tool in the QGIS GUI, colour-coded by the value of the field I’m using. This is the result that I want to replicate.

enter image description here

I get the following vector layer from my Python code. The lines with NULL values are marked in black, and these are what I’m hoping to resolve.

enter image description here

5
  • Which QGIS version are you using?
    – Joseph
    Feb 10, 2017 at 15:30
  • I'm using version 2.14.8. I've also managed to figure out a workaround, but it essentially involves implementing a join attributes by location method (which I've managed to do).
    – Benjamin
    Feb 10, 2017 at 22:59
  • Did you test it using the latest version of QGIS (as of now QGIS 2.18.3)?
    – Joseph
    Feb 13, 2017 at 10:19
  • I haven't. The reason I downloaded 2.14.8 was that it was saying it was the latest stable release for PyQGIS development.
    – Benjamin
    Feb 13, 2017 at 11:37
  • The latest LTR version is 2.14.11, but can't confirm if it is the most stable version. I still have 2.14.3 :)
    – Joseph
    Feb 13, 2017 at 11:39

1 Answer 1

1

Your code seems ok, but there is still some problem with the spatial operation (that's because you however get a result, even if wrong).

Please, can you try to use ['intersects'] when you call the algorithm? I mean, using this line:

newLayerp = general.runalg("qgis:joinattributesbylocation", line_layer, polygon_region, ['intersects'], 2, 1, "mean,max", 1, None)

instead of what you have provided?

1
  • With the change you suggested, I still seem to be getting NULL values along the same sections of road.
    – Benjamin
    Jan 3, 2017 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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