我需要将经度和纬度坐标转换为国家或城市,在python中有这样的示例吗?
提前感谢!
发布于 2018-10-02 22:59:38
自那以后,谷歌已经降低了对其API的无钥匙访问权限。去谷歌注册一个密钥,你每天会得到大约1000个免费的查询。接受答案中的代码应该像这样修改(不能添加注释,没有足够的代表)。
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))
发布于 2017-10-05 23:27:08
一般来说,Google API是最好的方法。它不适合我的情况,因为我必须处理很多条目,并且api很慢。
我编写了一个小版本,做同样的事情,但首先下载一个巨大的几何图形,并在机器上计算国家。
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
发布于 2020-12-29 14:08:39
# Python3 program for reverse geocoding.
# importing necessary libraries
import reverse_geocoder as rg
from pandas import DataFrame
import pandas as pd
def reverseGeocode(coordinates):
result = rg.search(coordinates)
return result
# Coorinates tuple.Can contain more than one pair.
if __name__ == "__main__":
result = []
path = "CSV_NAME.CSV"
df = pd.read_csv(path, error_bad_lines=False)
for i in range(0, len(df)):
coordinates =(df["latitude"], df["longitude"])
result.append(reverseGeocode(coordinates))
print(result)
https://stackoverflow.com/questions/20169467
复制相似问题