在Python中打印浮点值(Mapbox地理坐标)

时间:2019-05-06 03:35:55

标签: python json mapbox geocoding

我正在抓取一个网站,以获取地址,然后我使用其API通过MapBox地理编码器运行该地址。

基本代码是:

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


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

我在抓取网页时动态输入地址。

当我打印result时,我的地理代码输出是:

{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'}]}

然后我使用以下方法获取输出的坐标

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

但是,当我尝试为同一输出打印coordinates(地理坐标)时,我得到:

[-122.41, 37.78]

如何获取此信息以打印坐标的全部浮动?当前,似乎将数字四舍五入到小数点后两位。我尝试使用:print ["%0.6f" % i for i in coordinates],但这只是在其末尾添加零。谢谢。

1 个答案:

答案 0 :(得分:1)

这是这些坐标的完整浮点数。

如果您检查json,您会发现,仅 第二个center(在索引1处)有两个小数位。其余的至少有六个小数位。

运行:

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

导致:

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