pythonjsonmapboxgeocoding

Printing float values in Python (Mapbox geo-coordinates)


I'm scraping a website in order to get address which I then am running through the MapBox geo-coder using their API.

The basic code is:

geocoder = mapbox.Geocoder(access_token='my_token')


response = geocoder.forward(address)
result = response.json()

I'm feeding in the addresses on the fly, as I scrape the webpage.

When I print result, my geo-code output is:

{u'attribution': u'NOTICE: \xa9 2019 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/). This response and the information it contains may not be retained. POI(s) provided by Foursquare.', u'query': [u'1151', u'folsom', u'st', u'san', u'francisco', u'ca', u'94103'], u'type': u'FeatureCollection', u'features': [{u'center': [-122.408767, 37.775679], u'geometry': {u'type': u'Point', u'coordinates': [-122.408767, 37.775679]}, u'text': u'Folsom Street', u'place_type': [u'address'], u'properties': {u'accuracy': u'point'}, u'context': [{u'text': u'South of Market', u'id': u'neighborhood.293577'}, {u'text': u'94103', u'id': u'postcode.13892342768265050'}, {u'text': u'San Francisco', u'wikidata': u'Q62', u'id': u'place.15734669613361910'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'address': u'1151', u'relevance': 1, u'type': u'Feature', u'id': u'address.2415498141648290', u'place_name': u'1151 Folsom Street, San Francisco, California 94103, United States'}, {u'center': [-122.41, 37.78], u'geometry': {u'type': u'Point', u'coordinates': [-122.41, 37.78]}, u'text': u'94103', u'place_type': [u'postcode'], u'properties': {}, u'bbox': [-122.42770549164, 37.7635596779962, -122.397810189758, 37.7880518462807], u'context': [{u'text': u'San Francisco', u'wikidata': u'Q62', u'id': u'place.15734669613361910'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'relevance': 0.75, u'type': u'Feature', u'id': u'postcode.13892342768265050', u'place_name': u'San Francisco, California 94103, United States'}, {u'center': [-120.351259, 36.149281], u'geometry': {u'type': u'Point', u'coordinates': [-120.351259, 36.149281], u'interpolated': True}, u'text': u'Folsom Street', u'place_type': [u'address'], u'properties': {u'accuracy': u'interpolated'}, u'context': [{u'text': u'93210', u'id': u'postcode.15655570180150430'}, {u'text': u'Coalinga', u'wikidata': u'Q985263', u'id': u'place.11616473478769790'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'address': u'1151', u'relevance': 0.4, u'type': u'Feature', u'id': u'address.165808680526830', u'place_name': u'1151 Folsom Street, Coalinga, California 93210, United States'}, {u'center': [-121.270558, 37.935765], u'geometry': {u'omitted': True, u'type': u'Point', u'coordinates': [-121.270558, 37.935765], u'interpolated': True}, u'text': u'Folsom Street', u'place_type': [u'address'], u'properties': {u'accuracy': u'interpolated'}, u'context': [{u'text': u'Stockton Metropolitan Airport', u'id': u'neighborhood.289380'}, {u'text': u'95206', u'id': u'postcode.5171535940442000'}, {u'text': u'Stockton', u'wikidata': u'Q49240', u'id': u'place.8554008982780520'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'address': u'1151', u'relevance': 0.4, u'type': u'Feature', u'id': u'address.2752377031224260', u'place_name': u'1151 Folsom Street, Stockton, California 95206, United States'}, {u'center': [-121.3078506, 38.5879294], u'geometry': {u'type': u'Point', u'coordinates': [-121.3078506, 38.5879294]}, u'text': u'Folsom Boulevard', u'place_type': [u'address'], u'properties': {u'accuracy': u'street'}, u'context': [{u'text': u'Cordova Gardens', u'id': u'neighborhood.289486'}, {u'text': u'95670', u'id': u'postcode.7919884525467840'}, {u'text': u'Rancho Cordova', u'wikidata': u'Q986928', u'id': u'place.5058833534899630'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'relevance': 0.398, u'type': u'Feature', u'id': u'address.8435179963733262', u'place_name': u'Folsom Boulevard, Rancho Cordova, California 95670, United States'}]}

I then get the coordinates of the output, using:

coordinates = result['features'][1]['center']

However, when I try and print coordinates (geo-coordinates) for this same output, I get:

[-122.41, 37.78]

How can I get this to print the full float of the coordinates? Currently, it seems to be rounding the numbers to two decimal places. I've tried using: print ["%0.6f" % i for i in coordinates] but this just adds zeros to the end of it.


Solution

  • That is the full float of those coordinates.

    If you examine the json, you'll see that for only the second center (at index 1) there are two decimal places. The rest have at least six decimal places.

    running:

    for coordinates in result["features"]:
        print(coordinates["center"])
    

    resulted in:

    [-122.408767, 37.775679]
    [-122.41, 37.78]
    [-120.351259, 36.149281]
    [-121.270558, 37.935765]
    [-121.3078506, 38.5879294]