6

I'm writing a script in pyQGIS, which would emulate measurement of a line by "walking" along it with a compass with constant length. I have a problem with the Clip algorythm from processing module, namely it returns None, regardless of whether I add layers as inputs or selected features. What do I do wrong? I believe, that should the clip result in an empty layer, it would still be there, just empty? Below is the piece of code with problems,

while d.measureLine(QgsPoint(coorFin[0],coorFin[1]),QgsPoint(coor[0],coor[1]))>step: # If the distance between current and last point is more than chosen step
 # Create buffer polygon around a point
 bufCirc = point.geometry().buffer(step,10) # do a buffer from existing point 
 buforek = QgsFeature()
 buforek.setGeometry(bufCirc)
# Create layer and put the polygon inside
 bufferLayer = QgsVectorLayer("Polygon", "temporary_polygons", "memory")
 pz = bufferLayer.dataProvider()
 bufferLayer.setCrs(my_crs)
 pz.addFeatures([buforek])
 bufferLayer.commitChanges()
# select the buffer
 buforSel = bufferLayer.setSelectedFeatures([buforek]) 
# Clip coastline with the buffer
 liniaWarstwa = processing.runandload("qgis:clip", selectedCoastLine, buforSel, "memory:liniaWarstwa")
 print liniaWarstwa
 layer = QgsMapLayerRegistry.instance().mapLayersByName("memory:liniaWarstwa")[0]

The print prints "none", QgsMapLayerRegistry says "index out of range".

Joseph
76.7k8 gold badges173 silver badges286 bronze badges
asked Mar 26, 2016 at 12:01
1
  • 2
    Have you tried it without a memory layer? I've had issues recently where memory layers could not be read by processing algorithms. Maybe check with a file on disc, and see if the problem persists or not. Commented Apr 1, 2016 at 9:30

2 Answers 2

3

Aside of what Joseph mentioned, there is a number of other things you need to keep in mind when clipping from/to memory, so a little check-list for all of you using processing algorithms and memory layers:

  • check, that your version of processing plugin is the newest (2.12.2 behaves in a way described by Joseph), update if not.
  • Make sure all layers are in the same crs, as is your canvas. Have it explicitly set in code.
  • Run your desired algorithm from GUI and check what name does an output have. For example Polish version of QGIS has the output names translated, so I had to put "Przycięte" instead of "Clipped"... Alternatively you can change language to English, which may be safer, to avoid specific characters of your language.
  • Have both clip layers loaded onto canvas (use QgsMapLayerRegistry.instance().addMapLayer(layerIHoldInMemory) )
  • But the most important part of my solution was: Don't use QGIS versions newer than 2.4. It seems, that there is some sort of problem with processing polygons/lines, it works nicely, but on QGIS 2.2 Valmiera.
answered Apr 4, 2016 at 13:46
3
  • 1
    It might be really helpful if you post your code as well =) Commented Apr 4, 2016 at 13:51
  • 2
    I won't because in fact it does work randomly. Sometimes it clips correctly, sometimes it does not even if I don't change a thing in the code. What I listed above is a bunch of advice, a list of factors maybe, but I should add, that it will not result in script doing what you want. Commented Apr 5, 2016 at 18:06
  • I understand, that's a fair point :) Commented Apr 6, 2016 at 9:16
2

In the newer versions of the Processing plugin, it would seem that you can no longer define the name of the output memory layer. The name of the algorithm is used instead (see similar post).

Therefore, you should replace the last section of your code with something similar:

# Clip coastline with the buffer
 processing.runandload("qgis:clip", selectedCoastLine, buforSel, None)
 layer = QgsMapLayerRegistry.instance().mapLayersByName("Clipped")[0]
answered Mar 29, 2016 at 12:44
4
  • It didn't help, sadly. I updated the plugin, tried what you propose and nothing happened again. I have even tried to use runalg, or output to a location on my hdd, but nothing works. Recently I'm getting "Unable to execute algorithm Wrong parameter value: Polygon" message, the "polygon" is in constructor of buffer layer (if I change it to e.g. "Polygon?crs=EPSG:xxxx" the word in the message changes accordingly). Commented Mar 31, 2016 at 20:23
  • I think when you added your buffer layer, you need to add bufferLayer.startEditing() before you can add any features to it. Commented Apr 1, 2016 at 9:39
  • Thank you for all answers. I did what you suggested and a few other things (including figuring out, that in Polish version of QGIS the name of output is in Polish...), so I guess I can mark your answer as valid. Commented Apr 1, 2016 at 20:57
  • 1
    @Julian_P - Glad you got it solved but I would strongly suggest that you post your working solution as an answer and accept that. This can help others in a similar situation as you ;) Commented Apr 4, 2016 at 8:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.