Geocoding is the process of taking input text, such as an address or the name of a place, and returning a latitude/longitude location. To put it simply, Geocoding is converting physical address to latitude and longitude.
地理编码是获取输入文本(例如地址或地点名称)并返回纬度/经度位置的过程。 简而言之,地理编码会将物理地址转换为纬度和经度。
There are many geocoding API options available in python. Some of the popular ones are GeoPy, OpenCage geocoder, google geocoding. Geopy is one of the few API services which provides unlimited access for non-commercial use. For Google API and OpenCage geocoders, there is a limit of 2500 requests per/day. Using geopy, the latitudes and longitudes for some addresses in my dataset, showed different countries instead of the US. With OpenCage geocoder, surprisingly all the addresses were accurate so I used OpenCage encoder.
python中提供了许多地理编码API选项。 一些受欢迎的是GeoPy,OpenCage地理编码器,google地理编码。 Geopy是为非商业用途提供无限访问的少数API服务之一。 对于Google API和OpenCage地理编码器,每天限制为2500个请求。 使用geopy,我的数据集中某些地址的经度和纬度显示了不同的国家,而不是美国。 使用OpenCage 地理编码器时 ,令人惊讶的是所有地址都是准确的,因此我使用了OpenCage编码器。
使用OpenCage地理编码器和熊猫 (Working with OpenCage geocoder and pandas)
To use OpenCage Geocoder in python, the python library should be installed first using pip install opencage
.More info about this library can be found here: OpenCageGeocode on Github
要在python中使用OpenCage Geocoder,应首先使用pip install opencage
安装python库。有关此库的更多信息,请参见: Github上的OpenCageGeocode
Once the library is installed, you will need an OpenCage geocoder account to generate an API key. A free account can be created using opencagedata.com. Once you signup for an account you can find API keys in Dashboard as shown in the below image.
安装库后,您将需要一个OpenCage地理编码器帐户来生成API密钥。 可以使用opencagedata.com创建免费帐户。 注册帐户后,即可在仪表板中找到API密钥,如下图所示。
例 (Example)
from opencage.geocoder import OpenCageGeocode
key = "Enter_your_Api_Key"
geocoder = OpenCageGeocode(key)
address='1108 ROSS CLARK CIRCLE,DOTHAN,HOUSTON,AL'
result = geocoder.geocode(address, no_annotations="1")
result[0]['geometry']
Output: {‘lat’: 31.2158271, ‘lng’: -85.3634326}
输出:{'lat':31.2158271,'lng':-85.3634326}
We got the latitude and longitude for one hospital named Southeast Alabama Medical Center. In most cases, we will have multiple addresses that need to be plotted in maps as we do now. In this case, using pandas to create a data frame will be a lot easier. The dataset I used contains the list of all Hospitals in the US along with the COVID-19 total cases for the counties where hospitals are located. The dataset can be downloaded from here.
我们获得了一家名为阿拉巴马州东南医疗中心的医院的经度和纬度。 在大多数情况下,像现在一样,我们将需要在地图中绘制多个地址。 在这种情况下,使用熊猫创建数据框会容易得多。 我使用的数据集包含美国所有医院的列表以及医院所在县的COVID-19总病例数。 数据集可从此处下载。
import pandas as pd
data=pd.read_csv(‘Final.csv’)
data.head(10)
We have a data frame that contains the list of Facility Name of all Hospitals in the US and their addresses, so we just need to find location coordinates.
我们有一个数据框,其中包含美国所有医院的设施名称及其地址的列表,因此我们只需要查找位置坐标即可。
First, we should convert the Address column to the list. So, it will be easier to loop all the addresses.
首先,我们应该将“地址”列转换为列表。 因此,将更容易循环所有地址。
Next, enter your API key from OpenCage geocoder website and create empty lists to store latitudes and longitudes. After creating empty list, create a loop which gives latitude’s and longitude’s for all addresses
接下来,从OpenCage地理编码器网站输入您的API密钥,并创建一个空列表来存储纬度和经度。 创建空列表后,创建一个循环,该循环为所有地址提供纬度和经度
addresses = data["Full_Address"].values.tolist()
key = "Enter-your-key-here"
geocoder = OpenCageGeocode(key)
latitudes = []
longitudes = []
for address in addresses:
result = geocoder.geocode(address, no_annotations="1")
if result and len(result):
longitude = result[0]["geometry"]["lng"]
latitude = result[0]["geometry"]["lat"]
else:
longitude = "N/A"
latitude = "N/A"
latitudes.append(latitude)
longitudes.append(longitude)
We have latitudes and longitudes for the list of all the addresses in the data frame. we can add this latitudes and longitudes to our existing data frame using this simple pandas command.
我们在数据框中列出了所有地址的经度和纬度。 我们可以使用此简单的pandas命令将此纬度和经度添加到我们现有的数据框中。
data["latitudes"] = latitudes
data["longitudes"] = longitudes
data.head(10)
Finally, we got the latitude and longitudes for all the hospital addresses. To better understand this location coordinates let’s plot all this location coordinates as points in map using folium maps.
最后,我们获得了所有医院地址的经度和纬度。 为了更好地理解此位置坐标,让我们使用叶片地图在地图上将所有这些位置坐标绘制为点。
folium_map= folium.Map(location=[33.798259,-84.327062],zoom_start=4.4,tiles=’CartoDB dark_matter’)FastMarkerCluster(data[[‘latitudes’, ‘longitudes’]].values.tolist()).add_to(folium_map)folium.LayerControl().add_to(folium_map) for row in final.iterrows():
row=row[1]
folium.CircleMarker(location=(row["latitudes"],
row["longitudes"]),
radius= 10,
color="#007849",
popup=row[‘Facility_Name’],
fill=False).add_to(folium_map)
folium_map
Now, we can see the location points of all the hospitals in the USA. I used CircleMarker cluster to better help understand the regions with most number of hospitals.
现在,我们可以看到美国所有医院的位置。 我使用CircleMarker集群来更好地帮助了解医院数量最多的地区。
反向地理编码 (Reverse Geocoding)
Reverse geocoding, on the other hand, converts geographic coordinates to a description of a location, usually the name of a place or an addressable location. Geocoding relies on a computer representation of address points, the street/road network, together with postal and administrative boundaries.
另一方面, 反向地理编码会将地理坐标转换为位置的描述,通常是位置名称或可寻址位置。 地理编码依赖于地址点,街道/道路网络以及邮政和行政边界的计算机表示。
For reverse geocoding, I found the output format of Geopy API more detailed when compared to OpenCage Geocoder. And also, there is no limit for Geopy API so we will Geopy instead of OpenCage Geocoder.
对于反向地理编码,与OpenCage Geocoder相比,我发现Geopy API的输出格式更加详细。 而且,对于Geopy API没有限制,因此我们将使用Geopy代替OpenCage Geocoder。
OpenCage Reverse Geocoder Example
OpenCage反向地理编码器示例
result = geocoder.reverse_geocode(31.2158271,-85.3634326)
result[0][‘formatted’]
Output : Southeast Health Medical Center, Alma Street, Dothan, AL 36302, United States of America
产出:美利坚合众国阿拉巴马州多森市阿尔玛街东南健康医学中心,美国36302
Geopy Reverse Geocoder Example
Geopy反向地理编码器示例
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="test_app")
location = geolocator.reverse("31.2158271,-85.3634326")
location.raw[‘display_name’]
Output: ‘Southeast Health Campus, 1108, Ross Clark Circle, Morris Heights, Dothan, Houston County, Alabama, 36301, United States of America
产出:'东南健康校园,1108,罗斯克拉克圈,莫里斯高地,多森,休斯敦县,阿拉巴马州,36301,美国
使用Geopy Geocoder和熊猫 (Working with Geopy Geocoder and pandas)
For reverse geocoding, as above first, we will convert latitude and longitude to list and zip them together.
对于反向地理编码,如上所述,首先,我们将纬度和经度转换为列表并将它们压缩在一起。
lats=data['latitudes'].to_list()
lons=data['longitudes'].to_list()
# Creating a zip with latitudes and longitudes
coords=list(zip(lats,lons))
Since, we already created list, just like above we will create a loop to find address for each location coordinate and append them together.
因为我们已经创建了列表,所以像上面一样,我们将创建一个循环以查找每个位置坐标的地址并将它们附加在一起。
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="test_app")
full_address=[]
for i in range(len(coords)):
location = geolocator.reverse(coords[i])
address=location.raw['address']['country']
full_address.append(address)
#Creating dataframe with all the addresses
addres=pd.DataFrame(data=full_address , columns=['Address'])
addres
Finally, we have the address list of all hospitals in the US.
最后,我们拥有美国所有医院的地址列表。
For interested readers, I put the code in my GitHub Repo here. If you have any doubts, contact me using linkedin.
对于感兴趣的读者,我将代码放在此处的 GitHub Repo中。 如有任何疑问,请使用linkedin与我联系。
翻译自: https://towardsdatascience.com/geocoding-and-reverse-geocoding-using-python-36a6ad275535
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392288.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!