comparison make-tile @ 10:1944acce0e6f

Add --output= option.
author David Barts <n5jrn@me.com>
date Thu, 26 Aug 2021 08:37:46 -0700
parents 94762476b171
children 6e4a8ddacf61
comparison
equal deleted inserted replaced
9:a56d3d3b5efd 10:1944acce0e6f
212 212
213 # Parse command-line arguments. 213 # Parse command-line arguments.
214 parser = argparse.ArgumentParser(description='Render GeoPDF into GeoTIFF suitable for tiling.') 214 parser = argparse.ArgumentParser(description='Render GeoPDF into GeoTIFF suitable for tiling.')
215 parser.add_argument('--layers', '-l', type=str, 215 parser.add_argument('--layers', '-l', type=str,
216 help='List of layers to include (see documentation for default).') 216 help='List of layers to include (see documentation for default).')
217 parser.add_argument('--output', '-o', type=str,
218 help='Name of output file (default: *.tiff).')
217 parser.add_argument('--resolution', '-r', type=positive_int, default=300, 219 parser.add_argument('--resolution', '-r', type=positive_int, default=300,
218 help='Resolution to render at in DPI (default: 300).') 220 help='Resolution to render at in DPI (default: 300).')
219 parser.add_argument('file', nargs=1, 221 parser.add_argument('file', nargs=1,
220 help='File to read (must be a GeoPDF).') 222 help='File to read (must be a GeoPDF).')
221 try: 223 try:
226 # File must end with '.pdf' (case insensitive), or else! 228 # File must end with '.pdf' (case insensitive), or else!
227 if not isinstance(args.file, str): 229 if not isinstance(args.file, str):
228 # How silly, it gave us a list or a tuple, despite only one of them! 230 # How silly, it gave us a list or a tuple, despite only one of them!
229 args.file = args.file[0] 231 args.file = args.file[0]
230 if not args.file.lower().endswith('.pdf'): 232 if not args.file.lower().endswith('.pdf'):
231 sys.steder.write("{0}: input file must end with .pdf (case insensitive)\n".format(MYNAME)) 233 sys.stderr.write("{0}: input file must end with .pdf (case insensitive)\n".format(MYNAME))
234 sys.exit(2)
232 235
233 # Default the set of layers to delete, if necessary 236 # Default the set of layers to delete, if necessary
234 if args.layers is None: 237 if args.layers is None:
235 args.layers = get_default_layers(args.file) 238 args.layers = get_default_layers(args.file)
239
240 # Default the output file, if necessary
241 if args.output is None:
242 args.output = os.path.splitext(args.file)[0] + ".tiff"
243 elif os.path.splitext(args.output)[1].lower() not in set([".tif", ".tiff"]):
244 sys.stderr.write("{0}: output file must end with .tif or .tiff (case insensitive)\n".format(MYNAME))
245 sys.exit(2)
236 246
237 with TemporaryDirectory() as td: 247 with TemporaryDirectory() as td:
238 # Get scratch file name. This goes under TMPDIR; if the default temporary 248 # Get scratch file name. This goes under TMPDIR; if the default temporary
239 # area is too small, set that environment variable accordingly! 249 # area is too small, set that environment variable accordingly!
240 tf = os.path.join(td, "temp.tiff") 250 tf = os.path.join(td, "temp.tiff")
254 minx = find_edge(im, Orientation.VERTICAL, Direction.ASCENDING) 264 minx = find_edge(im, Orientation.VERTICAL, Direction.ASCENDING)
255 maxx = find_edge(im, Orientation.VERTICAL, Direction.DESCENDING) 265 maxx = find_edge(im, Orientation.VERTICAL, Direction.DESCENDING)
256 miny = find_edge(im, Orientation.HORIZONTAL, Direction.ASCENDING) 266 miny = find_edge(im, Orientation.HORIZONTAL, Direction.ASCENDING)
257 maxy = find_edge(im, Orientation.HORIZONTAL, Direction.DESCENDING) 267 maxy = find_edge(im, Orientation.HORIZONTAL, Direction.DESCENDING)
258 268
259 # Determine output file name.
260 outf = os.path.splitext(args.file)[0] + ".tiff"
261
262 # Crop. 269 # Crop.
263 proc = subprocess.Popen( 270 proc = subprocess.Popen(
264 gdalcmd("gdal_translate", "-q", tf, outf, "-of", "GTiff", "-srcwin", str(minx), str(miny), str(maxx-minx+1), str(maxy-miny+1)), 271 gdalcmd("gdal_translate", "-q", tf, args.output, "-of", "GTiff", "-srcwin", str(minx), str(miny), str(maxx-minx+1), str(maxy-miny+1)),
265 stdout=subprocess.DEVNULL, stderr=sys.stderr) 272 stdout=subprocess.DEVNULL, stderr=sys.stderr)
266 waitfor(proc) 273 waitfor(proc)
267 274
268 # AMF... 275 # AMF...
269 sys.exit(estat) 276 sys.exit(estat)