PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ parse_command_line()

def raster2pgsql.parse_command_line ( )
Collects, parses and validates command line arguments.

Definition at line 78 of file raster2pgsql.py.

Referenced by main().

78 def parse_command_line():
79  """Collects, parses and validates command line arguments."""
80 
81  prs = OptionParser(version="%prog $Revision: 13496 $")
82 
83  # Mandatory parameters
84  grp0 = OptionGroup(prs, "Source and destination",
85  "*** Mandatory parameters always required ***")
86  grp0.add_option("-r", "--raster", dest="raster", action="append", default=None,
87  help="append raster to list of input files, at least one raster "
88  "file required. You may use wildcards (?,*) for specifying multiple files.")
89  grp0.add_option("-t", "--table", dest="table", action="store", default=None,
90  help="raster destination in form of [<schema>.]<table> or base raster table for overview level>1, required")
91  prs.add_option_group(grp0);
92 
93  # Optional parameters - raster manipulation
94  grp_r = OptionGroup(prs, "Raster processing",
95  "Optional parameters used to manipulate input raster dataset")
96  grp_r.add_option("-s", "--srid", dest="srid", action="store", type="int", default=-1,
97  help="assign output raster with specified SRID")
98  grp_r.add_option("-b", "--band", dest="band", action="store", type="int", default=None,
99  help="specify number of the band to be extracted from raster file")
100  grp_r.add_option("-k", "--block-size", dest="block_size", action="store", default=None,
101  help="cut raster(s) into tiles to be inserted one by table row."
102  "BLOCK_SIZE is expressed as WIDTHxHEIGHT. Incomplete tiles are completed with nodata values")
103  grp_r.add_option("-R", "--register", dest="register", action="store_true", default=False,
104  help="register the raster as a filesystem (out-db) raster")
105  grp_r.add_option("-l", "--overview-level", dest="overview_level", action="store", type="int", default=1,
106  help='create overview tables named as o_<LEVEL>_<RASTER_TABLE> and '
107  'populate with GDAL-provided overviews (regular blocking only)')
108  prs.add_option_group(grp_r);
109 
110  # Optional parameters - database/table manipulation
111  grp_t = OptionGroup(prs, 'Database processing',
112  'Optional parameters used to manipulate database objects')
113  grp_t.add_option('-c', '--create', dest='create_table', action='store_true', default=False,
114  help='create new table and populate it with raster(s), this is the default mode')
115  grp_t.add_option('-a', '--append', dest='append_table', action='store_true', default=False,
116  help='append raster(s) to an existing table')
117  grp_t.add_option("-d", "--drop", dest="drop_table", action="store_true", default=False,
118  help="drop table, create new one and populate it with raster(s)")
119  grp_t.add_option("-f", "--field", dest="column", action="store", default=g_rt_column,
120  help="specify name of destination raster column, default is 'rast'")
121  grp_t.add_option("-F", "--filename", dest="filename", action="store_true", default=False,
122  help="add a column with the name of the file")
123  grp_t.add_option("-I", "--index", dest="index", action="store_true", default=False,
124  help="create a GiST index on the raster column")
125  grp_t.add_option("-M", "--vacuum", dest="vacuum", action="store_true", default=False,
126  help="issue VACUUM command against all generated tables")
127  grp_t.add_option('-V', '--create-raster-overviews', dest='create_raster_overviews_table',
128  action='store_true', default=False,
129  help='create RASTER_OVERVIEWS table used to store overviews metadata')
130  prs.add_option_group(grp_t);
131 
132  # Other optional parameters
133  grp_u = OptionGroup(prs, "Miscellaneous", "Other optional parameters")
134  grp_u.add_option("-e", "--endian", dest="endian", action="store", type="int", default=g_rt_endian,
135  help="control endianness of generated binary output of raster; "
136  "specify 0 for XDR and 1 for NDR (default); "
137  "only NDR output is supported now")
138  grp_u.add_option("-w", "--raster-version", dest="version",
139  action="store", type="int", default=g_rt_version,
140  help="specify version of raster protocol, default is 0; "
141  "only value of zero is supported now")
142  grp_u.add_option("-o", "--output", dest="output", action="store", default=sys.stdout,
143  help="specify output file, otherwise send to stdout")
144  grp_u.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
145  help="verbose mode. Useful for debugging")
146  prs.add_option_group(grp_u);
147 
148  (opts, args) = prs.parse_args()
149 
150  # Validate options
151  if opts.create_table and opts.drop_table and opts.append_table:
152  prs.error("options -c, -a and -d are mutually exclusive")
153  if opts.create_table and opts.drop_table:
154  prs.error("options -c and -d are mutually exclusive")
155  if opts.create_table and opts.append_table:
156  prs.error("options -c and -a are mutually exclusive")
157  if opts.append_table and opts.drop_table:
158  prs.error("options -a and -d are mutually exclusive")
159  if (not opts.create_table and not opts.drop_table and not opts.append_table) or opts.drop_table:
160  opts.create_table = True
161 
162  if opts.raster is None:
163  prs.error("use option -r to specify at least one input raster. Wildcards (?,*) are accepted.")
164 
165  if opts.block_size is not None and len(opts.raster) != 1:
166  prs.error("regular blocking supports single-raster input only")
167 
168  if opts.block_size is not None:
169  if len(opts.block_size.split('x')) != 2 and len(opts.block_size.split('X')) != 2:
170  prs.error("invalid format of block size, expected WIDTHxHEIGHT")
171 
172  if opts.overview_level > 1 and opts.block_size is None:
173  prs.error("regular blocking mode required to enable overviews support (level > 1)")
174 
175  if opts.create_raster_overviews_table and opts.overview_level <= 1:
176  prs.error('create table for RASTER_OVERVIEWS available only if overviews import requested')
177 
178  # XXX: Now, if --band=Nth, then only Nth band of all specified rasters is dumped/imported
179  # This behavior can be changed to support only single-raster input if --band option used.
180  #if opts.band is not None and len(opts.raster) > 1:
181  # prs.error("option -b requires single input raster only, specify -r option only once")
182 
183  if opts.table is None:
184  prs.error("use option -t to specify raster destination table")
185  if len(opts.table.split('.')) > 2:
186  prs.error("invalid format of table name specified with option -t, expected [<schema>.]table")
187 
188  if opts.output is None:
189  prs.error("failed to initialise output file, try to use option -o explicitly")
190 
191  if opts.version is not None:
192  if opts.version != g_rt_version:
193  prs.error("invalid version of WKT Raster protocol specified, only version 0 is supported")
194  else:
195  prs.error("use option -w to specify version of WKT Raster protocol")
196 
197  if opts.endian is not None:
198  if opts.endian != NDR and opts.endian != XDR:
199  prs.error("invalid endianness value, valid ones are 0 for NDR or 1 for XDR")
200  else:
201  prs.error("use option -e to specify endianness of binary output")
202 
203  return (opts, args)
204 
205 
def parse_command_line()
Definition: raster2pgsql.py:78
Here is the caller graph for this function: