32from optparse
import OptionParser
37 sys.stderr.write(
"LOG - " +
str(msg) +
"\n")
40 """Translate WKT Raster pixel type to GDAL type"""
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
50 if hasattr(osgeo.gdalconst,
'GDT_Float16'):
51 pixtypes[
'16BF'] = osgeo.gdalconst.GDT_Float16
52 return pixtypes.get(pt,
'UNKNOWN')
55 """Translate WKT Raster pixel type to NumPy data type"""
58 '16BSI' : numpy.int16,
59 '16BUI' : numpy.uint16,
60 '32BSI' : numpy.int32,
61 '32BUI' : numpy.uint32,
62 '32BF' : numpy.float32,
63 '64BF' : numpy.float64
65 if hasattr(numpy,
'float16'):
66 numpytypes[
'16BF'] = numpy.float16
67 return numpytypes.get(pt, numpy.uint8)
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")
88 (opts, args) = prs.parse_args()
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")
100 VERBOSE = opts.verbose
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)
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)
119 for b
in range(1, rt.num_bands +1):
120 logit(
"--- BAND %d ---------------------------------" % b)
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
133 band = out_ds.GetRasterBand(b)
134 assert band
is not None
135 band.WriteArray(raster)
RASTER driver (read-only)