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

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密钥,如下图所示。

Image for post

(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)
Image for post
Hospital location data frame
医院位置数据框

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集群来更好地帮助了解医院数量最多的地区。

Image for post
A snapshot of the map visualization (clustered locations) created using Folium
使用Folium创建的地图可视化快照(聚集位置)

反向地理编码 (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
Image for post

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,一经查实,立即删除!

相关文章

java开发简历编写_如何通过几个简单的步骤编写出色的初级开发人员简历

java开发简历编写So you’ve seen your dream junior developer role advertised, and are thinking about applying. It’s time to write that Resume! Nothing better than sitting down to a blank piece of paper and not knowing how to start, right?因此,您…

leetcode 628. 三个数的最大乘积(排序)

给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。 示例 1: 输入: [1,2,3] 输出: 6 解题思路 最大的乘积可能有两种情况 1.两个最小负数和一个最大正数 2.三个最大正数 代码 class Solution {public int maximumProduct(int[…

[Object-C语言随笔之三] 类的创建和实例化以及函数的添加和调用!

上一小节的随笔写了常用的打印以及很基础的数据类型的定义方式,今天就来一起学习下如何创建类与函数的一些随笔; 首先类的创建:在Xcode下,菜单File-New File,然后出现选择class模板,如下图&…

2024-AI人工智能学习-安装了pip install pydot但是还是报错

2024-AI人工智能学习-安装了pip install pydot但是还是报错 出现这样子的错误: /usr/local/bin/python3.11 /Users/wangyang/PycharmProjects/studyPython/tf_model.py 2023-12-24 22:59:02.238366: I tensorflow/core/platform/cpu_feature_guard.cc:182] This …

grafana 创建仪表盘_创建仪表盘前要问的三个问题

grafana 创建仪表盘可视化 (VISUALIZATIONS) It’s easier than ever to dive into dashboarding, but are you doing it right?深入仪表板比以往任何时候都容易,但是您这样做正确吗? Tableau, Power BI, and many other business intelligence tools …

qq群 voiceover_如何在iOS上使用VoiceOver为所有人构建应用程序

qq群 voiceoverby Jayven N由Jayven N 如何在iOS上使用VoiceOver为所有人构建应用程序 (How to build apps for everyone using VoiceOver on iOS) 辅助功能入门 (Getting started with accessibility) There’s always those topics that people don’t talk about enough. S…

IntelliJ IDEA代码常用的快捷键(自查)

IntelliJ IDEA代码常用的快捷键有: Alt回车 导入包,自动修正 CtrlN 查找类 CtrlShiftN 查找文件 CtrlAltL 格式化代码 CtrlAltO 优化导入的类和包 AltInsert 生成代码(如get,set方法,构造函数等) CtrlE或者AltShiftC 最近更改的代码 CtrlR…

leetcode 1489. 找到最小生成树里的关键边和伪关键边(并查集)

给你一个 n 个点的带权无向连通图,节点编号为 0 到 n-1 ,同时还有一个数组 edges ,其中 edges[i] [fromi, toi, weighti] 表示在 fromi 和 toi 节点之间有一条带权无向边。最小生成树 (MST) 是给定图中边的一个子集,它连接了所有…

带彩色字体的man pages(debian centos)

1234567891011121314151617181920212223242526272829303132333435363738我的博客已迁移到xdoujiang.com请去那边和我交流简介most is a paging program that displays,one windowful at a time,the contents of a file on a terminal. It pauses after each windowful and prin…

提取json对象中的数据,转化为数组

var xx1 ["乐谱中的调号为( )调", "写出a自然小调音阶。", "以G为冠音,构写增四、减五音程。", "调式分析。", "将下列乐谱移为C大调。", "正确组合以下乐谱。", "以下…

java 同步块的锁是什么,java – 同步块 – 锁定多个对象

我添加了另一个答案,因为我还没有添加评论给其他人的帖子。>事实上,同步是用于代码,而不是对象或数据。在同步块中用作参数的对象引用表示锁定。所以如果你有如下代码:class Player {// Same instance shared for all players.…

大数据对社交媒体的影响_数据如何影响媒体,广告和娱乐职业

大数据对社交媒体的影响In advance of our upcoming event — Data Science Salon: Applying AI and ML to Media, Advertising, and Entertainment, we asked our speakers, who are some of nation’s leading data scientists in the media, advertising, and entertainment…

Go-项目结构和代码组织

简介 做大量的输入,通过对比、借鉴,加上自己的经验,产出一个尽可能优的方案。 开源界优秀项目的结构示例 因为最新的 Go 版本已经使用 module 作为版本依赖,所以,所有项目的 vendor 我都忽略,建议直接使用 …

iref streams_如何利用Neo4j Streams并建立即时数据仓库

iref streamsby Andrea Santurbano通过安德里亚桑图尔巴诺(Andrea Santurbano) 如何利用Neo4j Streams并建立即时数据仓库 (How to leverage Neo4j Streams and build a just-in-time data warehouse) In this article, we’ll show how to create a Just-In-Time Data Wareho…

Nodejs正则表达式函数之match、test、exec、search、split、replace使用详解

1. Match函数使用指定的正则表达式函数对字符串惊醒查找,并以数组形式返回符合要求的字符串原型:stringObj.match(regExp)参数:stringObj 必选项,需要去进行匹配的字符串RegExp 必选项,指定的正则表达式返回值&#xf…

Zabbix 3.0 从入门到精通(zabbix使用详解)

第1章 zabbix监控 1.1 为什么要监控 在需要的时刻,提前提醒我们服务器出问题了 当出问题之后,可以找到问题的根源 网站/服务器 的可用性 1.1.1 网站可用性 在软件系统的高可靠性(也称为可用性,英文描述为HA,High Avail…

python 装饰器装饰类_5分钟的Python装饰器指南

python 装饰器装饰类重点 (Top highlight)There’s no doubt that Python decorators are one of the more advanced and tougher-to-understand programming concepts. This doesn’t mean you should avoid learning them — as you encounter them in production code soone…

php中颜色的索引值,计算PHP中两种颜色之间的平均颜色,使用索引号作为参考值...

我们假设为了讨论的目的,每个颜色都有一个“值”.那么,你想要的就足够简单:$index 0.2;$val1 get_value_of_color($color1);$val2 get_value_of_color($color2);$newval $val1 * $index $val2 * (1 - $index);$newcolor get_color_from_value($newval);所以,很…

leetcode 989. 数组形式的整数加法

对于非负整数 X 而言,X 的数组形式是每位数字按从左到右的顺序形成的数组。例如,如果 X 1231,那么其数组形式为 [1,2,3,1]。 给定非负整数 X 的数组形式 A,返回整数 XK 的数组形式。 示例 1: 输入:A […

您需要了解的WordPress漏洞以及如何修复它们

by Joel S. Syder乔尔赛德(Joel S.Syder) 您需要了解的WordPress漏洞以及如何修复它们 (WordPress vulnerabilities you need to know about — and how to fix them) WordPress is an incredibly useful and versatile platform for all kinds of blogging. It’s become ver…