diff 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
line wrap: on
line diff
--- a/make-tile	Wed Aug 25 08:45:36 2021 -0700
+++ b/make-tile	Thu Aug 26 08:37:46 2021 -0700
@@ -214,6 +214,8 @@
 parser = argparse.ArgumentParser(description='Render GeoPDF into GeoTIFF suitable for tiling.')
 parser.add_argument('--layers', '-l', type=str,
     help='List of layers to include (see documentation for default).')
+parser.add_argument('--output', '-o', type=str,
+    help='Name of output file (default: *.tiff).')
 parser.add_argument('--resolution', '-r', type=positive_int, default=300,
     help='Resolution to render at in DPI (default: 300).')
 parser.add_argument('file', nargs=1,
@@ -228,12 +230,20 @@
     # How silly, it gave us a list or a tuple, despite only one of them!
     args.file = args.file[0]
 if not args.file.lower().endswith('.pdf'):
-    sys.steder.write("{0}: input file must end with .pdf (case insensitive)\n".format(MYNAME))
+    sys.stderr.write("{0}: input file must end with .pdf (case insensitive)\n".format(MYNAME))
+    sys.exit(2)
 
 # Default the set of layers to delete, if necessary
 if args.layers is None:
     args.layers = get_default_layers(args.file)
 
+# Default the output file, if necessary
+if args.output is None:
+    args.output = os.path.splitext(args.file)[0] + ".tiff"
+elif os.path.splitext(args.output)[1].lower() not in set([".tif", ".tiff"]):
+    sys.stderr.write("{0}: output file must end with .tif or .tiff (case insensitive)\n".format(MYNAME))
+    sys.exit(2)
+
 with TemporaryDirectory() as td:
     # Get scratch file name. This goes under TMPDIR; if the default temporary
     # area is too small, set that environment variable accordingly!
@@ -256,12 +266,9 @@
         miny = find_edge(im, Orientation.HORIZONTAL, Direction.ASCENDING)
         maxy = find_edge(im, Orientation.HORIZONTAL, Direction.DESCENDING)
 
-    # Determine output file name.
-    outf = os.path.splitext(args.file)[0] + ".tiff"
-
     # Crop.
     proc = subprocess.Popen(
-        gdalcmd("gdal_translate", "-q", tf, outf, "-of", "GTiff", "-srcwin", str(minx), str(miny), str(maxx-minx+1), str(maxy-miny+1)),
+        gdalcmd("gdal_translate", "-q", tf, args.output, "-of", "GTiff", "-srcwin", str(minx), str(miny), str(maxx-minx+1), str(maxy-miny+1)),
         stdout=subprocess.DEVNULL, stderr=sys.stderr)
     waitfor(proc)