Skip to main content
added 1 character in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

I could suggest a PyQGIS way of implemeningimplementing this task.

# referring to a needed layer
layer = iface.activeLayer()

# creating a temporal layer from selected features
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

# specifying important fields
needed_fields = ["id"]
all_fields = [field.name() for field in layer.fields()]
other_fields = list(set(all_fields) - set(needed_fields))

# getting indexes of other fields
idx = [temp_layer.fields().indexFromName(field) for field in other_fields]

# deleting other fields based on their indexes
temp_layer.dataProvider().deleteAttributes(idx)
temp_layer.updateFields()

# adding temporal output to the map canvas
QgsProject.instance().addMapLayer(temp_layer)

References:

I could suggest a PyQGIS way of implemening this task.

# referring to a needed layer
layer = iface.activeLayer()

# creating a temporal layer from selected features
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

# specifying important fields
needed_fields = ["id"]
all_fields = [field.name() for field in layer.fields()]
other_fields = list(set(all_fields) - set(needed_fields))

# getting indexes of other fields
idx = [temp_layer.fields().indexFromName(field) for field in other_fields]

# deleting other fields based on their indexes
temp_layer.dataProvider().deleteAttributes(idx)
temp_layer.updateFields()

# adding temporal output to the map canvas
QgsProject.instance().addMapLayer(temp_layer)

References:

I could suggest a PyQGIS way of implementing this task.

# referring to a needed layer
layer = iface.activeLayer()

# creating a temporal layer from selected features
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

# specifying important fields
needed_fields = ["id"]
all_fields = [field.name() for field in layer.fields()]
other_fields = list(set(all_fields) - set(needed_fields))

# getting indexes of other fields
idx = [temp_layer.fields().indexFromName(field) for field in other_fields]

# deleting other fields based on their indexes
temp_layer.dataProvider().deleteAttributes(idx)
temp_layer.updateFields()

# adding temporal output to the map canvas
QgsProject.instance().addMapLayer(temp_layer)

References:

added 436 characters in body
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

I could suggest a PyQGIS way of implemening this task.

# referring to a needed layer
layer = iface.activeLayer()

# creating a temporal layer from selected features
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

# specifying important fields
needed_fields = ["id"]
all_fields = [field.name() for field in layer.fields()]
other_fields = list(set(all_fields) - set(needed_fields))

# getting indexes of other fields
idx = [temp_layer.fields().indexFromName(field) for field in other_fields]

# deleting other fields based on their indexes
temp_layer.dataProvider().deleteAttributes(idx)
temp_layer.updateFields()

# adding temporal output to the map canvas
QgsProject.instance().addMapLayer(temp_layer)

References:

I could suggest a PyQGIS way of implemening this task.

# referring to a needed layer
layer = iface.activeLayer()

# creating a temporal layer from selected features
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

# specifying important fields
needed_fields = ["id"]
all_fields = [field.name() for field in layer.fields()]
other_fields = list(set(all_fields) - set(needed_fields))

# getting indexes of other fields
idx = [temp_layer.fields().indexFromName(field) for field in other_fields]

# deleting other fields based on their indexes
temp_layer.dataProvider().deleteAttributes(idx)
temp_layer.updateFields()

# adding temporal output to the map canvas
QgsProject.instance().addMapLayer(temp_layer)

I could suggest a PyQGIS way of implemening this task.

# referring to a needed layer
layer = iface.activeLayer()

# creating a temporal layer from selected features
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

# specifying important fields
needed_fields = ["id"]
all_fields = [field.name() for field in layer.fields()]
other_fields = list(set(all_fields) - set(needed_fields))

# getting indexes of other fields
idx = [temp_layer.fields().indexFromName(field) for field in other_fields]

# deleting other fields based on their indexes
temp_layer.dataProvider().deleteAttributes(idx)
temp_layer.updateFields()

# adding temporal output to the map canvas
QgsProject.instance().addMapLayer(temp_layer)

References:

Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

I could suggest a PyQGIS way of implemening this task.

# referring to a needed layer
layer = iface.activeLayer()

# creating a temporal layer from selected features
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

# specifying important fields
needed_fields = ["id"]
all_fields = [field.name() for field in layer.fields()]
other_fields = list(set(all_fields) - set(needed_fields))

# getting indexes of other fields
idx = [temp_layer.fields().indexFromName(field) for field in other_fields]

# deleting other fields based on their indexes
temp_layer.dataProvider().deleteAttributes(idx)
temp_layer.updateFields()

# adding temporal output to the map canvas
QgsProject.instance().addMapLayer(temp_layer)