Skip to content

Commit

Permalink
Merge 692e93a into ed53e0b
Browse files Browse the repository at this point in the history
  • Loading branch information
critical-path committed Sep 26, 2018
2 parents ed53e0b + 692e93a commit b2bdb90
Show file tree
Hide file tree
Showing 6 changed files with 1,161 additions and 1 deletion.
35 changes: 35 additions & 0 deletions README.md
Expand Up @@ -54,6 +54,7 @@ $ mapbox ...
* [staticmap](#staticmap)
* [upload](#upload)
* [datasets](#datasets)
* [tilequery](#tilequery)

For any command that takes waypoints or features as an input you can either specify:

Expand Down Expand Up @@ -422,6 +423,40 @@ Options:
--help Show this message and exit.
```

### tilequery

```
Usage: mapbox tilequery [OPTIONS] MAP_ID... LON LAT
Returns data about specific features from vector tilesets.
$ mapbox tilequery <map_id> <lon> <lat>
$ mapbox tilequery mapbox.mapbox-streets-v10 0.0 1.1
Note: Preface negative longitude or latitude arguments with --.
$ mapbox tilequery <map_id> -- <-lon> <lat>
$ mapbox tilequery mapbox.mapbox-streets-v10 -- -0.0 1.1
$ mapbox tilequery <map_id> <lon> -- <-lat>
$ mapbox tilequery mapbox.mapbox-streets-v10 0.0 -- -1.1
An access token is required. See "mapbox --help".
Options:
-r, --radius INTEGER RANGE The approximate distance in meters to query
-l, --limit INTEGER RANGE The number of features to return
--dedupe / --no-dedupe Whether to remove duplicate results
-g, --geometry [linestring|point|polygon]
The geometry type to query
-y, --layer TEXT The list of layers to query
-o, --output FILENAME Whether to save the results to a file
--help Show this message and exit.
```

## Alternative command syntax

When saving a fraction of a second matters you can call the mapboxcli module
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Expand Up @@ -2,3 +2,4 @@
* [Geocoding](geocoding.md)
* [Mapmatching](mapmatching.md)
* [Staticmaps](static_maps.md)
* [Tilequery](tilequery.md)
49 changes: 49 additions & 0 deletions examples/tilequery.md
@@ -0,0 +1,49 @@
# Tilequery

Access the Mapbox Tilequery API from the command line.

# Querying a Tileset

To query a tileset for specific features, run `mapbox tilequery`, passing in values for `map_id`, `lon`, and `lat`.

```
mapbox tilequery mapbox.mapbox-streets-v10 0.0 1.1
```

Use the `--radius`, `--limit`, `--dedupe`, `--geometry`, `--layer`, and `--output` options for additional functionality.

# Notes

To query multiple tilesets, pass in multiple values for `map_id`.

```
mapbox tilequery mapbox.mapbox-streets-v10 mapbox.mapbox-outdoors-v10 0.0 1.1
```

To query a negative longitudinal or latitudinal value, precede it with `--`.

__longitude__:

```
mapbox tilequery mapbox.mapbox-streets-v10 -- -0.0 1.1
```

__latitude__:

```
mapbox tilequery mapbox.mapbox-streets-v10 0.0 -- -1.1
```

To query multiple layers, use the `--layer` option more than once.

```
mapbox tilequery --layer layer0 --layer layer1 mapbox.mapbox-streets-v10 0.0 1.1
```

# Viewing Help

For help, use the `--help` option.

```
mapbox tilequery --help
```
11 changes: 10 additions & 1 deletion mapboxcli/scripts/cli.py
Expand Up @@ -12,7 +12,15 @@
import mapboxcli
from mapboxcli.compat import configparser
from mapboxcli.scripts import (
config, geocoding, directions, mapmatching, uploads, static, datasets)
config,
geocoding,
directions,
mapmatching,
uploads,
static,
datasets,
tilequery
)


def configure_logging(verbosity):
Expand Down Expand Up @@ -102,3 +110,4 @@ def main_group(ctx, verbose, quiet, access_token, config):
main_group.add_command(uploads.upload)
main_group.add_command(static.staticmap)
main_group.add_command(datasets.datasets)
main_group.add_command(tilequery.tilequery)
121 changes: 121 additions & 0 deletions mapboxcli/scripts/tilequery.py
@@ -0,0 +1,121 @@
import json

import click

import mapbox

from mapboxcli.errors import MapboxCLIException


@click.command(
short_help="Returns data about specific features from vector tilesets"
)

@click.argument(
"map_id",
nargs=-1,
required=True
)

@click.argument(
"lon",
nargs=1,
required=True
)

@click.argument(
"lat",
nargs=1,
required=True
)

@click.option(
"--radius",
"-r",
type=click.IntRange(0, ),
help="The approximate distance in meters to query"
)

@click.option(
"--limit",
"-l",
type=click.IntRange(1, 50),
help="The number of features to return"
)

@click.option(
"--dedupe/--no-dedupe",
default=True,
help="Whether to remove duplicate results"
)

@click.option(
"--geometry",
"-g",
type=click.Choice(mapbox.Tilequery.valid_geometries),
help="The geometry type to query"
)

@click.option(
"--layer",
"-y",
multiple=True,
help="The layer to query"
)

@click.option(
"--output",
"-o",
type=click.File("w"),
help="Whether to save the results to a file"
)

@click.pass_context
def tilequery(ctx, map_id, lon, lat, radius,
limit, dedupe, geometry, layer, output):
"""Returns data about specific features from vector tilesets.
$ mapbox tilequery <map_id> <lon> <lat>
$ mapbox tilequery mapbox.mapbox-streets-v10 0.0 1.1
Note: Preface negative longitude or latitude arguments with --.
$ mapbox tilequery <map_id> -- <-lon> <lat>
$ mapbox tilequery mapbox.mapbox-streets-v10 -- -0.0 1.1
$ mapbox tilequery <map_id> <lon> -- <-lat>
$ mapbox tilequery mapbox.mapbox-streets-v10 0.0 -- -1.1
An access token is required. See "mapbox --help".
"""

access_token = (ctx.obj and ctx.obj.get("access_token")) or None

service = mapbox.Tilequery(access_token=access_token)

try:
res = service.tilequery(
list(map_id),
lon=float(lon),
lat=float(lat),
radius=radius,
limit=limit,
dedupe=dedupe,
geometry=geometry,
layers=list(layer) if layer else None,
)
except mapbox.errors.ValidationError as exc:
raise click.BadParameter(str(exc))

if res.status_code == 200:
if output:
output.write(json.dumps(res.geojson()))
else:
click.echo(json.dumps(res.geojson()))
else:
raise MapboxCLIException(res.text.strip())

0 comments on commit b2bdb90

Please sign in to comment.