Python 如何将经纬度转换为国家或城市?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20169467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert from longitude and latitude to country or city?
提问by godzilla
I need to convert longitude and latitude coordinates to either country or city, is there an example of this in python?
我需要将经度和纬度坐标转换为国家或城市,python 中有这样的例子吗?
thanks in advance!
提前致谢!
采纳答案by Holy Mackerel
I use Google's API.
我使用谷歌的 API。
from urllib2 import urlopen
import json
def getplace(lat, lon):
url = "http://maps.googleapis.com/maps/api/geocode/json?"
url += "latlng=%s,%s&sensor=false" % (lat, lon)
v = urlopen(url).read()
j = json.loads(v)
components = j['results'][0]['address_components']
country = town = None
for c in components:
if "country" in c['types']:
country = c['long_name']
if "postal_town" in c['types']:
town = c['long_name']
return town, country
print(getplace(51.1, 0.1))
print(getplace(51.2, 0.1))
print(getplace(51.3, 0.1))
Output:
输出:
(u'Hartfield', u'United Kingdom')
(u'Edenbridge', u'United Kingdom')
(u'Sevenoaks', u'United Kingdom')
回答by pfctdayelise
This is called reverse geocoding. There is one library I could find in Python which is focused on this: https://github.com/thampiman/reverse-geocoder
这称为反向地理编码。我可以在 Python 中找到一个专注于此的库:https: //github.com/thampiman/reverse-geocoder
Some related questions with other ideas:
一些与其他想法相关的问题:
回答by linqu
In general the Google API is the best approach. It was not suitable for my case as I had to process a lot of entries and the api is slow.
一般来说,谷歌 API 是最好的方法。它不适合我的情况,因为我必须处理大量条目并且 api 很慢。
I coded a small version that does the same but downloads a huge geometry first and computes the countries on the machine.
我编写了一个执行相同操作的小版本,但首先下载一个巨大的几何图形并计算机器上的国家/地区。
import requests
from shapely.geometry import mapping, shape
from shapely.prepared import prep
from shapely.geometry import Point
data = requests.get("https://raw.githubusercontent.com/datasets/geo-countries/master/data/countries.geojson").json()
countries = {}
for feature in data["features"]:
geom = feature["geometry"]
country = feature["properties"]["ADMIN"]
countries[country] = prep(shape(geom))
print(len(countries))
def get_country(lon, lat):
point = Point(lon, lat)
for country, geom in countries.iteritems():
if geom.contains(point):
return country
return "unknown"
print(get_country(10.0, 47.0))
# Austria
回答by tibernut
Google has since depreciated keyless access to their API. Head over to google and register for a key, you get ~ 1,000 free queries a day. Code in accepted answer should be modified like this (can't add a comment, not enough rep).
自那以后,谷歌已经贬低了对其 API 的无密钥访问。前往谷歌并注册一个密钥,您每天可以获得约 1,000 次免费查询。接受的答案中的代码应该像这样修改(不能添加评论,没有足够的代表)。
from urllib.request import urlopen
import json
def getplace(lat, lon):
key = "yourkeyhere"
url = "https://maps.googleapis.com/maps/api/geocode/json?"
url += "latlng=%s,%s&sensor=false&key=%s" % (lat, lon, key)
v = urlopen(url).read()
j = json.loads(v)
components = j['results'][0]['address_components']
country = town = None
for c in components:
if "country" in c['types']:
country = c['long_name']
if "postal_town" in c['types']:
town = c['long_name']
return town, country
print(getplace(51.1, 0.1))
print(getplace(51.2, 0.1))
print(getplace(51.3, 0.1))

