2

Is there any tool or plugin providing export of selected features from more than one layer on one click?

I need to create multiple exports from three layers. Feature from layer one, then feature from layer two and feature from layer three.

I need 3x click on selected layer, choose save as, click save only selected and select path and name. For 100 exports it is not very effective approach.

2

Not sure if there is a plugin for this but you could run the following code in your Python Console which:

  1. Creates a new memory layer (you can define the type and crs).
  2. Iterates through each layer in the ToC and adds the attribute fields.
  3. Fetches all selected features in each layer and writes it to the memory layer.
  4. Saves the memory layer as a shapefile (you can define the path). Memory layer is then deleted.
  5. (Optional) - You can add the saved layer back into QGIS

def run(name):
    # To create polygon memory layer, use "Polygon"; "Point" for point layer; "LineString" for line layer
    mem_layer = QgsVectorLayer("Polygon?crs=epsg:4326", "Layer_Name", "memory")
    mem_layer.startEditing()
    for layer in QgsMapLayerRegistry.instance().mapLayers().values():
        attr = layer.dataProvider().fields().toList()
        mem_layer.dataProvider().addAttributes(attr)
        mem_layer.updateFields()
        feats = [ feat for feat in layer.selectedFeatures() ]           
        mem_layer.dataProvider().addFeatures(feats)
    mem_layer.commitChanges()
    # Change path accordingly
    path = r"C:/Users/You/Desktop/Folder/" + name
    new_layer = QgsVectorFileWriter.writeAsVectorFormat(mem_layer, path, "utf-8", None, "ESRI Shapefile")
    del mem_layer
    #QgsMapLayerRegistry.instance().addMapLayer(QgsVectorLayer(path + ".shp", name, "ogr"))

To run the code, type run('Enter_Name') in the console. "Enter_Name" will be used as the name of the output so you can change this accordingly, keep in mind that you need to include the single quotation marks. If you want to add the output to the ToC, uncomment the last line.

The code also assumes that the attribute fields for your 3 layers are the same, otherwise the attributes of each layer may not be saved in the correct fields.

  • 1
    Thank you very much for your code. I will try it tomorrow and modify for my purposes. – Bulva Jun 20 '16 at 12:46
  • @Bulva - Most welcome, hope it will work for you :) – Joseph Jun 20 '16 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.