File: rtgdalraster.py

package info (click to toggle)
postgis 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 70,052 kB
  • sloc: ansic: 162,204; sql: 93,950; xml: 53,121; cpp: 12,646; perl: 5,658; sh: 5,369; makefile: 3,434; python: 1,205; yacc: 447; lex: 151; pascal: 58
file content (99 lines) | stat: -rwxr-xr-x 3,717 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /usr/bin/env python
#
#
# A simple utility for passing parameters to ST_AsGDALRaster
# for a GDAL raster output
# This utility is handy for debugging purposes.
#
# Example:
#   rtgdalraster.py -d "dbname=postgres" -r "(SELECT rast FROM pele LIMIT 1)" -f JPEG -o pele.jpg
#
#
###############################################################################
# Copyright (C) 2011 Regents of the University of California
#   <bkpark@ucdavis.edu>
# Copyright (C) 2009 Mateusz Loskot <mateusz@loskot.net>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
###############################################################################
from __future__ import print_function
from optparse import OptionParser
import sys
import os
import psycopg2

def logit(msg):
	if VERBOSE is True:
		sys.stderr.write("LOG - " + str(msg) + "\n")

###############################################################################
try:
	prs = OptionParser(version="%prog",
		usage="%prog -d <DB> -r <RASTER> -o <FILE> [-f <FORMAT>] [-c <OPTIONS>]",
		description="Output raster in a GDAL raster format")
	prs.add_option("-d", "--db", dest="db", action="store", default=None,
		help="PostgreSQL database connection string, required")
	prs.add_option("-r", "--raster", dest="raster", action="store", default=None,
		help="sql expression to create or access a existing raster, required")
	prs.add_option("-o", "--output", dest="output", action="store", default=None,
		help="file to put the output of ST_AsGDALRaster if successful, required")
	prs.add_option("-f", "--format", dest="format", action="store", default="GTiff",
		help="format of GDAL raster output, optional, default=GTiff")
	prs.add_option("-c", "--config", dest="config", action="store", default=None,
		help="comma separated list of GDAL raster creation options, optional")
	prs.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
		help="be excessively verbose and useful for debugging")

	(opts, args) = prs.parse_args()

	if opts.db is None:
		prs.error("use -d option to specify database connection string")
	if opts.raster is None:
		prs.error("use -r option to specify a sql expression for a raster")
	if opts.output is None:
		prs.error("use -o option to specify raster output file")

	if opts.config is not None:
		opts.cfg = opts.config.split(',')
	else:
		opts.cfg = None

	global VERBOSE
	VERBOSE = opts.verbose

	conn = psycopg2.connect(opts.db)
	logit("Connected to %s" % opts.db)
	logit("Raster expression is %s" % opts.raster)

	cur = conn.cursor()
	sql = "SELECT ST_AsGDALRaster(%s, %%s, %%s::text[], NULL::integer)" % (opts.raster)
	cur.execute(sql, (opts.format, opts.cfg))
	logit("Number of rows: %i" % cur.rowcount)
	rec = cur.fetchone()[0];
	logit("size of raster (bytes): %i" % len(rec))

	fh = open(opts.output, 'wb', -1)
	fh.write(rec);
	fh.flush();
	fh.close();
	logit("size of %s (bytes): %i" % (opts.output, os.stat(opts.output)[6]));

	cur.close();
	conn.close();

	print("raster outputted to %s" % opts.output);

except Exception as e:
    print("ERROR: ", e)