root/spike/wktraster/scripts/window.py

Revision 4119, 2.7 KB (checked in by pracine, 3 years ago)

[wktraster] Fixed ticket 150. We now store GDAL style georeference information: X & Y of upper left CORNER of upper left pixel instead of the CENTER of the upper left pixel.

Also renamed RT_GeoReference() to RT_GdalGeoTransform() and added RT_ESRIWorldFile().

  • Property svn:executable set to *
  • Property svn:keywords set to
    Id
    Revision
Line 
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##############################################################################
14from osgeo import gdal
15from osgeo import osr
16import osgeo.gdalconst as gdalc
17import sys
18
19if 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
28def 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
34def 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
43infile = sys.argv[1]
44inxoff = int(sys.argv[2])
45inyoff = int(sys.argv[3])
46inxsize = int(sys.argv[4])
47inysize = int(sys.argv[5])
48print "=== INPUT ==="
49print "File: %s" % infile
50print "Window:"
51print "- upper-left: %d x %d" % (inxoff, inyoff)
52print "- dimensions: %d x %d" % (inxsize, inysize)
53
54ds = gdal.Open(infile, gdalc.GA_ReadOnly);
55if ds is None:
56    sys.exit('Cannot open input file: ' + str(infile))
57
58xsize = ds.RasterXSize
59ysize = ds.RasterYSize
60print "=== RASTER ==="
61print "- dimensions: %d x %d" % (xsize, ysize)
62
63if inxsize > xsize or inysize > ysize or inxoff > xsize or inyoff > ysize:
64    print "Invalid size of input window"
65    sys.exit(1)
66
67gt = ds.GetGeoTransform()
68res = ( gt[1], gt[5] ) # X/Y pixel resolution
69ulp = ( gt[0], gt[3] ) # X/Y upper left pixel corner
70rot = ( gt[2], gt[4] ) # X-/Y-axis rotation
71
72if is_georeferenced(gt):
73    print "- pixel size:", res
74    print "- upper left:", ulp
75    print "- rotation  :", rot
76else:
77    print "No georeferencing is available"
78    sys.exit(1)
79
80print "=== WINDOW ==="
81print "- upper-left :", calculate_corner(gt, inxoff, inyoff)
82print "- lower-left :", calculate_corner(gt, inxoff, ysize)
83print "- upper-right:", calculate_corner(gt, xsize, inyoff)
84print "- lower-right:", calculate_corner(gt, xsize, ysize)
85print "- center     :", calculate_corner(gt, xsize/2, ysize/2)
Note: See TracBrowser for help on using the browser.