3

In my attribute table there is a large number of fields. I want to export the data with selected fields.

How to do this in QGIS?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Bruno B
  • 839
  • 3
  • 13
  • Are you asking how to select features and copy and paste them? – Erik Mar 11 '22 at 13:11
  • what kind of export do you expect to have ? you can also use the Save only selected features in Save Vector Layer as ... – Taras Mar 11 '22 at 13:26
  • 1
    @Taras I think they want to only save a selection of columns (fields) rather than features (rows). – Spacedman Mar 11 '22 at 13:39
  • @Spacedman, I can barely imagine this ... Besides the output format was not specified – Taras Mar 11 '22 at 13:43

2 Answers2

13

Two ways:

  1. The "Save Vector Layer" dialog lets me choose which fields to save.

enter image description here

  1. Use either the "Drop Fields" or "Retain Fields" processing tools (in the "Vector Table" section of the processing toolbox) to create a new temporary scratch layer with a new set of fields. Then export that and delete it if you won't need it again.

You can probably set up a graphical model to do all this if you are going to be doing this a number of times.

Spacedman
  • 63,755
  • 5
  • 81
  • 115
4

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:

Matt
  • 16,843
  • 3
  • 21
  • 52
Taras
  • 32,823
  • 4
  • 66
  • 137