PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
rtrowdump.py
Go to the documentation of this file.
1#! /usr/bin/env python
2#
3#
4# Brute-force dump of single row from WKT Raster table as GeoTIFF.
5# This utility is handy for debugging purposes.
6#
7# WARNING: The main purpose of this program is to test and
8# debug WKT Raster implementation. It is NOT supposed to be an
9# efficient performance killer, by no means.
10#
11
28import rtreader
29import numpy
30import osgeo.gdalconst
31from osgeo import gdal
32from optparse import OptionParser
33import sys
34
35def logit(msg):
36 if VERBOSE is True:
37 sys.stderr.write("LOG - " + str(msg) + "\n")
38
39def pt2gdt(pt):
40 """Translate WKT Raster pixel type to GDAL type"""
41 pixtypes = {
42 '8BUI' : osgeo.gdalconst.GDT_Byte,
43 '16BSI' : osgeo.gdalconst.GDT_Int16,
44 '16BUI' : osgeo.gdalconst.GDT_UInt16,
45 '32BSI' : osgeo.gdalconst.GDT_Int32,
46 '32BUI' : osgeo.gdalconst.GDT_UInt32,
47 '32BF' : osgeo.gdalconst.GDT_Float32,
48 '64BF' : osgeo.gdalconst.GDT_Float64
49 }
50 if hasattr(osgeo.gdalconst, 'GDT_Float16'):
51 pixtypes['16BF'] = osgeo.gdalconst.GDT_Float16
52 return pixtypes.get(pt, 'UNKNOWN')
53
54def pt2numpy(pt):
55 """Translate WKT Raster pixel type to NumPy data type"""
56 numpytypes = {
57 '8BUI' : numpy.uint8,
58 '16BSI' : numpy.int16,
59 '16BUI' : numpy.uint16,
60 '32BSI' : numpy.int32,
61 '32BUI' : numpy.uint32,
62 '32BF' : numpy.float32,
63 '64BF' : numpy.float64
64 }
65 if hasattr(numpy, 'float16'):
66 numpytypes['16BF'] = numpy.float16
67 return numpytypes.get(pt, numpy.uint8)
68
69
70try:
71
72 prs = OptionParser(version="%prog $Revision: 4037 $",
73 usage="%prog -d <DB> -t <TABLE> [-c <COLUMN>]",
74 description="Brute-force dump of single row from WKT Raster table as GeoTIF")
75 prs.add_option("-d", "--db", dest="db", action="store", default=None,
76 help="PostgreSQL database connection string, required")
77 prs.add_option("-t", "--table", dest="table", action="store", default=None,
78 help="table with raster column [<schema>.]<table>, required")
79 prs.add_option("-c", "--column", dest="column", action="store", default="rast",
80 help="raster column, optional, default=rast")
81 prs.add_option("-w", "--where", dest="where", action="store", default="",
82 help="SQL WHERE clause to filter record")
83 prs.add_option("-o", "--output", dest="output", action="store", default=None,
84 help="GeoTIFF output file for pixel data read from WKT Raster table")
85 prs.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
86 help="be excessively verbose and useful for debugging")
87
88 (opts, args) = prs.parse_args()
89
90 if opts.db is None:
91 prs.error("use -d option to specify database connection string")
92 if opts.table is None:
93 prs.error("use -t option to specify raster table")
94 if opts.column is None:
95 prs.error("use -c option to specify raster column in raster table")
96 if opts.output is None:
97 prs.error("use -o option to specify raster output file")
98
99 global VERBOSE
100 VERBOSE = opts.verbose
101
102 rt = rtreader.RasterReader(opts.db, opts.table, opts.column, opts.where)
103 if VERBOSE is True:
104 rt.logging = True
105
106 logit("Connected to %s" % opts.db)
107 logit("Source WKT raster:")
108 logit("\trow=%s" % opts.where)
109 logit("\twidth=%d, height=%d, bands=%d, pixel types=%s" \
110 %(rt.width, rt.height, rt.num_bands, str(rt.pixel_types)))
111 logit("Target GeoTIFF: %s" % opts.output)
112
113 out_format = "GTiff"
114 out_driver = gdal.GetDriverByName(out_format)
115 out_data_type = pt2gdt(rt.pixel_types[0])
116 out_ds = out_driver.Create(opts.output, rt.width, rt.height, rt.num_bands, out_data_type)
117
118
119 for b in range(1, rt.num_bands +1):
120 logit("--- BAND %d ---------------------------------" % b)
121
122
125 raster = numpy.zeros((rt.height, rt.width), pt2numpy(out_data_type))
126 for width_index in range(0, rt.width):
127 for height_index in range(0, rt.height):
128 pixel = rt.get_value(b, width_index + 1, height_index + 1)
129 raster[height_index, width_index] = pixel
130
131 logit(str(raster))
132
133 band = out_ds.GetRasterBand(b)
134 assert band is not None
135 band.WriteArray(raster)
136
137except rtreader.RasterError as e:
138 print("ERROR - ", e)
RASTER driver (read-only)
Definition rtreader.py:37
#define str(s)
pt2gdt(pt)
Definition rtrowdump.py:39
pt2numpy(pt)
Definition rtrowdump.py:54
logit(msg)
Definition rtrowdump.py:35