---
title: "Finding catchment for Hengistbury head using pg_routing"
author: "Duncan Golicher"
date: "16/11/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE,message = FALSE)
```

```{r}
library(giscourse)
library(mapview)
library(tidyverse)

hengistbury<-get_node("Hengistbury head")
bu<-get_node("Bournemouth University") 
shortest<-shortest_route(c(hengistbury$id,bu$id) )
mapview(shortest)
```




```{r}
con<-connect11()
  query<-sprintf("SELECT * FROM pgrouting.ways_vertices_pgr ver
JOIN ( SELECT * FROM
pgr_drivingDistance( 'SELECT gid id, source, target,
length_m/1000 as cost, length_m/1000 as reverse_cost,
the_geom FROM pgrouting.ways', %s, %s ) ) r
on ver.id = r.node",34649,10)
dist<-st_read(con,query=query)
  dbDisconnect(con)
mapview(dist)
```


```{r}

library(concaveman)


dist %>% filter(agg_cost < 2)  %>% concaveman() ->km_2
dist %>% filter(agg_cost < 4)  %>% concaveman() ->km_4
dist %>% filter(agg_cost < 6)  %>% concaveman() ->km_6
dist %>% filter(agg_cost < 10)  %>% concaveman() ->km_10

mapview(km_2,alpha.regions=0) + 
  mapview(km_4,alpha.regions=0) +
  mapview(km_6,alpha.regions=0) +
  mapview(km_10,alpha.regions=0) 


```




