日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

使用Python进行地理编码和反向地理编码

發(fā)布時(shí)間:2023/11/29 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Python进行地理编码和反向地理编码 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

地理編碼是獲取輸入文本(例如地址或地點(diǎn)名稱(chēng))并返回緯度/經(jīng)度位置的過(guò)程。 簡(jiǎn)而言之,地理編碼會(huì)將物理地址轉(zhuǎn)換為緯度和經(jīng)度。

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選項(xiàng)。 一些受歡迎的是GeoPy,OpenCage地理編碼器,google地理編碼。 Geopy是為非商業(yè)用途提供無(wú)限訪問(wèn)的少數(shù)API服務(wù)之一。 對(duì)于Google API和OpenCage地理編碼器,每天限制為2500個(gè)請(qǐng)求。 使用geopy,我的數(shù)據(jù)集中某些地址的經(jīng)度和緯度顯示了不同的國(guó)家,而不是美國(guó)。 使用OpenCage 地理編碼器時(shí) ,令人驚訝的是所有地址都是準(zhǔn)確的,因此我使用了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,應(yīng)首先使用pip install opencage安裝python庫(kù)。有關(guān)此庫(kù)的更多信息,請(qǐng)參見(jiàn): 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.

安裝庫(kù)后,您將需要一個(gè)OpenCage地理編碼器帳戶來(lái)生成API密鑰。 可以使用opencagedata.com創(chuàng)建免費(fèi)帳戶。 注冊(cè)帳戶后,即可在儀表板中找到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.

我們獲得了一家名為阿拉巴馬州東南醫(yī)療中心的醫(yī)院的經(jīng)度和緯度。 在大多數(shù)情況下,像現(xiàn)在一樣,我們將需要在地圖中繪制多個(gè)地址。 在這種情況下,使用熊貓創(chuàng)建數(shù)據(jù)框會(huì)容易得多。 我使用的數(shù)據(jù)集包含美國(guó)所有醫(yī)院的列表以及醫(yī)院所在縣的COVID-19總病例數(shù)。 數(shù)據(jù)集可從此處下載。

import pandas as pd
data=pd.read_csv(‘Final.csv’)
data.head(10)Hospital location data frame醫(yī)院位置數(shù)據(jù)框

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.

我們有一個(gè)數(shù)據(jù)框,其中包含美國(guó)所有醫(yī)院的設(shè)施名稱(chēng)及其地址的列表,因此我們只需要查找位置坐標(biāo)即可。

First, we should convert the Address column to the list. So, it will be easier to loop all the addresses.

首先,我們應(yīng)該將“地址”列轉(zhuǎn)換為列表。 因此,將更容易循環(huán)所有地址。

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

接下來(lái),從OpenCage地理編碼器網(wǎng)站輸入您的API密鑰,并創(chuàng)建一個(gè)空列表來(lái)存儲(chǔ)緯度和經(jīng)度。 創(chuàng)建空列表后,創(chuàng)建一個(gè)循環(huán),該循環(huán)為所有地址提供緯度和經(jīng)度

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.

我們?cè)跀?shù)據(jù)框中列出了所有地址的經(jīng)度和緯度。 我們可以使用此簡(jiǎn)單的pandas命令將此緯度和經(jīng)度添加到我們現(xiàn)有的數(shù)據(jù)框中。

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.

最后,我們獲得了所有醫(yī)院地址的經(jīng)度和緯度。 為了更好地理解此位置坐標(biāo),讓我們使用葉片地圖在地圖上將所有這些位置坐標(biāo)繪制為點(diǎn)。

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.

現(xiàn)在,我們可以看到美國(guó)所有醫(yī)院的位置。 我使用CircleMarker集群來(lái)更好地幫助了解醫(yī)院數(shù)量最多的地區(qū)。

A snapshot of the map visualization (clustered locations) created using Folium使用Folium創(chuàng)建的地圖可視化快照(聚集位置)

反向地理編碼 (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.

另一方面, 反向地理編碼會(huì)將地理坐標(biāo)轉(zhuǎn)換為位置的描述,通常是位置名稱(chēng)或可尋址位置。 地理編碼依賴于地址點(diǎn),街道/道路網(wǎng)絡(luò)以及郵政和行政邊界的計(jì)算機(jī)表示。

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.

對(duì)于反向地??理編碼,與OpenCage Geocoder相比,我發(fā)現(xiàn)Geopy API的輸出格式更加詳細(xì)。 而且,對(duì)于Geopy API沒(méi)有限制,因此我們將使用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

產(chǎn)出:美利堅(jiān)合眾國(guó)阿拉巴馬州多森市阿爾瑪街東南健康醫(yī)學(xué)中心,美國(guó)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

產(chǎn)出:'東南健康校園,1108,羅斯克拉克圈,莫里斯高地,多森,休斯敦縣,阿拉巴馬州,36301,美國(guó)

使用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.

對(duì)于反向地??理編碼,如上所述,首先,我們將緯度和經(jīng)度轉(zhuǎn)換為列表并將它們壓縮在一起。

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.

因?yàn)槲覀円呀?jīng)創(chuàng)建了列表,所以像上面一樣,我們將創(chuàng)建一個(gè)循環(huán)以查找每個(gè)位置坐標(biāo)的地址并將它們附加在一起。

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.

最后,我們擁有美國(guó)所有醫(yī)院的地址列表。

For interested readers, I put the code in my GitHub Repo here. If you have any doubts, contact me using linkedin.

對(duì)于感興趣的讀者,我將代碼放在此處的 GitHub Repo中。 如有任何疑問(wèn),請(qǐng)使用linkedin與我聯(lián)系。

翻譯自: https://towardsdatascience.com/geocoding-and-reverse-geocoding-using-python-36a6ad275535

總結(jié)

以上是生活随笔為你收集整理的使用Python进行地理编码和反向地理编码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。