Paquetes de R
x <- c("rgdal","magrittr","dplyr","ggplot2","sf","tidyverse","broom","sp")
lapply(x, library, character.only = TRUE) # carga los paquetes requeridos
https://www.keene.edu/campus/maps/tool
Guardar las coordenadas en archivo .csv
rutamc<-"D:/Documents/Claudia/Midropbox/Investigacion y escritos/mapasnacionales"
ylim=c(25.47, 25.67)
xlim=c(-103.30, -103.52)
tormun <- readOGR(rutamc,"muntor", use_iconv = TRUE, encoding = "latin1")
## OGR data source with driver: ESRI Shapefile
## Source: "D:\Documents\Claudia\Midropbox\Investigacion y escritos\mapasnacionales", layer: "muntor"
## with 36105 features
## It has 11 fields
rutacam<-"D:/Documents/Claudia/Midropbox/Investigacion y escritos/mapasnacionales/"
rutacgs<-read.csv(paste0(rutacam,"coorstxt.txt"),header=TRUE, sep=",",encoding="latin")
rutacgs$id <- rownames(rutacgs)
names(rutacgs)<-c("long","lat","id")
##Gráfico de puntos
map <- ggplot() +
geom_polygon(data = tormun, aes(x = long, y = lat, group = group), colour = "black", fill= "white", size = .5) + geom_point(data=rutacgs, aes(x=long, y=lat), color="red")+ coord_sf(xlim=xlim, ylim=ylim)
map + theme_void()
d<-rutacgs
coordinates(d) <- ~long+lat
## lista de lineas por id
lislin <- lapply(split(d, d$id), function(x) Lines(list(Line(coordinates(x))), x$id[1L]))
b<-SpatialLinesDataFrame(SpatialLines(lislin, CRS(as.character(NA))),rutacgs)
map <- ggplot() +
geom_polygon(data = tormun, aes(x = long, y = lat, group = group), colour = "black", fill= "white", size = .5) + geom_path(data=b, aes(x=long, y=lat), color="red")+ coord_sf(xlim=xlim, ylim=ylim)
map + theme_void()
library(geosphere)
rutacgs["distance"] <- c(NA,sapply(seq.int(2,nrow(rutacgs)),function(i){
distm(c(rutacgs$long[i-1],rutacgs$lat[i-1]),c(rutacgs$long[i], rutacgs$lat[i]),fun = distHaversine)}))
sum(rutacgs$distance,na.rm=TRUE)
## [1] 7636.267
df<-rutacgs
df["long2"] <- c(sapply(seq.int(1,nrow(df)), function(i){df$long[i+1]})
)
df["lat2"] <- c(sapply(seq.int(1,nrow(df)), function(i){df$lat[i+1]})
)
map <- ggplot() +
geom_polygon(data = tormun, aes(x = long, y = lat, group = group), colour = "black", fill= "white", size = .5) + geom_segment(aes(x = long, y = lat, xend = long2, yend = lat2, colour = "red"), data = df)+ coord_sf(xlim=xlim, ylim=ylim)
map + theme_void()
ggsave("ruta_map.png", width = 1300, height = 1300, units = "mm", dpi = "retina",limitsize = FALSE)