PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
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
24from __future__ import print_function
25from osgeo import gdal
26from osgeo import osr
27import osgeo.gdalconst as gdalc
28import sys
29
30if 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
45def 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
54infile = sys.argv[1]
55inxoff = int(sys.argv[2])
56inyoff = int(sys.argv[3])
57inxsize = int(sys.argv[4])
58inysize = int(sys.argv[5])
59print("=== INPUT ===")
60print("File: %s" % infile)
61print("Window:")
62print("- upper-left: %d x %d" % (inxoff, inyoff))
63print("- dimensions: %d x %d" % (inxsize, inysize))
64
65ds = gdal.Open(infile, gdalc.GA_ReadOnly);
66if ds is None:
67 sys.exit('Cannot open input file: ' + str(infile))
68
69xsize = ds.RasterXSize
70ysize = ds.RasterYSize
71print("=== RASTER ===")
72print("- dimensions: %d x %d" % (xsize, ysize))
73
74if inxsize > xsize or inysize > ysize or inxoff > xsize or inyoff > ysize:
75 print("Invalid size of input window")
76 sys.exit(1)
77
78gt = ds.GetGeoTransform()
79res = ( gt[1], gt[5] ) # X/Y pixel resolution
80ulp = ( gt[0], gt[3] ) # X/Y upper left pixel corner
81rot = ( gt[2], gt[4] ) # X-/Y-axis rotation
82
83if is_georeferenced(gt):
84 print("- pixel size:", res)
85 print("- upper left:", ulp)
86 print("- rotation :", rot)
87else:
88 print("No georeferencing is available")
89 sys.exit(1)
90
91print("=== WINDOW ===")
92print("- upper-left :", calculate_corner(gt, inxoff, inyoff))
93print("- lower-left :", calculate_corner(gt, inxoff, ysize))
94print("- upper-right:", calculate_corner(gt, xsize, inyoff))
95print("- lower-right:", calculate_corner(gt, xsize, ysize))
96print("- center :", calculate_corner(gt, xsize/2, ysize/2))
#define str(s)
is_georeferenced(gt)
Definition window.py:39
calculate_corner(gt, x, y)
Definition window.py:45