Skip to main content
deleted 8 characters in body
Source Link
Taras
  • 36k
  • 7
  • 77
  • 153

Since QGIS 3.0, there is another approach available. Use the materialize() method from the QgsFeatureSource class.

Over the entire layer with the allFeatureIds() method:

# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() # or with: layer = QgsMapLayerRegistryQgsProject.instance().mapLayersByName('name_of_the_layer''LAYER_NAME')[0]   # create a new layer from all features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds()))   # add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

Over the selection of all features with the selectedFeatureIds() method from the QgsVectorLayer class:

# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() # or with: # or with: layer = QgsMapLayerRegistryQgsProject.instance().mapLayersByName('name_of_the_layer''LAYER_NAME')[0]   # select all features in the original layer layer.selectAll()   # create a new layer from selected features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))   # remove selection in the original layer layer.removeSelection()   # add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

References:

Since QGIS 3.0 there is another approach available. Use the materialize() method from the QgsFeatureSource class.

Over the entire layer with the allFeatureIds() method:

# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() # or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] # create a new layer from all features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds())) # add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

Over the selection of all features with the selectedFeatureIds() method from the QgsVectorLayer class:

# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() # or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] # select all features in the original layer layer.selectAll() # create a new layer from selected features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds())) # remove selection in the original layer layer.removeSelection() # add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

References:

Since QGIS 3.0, there is another approach available. Use the materialize() method from the QgsFeatureSource class.

Over the entire layer with the allFeatureIds() method:

# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() # or with: layer = QgsProject.instance().mapLayersByName('LAYER_NAME')[0]   # create a new layer from all features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds()))   # add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

Over the selection of all features with the selectedFeatureIds() method from the QgsVectorLayer class:

# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() # or with: # or with: layer = QgsProject.instance().mapLayersByName('LAYER_NAME')[0]   # select all features in the original layer layer.selectAll()   # create a new layer from selected features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))   # remove selection in the original layer layer.removeSelection()   # add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

References:

added 130 characters in body
Source Link
Taras
  • 36k
  • 7
  • 77
  • 153

Another approachSince QGIS 3.0 there is to useanother approach available. Use the materialize()materialize() method from the QgsFeatureSourceQgsFeatureSource class. It is available since QGIS 3.0.

Over the entire layer with the allFeatureIds()allFeatureIds() method:

#choose# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() #or# or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #create# create a new layer from all features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds())) #add# add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

Over the selection of all features with the selectedFeatureIds()selectedFeatureIds() method from the QgsVectorLayerQgsVectorLayer class:

#choose# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() #or# or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #select# select all features in the original layer layer.selectAll() #create# create a new layer from selected features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds())) #remove# remove selection in the original layer layer.removeSelection() #add# add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

References:

Another approach is to use the materialize() method from the QgsFeatureSource class. It is available since QGIS 3.0.

Over the entire layer with the allFeatureIds() method:

#choose a layer layer = iface.activeLayer() #or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #create a new layer from all features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds())) #add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

Over the selection of all features with the selectedFeatureIds() method from the QgsVectorLayer class:

#choose a layer layer = iface.activeLayer() #or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #select all features in the original layer layer.selectAll() #create a new layer from selected features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds())) #remove selection in the original layer layer.removeSelection() #add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

References:

Since QGIS 3.0 there is another approach available. Use the materialize() method from the QgsFeatureSource class.

Over the entire layer with the allFeatureIds() method:

# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() # or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] # create a new layer from all features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds())) # add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

Over the selection of all features with the selectedFeatureIds() method from the QgsVectorLayer class:

# imports from qgis.utils import iface from qgis.core import QgsFeatureRequest, QgsProject # choose a layer layer = iface.activeLayer() # or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] # select all features in the original layer layer.selectAll() # create a new layer from selected features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds())) # remove selection in the original layer layer.removeSelection() # add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

References:

deleted 2 characters in body
Source Link
MrXsquared
  • 36.3k
  • 22
  • 79
  • 128

Another approach is to use the materialize() method from the QgsFeatureSource class. It is available since QGIS 3.0.

Over the entire layer with the allFeatureIds() method  :

#choose a layer layer = iface.activeLayer() #or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #create a new layer from all features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds())) #add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

Over the selection of all features with the selectedFeatureIds() method from the QgsVectorLayer class  :

#choose a layer layer = iface.activeLayer() #or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #select all features in the original layer layer.selectAll() #create a new layer from selected features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds())) #remove selection in the original layer layer.removeSelection() #add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

References:

Another approach is to use the materialize() method from the QgsFeatureSource class. It is available since QGIS 3.0.

Over the entire layer with the allFeatureIds() method  :

#choose a layer layer = iface.activeLayer() #or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #create a new layer from all features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds())) #add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

Over the selection of all features with the selectedFeatureIds() method from the QgsVectorLayer class  :

#choose a layer layer = iface.activeLayer() #or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #select all features in the original layer layer.selectAll() #create a new layer from selected features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds())) #remove selection in the original layer layer.removeSelection() #add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

References:

Another approach is to use the materialize() method from the QgsFeatureSource class. It is available since QGIS 3.0.

Over the entire layer with the allFeatureIds() method:

#choose a layer layer = iface.activeLayer() #or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #create a new layer from all features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds())) #add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

Over the selection of all features with the selectedFeatureIds() method from the QgsVectorLayer class:

#choose a layer layer = iface.activeLayer() #or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0] #select all features in the original layer layer.selectAll() #create a new layer from selected features new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds())) #remove selection in the original layer layer.removeSelection() #add a new layer to the map QgsProject.instance().addMapLayer(new_layer) 

References:

Source Link
Taras
  • 36k
  • 7
  • 77
  • 153
Loading