-
Notifications
You must be signed in to change notification settings - Fork 0
/
POI_LIST.R
191 lines (157 loc) · 6.93 KB
/
POI_LIST.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
190
191
# Geocoding script for large list of addresses.
# Shane Lynn 10/10/2013
#load up the ggmap library
#======================================================================================================
# ADMIN
#======================================================================================================
want = c("tidyverse", "broom", # Data management
"rvest", # Scraping
"rio", "readtext", # Leggere/scrivere dati
"hunspell", "quanteda","tidytext", # QTA
"dplyr","RCurl","plyr",
"topicmodels","lda","ldatuning","stm", # librerie per topic models
"ggrepel", # una miglioria ai grafici ggplot
"factoextra", # libreria per scree plot CA
"wordcloud2",
"reshape2","ggplot2",
"gmapsdistance","geosphere","ggmap","googleway",
"matlib","expm","tibble",
"dplyr","stringr","tidyverse",
"rstudioapi","RColorBrewer","colorRamps",
"igraph","ggplot2","plotly","htmlwidgets","pheatmap",
"eurostat","iotables",
"knitr","xlsx",
"patchwork"
)
have = want %in% rownames(installed.packages())
if ( any(!have) ) { install.packages( want[!have] ) }
junk <- lapply(want, library, character.only = TRUE)
rm(have,want,junk)
rm(list = ls()) # Rimuove tutti gli oggetti presenti nel workspace
###############################################################################
#########################AUTOMATICALLY SET FOLDER PATH#########################
###############################################################################
setwd(dirname(getActiveDocumentContext()$path))
getwd()
###############################################################################
#########################SET GOOGLE API KEY####################################
###############################################################################
ggmap::register_google(key = "")
#set.api.key("")
set.api.key("")
key <- ''
###############################################################################
###############################READ CSV WITH ALL CITY NAME#####################
###############################################################################
# Select the file from the file chooser
#fileToLoad <- file.choose(new = TRUE)
#City_list <- read.csv(file = 'data/ListaComuni.csv',sep = ";")
City_list <- read.csv(file = 'data/ListaComuniChisone.csv',sep = ";")
###############################################################################
##############################DOWNLOAD LAT LONG WITH GEOCODE###################
###############################################################################
# Initialize the data frame
geocoded <- data.frame(stringsAsFactors = FALSE)
# Loop through the addresses to get the latitude and longitude of each address and add it to the
# origAddress data frame in new columns lat and lon
for(i in 1:nrow(City_list)){
# Print("Working...")
result <- geocode(paste(City_list$CITY_NAME[i],"Turin,Italy",
sep = ",", collapse = NULL),
output = "latlona", source = "google")
City_list$lon[i] <- as.numeric(result[1])
City_list$lat[i] <- as.numeric(result[2])
City_list$geoAddress[i] <- as.character(result[3])
}
# Write a CSV file containing origAddress to the working directory
write.csv(City_list, "data/geocodedChisone.csv", row.names=FALSE)
#City_list <- read.csv(file ="data/geocoded.csv")
############################################################################
#########################LIST OF GOOGLE PLACE TYPE##########################
############################################################################
#theatre, cinema, museum, biblioteque, siti archelogici, siti religiosi,
#siti paleontologici, siti archelogici
#places <- c("museum","cinema",
# "church","synagogue","mosque",
# "library",
# "tourist_attraction","point_of_interest","place_of_worship",
# "park","city_hall")
string <- c("chiesa")
places <- c("church")
#QUERY 2
#"museum","church","synagogue","mosque","park","city_hall","tourist_attraction"
#QUERY 3
#"museum","cinema"
#"church", "synagogue","mosque"
#"library"
#"tourist_attraction","point_of_interest","place_of_worship"
#"park","city_hall"
#QUERY 3
#"museum","cinema"
#"church", "synagogue","mosque"
#"library"
#"tourist_attraction","point_of_interest","place_of_worship"
#"park","city_hall"
###############################################################################
########################LIST OF PLACES FOR EACH CITY###########################
###############################################################################
#DEFINE AN EMPTY DATAFRAME
all_data <- data.frame(
city_name=character(),
poi_name=character(),
type=character(),
#address=character(),
viciniy=character(),
# lat=double(),
# lng=double(),
stringsAsFactors=FALSE
)
col_names <- colnames(all_data)
#length(City_list$CITY_NAME)
for(i in 1:length(City_list$CITY_NAME)){#LOOP ON CITY NAMES #length(City_list$CITY_NAME)
for(p in 1:length(places)){#LOOP ON PLACE TYPES
#DOWNLOAD LIST OF ALL SITES FOR EACH CITY AND PLACE TYPE
df_places <- google_places(#search_string = paste(string[p],",",City_list$CITY_NAME[i]),
location = c(City_list$lat[i], City_list$lon[i]),
place_type = places[p],
radius = 2000,
keyword = string[p],
#rankby = "distance",
key = key)
for(n in 1:length(df_places$results$name)){ #INTERNAL LOOP ON ALL POI
if(length(df_places$results$types[[n]])!=1){
a <- 2
}else{
a <- 1
}
for(t in 1:a){ #INTERNAL LOOP ON PLACE TYPE WHEN MORE THAN ONE
temp <- c(City_list$CITY_NAME[i],
df_places$results$name[n],
df_places$results$types[[n]][t],
#df_places$results$formatted_address[n]
df_places$results$vicinity[n]#,
#df_places$results$geometry$location$lat[n],
#df_places$results$geometry$location$lon[n]
)
if(length(df_places$results$types[[n]][t])>0){
if(grepl(City_list$CITY_NAME[i], df_places$results$vicinity[n], fixed = TRUE)){
print(paste(
temp[1]," ",
temp[2]," ",
temp[3]," ",
temp[4]," "#,
#temp[5]," ",
#temp[6]
)
)
all_data <- rbind(all_data,temp
#c(temp[1],temp[2],temp[3],temp[4],temp[5],temp[6])
)
colnames(all_data) <- col_names
}
}
}
}
}
}
write.csv(all_data, "data/chiesa - chisone - 31052021.csv", row.names=FALSE)