I'm trying to develop a QGIS plugin for converting the input raster to QgsRasterLayer type and, however, I don't know how to get all the header information to write it in the output raster.

This is an example of the beginning of the input raster:

ncols        1914
nrows        1316
xllcorner    511542.500000000000
yllcorner    4660867.500000000000
cellsize     5.000000000000
NODATA_value  -999
-999.0 -999 -999 -999 -999...

I know how to get cols, rows and nodatavalue but I need to know how to get xllcorner, yllcorner and cellsize too.

This is the code that I wrote for that:

def inversion(self):


    cols = lyr.width()
    rows = lyr.height()
    dpr = lyr.dataProvider()
    bl = dpr.block(1, dpr.extent(), cols, rows)

    elevation = []
    for i in range(rows):
        elevationline = []
        for j in range(cols):
            elevationline.append(bl.value(i, j))


I tried out following code at Python Console of QGIS for printing values you need:

lyr = iface.activeLayer()

xsize = lyr.rasterUnitsPerPixelX() #x cell size
ysize = lyr.rasterUnitsPerPixelY() #y cell size

print(xsize, ysize)

extent = lyr.extent()

xmin, ymin, xmax, ymax = extent.toRectF().getCoords()

xllcorner = xmin
yllcorner = ymax

Result, with my particular raster, was:

73.98872322814125 73.9887232281416
354971.3488602247 4473428.402390091

So, you can adapt above code for your plugin.

  • Thank you. I adapted the code for my plugin and now I have xllcorner, yllcorner and cellsize. I only needed to change yllcorner=ymin – scf Dec 27 '18 at 17:18
  • You're welcome. If you think your qüestion was answered please mark as accepted. – xunilk Dec 27 '18 at 19:26

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.