I have a big map with several layers. I want to extract a small area in the map and merge the layers.

I tried to merge the layers and than to select the zone I am interested, but the merging is impossible due to the size of the original map (the algorithm started and after 20 minutes it was at 1%)

Is it possible to select the same area from multiple layers and create a new merged layer (without extracting zone from the original layers one-by-one) ?

  • I would like to generate a point layer selecting points from 2 layers. I want to use this technique but I'm totally new to python, and have merely no experience in coding, though I've had introductory lessons on R. What am I supposed to change in the code to make it work? Thanks a lot! – Julie.m May 10 '18 at 12:59

If you are using QGIS and the area you are wanting to select can drawn as a polygon, you can use the graphical modeler to batch process clipping each individual layer then merging all the smaller layers together.

Using the graphical modeler allows you to automate the process and reduce the number of intermediary layer being saved so you will be left with just the clipped and merge area.

Essentially this is what you tried but backwards (merge-clip -> clip-merge). But because the layers are pre-clipped when you merge you are using smaller files and so the process should be faster overall.

  • Exactly what I need ! A tool for automating the clipping and merging phases ! It works very well. Thank you – Mauro Dell'Amico Apr 18 '17 at 6:33

You could use a bit of Python to get the selected features from multiple layers and save them to a new shapefile:

  1. First select your features from your layers.
  2. In the Python Console, run the following code to define a function which iterates through each layer and saves the selected features into a memory layer which is finally saved as a shapefile (edit the code to change the path of the output):

    def run(name, crs):
        # To create polygon memory layer, use "Polygon"; "Point" for point layer; "LineString" for line layer
        mem_layer = QgsVectorLayer("Polygon?crs=epsg:" + str(crs), "Layer_Name", "memory")
        for layer in QgsMapLayerRegistry.instance().mapLayers().values():
            attr = layer.dataProvider().fields().toList()
            feats = [ feat for feat in layer.selectedFeatures() ]           
        path = r"path/to/directory/" + name
        new_layer = QgsVectorFileWriter.writeAsVectorFormat(mem_layer, path, "utf-8", None, "ESRI Shapefile")
        del mem_layer
        QgsMapLayerRegistry.instance().addMapLayer(QgsVectorLayer(path + ".shp", name, "ogr"))
  3. Then run the following to execute the function where, for example, the output shapefile name is "merged" and its CRS set at ESPG:27700:

    run('merged', 27700)
  • 1
    Thanks, I suppose it works, but it is too advanced for me. The grapfhical modeler solved my problem. – Mauro Dell'Amico Apr 18 '17 at 6:34
  • @MauroDellamico - Most welcome, you should accept Knightshound's answer by clicking the green-faded tick on the left-hand side :) – Joseph Apr 18 '17 at 9:04

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.