1

I have a process which I wish to automize with a Python Script. This Python Script accepts 2 input layers (1 point layer and 1 line layer; both layers do not have the same CRS). The script should then calculate the nearest line to each point.

My solution for this includes 3 steps:

1st step: reproject input layer (the line layer to be exact) with the processing algorithm "qgis:reprojectlayer" (in order to have two inputlayers with the same CRS)

2nd step: Calculation of the nearest lines to each point with the NNJoin plugin

3rd step: Transfer attribut with the field calculator

Before I started writing the Python script, I opened the Python console to test the commands I would need. First of all, I tried to test the qgis:reprojectlayer algorithm (for that, I did a "import processing"). And then, I checked the processing history and copied the usage of the algorithm into the console (I have run the algorithm before in order to do that).

enter image description here

And here is the problem: If I run this algorithm from the toolbox, it generates a output file which is instantly loaded into qgis. The same happens if I run the algorithm directly in the history window (with a doubleclick). But whenever I copy the functioncall (from the history) into my python console and run the command, it looks like this:

enter image description here

It says OUTPUT: memory:, but it does not load anything into qgis and I have also tried it with a different output-file-path but the output folder remains empty. It looks like the algorithm does not generate a file? And how can I assign the output file (which should be generated by the reprojectlayer-algorithm) to a variable within my python script?

My Python Script looks like this at the moment (I defined the inputs and output): enter image description here

Any suggestions?

1
  • 2
    Please include code in questions using the Code Sample button and not images of code.
    – artwork21
    Dec 6, 2017 at 13:40

1 Answer 1

4

To load a processing memory layer into QGIS try this:

result = processing.runalg('qgis:reprojectlayer', layer, "EPSG:102013", None) # None parameter defines output as in memory
inMemoryLayer = processing.getObject(result['OUTPUT']) # this gets the memory layer object
QgsMapLayerRegistry.instance().addMapLayer(inMemoryLayer) # adds the memory layer into Q
2
  • Do I need no output parameter? does this return the result varible? Maybe you can explain to me what these 3 line do exactly. Thank you!
    – applebrown
    Dec 6, 2017 at 13:51
  • Added some comments to explain more
    – artwork21
    Dec 6, 2017 at 17:50

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.