2

How can we determine the coordinates of the corners of the raster layer?

The image opens function QgsRasterLayer.

3

I have just found the following snippet, it might be what you are looking for:

from osgeo import gdal, osr

bag = gdal.Open('F00574_MB_2m_MLLW_2of3.bag')  # replace it with your file
                                               # raster is projected
bag_gtrn = bag.GetGeoTransform()
bag_proj = bag.GetProjectionRef()
bag_srs = osr.SpatialReference(bag_proj)
geo_srs =bag_srs.CloneGeogCS()                 # new srs obj to go from x,y -> φ,λ
transform = osr.CoordinateTransformation( bag_srs, geo_srs)

bag_bbox_cells = (
    (0., 0.),
    (0, bag.RasterYSize),
    (bag.RasterXSize, bag.RasterYSize),
    (bag.RasterXSize, 0),
  )

geo_pts = []
for x, y in bag_bbox_cells:
    x2 = bag_gtrn[0] + bag_gtrn[1] * x + bag_gtrn[2] * y
    y2 = bag_gtrn[3] + bag_gtrn[4] * x + bag_gtrn[5] * y
    geo_pt = transform.TransformPoint(x2, y2)[:2]
    geo_pts.append(geo_pt)
    print x, y, '->', x2, y2, '->', geo_pt

The results of this code are:

# Pixel Coord ->     Proj Coords    ->    (φ,λ) coords
0.0 0.0       -> 369179.0 4775259.0 -> (-70.608087740520943, 43.118768822635147)                                
0.0 1083      -> 369179.0 4773093.0 -> (-70.607577288471774, 43.099271863165256)                               
1841 1083     -> 372861.0 4773093.0 -> (-70.56234820699548, 43.099898472783011)                               
1841 0.0      -> 372861.0 4775259.0 -> (-70.562844311963829, 43.119395856927362)
1

When I do not know a variable in Python, I use the command dir(variable) to examine the options available in the class from which it is derived

and thus in this case:

In QGIS 1.8

rlayer = qgis.utils.iface.activeLayer()
ext =rlayer.extent()
dir(ext)
 [...,'asPolygon', 'asWktCoordinates', 'center', ...,'height',...,'width', 'xMaximum', 'xMinimum',
 'yMaximum', 'yMinimum']

so I try what seems interesting:

ext.asPolygon()
Qt4.QtCore.QString(u'162012.44098000 158167.46835000, 162012.44098000 168174.76215000, 178013.37218000 168174.76215000, 178013.37218000 158167.46835000, 162012.44098000 158167.46835000')

gives you the coordinates of the corners of the raster layer

In QGIS master

ext =rlayer.extent()
dir(ext)
[...,'.asPolygon', 'asWktCoordinates', 'asWktPolygon', 'center',...height', ..., 'width',...,
'xMaximum', 'xMinimum', 'yMaximum', 'yMinimum']

same approach

l.asWktPolygon()
PyQt4.QtCore.QString(u'POLYGON((162012.4409800000139512 158167.468349999981001, 178013.3721800000057556 158167.468349999981001, 178013.3721800000057556 168174.7621499999950174, 162012.4409800000139512 168174.7621499999950174, 162012.4409800000139512 158167.468349999981001))')
# or
l.asPolygon()
PyQt4.QtCore.QString(u'162012.44098000 158167.46835000, 162012.44098000 168174.76215000, 178013.37218000 168174.76215000, 178013.37218000 158167.46835000, 162012.44098000 158167.46835000')

gives you the coordinates of the corners of the raster layer and you can use the other elements

l.xMinimum(),l.yMinimum(),l.xMaximum(), l.yMaximum()
(162012.44098000001, 158167.46834999998, 178013.37218000001, 168174.76215)
l.center()
(170013,163171)

It may take time but it allows you to learn the class

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.