How can we determine the coordinates of the corners of the raster layer?
The image opens function QgsRasterLayer.
Answer
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)
No comments:
Post a Comment