PostGIS  2.1.10dev-r@@SVN_REVISION@@
rtgdalraster.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 #
3 # $Id$
4 #
5 # A simple utility for passing parameters to ST_AsGDALRaster
6 # for a GDAL raster output
7 # This utility is handy for debugging purposes.
8 #
9 # Example:
10 # rtgdalraster.py -d "dbname=postgres" -r "(SELECT rast FROM pele LIMIT 1)" -f JPEG -o pele.jpg
11 #
12 #
13 ###############################################################################
14 # Copyright (C) 2011 Regents of the University of California
15 # <bkpark@ucdavis.edu>
16 # Copyright (C) 2009 Mateusz Loskot <mateusz@loskot.net>
17 #
18 # This program is free software; you can redistribute it and/or
19 # modify it under the terms of the GNU General Public License
20 # as published by the Free Software Foundation; either version 2
21 # of the License, or (at your option) any later version.
22 #
23 # This program is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
27 #
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software Foundation,
30 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #
32 ###############################################################################
33 from optparse import OptionParser
34 import sys
35 import os
36 import psycopg2
37 
38 def logit(msg):
39  if VERBOSE is True:
40  sys.stderr.write("LOG - " + str(msg) + "\n")
41 
42 ###############################################################################
43 try:
44  prs = OptionParser(version="%prog",
45  usage="%prog -d <DB> -r <RASTER> -o <FILE> [-f <FORMAT>] [-c <OPTIONS>]",
46  description="Output raster in a GDAL raster format")
47  prs.add_option("-d", "--db", dest="db", action="store", default=None,
48  help="PostgreSQL database connection string, required")
49  prs.add_option("-r", "--raster", dest="raster", action="store", default=None,
50  help="sql expression to create or access a existing raster, required")
51  prs.add_option("-o", "--output", dest="output", action="store", default=None,
52  help="file to put the output of ST_AsGDALRaster if successful, required")
53  prs.add_option("-f", "--format", dest="format", action="store", default="GTiff",
54  help="format of GDAL raster output, optional, default=GTiff")
55  prs.add_option("-c", "--config", dest="config", action="store", default=None,
56  help="comma separated list of GDAL raster creation options, optional")
57  prs.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
58  help="be excessively verbose and useful for debugging")
59 
60  (opts, args) = prs.parse_args()
61 
62  if opts.db is None:
63  prs.error("use -d option to specify database connection string")
64  if opts.raster is None:
65  prs.error("use -r option to specify a sql expression for a raster")
66  if opts.output is None:
67  prs.error("use -o option to specify raster output file")
68 
69  if opts.config is not None:
70  opts.cfg = opts.config.split(',')
71  else:
72  opts.cfg = None
73 
74  global VERBOSE
75  VERBOSE = opts.verbose
76 
77  conn = psycopg2.connect(opts.db)
78  logit("Connected to %s" % opts.db)
79  logit("Raster expression is %s" % opts.raster)
80 
81  cur = conn.cursor()
82  sql = "SELECT ST_AsGDALRaster(%s, %%s, %%s::text[], NULL::integer)" % (opts.raster)
83  cur.execute(sql, (opts.format, opts.cfg))
84  logit("Number of rows: %i" % cur.rowcount)
85  rec = cur.fetchone()[0];
86  logit("size of raster (bytes): %i" % len(rec))
87 
88  fh = open(opts.output, 'wb', -1)
89  fh.write(rec);
90  fh.flush();
91  fh.close();
92  logit("size of %s (bytes): %i" % (opts.output, os.stat(opts.output)[6]));
93 
94  cur.close();
95  conn.close();
96 
97  print "raster outputted to %s" % opts.output;
98 
99 except Exception, e:
100  print "ERROR: ", e
def logit(msg)
Definition: rtgdalraster.py:38