44 """Reader of RASTER data stored in specified column and row (where) in a table""" 48 def __init__(self, connstr, table, column, where = ""):
62 logging = property(fset =
lambda self, v: setattr(self,
'_logging', v))
63 db = property(fget =
lambda self: self.
_get_db())
64 table = property(fget =
lambda self: self.
_table)
65 column = property(fget =
lambda self: self.
_column)
76 def copy_to(self, file, raster_format='TIFF', output_format='HEX', sep='\t'):
78 Proxy for SQL command COPY TO, 79 Converts selected rasters to specified raster_format with output sent either to 80 single hex-based plain text file or one or more binary files in raster_format, 81 one raster binary file per tuple from the raster table. 82 The BIN output uses HEX output as intermediate stage. 83 raster_format - TIFF|JPEG|PNG 84 output_format - HEX|BIN; BIN is a binary file in raster_format 85 sep - if output_format=HEX, separates rid value from hex-encoded binary. 89 with open(filehex,
'w')
as f:
90 select =
"SELECT rid, encode(ST_As%s(%s), 'hex') As rt FROM %s" % (raster_format, self.
_column, self.
_table)
92 select +=
' WHERE %s' % self.
_where 93 sql =
"COPY (%s) TO STDOUT (DELIMITER '%s')" % (select, sep)
94 cur = self.
_conn.cursor()
95 cur.copy_expert(sql, f)
97 if output_format ==
'BIN':
99 with open(filehex,
'r') as f: 100 dirname = os.path.dirname(file) 101 ext = raster_format.lower() 102 for line
in f.readlines():
103 rid, raster = line.split()
104 filebin = self.
_table +
'_' + self.
_column +
'_' + rid +
'.' + ext
105 filebin = os.path.join(dirname, filebin)
106 with open(filebin,
'w+')
as fbin:
107 fbin.write(binascii.unhexlify(raster))
113 sys.stderr.write(
'[rtreader] ' + str(m) +
'\n')
116 n = filter(
lambda db: db[:6] ==
'dbname', self.
_connstr.split())[0].split(
'=')[1]
117 return n.strip(
'\'').strip()
119 def _get_width(self):
122 def _get_height(self):
125 def _get_num_bands(self):
128 def _get_pixel_types(self):
136 if self.
_conn is None:
138 except Exception
as e:
141 def _query_single_row(self, sql):
142 assert self.
_conn is not None 146 cur = self.
_conn.cursor()
148 except Exception
as e:
149 raise RasterError(
"Failed to execute query %s: %s" % (sql, e))
153 raise RasterError(
"No tupes returned for query: %s" % sql)
156 def _query_value(self, band, x, y):
157 sql =
'SELECT st_value(%s, %d, %d, %d) FROM %s' % \
160 sql +=
' WHERE %s' % self.
_where 164 raise RasterError(
"Value of pixel %dx%d of band %d is none" %(x, y, band))
167 def _query_raster_size(self, dim, force = False):
168 if self.
_sizes is None or force
is True:
169 sql =
'SELECT st_width(%s), st_height(%s), st_numbands(%s) FROM %s' % \
172 sql +=
' WHERE %s' % self.
_where 178 raise RasterError(
"Falied to query %dx%d of band %d is none" %(x, y, band))
181 def _query_pixel_types(self):
189 sql +=
' st_bandpixeltype(%s, %d) ' % (self.
_column, nband)
190 sql +=
' FROM ' + self.
_table
def _query_raster_size(self, dim, force=False)
def _query_pixel_types(self)
def copy_to(self, file, raster_format='TIFF', output_format='HEX', sep='\t')
def _query_value(self, band, x, y)
def __init__(self, connstr, table, column, where="")
def _query_single_row(self, sql)
def get_value(self, band, x, y)
RASTER driver (read-only)
def _get_pixel_types(self)