PostGIS  3.0.6dev-r@@SVN_REVISION@@
rtpixdump.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 #
3 #
4 # Brute-force dump of all pixels of all bands in WKT Raster field/row to text.
5 # This utility is handy for debugging purposes.
6 #
7 # Copyright (C) 2009 Mateusz Loskot <mateusz@loskot.net>
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software Foundation,
21 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #
23 
24 from __future__ import print_function
25 import rtreader
26 from optparse import OptionParser
27 import sys
28 
29 def logit(msg):
30  if VERBOSE is True:
31  sys.stderr.write("LOG - " + msg + "\n")
32 
33 
34 try:
35 
36  prs = OptionParser(version="%prog $Revision$",
37  usage="%prog -d <DB> -t <TABLE> [-c <COLUMN>]",
38  description="Brute-force dump of all pixel values of WKT Raster dataset")
39  prs.add_option("-d", "--db", dest="db", action="store", default=None,
40  help="PostgreSQL database connection string, required")
41  prs.add_option("-t", "--table", dest="table", action="store", default=None,
42  help="table with raster column [<schema>.]<table>, required")
43  prs.add_option("-c", "--column", dest="column", action="store", default="rast",
44  help="raster column, optional, default=rast")
45  prs.add_option("-w", "--where", dest="where", action="store", default=None,
46  help="SQL WHERE clause to filter record - NOT IMPLEMENTED")
47  prs.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
48  help="be excessively verbose and useful for debugging")
49 
50  (opts, args) = prs.parse_args()
51 
52  if opts.db is None:
53  prs.error("use -d option to specify database connection string")
54  if opts.table is None:
55  prs.error("use -t option to specify raster table")
56  if opts.column is None:
57  prs.error("use -c option to specify raster column in raster table")
58 
59  global VERBOSE
60  VERBOSE = opts.verbose
61 
62  rast = rtreader.RasterReader(opts.db, opts.table, opts.column)
63  logit("Connected to %s" % opts.db)
64  logit("Raster width=%d, height=%d, bands=%d" %(rast.width, rast.height, rast.num_bands))
65 
66  for band in range(1, rast.num_bands + 1):
67  logit("--- BAND %d ---------------------------------" % band)
68  sys.stderr.write("\n")
69  for y in range(1, rast.height + 1):
70  scanline = ""
71  for x in range(1, rast.width + 1):
72  scanline += str(int(rast.get_value(band, x, y))) + '\t'
73  print(scanline)
74  print() # Bands separator
75 
76 except rtreader.RasterError as e:
77  print("ERROR - ", e)
RASTER driver (read-only)
Definition: rtreader.py:37
#define str(s)
def logit(msg)
Definition: rtpixdump.py:29