-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatToCity.R
44 lines (37 loc) · 1.36 KB
/
latToCity.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
library(tidyverse)
library(RMySQL)
library(RbaiduLBS)
con <- dbConnect(MySQL(), host = '10.21.3.101', port = 3306, username = 'r',
password = '123456', dbname = 'shiny_data')
dbSendQuery(con, 'set names gbk')
geo_df <- dbGetQuery(con,
"SELECT DISTINCT ROUND(lat, 1) AS lat, ROUND(lon, 1) AS lon
FROM yz_app_track_db.app_start
WHERE del_status = 0
AND CONCAT(ROUND(lat, 1), '-', ROUND(lon, 1)) NOT IN (
SELECT CONCAT(ROUND(lat, 1), '-', ROUND(lon, 1))
FROM shiny_data.lat_lon_city);")
city <- pbapply::pbapply(geo_df, 1, function(x) {
tryCatch({
result <- revGeocoding(c(x[1], x[2]))
return(
c(
result$result$addressComponent$province,
result$result$addressComponent$city
)
)
}, error = function(e)return(c(e$message, ''))
)
}) %>%
t()
lat_lon_city <- cbind(geo_df[, 1:2], city)
colnames(lat_lon_city) <- c('lat', 'lon', 'province', 'city')
temp <- lat_lon_city
temp$province <- as.character(temp$province)
temp$city <- as.character(temp$city)
Encoding(temp$city) <- 'utf-8'
Encoding(temp$province) <- 'utf-8'
# for empty lat_lon_city
# dbWriteTable(con, 'lat_lon_city', temp, row.names = FALSE, overwrite = TRUE)
dbWriteTable(con, 'lat_lon_city', temp, row.names = FALSE, append = TRUE)
dbDisconnect(con)