1

Is there a way to merge all the layers with same name using python script in QGIS in the following list;

names = [layer.name() for layer in QgsMapLayerRegistry.instance().mapLayers().values()]
print names

Output:

[u'Pois', u'Pois', u'Pois', u'Pois', u'Pois', u'Pois', u'Pois', u'Pois', u'Pois']

So that when I list my current mapLayers, all I get is one layer with all the other layers merged into it.

3

You can use processing algorithms :

import processing
processing.alghelp("qgis:mergevectorlayers")

pois_lyr_list = []
lyr_list = QgsMapLayerRegistry.instance().mapLayers()
for layer in lyr_list:
    if layer.name() == u'Pois':
         pois_lyr_list.append(layer)

processing.runandload("qgis:mergevectorlayers", pois_lyr_list, "memory:merged")
  • I think @raosaeedali wants to merge layers which have the same name :) – Joseph Feb 7 '18 at 12:00
  • @Joseph exactly. – raosaeedali Feb 7 '18 at 12:06
  • @joseph, Ok I added a loop to add in list only layers with name == u'Pois' – SYG Feb 7 '18 at 12:07
  • Getting the following error; AttributeError: 'unicode' object has no attribute 'name' – raosaeedali Feb 7 '18 at 12:17
  • Figured it out. It was because in lyr_list u'Pois20180207171404510 was shown instead of u'Pois'. – raosaeedali Feb 7 '18 at 12:21

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.