Пример #1
0
def distrib(ctx, use_rs):

    """Distribute features from a collection.

    Print the features of GeoJSON objects read from stdin.
    """

    verbosity = (ctx.obj and ctx.obj['verbosity']) or 2
    logger = logging.getLogger('fio')
    stdin = click.get_text_stream('stdin')
    try:
        source = helpers.obj_gen(stdin)
        for i, obj in enumerate(source):
            obj_id = obj.get('id', 'collection:' + str(i))
            features = obj.get('features') or [obj]
            for j, feat in enumerate(features):
                if obj.get('type') == 'FeatureCollection':
                    feat['parent'] = obj_id
                feat_id = feat.get('id', 'feature:' + str(i))
                feat['id'] = feat_id
                if use_rs:
                    click.echo(u'\u001e', nl=False)
                click.echo(json.dumps(feat))
    except Exception:
        logger.exception("Exception caught during processing")
        raise click.Abort()
Пример #2
0
def filter(ctx, filter_expression, use_rs):
    """
    Filter GeoJSON features by python expression.

    Features are read from stdin.

    The expression is evaluated in a restricted namespace containing:
        - sum, pow, min, max and the imported math module
        - shape (optional, imported from shapely.geometry if available)
        - bool, int, str, len, float type conversions
        - f (the feature to be evaluated,
             allows item access via javascript-style dot notation using munch)

    The expression will be evaluated for each feature and, if true,
    the feature will be included in the output.  For example:

    \b
        $ fio cat data.shp \\
            | fio filter "f.properties.area > 1000.0" \\
            | fio collect > large_polygons.geojson
    """

    logger = logging.getLogger(__name__)
    stdin = click.get_text_stream('stdin')

    try:
        source = obj_gen(stdin)
        for i, obj in enumerate(source):
            features = obj.get('features') or [obj]
            for j, feat in enumerate(features):
                if not eval_feature_expression(feat, filter_expression):
                    continue

                if use_rs:
                    click.echo(u'\u001e', nl=False)
                click.echo(json.dumps(feat))

    except Exception:
        logger.exception("Exception caught during processing")
        raise click.Abort()
Пример #3
0
def filter(ctx, filter_expression, use_rs):
    """
    Filter GeoJSON features by python expression.

    Features are read from stdin.

    The expression is evaluated in a restricted namespace containing:
        - sum, pow, min, max and the imported math module
        - shape (optional, imported from shapely.geometry if available)
        - bool, int, str, len, float type conversions
        - f (the feature to be evaluated,
             allows item access via javascript-style dot notation using munch)

    The expression will be evaluated for each feature and, if true,
    the feature will be included in the output.  For example:

    \b
        $ fio cat data.shp \\
            | fio filter "f.properties.area > 1000.0" \\
            | fio collect > large_polygons.geojson
    """

    logger = logging.getLogger(__name__)
    stdin = click.get_text_stream('stdin')

    try:
        source = obj_gen(stdin)
        for i, obj in enumerate(source):
            features = obj.get('features') or [obj]
            for j, feat in enumerate(features):
                if not eval_feature_expression(feat, filter_expression):
                    continue

                if use_rs:
                    click.echo(u'\u001e', nl=False)
                click.echo(json.dumps(feat))

    except Exception:
        logger.exception("Exception caught during processing")
        raise click.Abort()
def bounds(ctx, precision, explode, with_id, with_obj, use_rs):
    """Print the bounding boxes of GeoJSON objects read from stdin.

    Optionally explode collections and print the bounds of their
    features.

    To print identifiers for input objects along with their bounds
    as a {id: identifier, bbox: bounds} JSON object, use --with-id.

    To print the input objects themselves along with their bounds
    as GeoJSON object, use --with-obj. This has the effect of updating
    input objects with {id: identifier, bbox: bounds}.
    """
    verbosity = (ctx.obj and ctx.obj['verbosity']) or 2
    logger = logging.getLogger('fio')
    stdin = click.get_text_stream('stdin')
    stdout = click.get_text_stream('stdout')
    try:
        source = obj_gen(stdin)
        for i, obj in enumerate(source):
            obj_id = obj.get('id', 'collection:' + str(i))
            xs = []
            ys = []
            features = obj.get('features') or [obj]
            for j, feat in enumerate(features):
                feat_id = feat.get('id', 'feature:' + str(i))
                w, s, e, n = fiona.bounds(feat)
                if precision > 0:
                    w, s, e, n = (round(v, precision)
                                  for v in (w, s, e, n))
                if explode:
                    if with_id:
                        rec = {
                            'parent': obj_id,
                            'id': feat_id,
                            'bbox': (w, s, e, n)}
                    elif with_obj:
                        feat.update(parent=obj_id, bbox=(w, s, e, n))
                        rec = feat
                    else:
                        rec = (w, s, e, n)
                    if use_rs:
                        click.echo(u'\u001e', nl=False)
                    click.echo(json.dumps(rec))
                else:
                    xs.extend([w, e])
                    ys.extend([s, n])
            if not explode:
                w, s, e, n = (min(xs), min(ys), max(xs), max(ys))
                if with_id:
                    rec = {'id': obj_id, 'bbox': (w, s, e, n)}
                elif with_obj:
                    obj.update(id=obj_id, bbox=(w, s, e, n))
                    rec = obj
                else:
                    rec = (w, s, e, n)
                if use_rs:
                    click.echo(u'\u001e', nl=False)
                click.echo(json.dumps(rec))

    except Exception:
        logger.exception("Exception caught during processing")
        raise click.Abort()