PostGIS  3.0.6dev-r@@SVN_REVISION@@
window.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 #
3 #
4 # Calculates coordinates of window corners of given raster dataset.
5 # It's just a simple helper for testing and debugging WKT Raster.
6 #
7 
24 from __future__ import print_function
25 from osgeo import gdal
26 from osgeo import osr
27 import osgeo.gdalconst as gdalc
28 import sys
29 
30 if len(sys.argv) != 6:
31  print("Usage: window.py <raster> <x> <y> <xsize> <ysize>")
32  print("\traster - GDAL supported dataset")
33  print("\tx - column - 1..N where N is raster X dimension")
34  print("\ty - row - 1..N where N is raster Y dimension")
35  print("\txsize - x-dimension of requested window (xsize <= xsize of raster - x)")
36  print("\tysize - y-dimension of requested window (ysize <= ysize of raster - y)")
37  sys.exit(0)
38 
40  if gt[0] == 0.0 and gt[1] == 1.0 and gt[3] == 0.0 and gt[5] == 1.0:
41  return False
42  else:
43  return True
44 
45 def calculate_corner(gt, x, y):
46  if is_georeferenced(gt):
47  xgeo = gt[0] + gt[1] * x + gt[2] * y
48  ygeo = gt[3] + gt[4] * x + gt[5] * y
49  else:
50  xgeo = x
51  xgeo = y
52  return (xgeo, ygeo)
53 
54 infile = sys.argv[1]
55 inxoff = int(sys.argv[2])
56 inyoff = int(sys.argv[3])
57 inxsize = int(sys.argv[4])
58 inysize = int(sys.argv[5])
59 print("=== INPUT ===")
60 print("File: %s" % infile)
61 print("Window:")
62 print("- upper-left: %d x %d" % (inxoff, inyoff))
63 print("- dimensions: %d x %d" % (inxsize, inysize))
64 
65 ds = gdal.Open(infile, gdalc.GA_ReadOnly);
66 if ds is None:
67  sys.exit('Cannot open input file: ' + str(infile))
68 
69 xsize = ds.RasterXSize
70 ysize = ds.RasterYSize
71 print("=== RASTER ===")
72 print("- dimensions: %d x %d" % (xsize, ysize))
73 
74 if inxsize > xsize or inysize > ysize or inxoff > xsize or inyoff > ysize:
75  print("Invalid size of input window")
76  sys.exit(1)
77 
78 gt = ds.GetGeoTransform()
79 res = ( gt[1], gt[5] ) # X/Y pixel resolution
80 ulp = ( gt[0], gt[3] ) # X/Y upper left pixel corner
81 rot = ( gt[2], gt[4] ) # X-/Y-axis rotation
82 
83 if is_georeferenced(gt):
84  print("- pixel size:", res)
85  print("- upper left:", ulp)
86  print("- rotation :", rot)
87 else:
88  print("No georeferencing is available")
89  sys.exit(1)
90 
91 print("=== WINDOW ===")
92 print("- upper-left :", calculate_corner(gt, inxoff, inyoff))
93 print("- lower-left :", calculate_corner(gt, inxoff, ysize))
94 print("- upper-right:", calculate_corner(gt, xsize, inyoff))
95 print("- lower-right:", calculate_corner(gt, xsize, ysize))
96 print("- center :", calculate_corner(gt, xsize/2, ysize/2))
#define str(s)
def is_georeferenced(gt)
Definition: window.py:39
def calculate_corner(gt, x, y)
Definition: window.py:45