#! /usr/bin/env python
#
# $Id$
#
# Brute-force dump of all pixels of all bands in WKT Raster field/row to text.
# This utility is handy for debugging purposes.
#
# 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 3 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 St, Fifth Floor, Boston, MA  02110-1301  USA
#
###############################################################################
# BEGIN CONFIGURATION
rt_connstr = "dbname='test' host='localhost' user='mloskot'" # PostgreSQL format
rt_table = "mytable";   # Name of table with RASTER column
rt_column = "rast";     # Name of RASTER column in the table 
rt_where = None         # SQL WHERE clause, leave None if you don't need
# END CONFIGURATION
###############################################################################
import rtreader
import sys

if len(sys.argv) != 1:
    print "Usage:"
    print "1. Edit CONFIGURATION block"
    print "2. Run rtpixdump.py without any input parameters"
    sys.exit(1)

def logit(msg):
    sys.stderr.write("LOG - " + msg + "\n")

try:
    rast = rtreader.RasterReader(rt_connstr, rt_table, rt_column)
    logit("Connected to %s" % rt_connstr)
    logit("Raster width=%d, height=%d, bands=%d" %(rast.width, rast.height, rast.num_bands))
    
    for band in range(1, rast.num_bands + 1):
        sys.stderr.write("--- BAND %d ---------------------------------\n" % band)
        for y in range(1, rast.height + 1):
            scanline = ""
            for x in range(1, rast.width + 1):
                scanline += str(int(rast.get_value(band, x, y))) + '\t'
            print scanline
        print # Bands separator

except rtreader.RasterError, e:
    print "ERROR - ", e
