Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Geocoding, requests style #6

Merged
merged 8 commits into from
Jul 29, 2015
Merged

Geocoding, requests style #6

merged 8 commits into from
Jul 29, 2015

Conversation

sgillies
Copy link
Contributor

Including a CLI. We've got a name conflict with another package to resolve, so for now it's 'mapbaux'.

$ mapbaux geocode --access-token='MY_TOKEN' '1600 Pennsylvania Ave NW' | geojsonio

mapbaux-geocode

@dnomadb this is about 30% ready to go, I'd still like to get tests passing on Travis before we merge.

Sean Gillies added 7 commits July 29, 2015 09:46
Includes a Geocoder class and a mapbaux-geocode command.

It's 'mapbaux' for now because there's another command sitting on
'mapbox'.
100% test coverage. \m/
@sgillies
Copy link
Contributor Author

@dnomadb @sbma44 @rclark @tmcw this is ready for wider review.

As in the README, usage is like this.

>>> import mapbox
>>> geocoder = mapbox.Geocoder(access_token='YOUR_ACCESS_TOKEN')
>>> response = geocoder.fwd('Chester, NJ')
>>> print response.status_code
200

Rate limit info is in the headers.

>>> import pprint
>>> pprint.pprint(response.headers.items())
[('x-rate-limit-remaining', '599'),
 ('content-length', '2488'),
 ('via', '1.1 38ddaa14c744d77c512b550361fc57b5.cloudfront.net (CloudFront)'),
 ('date', 'Wed, 29 Jul 2015 19:28:54 GMT'),
 ('x-cache', 'Miss from cloudfront'),
 ('access-control-expose-headers',
  'x-rate-limit-interval,x-rate-limit-limit,x-rate-limit-remaining,x-rate-limit-reset'),
 ('x-powered-by', 'Express'),
 ('server', 'nginx'),
 ('last-modified', 'Wed, 29 Jul 2015 19:28:54 GMT'),
 ('connection', 'keep-alive'),
 ('etag', '"cee0e2b6e2919fbddb44552c59865744"'),
 ('x-amz-cf-id', 'OItwPHnRuB2Q2XmYWJdgLi7tOcz34Fa_PFPfmVrqOa7_lSP7SOiOhA=='),
 ('x-rate-limit-reset', '1438198194'),
 ('cache-control', 'max-age=3600'),
 ('x-rate-limit-interval', '60'),
 ('x-rate-limit-limit', '600'),
 ('access-control-allow-origin', '*'),
 ('content-type', 'application/vnd.geo+json; charset=utf-8')]

The response's json() method deserializes the response body.

>>> pprint.pprint(response.json())
{u'attribution': u'NOTICE: You may cache this data for up to 30 days. \xa9 2015 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service. (https://www.mapbox.com/about/maps/)',
 u'features': [{u'bbox': [-74.74063500935817,
                          40.71945199088977,
                          -74.62370799008399,
                          40.83360800937651],
                u'center': [-74.7083, 40.7851],
                u'context': [{u'id': u'postcode.1038615627',
                              u'text': u'07930'},
                             {u'id': u'region.2035022604',
                              u'text': u'New Jersey'},
                             {u'id': u'country.4150104525',
                              u'text': u'United States'}],
                u'geometry': {u'coordinates': [-74.7083, 40.7851],
                              u'type': u'Point'},
                u'id': u'place.7274',
                u'place_name': u'Chester, 07930, New Jersey, United States',
                u'properties': {},
                u'relevance': 0.999,
                u'text': u'Chester',
                u'type': u'Feature'},
               {u'bbox': [-75.56053799999998,
                          38.788656999999986,
                          -73.88665199999998,
                          41.35735999999999],
                u'center': [-74.436931, 40.07478],
                u'context': [{u'id': u'country.4150104525',
                              u'text': u'United States'}],
                u'geometry': {u'coordinates': [-74.436931, 40.07478],
                              u'type': u'Point'},
                u'id': u'region.2035022604',
                u'place_name': u'New Jersey, United States',
                u'properties': {},
                u'relevance': 0.49,
                u'text': u'New Jersey',
                u'type': u'Feature'},
               {u'bbox': [43.6384461237896,
                          16.947442118958687,
                          47.745127394014034,
                          19.531262926143192],
                u'center': [45.622228, 18.242401],
                u'context': [{u'id': u'country.3267341775',
                              u'text': u'Saudi Arabia'}],
                u'geometry': {u'coordinates': [45.622228, 18.242401],
                              u'type': u'Point'},
                u'id': u'region.1348222286',
                u'place_name': u'Najran, Saudi Arabia',
                u'properties': {},
                u'relevance': 0.49,
                u'text': u'Najran',
                u'type': u'Feature'},
               {u'bbox': [-2.943733999999999,
                          53.164426999999996,
                          -2.8541059999999994,
                          53.26897499999998],
                u'center': [-2.8919, 53.1905],
                u'context': [{u'id': u'postcode.451190028',
                              u'text': u'CH1 2HG'},
                             {u'id': u'region.1961842095',
                              u'text': u'Cheshire West and Chester'},
                             {u'id': u'country.2794958072',
                              u'text': u'United Kingdom'}],
                u'geometry': {u'coordinates': [-2.8919, 53.1905],
                              u'type': u'Point'},
                u'id': u'place.122617',
                u'place_name': u'Chester, CH1 2HG, Cheshire West and Chester, United Kingdom',
                u'properties': {},
                u'relevance': 0.49,
                u'text': u'Chester',
                u'type': u'Feature'},
               {u'bbox': [-75.42394100421112,
                          39.822347993547226,
                          -75.3437349914335,
                          39.875822009918124],
                u'center': [-75.3557, 39.8496],
                u'context': [{u'id': u'postcode.1785325442',
                              u'text': u'19013'},
                             {u'id': u'region.2184819983',
                              u'text': u'Pennsylvania'},
                             {u'id': u'country.4150104525',
                              u'text': u'United States'}],
                u'geometry': {u'coordinates': [-75.3557, 39.8496],
                              u'type': u'Point'},
                u'id': u'place.10507',
                u'place_name': u'Chester, 19013, Pennsylvania, United States',
                u'properties': {},
                u'relevance': 0.49,
                u'text': u'Chester',
                u'type': u'Feature'}],
 u'query': [u'chester', u'nj'],
 u'type': u'FeatureCollection'}

@sbma44
Copy link
Member

sbma44 commented Jul 29, 2015

Nice! Thank you so much for getting this going. One suggestion: elsewhere we've using the MapboxAccessToken environment variable. If a token isn't specified in the command line invocation, this might be a nice fallback.

@sgillies
Copy link
Contributor Author

@sbma44 agreed.

sgillies added a commit that referenced this pull request Jul 29, 2015
@sgillies sgillies merged commit 1990a51 into master Jul 29, 2015
@sgillies sgillies deleted the geocoding-requests-style branch July 29, 2015 23:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants