-
Notifications
You must be signed in to change notification settings - Fork 1
/
google_geo_ex.R
executable file
·99 lines (77 loc) · 2.31 KB
/
google_geo_ex.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
rm(list=ls())
library(DBI)
library(tidyverse)
library(stringr)
library(openxlsx)
library(data.table)
source('/Volumes/GoogleDrive/Team Drives/delivery/data/cred.txt')
#Grab data
data_fn <- '/Volumes/GoogleDrive/Team Drives/delivery/data/Aug_C3_P1.xlsx'
data <- read.xlsx(data_fn, na.strings = "")
glimpse(data)
geo_data <- data %>%
select(address) %>%
distinct(.)
# test <- data %>%
# group_by(address) %>%
# summarise(n = n()) %>%
# mutate(ad_id = 1:nrow(.))
#
# test2 <-left_join(data, test %>%
# select(-n), by = c("address"))
str(geo_data)
##
#Baidu API call
geocodeAddress <- function(address) {
require(RJSONIO)
full <- paste(address)
url <- "http://api.map.baidu.com/geocoder/v2/?address="
url <- URLencode(paste(url, full, '&output=json&ak=', ak_bd_brow, sep=''))
x <- fromJSON(url,simplify=FALSE)
if (x$status == 0) {
out <- c(x$result$location$lat,
x$result$location$lng)
} else {
out <- NA
}
Sys.sleep(0.0167) # Verified developer now 160/s (API only allows 50 requests per second for non-verified user)
out
}
#Google API call
#geocodeAddress <- function(address) {
# require(RJSONIO)
# full <- paste(address)
# url <- "https://maps.google.com/maps/api/geocode/json?address="
# url <- URLencode(paste(url, full, '&sensor=false&key=',apikey, sep = ""))
# x <- fromJSON(url, simplify = FALSE)
# if (x$status == "OK") {
#
# out <- c(x$results[[1]]$geometry$location$lat,
# x$results[[1]]$geometry$location$lng)
# } else {
# out <- NA
# }
# Sys.sleep(0.05) # API only allows 50 requests per second
# out
#}
#Initialize
geo_data$LAT<-NA
geo_data$LON<-NA
g_add=list()
for (i in 1:nrow(geo_data)) {
g_add <- geocodeAddress(geo_data$address[i])
geo_data$LAT[i] <- g_add[1]
geo_data$LON[i] <- g_add[2]
if ((i %% 100) ==0) cat(paste(i," "))
if (i == nrow(geo_data)) cat("Done!\n")
}
#add addresses w/geo_cords back to main dataset
data_result <- left_join(data, geo_data, by = c("address"))
#write.csv(geo_data, "geo_aug1.csv")
# from saved on whole data set
geo_aug1 <- fread(file = '/Volumes/GoogleDrive/Team Drives/delivery/data/geo_aug1.csv',
na.strings = c(""),
stringsAsFactors = F)
geo_test <- geo_aug1 %>%
select(-V1, -X)
data_geo <- bind_cols(data,geo_test)