1

I have a bunch of raster layers, which are packed in one big one, although displayed separately as the layers. I am wondering about an option to draw polygons around them.

I found some questions here:

Creating polygon with extents of raster file in QGIS

but it seems to apply to one particular raster.

enter image description here

I have already done it manually by creating a separate - polygon layer for it. Is there any option to make it straight away?

Geographos
  • 4,087
  • 2
  • 27
  • 88
  • 1
    You might look at the Image Footprint plugin. It may be "experimental" so you would have to allow them to see it in your plugin manager. Or you could go here: https://github.com/lmotta/imagefootprint_plugin/wiki I have not used it. – John Oct 26 '21 at 16:40

1 Answers1

2

Not sure what you mean by stacked. This code will create a polygon from each raster extent with the raster name as an attribute.

#List all rasters added to the map. You can add more if statements to filter by name etc.
rasterlist = [raster for raster in QgsProject.instance().mapLayers().values() if isinstance(raster, QgsRasterLayer)]

#Create polygon layer vl = QgsVectorLayer("Polygon?crs={}&index=yes".format(rasterlist[0].crs().authid()), "myLayer", "memory") provider = vl.dataProvider() provider.addAttributes([QgsField('rastername', QVariant.String)]) #Add a field to hold raster name vl.updateFields()

for r in rasterlist: new_geometry = QgsGeometry.fromRect(r.extent()) f = QgsFeature() f.setGeometry(new_geometry) f.setAttributes([r.name()]) provider.addFeature(f)

QgsProject.instance().addMapLayer(vl)

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161
  • It has covered a full map of the World. Is there something special, which I should include in the 'rastername' maybe? – Geographos Oct 27 '21 at 09:24