library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages -------------------------------------------------------------------------------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.0     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.4
## v tidyr   1.0.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'tidyr' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'purrr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'stringr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts ----------------------------------------------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Warning: package 'sf' was built under R version 3.6.3
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(ggmap)
## Warning: package 'ggmap' was built under R version 3.6.3
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(osrm)
## Warning: package 'osrm' was built under R version 3.6.3
## Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
## Routing: OSRM - http://project-osrm.org/
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.6.3
pozos <- read_sf('POZOS DE AGUA.kml')
## Warning in evalq((function (..., call. = TRUE, immediate. = FALSE, noBreaks. =
## FALSE, : automatically selected the first layer in a data source containing more
## than one.
bases <- read_sf('BASES.kml')
tanques <- read_sf('TANQUES DE AGUA.kml')
ggplot() +
  geom_sf(data = pozos, color='red') + 
  geom_sf(data = bases, color='blue') + 
  geom_sf(data = tanques, color='grey') +
  theme_light()

head(pozos)
## Simple feature collection with 6 features and 2 fields
## geometry type:  POINT
## dimension:      XYZ
## bbox:           xmin: -58.81328 ymin: -34.35528 xmax: -58.78897 ymax: -34.34056
## z_range:        zmin: 0 zmax: 0
## CRS:            4326
## # A tibble: 6 x 3
##   Name  Description                  geometry
##   <chr> <chr>                     <POINT [°]>
## 1 ES003 ""            Z (-58.789 -34.34478 0)
## 2 ES004 ""          Z (-58.79379 -34.34635 0)
## 3 ES005 ""          Z (-58.79474 -34.34254 0)
## 4 ES006 ""          Z (-58.78897 -34.34056 0)
## 5 ES007 ""          Z (-58.79917 -34.34575 0)
## 6 ES008 ""          Z (-58.81328 -34.35528 0)
pozos_e <- sample_n(pozos, 10)
recorrido_optimo <- osrmTrip(loc = pozos, returnclass = "sf")
recorrido_optimo <- recorrido_optimo[[1]]
head(recorrido_optimo)
## $trip
## Simple feature collection with 27 features and 4 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: -58.83046 ymin: -34.45459 xmax: -58.69891 ymax: -34.32732
## CRS:            EPSG:4326
## First 10 features:
##    start end duration distance                       geometry
## 1      1   3 1.671667   0.7650 LINESTRING (-58.78896 -34.3...
## 2      3   5 2.076667   0.8142 LINESTRING (-58.79474 -34.3...
## 3      5  11 6.123333   2.7387 LINESTRING (-58.79916 -34.3...
## 4     11  27 2.151667   0.8620 LINESTRING (-58.77809 -34.3...
## 5     27  21 6.575000   3.9716 LINESTRING (-58.78135 -34.3...
## 6     21  19 2.138333   1.0096 LINESTRING (-58.80974 -34.3...
## 7     19   8 2.048333   0.7881 LINESTRING (-58.8158 -34.36...
## 8      8   7 1.813333   0.7413 LINESTRING (-58.80951 -34.3...
## 9      7  10 1.571667   0.6190 LINESTRING (-58.81199 -34.3...
## 10    10   6 2.045000   0.7626 LINESTRING (-58.80736 -34.3...
## 
## $summary
## $summary$duration
## [1] 92.52667
## 
## $summary$distance
## [1] 68.3257
leaflet(recorrido_optimo$trip$geometry) %>%
    addTiles() %>%
    addPolylines(color = "red") %>%
    # agregamos tambien los puntos enlazados
    addCircleMarkers(data = pozos, popup = ~Name) %>% 
    addProviderTiles(providers$Stamen.Toner)
recorrido_optimo$summary
## $duration
## [1] 92.52667
## 
## $distance
## [1] 68.3257