Skip to main content

cligj

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 JSON Text Sequences proposed 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.

Metadata

Release files for cligj 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cligj 0.5.0
File Size Uploaded
cligj-0.5.0.tar.gz 5.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for cligj 0.5.0
File Interpreter ABI Platform
cligj-0.5.0-py3-none-any.whl Python 3 none any Details
cligj-0.5.0-py2-none-any.whl Python 2 none any Details

Total release size: 16.9 kB

Release files / cligj-0.5.0.tar.gz

Download URL cligj-0.5.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6c7d52d529a78712491974f975c33473f430c0f7beb18c0d7a402a743dcb460a
BLAKE2b-256 checksum
How to use checksums
7a7520752146e9c73e74a286f66d4ee84ed102b4855c04d9bb8892b52a1f166c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.14

Release files / cligj-0.5.0-py3-none-any.whl

Download URL cligj-0.5.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
20f24ce9abfde3f758aec3399e6811b936b6772f360846c662c19bf5537b4f14
BLAKE2b-256 checksum
How to use checksums
e4be30a58b4b0733850280d01f8bd132591b4668ed5c7046761098d665ac2174
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.4.6

Release files / cligj-0.5.0-py2-none-any.whl

Download URL cligj-0.5.0-py2-none-any.whl
Size 5.7 kB
Tags Python 2
SHA-256 checksum
How to use checksums
60c93dda4499562eb87509a8ff3535a7441053b766c9c26bcf874a732f939c7c
BLAKE2b-256 checksum
How to use checksums
b76183f3b3d57313f17250263f03cf1631d68c9f4267ce69059063bf8f73d7f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.14

Release history Release notifications | RSS feed

0.7.2

2 release files

0.7.1

2 release files

0.7.0

3 release files

0.6.0

3 release files

This release

0.5.0 This release

3 release files

0.4.0

3 release files

0.3.0

3 release files

0.2.0

3 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page