| 1 | #! /usr/bin/env python |
|---|
| 2 | # |
|---|
| 3 | # $Id$ |
|---|
| 4 | # |
|---|
| 5 | # Calculates coordinates of window corners of given raster dataset. |
|---|
| 6 | # It's just a simple helper for testing and debugging WKT Raster. |
|---|
| 7 | # |
|---|
| 8 | ############################################################################## |
|---|
| 9 | # Copyright (C) 2009 Mateusz Loskot <mateusz@loskot.net> |
|---|
| 10 | # |
|---|
| 11 | # This is free software; you can redistribute and/or modify it under |
|---|
| 12 | # the terms of the GNU General Public Licence. See the COPYING file. |
|---|
| 13 | ############################################################################## |
|---|
| 14 | from osgeo import gdal |
|---|
| 15 | from osgeo import osr |
|---|
| 16 | import osgeo.gdalconst as gdalc |
|---|
| 17 | import sys |
|---|
| 18 | |
|---|
| 19 | if len(sys.argv) != 6: |
|---|
| 20 | print "Usage: window.py <raster> <x> <y> <xsize> <ysize>" |
|---|
| 21 | print "\traster - GDAL supported dataset" |
|---|
| 22 | print "\tx - column - 1..N where N is raster X dimension" |
|---|
| 23 | print "\ty - row - 1..N where N is raster Y dimension" |
|---|
| 24 | print "\txsize - x-dimension of requested window (xsize <= xsize of raster - x)" |
|---|
| 25 | print "\tysize - y-dimension of requested window (ysize <= ysize of raster - y)" |
|---|
| 26 | sys.exit(0) |
|---|
| 27 | |
|---|
| 28 | def is_georeferenced(gt): |
|---|
| 29 | if gt[0] == 0.0 and gt[1] == 1.0 and gt[3] == 0.0 and gt[5] == 1.0: |
|---|
| 30 | return False |
|---|
| 31 | else: |
|---|
| 32 | return True |
|---|
| 33 | |
|---|
| 34 | def calculate_corner(gt, x, y): |
|---|
| 35 | if is_georeferenced(gt): |
|---|
| 36 | xgeo = gt[0] + gt[1] * x + gt[2] * y |
|---|
| 37 | ygeo = gt[3] + gt[4] * x + gt[5] * y |
|---|
| 38 | else: |
|---|
| 39 | xgeo = x |
|---|
| 40 | xgeo = y |
|---|
| 41 | return (xgeo, ygeo) |
|---|
| 42 | |
|---|
| 43 | infile = sys.argv[1] |
|---|
| 44 | inxoff = int(sys.argv[2]) |
|---|
| 45 | inyoff = int(sys.argv[3]) |
|---|
| 46 | inxsize = int(sys.argv[4]) |
|---|
| 47 | inysize = int(sys.argv[5]) |
|---|
| 48 | print "=== INPUT ===" |
|---|
| 49 | print "File: %s" % infile |
|---|
| 50 | print "Window:" |
|---|
| 51 | print "- upper-left: %d x %d" % (inxoff, inyoff) |
|---|
| 52 | print "- dimensions: %d x %d" % (inxsize, inysize) |
|---|
| 53 | |
|---|
| 54 | ds = gdal.Open(infile, gdalc.GA_ReadOnly); |
|---|
| 55 | if ds is None: |
|---|
| 56 | sys.exit('Cannot open input file: ' + str(infile)) |
|---|
| 57 | |
|---|
| 58 | xsize = ds.RasterXSize |
|---|
| 59 | ysize = ds.RasterYSize |
|---|
| 60 | print "=== RASTER ===" |
|---|
| 61 | print "- dimensions: %d x %d" % (xsize, ysize) |
|---|
| 62 | |
|---|
| 63 | if inxsize > xsize or inysize > ysize or inxoff > xsize or inyoff > ysize: |
|---|
| 64 | print "Invalid size of input window" |
|---|
| 65 | sys.exit(1) |
|---|
| 66 | |
|---|
| 67 | gt = ds.GetGeoTransform() |
|---|
| 68 | res = ( gt[1], gt[5] ) # X/Y pixel resolution |
|---|
| 69 | ulp = ( gt[0], gt[3] ) # X/Y upper left pixel corner |
|---|
| 70 | rot = ( gt[2], gt[4] ) # X-/Y-axis rotation |
|---|
| 71 | |
|---|
| 72 | if is_georeferenced(gt): |
|---|
| 73 | print "- pixel size:", res |
|---|
| 74 | print "- upper left:", ulp |
|---|
| 75 | print "- rotation :", rot |
|---|
| 76 | else: |
|---|
| 77 | print "No georeferencing is available" |
|---|
| 78 | sys.exit(1) |
|---|
| 79 | |
|---|
| 80 | print "=== WINDOW ===" |
|---|
| 81 | print "- upper-left :", calculate_corner(gt, inxoff, inyoff) |
|---|
| 82 | print "- lower-left :", calculate_corner(gt, inxoff, ysize) |
|---|
| 83 | print "- upper-right:", calculate_corner(gt, xsize, inyoff) |
|---|
| 84 | print "- lower-right:", calculate_corner(gt, xsize, ysize) |
|---|
| 85 | print "- center :", calculate_corner(gt, xsize/2, ysize/2) |
|---|