library(rpostgis)
## Loading required package: RPostgreSQL
## Loading required package: DBI
library(RPostgreSQL)
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.1.2, PROJ 4.9.3
library(mapview)
library(leaflet.extras)
## Loading required package: leaflet
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
library(elevatr)
library(tmap)
library(gdistance)
## Loading required package: igraph
## 
## Attaching package: 'igraph'
## The following object is masked from 'package:raster':
## 
##     union
## The following objects are masked from 'package:dplyr':
## 
##     as_data_frame, groups, union
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
## Loading required package: Matrix
## 
## Attaching package: 'gdistance'
## The following object is masked from 'package:igraph':
## 
##     normalize
conn <- dbConnect("PostgreSQL", host = "postgis", dbname = "nateng" ,user = "docker", password = "docker")
grassland<-st_read(conn,query="select * from calc_grass")
transects<-st_read(conn,query="select * from transects")
counts<-st_read(conn,query="select * from counts")
dbDisconnect(conn)
## [1] TRUE
grassland<-st_zm(grassland)
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(grassland) +tm_fill() +  tm_borders() + tm_scale_bar() + tm_compass(type = "8star", position = c("left", "top")) + tm_basemap(server = "OpenTopoMap") -> map
tmap_mode("plot")
## tmap mode set to plotting
map

library(raster)
grassland$bin<-1

raster_template <- raster(extent(grassland), resolution = 100,
                         crs = st_crs(grassland)$proj4string)

grass<-rasterize(grassland, raster_template, field = "bin")
grass2<-grass
grass2[is.na(grass2)]<-100
tr<-transition(grass2,transitionFunction = mean,directions=8)
points<-as(counts, "Spatial")
p<-unique(coordinates(points))

test<-shortestPath(tr,p[1,],p[19,],output="SpatialLines")

plot(grass)
points(points)
lines(test)