]> granicus.if.org Git - postgis/commitdiff
Added RasterReader.copy_to() utility based on SQL command COPY TO and PostGIS Raster...
authorMateusz Loskot <mateusz@loskot.net>
Thu, 6 Dec 2012 17:39:04 +0000 (17:39 +0000)
committerMateusz Loskot <mateusz@loskot.net>
Thu, 6 Dec 2012 17:39:04 +0000 (17:39 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@10805 b70326c6-7e19-0410-871a-916f4a2858ee

raster/scripts/python/rtreader.py

index 4a8f3fbc9a13e23e1452ca87c454baf10e78e0ea..a722c221fd0f4f0598d0b7816efc3596a6317e12 100755 (executable)
@@ -74,6 +74,39 @@ class RasterReader(object):
     def get_value(self, band, x, y):
         return self._query_value(band, x, y)
 
+    def copy_to(self, file, raster_format='TIFF', output_format='HEX', sep='\t'):
+        """
+        Proxy for SQL command COPY TO,
+        Converts selected rasters to specified raster_format with output sent either to 
+        single hex-based plain text file or one or more binary files in raster_format,
+        one raster binary file per tuple from the raster table.
+        The BIN output uses HEX output as intermediate stage.
+        raster_format - TIFF|JPEG|PNG
+        output_format - HEX|BIN; BIN is a binary file in raster_format
+        sep - if output_format=HEX, separates rid value from hex-encoded binary.
+        """
+        import os.path
+        filehex = file # No extension added, may be user-defiened
+        with open(filehex, 'w') as f:
+            select = "SELECT rid, encode(ST_As%s(%s), 'hex') As rt FROM %s" % (raster_format, self._column, self._table)
+            if self._where is not None and len(self._where) > 0:
+                select += ' WHERE %s' % self._where
+            sql = "COPY (%s) TO STDOUT (DELIMITER '%s')" % (select, sep)        
+            cur = self._conn.cursor()
+            cur.copy_expert(sql, f)
+
+        if output_format == 'BIN':
+            import binascii
+            with open(filehex, 'r') as f:
+                dirname = os.path.dirname(file)
+                ext = raster_format.lower()
+                for line in f.readlines():
+                    rid, raster = line.split()
+                    filebin = self._table + '_' + self._column + '_' + rid + '.' + ext
+                    filebin = os.path.join(dirname, filebin)
+                    with open(filebin, 'w+') as fbin:
+                        fbin.write(binascii.unhexlify(raster))
+
     # Private methods
 
     def _log(self, m):
@@ -103,7 +136,7 @@ class RasterReader(object):
         try:
             if self._conn is None:
                 self._conn = psycopg2.connect(self._connstr)
-        except Exception, e:
+        except Exception as e:
             raise RasterError("Falied to connect to %s: %s" % (self._connstr, e))
 
     def _query_single_row(self, sql):
@@ -113,9 +146,8 @@ class RasterReader(object):
         try:
             cur = self._conn.cursor()
             cur.execute(sql)
-        except Exception, e:
-            raise RasterError("Failed to execute query %s: %s" % (sql,
-                        e))
+        except Exception as e:
+            raise RasterError("Failed to execute query %s: %s" % (sql, e))
 
         row = cur.fetchone()
         if row is None: