31 from optparse
import OptionParser
38 sys.stderr.write(
"LOG - " + str(msg) +
"\n")
42 prs = OptionParser(version=
"%prog",
43 usage=
"%prog -d <DB> -r <RASTER> -o <FILE> [-f <FORMAT>] [-c <OPTIONS>]",
44 description=
"Output raster in a GDAL raster format")
45 prs.add_option(
"-d",
"--db", dest=
"db", action=
"store", default=
None,
46 help=
"PostgreSQL database connection string, required")
47 prs.add_option(
"-r",
"--raster", dest=
"raster", action=
"store", default=
None,
48 help=
"sql expression to create or access a existing raster, required")
49 prs.add_option(
"-o",
"--output", dest=
"output", action=
"store", default=
None,
50 help=
"file to put the output of ST_AsGDALRaster if successful, required")
51 prs.add_option(
"-f",
"--format", dest=
"format", action=
"store", default=
"GTiff",
52 help=
"format of GDAL raster output, optional, default=GTiff")
53 prs.add_option(
"-c",
"--config", dest=
"config", action=
"store", default=
None,
54 help=
"comma separated list of GDAL raster creation options, optional")
55 prs.add_option(
"-v",
"--verbose", dest=
"verbose", action=
"store_true", default=
False,
56 help=
"be excessively verbose and useful for debugging")
58 (opts, args) = prs.parse_args()
61 prs.error(
"use -d option to specify database connection string")
62 if opts.raster
is None:
63 prs.error(
"use -r option to specify a sql expression for a raster")
64 if opts.output
is None:
65 prs.error(
"use -o option to specify raster output file")
67 if opts.config
is not None:
68 opts.cfg = opts.config.split(
',')
73 VERBOSE = opts.verbose
75 conn = psycopg2.connect(opts.db)
76 logit(
"Connected to %s" % opts.db)
77 logit(
"Raster expression is %s" % opts.raster)
80 sql =
"SELECT ST_AsGDALRaster(%s, %%s, %%s::text[], NULL::integer)" % (opts.raster)
81 cur.execute(sql, (opts.format, opts.cfg))
82 logit(
"Number of rows: %i" % cur.rowcount)
83 rec = cur.fetchone()[0];
84 logit(
"size of raster (bytes): %i" % len(rec))
86 fh = open(opts.output,
'wb', -1)
90 logit(
"size of %s (bytes): %i" % (opts.output, os.stat(opts.output)[6]));
95 print "raster outputted to %s" % opts.output;