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,148
  • 21
  • 231
  • 413
Clement
  • 765
  • 1
  • 6
  • 23

1 Answers1

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,114
  • 8
  • 66
  • 134
Joseph
  • 75,746
  • 7
  • 171
  • 282