首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从经度和纬度转换到国家或城市?

如何从经度和纬度转换到国家或城市?
EN

Stack Overflow用户
提问于 2013-11-24 08:00:14
回答 3查看 35.7K关注 0票数 27

我需要将经度和纬度坐标转换为国家或城市,在python中有这样的示例吗?

提前感谢!

EN

回答 3

Stack Overflow用户

发布于 2018-10-02 22:59:38

自那以后,谷歌已经降低了对其API的无钥匙访问权限。去谷歌注册一个密钥,你每天会得到大约1000个免费的查询。接受答案中的代码应该像这样修改(不能添加注释,没有足够的代表)。

代码语言:javascript
复制
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))
票数 11
EN

Stack Overflow用户

发布于 2017-10-05 23:27:08

一般来说,Google API是最好的方法。它不适合我的情况,因为我必须处理很多条目,并且api很慢。

我编写了一个小版本,做同样的事情,但首先下载一个巨大的几何图形,并在机器上计算国家。

代码语言:javascript
复制
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
票数 8
EN

Stack Overflow用户

发布于 2020-12-29 14:08:39

代码语言:javascript
复制
# 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20169467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档