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 OpenCageGeocodekey = "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 pddata=pd.read_csv(‘Final.csv’)data.head(10)
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"] = latitudesdata["longitudes"] = longitudesdata.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集群来更好地帮助了解医院数量最多的地区。

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 Nominatimgeolocator = 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 longitudescoords=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 Nominatimgeolocator = 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 addressesaddres=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.taodudu.cc/news/show-997597.html

相关文章:

  • grafana 创建仪表盘_创建仪表盘前要问的三个问题
  • 大数据对社交媒体的影响_数据如何影响媒体,广告和娱乐职业
  • python 装饰器装饰类_5分钟的Python装饰器指南
  • 机器学习实际应用_机器学习的实际好处是什么?
  • mysql 时间推移_随着时间的推移可视化COVID-19新案例
  • 海量数据寻找最频繁的数据_寻找数据科学家的“原因”
  • kaggle比赛数据_表格数据二进制分类:来自5个Kaggle比赛的所有技巧和窍门
  • netflix_Netflix的Polynote
  • 气流与路易吉,阿戈,MLFlow,KubeFlow
  • 顶级数据恢复_顶级R数据科学图书馆
  • 大数据 notebook_Dockerless Notebook:数据科学期待已久的未来
  • 微软大数据_我对Microsoft的数据科学采访
  • 如何击败腾讯_击败股市
  • 如何将Jupyter Notebook连接到远程Spark集群并每天运行Spark作业?
  • twitter 数据集处理_Twitter数据清理和数据科学预处理
  • 使用管道符组合使用命令_如何使用管道的魔力
  • 2020年十大币预测_2020年十大商业智能工具
  • 为什么我们需要使用Pandas新字符串Dtype代替文本数据对象
  • nlp构建_使用NLP构建自杀性推文分类器
  • 时间序列分析 lstm_LSTM —时间序列分析
  • 泰晤士报下载_《泰晤士报》和《星期日泰晤士报》新闻编辑室中具有指标的冒险活动-第1部分:问题
  • 异常检测机器学习_使用机器学习检测异常
  • 特征工程tf-idf_特征工程-保留和删除的内容
  • 自我价值感缺失的表现_不同类型的缺失价值观和应对方法
  • 学习sql注入:猜测数据库_面向数据科学家SQL:学习简单方法
  • python自动化数据报告_如何:使用Python将实时数据自动化到您的网站
  • 学习深度学习需要哪些知识_您想了解的有关深度学习的所有知识
  • 置信区间估计 预测区间估计_估计,预测和预测
  • 地图 c-suite_C-Suite的模型
  • sap中泰国有预扣税设置吗_泰国餐厅密度细分:带有K-means聚类的python

使用Python进行地理编码和反向地理编码相关推荐

  1. Google Geocoding API---地理编码与反向地理编码

    什么是地理编码 地理编码是将地址(如"1600 Amphitheatre Parkway, Mountain View, CA")转换为地理坐标(如纬度 37.423021 和经度 ...

  2. 雅虎财经api_带有Yahoo API的Android反向地理编码– PlaceFinder

    雅虎财经api 在我之前的教程( 基于Android的基于位置的服务应用程序– GPS位置 )中,我向您展示了如何以经度和纬度坐标的形式检索用户的当前位置. 使用这些坐标,我们将提供有关用户位置的信息 ...

  3. 谷歌地图开发:地理编码和反地理编码

    谷歌地图Geocoding说明:内容主要来自谷歌官网的文档,谷歌地图android api地理和反地理编码反应迟钝内容有时不准确,这里使用的googlemap的webapi接口.嗯,需要vpn这个不多 ...

  4. IOS开发地理编码与反向编码

    IOS开发地理编码与反向编码 #import <CoreLocation/CoreLocation.h> 选中项目导入相应的框架 // // LJReveceGeocoderViewCon ...

  5. android谷歌反地理,Android反向地理编码显示不出来!

    我用google 的Geocoding API 接口来处理反向地理编码 ,但是运行时是空白界面,显示不出位置  下面是代码 ,请告诉我怎么办 public class MainActivity ext ...

  6. Elasticsearch:使用反向地理编码在地图上显示自定义区域统计数据

    在实际的许多应用中,我们可能并不一定按照行政区来进行划分区域,比如我们常说江浙一代,我们可以理解江苏和浙江这两个省合在一起,而不是把它们分开.我们有时也说长江三角区,它可能是跨几个省市的一个区域,而不 ...

  7. api地理编码_通过地理编码API使您的数据更有意义

    api地理编码 Motivation 动机 In my second semester of my Master's degree, I was working on a dataset which ...

  8. [转载] api地理编码_通过地理编码API使您的数据更有意义

    参考链接: Python | 反向地理编码以使用地理坐标获取地图上的位置 api地理编码 Motivation 动机 In my second semester of my Master's degr ...

  9. IOS 地理编码以及反地理编码

    2019独角兽企业重金招聘Python工程师标准>>> // //  ViewController.m //  地理编码以及反地理编码 // //  Created by dc008 ...

最新文章

  1. 金山android 杀毒软件,金山手机卫士
  2. [转]Effective C# 原则5:始终提供ToString()
  3. 一文读懂word embedding
  4. 边打“游戏”边学Vim!这款在线、交互的练习工具火了
  5. php new redis错误,解决PHP Redis扩展无法加载的问题(zend_new_interned_string in Unknown on line 0)...
  6. python快速编程入门教程-半小时带你快速入门Python编程,Python快速入门教程
  7. 最大并发连接数和最大会话数的区别
  8. Qt学习(七):定时器QTimer
  9. (21)Spring Boot过滤器、监听器【从零开始学Spring Boot】
  10. 高考计算机专业最低分数线是多少,2021最低多少分可以稳上二本 高考二本分数线是多少...
  11. Django 路由系统
  12. presto 使用 部署_探秘Presto+Alluxio高效云端SQL查询
  13. UCOII信号量与消息邮箱
  14. 全球及中国汽车节能减排行业投资可行性及十四五发展趋势研究报告2021-2027年
  15. matlab ode 初值,关于ODE45初值问题和erf函数的问题
  16. 关于pyecharts可视化进阶中国经济、人口等数据
  17. 策略产品的进修之路—了解策略和策略产品
  18. 桌面上 计算机 回收站不见了怎么办,桌面上的回收站图标不见了怎么办
  19. HTML5+JavaScript实现进度条效果
  20. pr预设的卷及内核锐化是什么_PR CC 特效

热门文章

  1. 2019.04.09 电商25 结算功能1
  2. JMETER从JSON响应中提取数据
  3. git 版本控制(一)
  4. windows下Call to undefined function curl_init() error问题
  5. java基本类型的默认值及其取值范围
  6. U-boot 打补丁,编译,设置环境变量,
  7. IE8无法调试?IE进入不了调试状态
  8. void Update ( ) 更新 void FixedUpdate ( )
  9. java版电子商务spring cloud分布式微服务b2b2c社交电商:服务容错保护(Hystrix断路器)...
  10. Windows下安装Python模块时环境配置