From da7d45b030f62427271620a81202471330d4f59c Mon Sep 17 00:00:00 2001 From: Sean Gillies Date: Wed, 23 Jul 2014 12:08:57 -0600 Subject: [PATCH] Document experimental json-seq feature. --- docs/cli.rst | 8 ++++++++ scripts/rio | 21 +++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index f746c981..d20433fb 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -145,6 +145,14 @@ for use with single-band rasters and reads from the first band. The resulting file, uploaded to Mapbox, looks like this: `sgillies.j1ho338j `__. +This command supports `JSON text sequences `__ as an experimental feature. + +.. code-block:: console + + $ rio shapes rasterio/tests/data/shade.tif --precision 1 --x-json-seq --x-text-sep-lf | head -n 2 + {"bbox": [-106.5, 39.6, -106.4, 39.6], "type": "FeatureCollection"} + {"geometry": {"coordinates": [[[-106.5, 39.6], [-106.5, 39.6], [-106.5, 39.6], [-106.5, 39.6], [-106.5, 39.6]]], "type": "Polygon"}, "id": "255", "properties": {"val": 255}, "type": "Feature"} + transform --------- diff --git a/scripts/rio b/scripts/rio index fde2a8b0..18417337 100644 --- a/scripts/rio +++ b/scripts/rio @@ -188,10 +188,13 @@ def transform_geom(transformer, g, precision=-1): help="Indentation level for JSON output") @click.option('--projected/--geographic', default=False, help="Output in projected coordinates (default is geographic).") -@click.option('--json-seq/--json-obj', default=False, - help="Write a JSON sequence (default is object).") +@click.option('--x-json-seq/--x-json-obj', default=False, + help="Write a JSON sequence (default is object). Requires specification of text separator. Experimental.") +@click.option('--x-text-sep-lf', 'text_sep', flag_value='\n', + help="Use LF as text separator. Experimental.") @click.pass_context -def shapes(ctx, src_path, precision, indent, projected, json_seq): +def shapes( + ctx, src_path, precision, indent, projected, x_json_seq, text_sep=None): verbosity = ctx.obj['verbosity'] logger = logging.getLogger('rio') def to_feature(i, geom): @@ -228,19 +231,25 @@ def shapes(ctx, src_path, precision, indent, projected, json_seq): feature_gen = (to_feature(i, g) for g, i in rasterio.features.shapes( src.read(1), transform=src.affine)) - if json_seq: + if x_json_seq: + if text_sep is None: + raise click.BadParameter('Text separator is required') sys.stdout.write( json.dumps(collection, indent=indent, sort_keys=True) - + "\n") + + text_sep) for feature in feature_gen: sys.stdout.write( json.dumps(feature, indent=indent, sort_keys=True) - + "\n") + + text_sep) + sys.stdout.flush() else: collection['features'] = list(feature_gen) sys.stdout.write( json.dumps(collection, indent=indent, sort_keys=True)) sys.exit(0) + except IOError: + logger.info("IOError caught") + sys.exit(0) except Exception: logger.exception("Failed. Exception caught") sys.exit(1)