api地理编码
Motivation
动机
In my second semester of my Master’s degree, I was working on a dataset which had all the records of the road accident in Victoria, Australia (2013-19). I was very curious to know, which national highways are the most dangerous in Victoria, and need attention to improve safety. The dataset did have the longitude and latitude coordinates where the accidents occurred, but it did not have other supporting and useful information like the name of roads where the accident took place, or the suburb, LGA etc. So I thought there must be a way to retrieve that information and analyse it, since its all geographic information which should be linked to the location coordinates. The answer was extracting the information through the process of reverse-geocoding.
在攻读硕士学位的第二学期,我正在研究一个数据集,其中包含澳大利亚维多利亚州(2013-19年)道路交通事故的所有记录。 我很好奇,维多利亚州哪些国道最危险,需要注意以提高安全性。 该数据集确实具有发生事故的经度和纬度坐标,但是它没有其他支持和有用的信息,例如发生事故的道路名称,郊区,LGA等。因此,我认为必须有一种方法检索并分析该信息,因为应该将所有地理信息链接到位置坐标。 答案是通过反向地理编码来提取信息。
What does Geocoding/Reverse-geocoding mean?
地理编码/反向地理编码是什么意思?
Geocoding is the process of converting addresses (like “1600 Amphitheatre Parkway, Mountain View, CA”) into geographic coordinates (like latitude 37.423021 and longitude -122.083739). This might be very useful if your planning to visualise the concentration of your data points in a map. Reverse geocoding is the process of converting geographic coordinates into a human-readable address. If you have good wrangling skills, you can extract other useful information like the nearby street/highway, district area or a landmark.
地理编码是将地址(如“ 1600 Amphitheatre Parkway,Mountain View,CA”)转换为地理坐标(如纬度37.423021和经度-122.083739)的过程。 如果您计划可视化地图中数据点的集中度,这可能非常有用。 反向地理编码是将地理坐标转换为人类可读地址的过程。 如果您有良好的摔跤技巧,则可以提取其他有用的信息,例如附近的街道/高速公路,地区或地标。
My aim was to use reverse-geocoding to extract the name of the highways on which those accidents occurred. There are many platforms available for geocoding, but I used the Google Maps Platform. It is one of the best platforms available which gives you accurate and detailed information. It also offers free services for up to 100,000 requests.
我的目的是使用反向地理编码来提取发生这些事故的高速公路的名称。 有许多可用于地理编码的平台,但是我使用了Google Maps Platform。 它是可为您提供准确和详细信息的最佳平台之一。 它还为多达100,000个请求提供免费服务。
Implementation for Reverse-Geocoding
反向地理编码的实现
I have used python here to implement reverse-geocoding.
我在这里使用python来实现反向地理编码。
Step 1: — Get your Google API Key and enable Geocoding API service. You can refer to this YouTube link to help you set it up.
步骤1:—获取您的Google API密钥并启用地址解析API服务。 您可以参考此YouTube 链接来帮助您进行设置。
Step 2: — Use the reverse_geocode function which converts the location coordinates and receives API output.
步骤2:—使用reverse_geocode函数转换位置坐标并接收API输出。
Remember you have to the input the location coordinates as a tuple. I have provided a screenshot of my implementation which converts 10 location coordinates and retrieves information in JSON format about the region via the API.
记住,您必须以元组的形式输入位置坐标。 我提供了实现的屏幕截图,该屏幕截图可转换10个位置坐标,并通过API以JSON格式检索有关该区域的信息。
![Image for post](https://miro.medium.com/max/9999/1*AANYko3_EUULBKlKtoEdIg.png)
Note: For geocoding you have to use the ‘geocode(<address>)’ method. The result should be stored in a list of size 2.
注意:对于地理编码,您必须使用'geocode(<address>)'方法。 结果应存储在大小为2的列表中。
Step3: — Extracting the information in the API output to get the required information.
步骤3:—在API输出中提取信息以获取所需的信息。
This is perhaps the trickiest part of the implementation especially in reverse geocoding. There is a lot of information retrieved and you might need to wrangle the data you want before any analysis. Here’s how the output of one set of location coordinates i.e. (-37.81125,144.93391) in the above data frame looks like: -
这可能是实现过程中最棘手的部分,尤其是在反向地理编码中。 检索到很多信息,您可能需要在进行任何分析之前先整理所需的数据。 这是上述数据框中的一组位置坐标即(-37.81125,144.93391)的输出如下所示:-
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/fa3939c644e8f92a612bf3d82b03cdbf.png)
As you can clearly see in the output, the accident occurred on the ‘City Link’ Highway in Victoria.
从输出中可以清楚地看到,事故发生在维多利亚州的“ City Link”高速公路上。
The problem I faced here was that the API output from all the accident records was not always in the same format as above. Many of them had different information put in different places. Hence I had to use my wrangling skills to extract the required information from each response.
我在这里遇到的问题是,所有事故记录的API输出并不总是与上述格式相同。 他们中许多人在不同的地方放置了不同的信息。 因此,我不得不使用自己的争吵技巧从每个响应中提取所需的信息。
So, I came up with the idea of extracting the route number of the highway instead of the name on which the accidents occurred. The highways in Victoria are numbered in the pattern starting with ‘M’ and a number. For example, M2 here is the ‘City Link Highway’ or also known as the Western Link. This format was very easy to extract using ‘re’ package of python since it followed a definite pattern. After the extraction, I was easily able to convert these codes to their respective Highway names. I have provided an illustration of that code as well.
因此,我想到了提取高速公路的路线号而不是事故发生的名称的想法。 维多利亚州的高速公路以“ M”和一个数字开头。 例如,这里的M2是“城市链接高速公路”或也称为“西方链接”。 这种格式很容易使用python的“ re”包提取,因为它遵循确定的模式。 提取后,我可以轻松地将这些代码转换为各自的公路名称。 我也提供了该代码的说明。
Note: You can also access the data through the JSON hierarchy, but then again it might not be the same always.
注意:您也可以通过JSON层次结构访问数据,但是同样,它也不一定总是相同的 。
![Image for post](https://miro.medium.com/max/9999/1*x1Q6gJzX5kmPpxjWLGDVmg.png)
Mission Accomplished!
任务完成!
I personally really enjoyed the process of getting more information to support my analysis of the data set. It gave me deeper insights and understanding of the frequency of road accidents taking place in Victoria. Many times the information is not as detailed as you would expect and you will have to merge other data sets to get more value out of your main data set. If the above explanations are not clear, do reach out to me via Linkedin & I will be happy to assist you. Thank You!
我个人真的很喜欢获得更多信息以支持我对数据集进行分析的过程。 它使我对维多利亚州发生道路交通事故的频率有了更深刻的了解和理解。 很多时候,信息没有您期望的那么详细,您将不得不合并其他数据集以从主数据集中获得更多价值。 如果以上解释不清楚,请通过Linkedin与我联系,我们很乐意为您提供帮助。 谢谢!
翻译自: https://medium.com/swlh/making-more-sense-of-your-data-through-geocoding-api-3bcbc609c993
api地理编码