Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |
Oops, something went wrong.