4

I am trying to make a QGIS plugin, and one of my goals is to use the geoprocessing tool "intersection" of QGIS into my Python code for the plugin. When I run it directly into QGIS, it works perfectly (especially transferring all attributes of both layers intersected).

When I run it in Python code, the result is the same but the attribute table does not contain all attributes (only one attribute...).

Here is an extract of my code for this purpose:

processing.runandload("qgis:lineintersections", final_layer1, final_layer2, "", "", output_intersect_path)

I also tested:

processing.runalg("qgis:lineintersections", final_layer1, final_layer2, "", "", output_intersect_path)
intersect_temp = QgsVectorLayer(output_intersect_path, output_intersect, "ogr")
QgsMapLayerRegistry.instance().addMapLayer(intersect_temp)

The problem lies in the parameters: final_layer1 and final_layer2 or the layers intersected. Then, the source help says "Field A" and "Field B", but I do not know what to put for those parameters in order to get all attributes transfered...

Does someone know how to do that ?


Here is my code:

    layer1_field = "last_fieldName"
    layer2_field = "last_fieldName"

    ouput_0 = processing.runalg("qgis:lineintersections", final_layer1, final_layer2, None, None, None)
    output_1 = processing.runalg("qgis:joinattributestable", ouput_0['OUTPUT'], final_layer1, layer1_field, layer1_field, None)
    result = processing.runandload("qgis:joinattributestable", output_1['OUTPUT_LAYER'], final_layer2, layer2_field, layer2_field, None)

Don`t know why it is not good at all....


Here are the source shapefiles to test.

  • Does anyone has a clue ? – m.siret Oct 7 '16 at 17:51
  • You used the Intersection tool or the Line intersections tool directly from QGIS? They are both different tools, the first provides you with all attributes. The latter does not. – Joseph Oct 10 '16 at 10:30
  • @Joseph : I used both, but Intersection tool does not create points intersection but lines intersection (my inputs are both lines)... – m.siret Oct 17 '16 at 14:03
  • @Joseph : I want point intersection with all attributes (just like Intersect tool in ArcGIS..). Do you have any idea ? – m.siret Oct 17 '16 at 14:04
5
+100

The problem

Indeed, the QGIS Lines Intersection algorithm doesn't preserve all fields. It is only capable of keeping one field from each line layer. Moreover, if the fields that you choose from layer1 and layer2 have the same name, the algorithm only keeps the first one (from layer1).

Short term solution

You can get an adjusted Processing script for intersecting lines from my resource repository (https://github.com/gacarrillor/QGIS-Resources.git). Just install my scripts from the QGIS Resource Sharing plugin and you'll get the Lines Intersection Keep All Fields tool.

enter image description here

enter image description here

Mid-term solution

I've created a Pull Request (check it here) to fix this issue in Processing. It would enable you to get a field from each layer, all fields from both layers, or all fields from one layer and one field from the other.

  • 1
    +1, many thanks for this Germán! I tested this on the OP's source shapefiles and obtained all the fields. If the OP can confirm this works then I will award you the bounty ;) – Joseph Oct 24 '16 at 10:44
  • When I want to use the short term solution, I appears an error reading the repositiory: "Error reading repository: German's QGIS Resources.Status code: 301 Moved Permanently"... Do you have a solution for this please ? – m.siret Oct 24 '16 at 14:15
  • For the mid term solution, I saw on github your request, but what should I do in order to get the tool ? – m.siret Oct 24 '16 at 14:15
  • Well, you could just download the python script (right click, Save Page As...) and add it directly to the Processing Toolbox by clicking on the "Add script from file" tool. – Germán Carrillo Oct 24 '16 at 14:21
  • 1
    MANY THANKS !!! It works finally ! Issue solved ! Many many thanks ! :) – m.siret Oct 24 '16 at 15:29
-1

You could try deleting the processing folder in your .qgis2 path and run the line intersection tool in qgis 2.16.

This is because qgis 2.16 uses processing as core so you dont need the one in the .qgis2 path. It uses the processing folder in the same place you installed qgis 2.16.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • I want to run it through Python in my plugin... Why should I delete this folder ? – m.siret Oct 20 '16 at 20:14
  • I do not have processing plugin... I have a processing folder but not a processing plugin folder – m.siret Oct 20 '16 at 20:15
  • @m.siret - Try removing the processing folder from /.qgis2/python/plugins/, load QGIS 2.16 and your layers and run the code. If you still do not get the same results I did, I will add a bounty as I am unsure what the problem could be... – Joseph Oct 21 '16 at 9:19
  • @Joseph I do not have any folder processing in the path you told me ! In the folder "plugins", I have my plugins not processing... – m.siret Oct 21 '16 at 13:55
  • @m.siret - In that case, I'm not sure what else is the problem so will provide a bounty in the hope that others could see where the issue lies. – Joseph Oct 21 '16 at 14:02

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.