楼主: oliyiyi
1324 1

Google Geo Data – Data Access Without Restrictions [推广有奖]

版主

泰斗

0%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
271951 个
通用积分
31269.3519
学术水平
1435 点
热心指数
1554 点
信用等级
1345 点
经验
383775 点
帖子
9598
精华
66
在线时间
5468 小时
注册时间
2007-5-21
最后登录
2024-4-18

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Geo-Distances are of great importance: Researchers from various disciplines refer to geographic distances – health researchers refer to geographic data when analyzing the spread of diseases, economists when evaluating the impact of transaction costs on human behavior, or sociologists when evaluating interpersonal distances (based on external factors) in human interaction.

However, each query sent to the Google Maps Distance Matrix API (currently available via the ggmap-package) is limited by the number of allowed elements, where the number of origins times the number of destinations defines the number of elements. The Google Maps Distance Matrix API has the following limits in place (Users of the standard API):

  • 2,500 free elements per day
  • 100 elements per query
  • 100 elements per 10 seconds

Thus, researchers face a limit in requesting distances. This code proposes a work-around, respectively, an approach to request the distances; specifically, the proposed code requests the driving distance and driving time between two geographical points via google maps without any API restrictions. However, the code is quiet flexible and could be adjusted to request line-distances, etc.

The example refers to the attached csv-file. The comments are part of the script.

Example csv-file

You need five R packages (data.table, httr, stringr, XML) to run the code.

Remarks, hints and further modifications are welcome.

In the first step, you have to load the relevant packages and the attached data files, which consists of four lot/lan distances.


library("data.table")library("httr")library("stringr")library("XML")# Read Data example (Data example provided in the header)newdata <- read.csv("D:/r_geocodes.csv", header = TRUE, sep=";")

Second, define the URL codes to request the distances via google maps.


newdata$URL <- with(newdata, paste("https://www.google.de/maps/dir/",lat1,"+",lon1,"/",lat2,",",lon2, sep=""))newdata$URL <- as.character(newdata$URL)

Next, define the relevant functions to download the data:


# Function Extracting the last n characters from a string substrRight <- function(x, n){  substr(x, nchar(x)-n+1, nchar(x))}######################################################################## Function to request google maps driving distancedownload.maybe <- function(url, refetch=FALSE, path=".") {  cnamet <- as.data.table(as.character(GET(url)))  cnamet <- as.character(cnamet)  # Compute Distance  dis<-substring((strsplit(substrRight(strsplit(cnamet,"km")[[1]][1], 9), ",")[[1]])[2], 2)  dis  # Compute Time  # Minutes  dur_m <- as.numeric(gsub( "[^[:alnum:],]", "", substrRight(strsplit(cnamet,"Min.")[[1]][1], 4) ))  dur_m    # Hours (if applicable)  durh_h_new<-as.numeric(gsub( "[^[:alnum:]]", "",                               ifelse(grepl("Std", substrRight(strsplit(cnamet,"Min")[[1]][1], 15))=="TRUE",                                      str_extract_all(substrRight(strsplit(substrRight(strsplit(cnamet,"Std")[[1]][1], 3),"Std.")[[1]][1], 5),"\(?[0-9,.]+\)?")[[1]],                                      "0")))  durh_h_new  # Change in Minutes  dur_fin<-dur_m+(durh_h_new*60)  dur_fin  # Combine all  fin<- as.character(paste (dis, dur_fin, sep = " ", collapse = NULL))  fin}

Finally, run the corresponding function for your data (here: example data set).


# First Row: Google URL# Second Column: Distance# Third Column: Driving Time (Hint: Always the current driving time . might differ due traffic!!!)files <- as.data.frame(t(as.data.frame(strsplit(sapply(newdata$URL, download.maybe, path=path), "\, |\,| "))))colnames(files)[1] <- "Distance in km"colnames(files)[2] <- "Driving Time in minutes"

That’s it. Now, you should get the following output data file.

The post Google Geo Data – Data Access Without Restrictionsappeared first on ThinkToStart.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Restrictions restriction without Google withou Google

已有 1 人评分经验 收起 理由
zl89 + 60 精彩帖子

总评分: 经验 + 60   查看全部评分

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html
沙发
kyle2014 发表于 2016-7-7 10:05:29 |只看作者 |坛友微信交流群
thanks
已有 1 人评分论坛币 收起 理由
oliyiyi + 15 精彩帖子

总评分: 论坛币 + 15   查看全部评分

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 21:29