apiKey<-"AIzaSyBPxhDW9_bUJ1juxXEGvY_QcNIZNJPUy7A" #la llave para que funcione la appi de google
library(ggplot2)
## Registered S3 methods overwritten by 'tibble':
## method from
## format.tbl pillar
## print.tbl pillar
#install.packages("ggmap")
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.0.5
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
register_google(apiKey) #me registro en google
qmap(c(-76.5,3.43),zoom=12,9, maptype="hybrid")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=3.43,-76.5&zoom=12&size=640x640&scale=2&maptype=hybrid&language=en-EN&key=xxx

#Cargar base de datos
library(readr)
MIO_STOPS <- read_delim("MIO_STOPS.txt","\t", escape_double = FALSE, trim_ws = TRUE)
## Parsed with column specification:
## cols(
## STOPID = col_double(),
## PLANVERSIONID = col_double(),
## SHORTNAME = col_character(),
## LONGNAME = col_character(),
## GPS_X = col_double(),
## GPS_Y = col_double(),
## DECIMALLONGITUDE = col_double(),
## DECIMALLATITUDE = col_double(),
## PASSANGERS_NUM = col_double(),
## TYPE_BUS = col_double()
## )
#Adicionar una columna de agrupamiento
k<-7
nIter<-100
grupos<-kmeans(MIO_STOPS[,c(7,8)],k,iter.max = nIter,nstart = 1)
#pegar el cluster a la base de MIO_STOPS
MIO_STOPS$grupos<-grupos$cluster
#poligono
cluster<-subset(MIO_STOPS,MIO_STOPS$grupos==1)
indicesC1<-chull(cluster[,c(7,8)])
poligono<-MIO_STOPS[indicesC1,]
#mapa<-ggplot(MIO_STOPS, aes(DECIMALLONGITUDE, DECIMALLATITUDE))
city<-get_map(c(-76.5,3.43),zoom=12)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=3.43,-76.5&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
mapa<-ggmap(city)
mapa<-mapa+geom_point(data=MIO_STOPS,aes(x=DECIMALLONGITUDE,y=DECIMALLATITUDE),
color=MIO_STOPS$grupos,size=MIO_STOPS$PASSANGERS_NUM*0.02 )
mapa

#Nota: El de polígono se queda pendiente.