Skip to main content

Click params for commmand line interfaces to GeoJSON

Project description

https://travis-ci.org/mapbox/cligj.svg https://coveralls.io/repos/mapbox/cligj/badge.png?branch=master

Common arguments and options for GeoJSON processing commands, using Click.

cligj is for Python developers who create command line interfaces for geospatial data. cligj allows you to quickly build consistent, well-tested and interoperable CLIs for handling GeoJSON.

Arguments

files_in_arg Multiple files

files_inout_arg Multiple files, last of which is an output file.

features_in_arg GeoJSON Features input which accepts multiple representations of GeoJSON features and returns the input data as an iterable of GeoJSON Feature-like dictionaries

Options

verbose_opt

quiet_opt

format_opt

JSON formatting options

indent_opt

compact_opt

Coordinate precision option

precision_opt

Geographic (default), projected, or Mercator switch

projection_geographic_opt

projection_projected_opt

projection_mercator_opt

Feature collection or feature sequence switch

sequence_opt

use_rs_opt

GeoJSON output mode option

geojson_type_collection_opt

geojson_type_feature_opt

def geojson_type_bbox_opt

Example

Here’s an example of a command that writes out GeoJSON features as a collection or, optionally, a sequence of individual features. Since most software that reads and writes GeoJSON expects a text containing a single feature collection, that’s the default, and a LF-delimited sequence of texts containing one GeoJSON feature each is a feature that is turned on using the --sequence option. To write sequences of feature texts that conform to the GeoJSON Text Sequences standard (and might contain pretty-printed JSON) with the ASCII Record Separator (0x1e) as a delimiter, use the --rs option

import click
import cligj
import json

def process_features(features):
    for feature in features:
        # TODO process feature here
        yield feature

@click.command()
@cligj.features_in_arg
@cligj.sequence_opt
@cligj.use_rs_opt
def pass_features(features, sequence, use_rs):
    if sequence:
        for feature in process_features(features):
            if use_rs:
                click.echo(u'\x1e', nl=False)
            click.echo(json.dumps(feature))
    else:
        click.echo(json.dumps(
            {'type': 'FeatureCollection',
             'features': list(process_features(features))}))

On the command line, the generated help text explains the usage

Usage: pass_features [OPTIONS] FEATURES...

Options:
--sequence / --no-sequence  Write a LF-delimited sequence of texts
                            containing individual objects or write a single
                            JSON text containing a feature collection object
                            (the default).
--rs / --no-rs              Use RS (0x1E) as a prefix for individual texts
                            in a sequence as per http://tools.ietf.org/html
                            /draft-ietf-json-text-sequence-13 (default is
                            False).
--help                      Show this message and exit.

And can be used like this

$ cat data.geojson
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'id': '1'}, {'type': 'Feature', 'id': '2'}]}

$ pass_features data.geojson
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'id': '1'}, {'type': 'Feature', 'id': '2'}]}

$ cat data.geojson | pass_features
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'id': '1'}, {'type': 'Feature', 'id': '2'}]}

$ cat data.geojson | pass_features --sequence
{'type': 'Feature', 'id': '1'}
{'type': 'Feature', 'id': '2'}

$ cat data.geojson | pass_features --sequence --rs
^^{'type': 'Feature', 'id': '1'}
^^{'type': 'Feature', 'id': '2'}

In this example, ^^ represents 0x1e.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cligj-0.6b1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distributions

cligj-0.6b1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

cligj-0.6b1-py2-none-any.whl (6.9 kB view details)

Uploaded Python 2

File details

Details for the file cligj-0.6b1.tar.gz.

File metadata

  • Download URL: cligj-0.6b1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.15

File hashes

Hashes for cligj-0.6b1.tar.gz
Algorithm Hash digest
SHA256 f44937f11519da45af14146975e1adaad73caf669dab6f9489ce9d3bcde510b4
MD5 9d175fa382940f9f4f5f4cd943368074
BLAKE2b-256 ca826f1296349fd23c037c5c2c57d5de6458902f92453e1ac3e3fa7c84b01b5c

See more details on using hashes here.

File details

Details for the file cligj-0.6b1-py3-none-any.whl.

File metadata

  • Download URL: cligj-0.6b1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.7

File hashes

Hashes for cligj-0.6b1-py3-none-any.whl
Algorithm Hash digest
SHA256 d380773acc6db25a5097da26e9886ced5b8271ebe7cf796b4ad0fd891220389a
MD5 828ca2de24fb4c84fa71bfc605dcd43d
BLAKE2b-256 65bfe282082672480a1d33843952b7acf9ae18fa8d152d6f70068b270e8765d9

See more details on using hashes here.

File details

Details for the file cligj-0.6b1-py2-none-any.whl.

File metadata

  • Download URL: cligj-0.6b1-py2-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.15

File hashes

Hashes for cligj-0.6b1-py2-none-any.whl
Algorithm Hash digest
SHA256 5607c00b5c0d5ee0716c2ae090f4bf1db503d14562f81eb5272c1f81507a9732
MD5 0c0d48b25da80e1c3921894bddd42619
BLAKE2b-256 9fb75aad6d7f343d908f0ee82713a96ad6b21f7f88d98d4ce3380a919fc60126

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page