-
Notifications
You must be signed in to change notification settings - Fork 1
/
LOCATION.R
56 lines (46 loc) · 2.18 KB
/
LOCATION.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
library(ggmap)
library(DatabaseConnector)
devtools::install_github("dkahle/ggmap")
## Put the information of SQL server and database
connectionDetails <- DatabaseConnector::createConnectionDetails(
dbms="sql server",
server="localhost",
user="user",
password="pw",
schema="@CDMDatabaseSchema")
conn <- DatabaseConnector::connect(connectionDetails)
## Put Google Geocoding API
ggmap::register_google(key = 'PutGoogleGeocodingAPI', account_type = "standard", client = NA, signature = NA, day_limit = 2500, second_limit = 2500)
## Put name of the nation
ggmap::geocode(location = 'Korea', source='google')
## Get Location table of CDM version_5 from SQL server
df_location <- DatabaseConnector::dbReadTable(conn, name = "@CDMDatabaseSchema.LOCATION")
## Make longitude and latitude data.frame
## Pasting addresses can be different based on each location table's characteristics
for (i in 1:nrow(df_location)){
lat_lon <- data.frame(lat=numeric(), lon=numeric())
na_df <- data.frame(lat=NA, lon=NA)
location_code <- paste0(df_location$address_1[i], df_location$address_2[i])
if(!is.null(location_code)){
lat_lon <- rbind(lat_lon, ggmap::geocode(location_code, source="google"))
}
else{
lat_lon <- rbind(lat_lon, na_df)
}
}
# Change the order of columns
colnames(lat_lon) <- c("longitude", "latitude")
colorder <- c("latitude", "longitude")
lat_lon <- lat_lon[, colorder]
# Combine with existing Location table
LOCATION_v6 <- cbind(df_location, lat_lon)
#Insert table
DatabaseConnector::insertTable(connection = conn,
tableName = "dbo.LOCATION_v6",
data = LOCATION_v6,
dropTableIfExists = FALSE,
createTable = TRUE,
tempTable = FALSE,
useMppBulkLoad = FALSE)
## Disconnect
disconnect(conn)