44 """Reader of RASTER data stored in specified column and row (where) in a table"""
48 def __init__(self, connstr, table, column, where = ""):
50 self.
_conn_conn =
None
62 logging = property(fset =
lambda self, v: setattr(self,
'_logging', v))
63 db = property(fget =
lambda self: self.
_get_db_get_db())
64 table = property(fget =
lambda self: self.
_table_table)
65 column = property(fget =
lambda self: self.
_column_column)
66 width = property(fget =
lambda self: self.
_get_width_get_width())
67 height = property(fget =
lambda self: self.
_get_height_get_height())
68 num_bands = property(fget =
lambda self: self.
_get_num_bands_get_num_bands())
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_column, self.
_table_table)
91 if self.
_where_where
is not None and len(self.
_where_where) > 0:
92 select +=
' WHERE %s' % self.
_where_where
93 sql =
"COPY (%s) TO STDOUT (DELIMITER '%s')" % (select, sep)
94 cur = self.
_conn_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_table +
'_' + self.
_column_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_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_conn
is None:
138 except Exception
as e:
141 def _query_single_row(self, sql):
142 assert self.
_conn_conn
is not None
146 cur = self.
_conn_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' % \
159 if len(self.
_where_where) > 0:
160 sql +=
' WHERE %s' % self.
_where_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_sizes
is None or force
is True:
169 sql =
'SELECT st_width(%s), st_height(%s), st_numbands(%s) FROM %s' % \
171 if len(self.
_where_where) > 0:
172 sql +=
' WHERE %s' % self.
_where_where
177 if self.
_sizes_sizes
is None:
178 raise RasterError(
"Falied to query %dx%d of band %d is none" %(x, y, band))
179 return self.
_sizes_sizes[dim]
181 def _query_pixel_types(self):
185 for i
in range(0, self.
num_bandsnum_bands):
189 sql +=
' st_bandpixeltype(%s, %d) ' % (self.
_column_column, nband)
190 sql +=
' FROM ' + self.
_table_table
RASTER driver (read-only)
def __init__(self, connstr, table, column, where="")
def copy_to(self, file, raster_format='TIFF', output_format='HEX', sep='\t')
def get_value(self, band, x, y)
def _query_single_row(self, sql)
def _query_value(self, band, x, y)
def _query_pixel_types(self)
def _query_raster_size(self, dim, force=False)
def _get_pixel_types(self)