Skip to content

Instantly share code, notes, and snippets.

@alpha-beta-soup
Created May 10, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpha-beta-soup/41b84387be8497c2c4ee to your computer and use it in GitHub Desktop.
Save alpha-beta-soup/41b84387be8497c2c4ee to your computer and use it in GitHub Desktop.
Click on a raster layer in the QGIS table of contents, and then run this script. The corners of the bounding box of the layer will be printed to the console in the project's projection system. This is useful for supplying these values to Leaflet when using `imageOverlay`, which requires the top left and bottom right coordinates.
from osgeo import gdal, osr
# Click on your layer in the TOC
alayer = qgis.utils.iface.activeLayer()
bag = gdal.Open(alayer.source())
bag_gtrn = bag.GetGeoTransform()
bag_proj = bag.GetProjectionRef()
bag_srs = osr.SpatialReference(bag_proj)
geo_srs =bag_srs.CloneGeogCS()
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment