| 1 | #! /usr/bin/env python |
|---|
| 2 | # |
|---|
| 3 | # $Id$ |
|---|
| 4 | # |
|---|
| 5 | # Brute-force dump of all pixels of all bands in WKT Raster field/row to text. |
|---|
| 6 | # This utility is handy for debugging purposes. |
|---|
| 7 | # |
|---|
| 8 | # Copyright (C) 2009 Mateusz Loskot <mateusz@loskot.net> |
|---|
| 9 | # |
|---|
| 10 | # This program is free software; you can redistribute it and/or modify |
|---|
| 11 | # it under the terms of the GNU General Public License as published by |
|---|
| 12 | # the Free Software Foundation; either version 3 of the License, or |
|---|
| 13 | # (at your option) any later version. |
|---|
| 14 | # |
|---|
| 15 | # This program is distributed in the hope that it will be useful, |
|---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | # GNU General Public License for more details. |
|---|
| 19 | # |
|---|
| 20 | # You should have received a copy of the GNU General Public License |
|---|
| 21 | # along with this program; if not, write to the Free Software |
|---|
| 22 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 23 | # |
|---|
| 24 | ############################################################################### |
|---|
| 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, e: |
|---|
| 77 | print "ERROR - ", e |
|---|