-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw_map.R
189 lines (153 loc) · 6.12 KB
/
draw_map.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
### Day 14 ###
#install.packages("ggmap")
library(ggplot2)
library(ggmap)
LatLngData <- data.frame(
Lat = runif(20, 24.96, 25.11),
Lng = runif(20, 121.44, 121.60))
google_map <- get_googlemap(center=c(121.52311,25.04126), zoom=12, maptype='satellite')
ggmap(google_map) +
geom_point(data=LatLngData, aes(x=Lng, y=Lat), colour='red')
### Day 15 ###
#install.packages("httr")
#install.packages("rjson")
library(httr)
library(rjson)
getLatLng <- function(address){
urlData <- GET(paste0("https://maps.googleapis.com/maps/api/geocode/json?language=zh-TW&address=", URLencode(address)))
jsonResult <- rjson::fromJSON(rawToChar(urlData$content))
Sys.sleep(1)
if(jsonResult$status != "OK"){
print(paste0("Google geocode API Error, Reason:", jsonResult$error_message))
return("error")
}
print("LatLng Got")
lat <<- jsonResult$results[[1]]$geometry$location$lat
lng <<- jsonResult$results[[1]]$geometry$location$lng
return(paste(lat, lng, sep=","))
}
address_data = read.csv("input/address.csv", stringsAsFactors=FALSE, header=FALSE)
result <- address_data %>%
rowwise() %>%
mutate(LatLng = getLatLng(V1)) %>%
filter(LatLng!="error") %>%
separate(LatLng, c("Lat", "Lng"), sep=",") %>%
mutate(Lat=as.numeric(Lat), Lng=as.numeric(Lng))
ggmap(google_map) +
geom_point(data=result, aes(x=Lng, y=Lat), colour='red')
### Day 16 ###
address_LatLng_data <- result #將前一天的資料存到address_LatLng_data
set.seed(20180102)
kmeans = kmeans(x = address_LatLng_data[, c('Lat','Lng')], centers = 10)
y_kmeans = kmeans$cluster
result <- address_LatLng_data %>%
ungroup() %>%
mutate(category = y_kmeans)
ggmap(get_googlemap(center=c(121.52311,25.04126), zoom=12, maptype='satellite'), extent='device') +
geom_point(data=result,
size=1.8,
aes(x=Lng, y=Lat, colour=factor(category)))
### Day 17 ###
set.seed(20180103)
kmeans_3 = kmeans(x = address_LatLng_data[, c('Lat','Lng')], centers = 3)
set.seed(20180103)
kmeans_7 = kmeans(x = address_LatLng_data[, c('Lat','Lng')], centers = 7)
set.seed(20180103)
kmeans_10 = kmeans(x = address_LatLng_data[, c('Lat','Lng')], centers = 10)
set.seed(20180103)
kmeans_20 = kmeans(x = address_LatLng_data[, c('Lat','Lng')], centers = 20)
result <- address_LatLng_data %>%
ungroup() %>%
mutate(category3 = kmeans_3$cluster,
category7 = kmeans_7$cluster,
category10 = kmeans_10$cluster,
category20 = kmeans_20$cluster)
ggmap(get_googlemap(center=c(121.52311,25.04126), zoom=12, maptype='satellite'), extent='device') +
geom_point(data=result,
size=1.8,
aes(x=Lng, y=Lat, colour=factor(category3)))
ggmap(get_googlemap(center=c(121.52311,25.04126), zoom=12, maptype='satellite'), extent='device') +
geom_point(data=result,
size=1.8,
aes(x=Lng, y=Lat, colour=factor(category7)))
ggmap(get_googlemap(center=c(121.52311,25.04126), zoom=12, maptype='satellite'), extent='device') +
geom_point(data=result,
size=1.8,
aes(x=Lng, y=Lat, colour=factor(category10)))
ggmap(get_googlemap(center=c(121.52311,25.04126), zoom=12, maptype='satellite'), extent='device') +
geom_point(data=result,
size=1.8,
aes(x=Lng, y=Lat, colour=factor(category20)))
### Day 18 ###
rad = function(x) {
return (x * pi / 180)
}
getDistance = function(p1, p2) { #計算兩點經緯度距離
R = 6378137; # 地球平均的半徑
dLat = rad(p2[1] - p1[1])
dLong = rad(p2[2] - p1[2])
a = sin(dLat / 2) * sin(dLat / 2) +
cos(rad(p1[1])) * cos(rad(p2[1])) *
sin(dLong / 2) * sin(dLong / 2);
c = 2 * atan2(sqrt(a), sqrt(1 - a))
d = R * c
return (d) # 回傳公尺
}
isCenterDistanceOverThreshold <- function(center, df, threshold){ # threshold 單位是公尺
for (i in 1:nrow(df)) {
if(threshold < getDistance(center, c(df$Lat[i], df$Lng[i]))){
return(TRUE)
}
}
return(FALSE)
}
unclassifiedAddress <- address_LatLng_data %>% ungroup()
classifiedAddresses <- address_LatLng_data[0,]
SEED <- 20180103
iterDistance <- c(300, 400, 500, 600, 700, 800, 1000)
category = 1 #分類編號
for(iter in 1:length(iterDistance)){
n = nrow(unclassifiedAddress) #未分類資料筆數
if(n <= 5){
print("地址過少 無法分類")
break
}
centersCount = as.integer(1 + 0.23*n) #預計產生的中心數
set.seed(SEED)
kmeans = kmeans(x = unclassifiedAddress[, c('Lat','Lng')], centers = centersCount)
y_kmeans = kmeans$cluster
y_centers = kmeans$centers
for(i in 1:centersCount){
subAddress <- unclassifiedAddress %>%
filter(y_kmeans==i)
if(!isCenterDistanceOverThreshold(y_centers[i,], subAddress, iterDistance[iter])){
subAddress$category <- category
category <- category + 1
classifiedAddresses <- rbind(classifiedAddresses, subAddress)
next
}
}
unclassifiedAddress <- unclassifiedAddress %>%
filter(!(V1 %in% classifiedAddresses$V1))
# ggmap(get_googlemap(center=c(121.52311,25.04126), zoom=12, maptype='satellite'), extent='device') +
# geom_point(data=classifiedAddresses,
# size=1.8,
# aes(x=Lng, y=Lat, colour=factor(category)))
print(paste0("iter-", iter," complete! remain:", nrow(unclassifiedAddress), " addresses"))
}
### Day 19 ###
# 一次K-means
kmeans = kmeans(x = address_LatLng_data[, c('Lat','Lng')], centers = 24)
y_kmeans = kmeans$cluster
single_kmeans <- address_LatLng_data %>%
ungroup() %>%
mutate(category = y_kmeans)
ggmap(get_googlemap(center=c(121.52311,25.04126), zoom=12, maptype='satellite'), extent='device') +
geom_point(data=single_kmeans,
size=1.8,
aes(x=Lng, y=Lat, colour=factor(category)))
# 多次K-means
ggmap(get_googlemap(center=c(121.52311,25.04126), zoom=12, maptype='satellite'), extent='device') +
geom_point(data=classifiedAddresses,
size=1.8,
aes(x=Lng, y=Lat, colour=factor(category)))