4

I would like to create a vector layer, add a point to it and then use it in qgis algorithms.

This is what I wrote :

#create temporary layer containing center point
 point=QgsFeature()
 point.setGeometry(QgsGeometry.fromPoint(QgsPoint(center[0],center[1])))
 pointlayer=QgsVectorLayer("Point?crs=epsg:2154", "temporary_points", "memory")
 pointlayer.startEditing()
 pointlayer.addFeatures([point])
 pointlayer.commitChanges()
 #intersection between point layer and polygon layers to get names codes
 fieldname_vector=processing.runalg('qgis:intersection', parcel, pointlayer, False, None)

But when the algorithm runs, it returns

 Unable to execute algorithm
Wrong parameter value: Point?crs=epsg:2154

Is there a way to use my layer without saving it, knowing that I'll erase it in the future ?

underdark
84.9k22 gold badges237 silver badges419 bronze badges
asked Nov 10, 2017 at 11:19
2
  • Ok thanks, do you know why it's impossible to use it without adding it to the layer's panel ? Commented Nov 10, 2017 at 12:34
  • Ignore my previous comment, you can load it in the Layers Panel without it being shown. A related question regarding this was asked before: Using in-memory vector layer with QGIS processing / SEXTANTE. Commented Nov 10, 2017 at 12:53

1 Answer 1

3

You need to add the memory layer to the QgsMapLayerRegistry before you can use it in processing. But you can add it without it being shown in the Layers Panel by using:

QgsMapLayerRegistry.instance().addMapLayer(pointlayer, False)

When finished, you can then do some cleanup by removing and deleting it:

QgsMapLayerRegistry.instance().removeMapLayer(pointlayer)
del pointlayer
artwork21
35.2k8 gold badges69 silver badges135 bronze badges
answered Nov 10, 2017 at 12:51
2
  • 5
    In QGIS 3 the QgsMapLayerRegistry has been removed but the layer can be added via QgsProject.instance().addMapLayer(pointlayer, False). Commented Dec 31, 2019 at 11:28
  • @Orienteerix - Thanks for the extra info :) Commented Jan 6, 2020 at 10:42

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.