-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.R
More file actions
195 lines (162 loc) · 6.29 KB
/
data_loader.R
File metadata and controls
195 lines (162 loc) · 6.29 KB
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
192
193
194
195
# load libraries
library(stringr)
library(stringi)
library(dplyr)
library(googlesheets4)
library(readr)
library(tidygeocoder)
library(httr)
set_config(config(ssl_verifypeer = 0L))
# Function to clean values
clean_values <- function(value) {
if (!grepl("^https?://", value)) {
# Remove leading and trailing parentheses
value <- gsub("^\\(|\\)$", "", value)
value <- gsub("(\\d),(\\d)", "\\1.\\2", value, perl = TRUE)
return(value)
}
return(value)
}
parse_dates <- function(timestamp){
tryCatch(
posix_time <- as.POSIXct(timestamp, format = "%Y-%m-%d %H:%M:%OS", tz = "UTC")
, finally = {
posix_time <- as.POSIXct(timestamp, format = "%Y-%m-%d %H:%M:%OS", tz = "UTC")
}
)
}
validate_coordinates <- function(lat, lon) {
if(is.na(lat) || is.na(lon)){
message("Invalid coordinates.")
return(NULL)
}
if (!is.numeric(lat) || !is.numeric(lon)) {
message("Invalid coordinates: Non-numeric values.")
return(NULL)
}
if (lat < -90 || lat > 90 || lon < -180 || lon > 180) {
message("Invalid coordinates: Out of range.")
return(NULL)
}
return(0)
}
# Function to extract and convert coordinates from various formats
extract_and_convert_coords <- function(coord_str) {
if(is.na(coord_str)[[1]]) return(NULL)
# Pattern for detecting and extracting coordinates in various formats
pattern <- "\\(?\\s*(-?\\d{1,3}\\.?\\d{0,15})[°, ]*\\s*([NS])?[,\\s]*\\s*(-?\\d{1,3}\\.?\\d{0,15})[°, ]*\\s*([EW])?\\)?"
matches <- regmatches(coord_str, gregexpr(pattern, coord_str))
if (length(matches[[1]]) > 0) {
parts <- strsplit(matches[[1]], ",")[[1]]
lat <- as.numeric(parts[1])
lon <- as.numeric(parts[2])
# Adjust for N/S and E/W if present
if (length(parts) > 2 && !is.na(parts[3])) {
if (toupper(parts[3]) == "S") lat <- -lat
if (toupper(parts[3]) == "W") lon <- -lon
}
return(c(lat, lon))
}
return(NULL)
}
# Function to apply URL encoding to a vector to prevent XSS or other input attacks
url_encode_vector <- function(x) {
if(!is.na(x)) URLencode(x)
}
#Preprocesado da informacion -- Extraccion da xeolocalizacion a partir do nome da praia
get_data <- function(update_all = FALSE, update_all_dataset = FALSE){
#Read google sheets data into R
tryCatch(
{
options(gargle_oauth_cache =".secrets")
json_key <- Sys.getenv("GOOGLE_SHEETS_JSON_KEY") #name of the file on .secrets
googlesheets4::gs4_auth(path=json_key)
ss <- 'https://docs.google.com/spreadsheets/d/1E7K92pX4aS7CmGJWjYavEL8menX2gBkHoxtT3YTXwoc/'
data <- googlesheets4::read_sheet(ss)
data$lat <- NA
data$lon <- NA
data$Provincia <- as.character(data$Provincia)
message(paste("Rows in cloud dataset = ", nrow(data)))
},
error = function(e) {
message(paste("Error o cargar os datos: "), e)
return()
}
)
names(data) <- make.names(names(data))
hour <- as.POSIXct(data$Hora, tz = "UTC")
data$Hora <- format(hour, format = "%H")
data$Marca.temporal <- sapply(data$Marca.temporal, parse_dates)
data$Marca.temporal <- as.POSIXct(data$Marca.temporal, origin = "1970-01-01", tz = "UTC")
# data already retrieved
if (file.exists("./data/praias.csv")) {
praias <- read_csv("./data/praias.csv", show_col_types = FALSE)
missing_columns <- setdiff(names(data), names(praias))
for(col in missing_columns){
# if new cols are included in the updated dataset, add them to the historical to not break dataset rbind
praias[[col]] <- NA
}
if(nrow(praias) > 0){
praias$Marca.temporal <- sapply(praias$Marca.temporal, parse_dates)
praias$Marca.temporal <- as.POSIXct(praias$Marca.temporal, origin = "1970-01-01", tz = "UTC")
if(update_all == TRUE){
# Get registers from current day
message("Updating today's data...")
today <- format(Sys.Date(), format = "%Y-%m-%d")
indexes <- which(format(data$Marca.temporal, format = "%Y-%m-%d") == today)
}
else if(update_all_dataset == TRUE){
# Update entire dataset from the cloud
message("Retrieving entire dataset...")
indexes <- as.numeric(rownames(data))
}
else if(update_all_dataset == FALSE && update_all == FALSE){
# Get only new data
max_date <- max(as.POSIXct(praias$Marca.temporal)) # Latest register processed in ./data/praias.csv
indexes <- which(as.POSIXct(data$Marca.temporal) > max_date)
}
# assign already learned or processed values to data
current_indexes <- as.numeric(rownames(praias))
data[current_indexes, ]$lat <- praias[current_indexes, ]$lat
data[current_indexes, ]$lon <- praias[current_indexes, ]$lon
}
}
else{
praias <- data
indexes <- as.numeric(rownames(praias))
}
message(paste("Processing ", length(indexes), " rows"))
### Update required data ####
for (idx in indexes) {
coord_str <- data$`Xeolocalización`[idx]
coord_str <- sapply(coord_str, clean_values)
value <- extract_and_convert_coords(coord_str)
if (is.null(value)) {
place_name <- data$Nome.da.praia..Concello[idx]
geo_result <- geo(place_name, method = "osm", full_results = FALSE)
if(is.na(geo_result$lat)){
# probar so co nome da praia...
praia_concello <- sapply(place_name, function(x) strsplit(x, ", ")[[1]], USE.NAMES=FALSE)
geo_result <- geo(praia_concello[1], method = "osm", full_results = FALSE)
if(is.na(geo_result$lat) & !is.null(praia_concello[2])){
geo_result <- geo(praia_concello[2], method = "osm", full_results = FALSE) #only concello if available
}
else if(is.null(praia_concello[2])){
# get concello from var name
geo_result <- geo(data$Concello[idx], method = "osm", full_results = FALSE)
}
}
if(!is.null(validate_coordinates(geo_result$lat, geo_result$long))){
data$lat[idx] <- geo_result$lat
data$lon[idx] <- geo_result$long
}
} else {
data$lat[idx] <- value[1]
data$lon[idx] <- value[2]
}
}
if(file.exists("./data/praias.csv")){
file.remove("./data/praias.csv")
}
write_csv(data, "./data/praias.csv")
}