使用Python从经纬度获取城市、州和国家名称
在本文中,我们将使用 Geopy 模块编写一个Python脚本,通过经纬度获取城市、州和国家/地区名称。geopy 使Python开发人员可以轻松定位地址、城市、国家/地区的坐标,和世界各地的地标。
要安装 Geopy 模块,请在终端中运行以下命令。
pip install geopy
方法:
- 导入 Geopy 模块
- 初始化 Nominatim API 以从输入字符串中获取位置。
- 使用 geolocator.reverse()函数获取位置。
- 现在从位置实例中提取数据
让我们一步一步来实现:
第 1 步:导入模块。
Python3
# import module
from geopy.geocoders import Nominatim
Python
# initialize Nominatim API
geolocator = Nominatim(user_agent="geoapiExercises")
Python3
# Latitude & Longitude input
Latitude = "25.594095"
Longitude = "85.137566"
location = geolocator.reverse(Latitude+","+Longitude)
# Display
print(location)
Python3
address = location.raw['address']
print(address)
Python3
city = address.get('city', '')
state = address.get('state', '')
country = address.get('country', '')
code = address.get('country_code')
zipcode = address.get('postcode')
print('City : ',city)
print('State : ',state)
print('Country : ',country)
print('Zip Code : ', zipcode)
Python3
# import module
from geopy.geocoders import Nominatim
# initialize Nominatim API
geolocator = Nominatim(user_agent="geoapiExercises")
# Latitude & Longitude input
Latitude = "25.594095"
Longitude = "85.137566"
location = geolocator.reverse(Latitude+","+Longitude)
address = location.raw['address']
# traverse the data
city = address.get('city', '')
state = address.get('state', '')
country = address.get('country', '')
code = address.get('country_code')
zipcode = address.get('postcode')
print('City : ', city)
print('State : ', state)
print('Country : ', country)
print('Zip Code : ', zipcode)
第 2 步:创建一个 Nominatim 对象并使用 geoapiExercises 参数初始化 Nominatim API。
Python
# initialize Nominatim API
geolocator = Nominatim(user_agent="geoapiExercises")
第 3 步:现在将纬度和经度分配给 geolocator.reverse() 方法。 reverse() 方法需要参数查询,并且还至少接受参数 exact_one,默认情况下为 True。
蟒蛇3
# Latitude & Longitude input
Latitude = "25.594095"
Longitude = "85.137566"
location = geolocator.reverse(Latitude+","+Longitude)
# Display
print(location)
输出:
[Location(Rajendra Nagar, Patna, Patna Rural, Patna, Bihar, 800001, India, (25.594023552508407, 85.13756080147536, 0.0))]
第 4 步:现在从给定的列表中获取信息,并使用原始函数() 解析为字典。
蟒蛇3
address = location.raw['address']
print(address)
输出:
{'suburb': 'Rajendra Nagar',
'city': 'Patna',
'county': 'Patna Rural',
'state_district': 'Patna',
'state': 'Bihar',
'postcode': '800001',
'country': 'India',
'country_code': 'in'}
第 5 步:现在遍历城市、州和国家/地区名称。
蟒蛇3
city = address.get('city', '')
state = address.get('state', '')
country = address.get('country', '')
code = address.get('country_code')
zipcode = address.get('postcode')
print('City : ',city)
print('State : ',state)
print('Country : ',country)
print('Zip Code : ', zipcode)
输出:
City : Patna
State : Bihar
Country : India
Zip Code : 800001
全面实施:
蟒蛇3
# import module
from geopy.geocoders import Nominatim
# initialize Nominatim API
geolocator = Nominatim(user_agent="geoapiExercises")
# Latitude & Longitude input
Latitude = "25.594095"
Longitude = "85.137566"
location = geolocator.reverse(Latitude+","+Longitude)
address = location.raw['address']
# traverse the data
city = address.get('city', '')
state = address.get('state', '')
country = address.get('country', '')
code = address.get('country_code')
zipcode = address.get('postcode')
print('City : ', city)
print('State : ', state)
print('Country : ', country)
print('Zip Code : ', zipcode)
输出:
City : Patna
State : Bihar
Country : India
Zip Code : 800001