TransWikia.com

QGIS: Extracting Raster LAYER Property Information (Source, name, etc.) and property name into a seperate table

Geographic Information Systems Asked on October 4, 2021

Currently we are trying to extract the layer property information from our vector layers and raster layers (inside some geopackages). More specifically the property information. However though it is very easy to do this with vector layer with Rasters it is a different story. While a work around would be to "vectorise" and do the attribute table part, we are looking for another way.

I know the information in the Raster is there because when I click on Properties on my raster i get this:

[![enter image description here][1]][1]

This is something we also want to run as batch process to process many layers at the same time. Note we are not trying to store the information inside the raster layer but outside of it in a table without geometry.

One Answer

You can use pyqgis, example:

#Create empty memory table
vl = QgsVectorLayer("None", "Rasterinfo", "memory")
pr = vl.dataProvider()

with edit(vl):
    #Add fields
    pr.addAttributes([QgsField("Name", QVariant.String),QgsField("Source", QVariant.String),QgsField("crs", QVariant.String)])
    vl.updateFields()
    for lyr in QgsProject.instance().mapLayers().values():
        #For each raster layer added to the map write data to table
        if type(lyr) == qgis._core.QgsRasterLayer:
            info = [lyr.name(), lyr.source(), lyr.crs().authid()]
            f = QgsFeature()
            f.setAttributes(info)
            (res, outFeats) = pr.addFeatures([f])
    
#Add table to map
QgsProject.instance().addMapLayer(vl)

enter image description here

Correct answer by BERA on October 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP