~johanvdw/+junk/cligj

« back to all changes in this revision

Viewing changes to README.rst

  • Committer: Package Import Robot
  • Author(s): Johan Van de Wauw
  • Date: 2015-01-18 20:21:27 UTC
  • Revision ID: package-import@ubuntu.com-20150118202127-s1cg5lb6usmwt7a6
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
cligj
 
2
======
 
3
 
 
4
.. image:: https://travis-ci.org/mapbox/cligj.svg
 
5
   :target: https://travis-ci.org/mapbox/cligj
 
6
 
 
7
.. image:: https://coveralls.io/repos/mapbox/cligj/badge.png?branch=master
 
8
   :target: https://coveralls.io/r/mapbox/cligj?branch=master
 
9
 
 
10
Common arguments and options for GeoJSON processing commands, using Click.
 
11
 
 
12
Example
 
13
-------
 
14
 
 
15
Here's an example of a command that writes out GeoJSON features as a collection
 
16
or, optionally, a sequence of individual features. Since most software that
 
17
reads and writes GeoJSON expects a text containing a single feature collection,
 
18
that's the default, and a LF-delimited sequence of texts containing one GeoJSON
 
19
feature each is a feature that is turned on using the ``--sequence`` option.
 
20
To write sequences of feature texts that conform to the `JSON Text Sequences
 
21
proposed standard
 
22
<http://tools.ietf.org/html/draft-ietf-json-text-sequence-13>`__ (and might
 
23
contain pretty-printed JSON) with the ASCII Record Separator (0x1e) as
 
24
a delimiter, use the ``--rs`` option
 
25
 
 
26
.. code-block:: python
 
27
 
 
28
    import click
 
29
    import cligj
 
30
    import json
 
31
 
 
32
    @click.command()
 
33
    @cligj.sequence_opt
 
34
    @cligj.use_rs_opt
 
35
    def features(sequence, use_rs):
 
36
        features = [
 
37
            {'type': 'Feature', 'id': '1'}, {'type': 'Feature', 'id': '2'}]
 
38
        if sequence:
 
39
            for feature in features:
 
40
                if use_rs:
 
41
                    click.echo(b'\x1e', nl=False)
 
42
                click.echo(json.dumps(feature))
 
43
        else:
 
44
            click.echo(json.dumps(
 
45
                {'type': 'FeatureCollection', 'features': features}))
 
46
 
 
47
On the command line it works like this.
 
48
 
 
49
.. code-block:: console
 
50
 
 
51
    $ features
 
52
    {'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'id': '1'}, {'type': 'Feature', 'id': '2'}]}
 
53
 
 
54
    $ features --sequence
 
55
    {'type': 'Feature', 'id': '1'}
 
56
    {'type': 'Feature', 'id': '2'}
 
57
 
 
58
    $ features --sequence --rs
 
59
    ^^{'type': 'Feature', 'id': '1'}
 
60
    ^^{'type': 'Feature', 'id': '2'}
 
61
 
 
62
In this example, ``^^`` represents 0x1e.