Replies: 1 comment
-
|
Hi, sure please send the shape file and I can take a look. You can try graph <- metric_graph$new(river_network_sp) and see if you get the same issue. You can also try setting perform_merges = TRUE and adjust the tolerance argument. However, the graph creating might take some time in this case if you have a large river network. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am an inla user who has started to work on rivers so I am trying to learn this new technique. I am trying to creating a graph based on a river shapefile but I am having issues where metric graph is saying the graph is disconnected, however as far as I can tell, the underlying spatial object is continuous (processed with SF and converted to SP object). The metric graph object says there are 3 sections of floating lines, ranging from 2-9 segments. However, when I plot these they are clearly joined to the network in the underlying SP object which was used to create the graph, here is the code and I can provide the shapefile if someone can offer some help, thank you:
Metric Graph
##6/11/24
#devtools::install_github("davidbolin/MetricGraph", ref="devel")
#remotes::install_github("davidbolin/rspde", ref = "devel")
library(rSPDE)
library(INLA)
library(fmesher)
library(MetricGraph)
library(sf)
library(dplyr)
library(ggplot2)
library(viridis)
Load yggplot2# Load your river network and catchment shapefiles
river_network <- st_read(here::here("SpatialData/EPA River Network Moy.shp"))
Remove the Z and M dimensions (if present)
river_network <- st_zm(river_network)%>%
st_cast("LINESTRING") %>%
st_make_valid()
river_network <- river_network %>%
mutate(length_km=as.numeric(st_length(river_network))/1000)
invalid_geoms <- !st_is_valid(river_network)
sum(invalid_geoms)
t<-river_network[invalid_geoms, ]
Now transform the CRS to your target CRS (Irish Transverse Mercator)
river_network <- st_transform(river_network, crs = "+proj=tmerc +lat_0=53.5 +lon_0=-8 +k=0.99982 +x_0=600000 +y_0=750000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=km +no_defs +type=crs")
Check the transformed object
print(river_network)
nrow(river_network)
SIMPLIFYING NETWORK HERE, REMOVE IF CAUSING ISSUES OR TOO SIMPLIFIED
river_network <- st_simplify(st_make_valid(river_network), dTolerance = 0.1) # Simplify by 100 meters
removing disconnected river segments -------------------------
library(igraph)
library(viridis)
touching_list = st_touches(river_network)
g = graph_from_adj_list(touching_list)
c = components(g)
table(c$membership)
hist(river_network$groups <- c$membership)
length(unique(river_network$groups))
river_network<-river_network %>% mutate(area=st_area(geometry))
#SELECT MAIN SPATIAL OBJECT --
river_network<-river_network %>% filter(groups==1)
##the SF object isnt working but the sp object is for some reason
river_network_sp <- as(river_network, "Spatial")
Create the MetricGraph object ---------------------------------------------------
graph <- metric_graph$new(river_network_sp, longlat = F, perform_merges = F)
Plot components to inspect disconnections
graph$plot(vertex_size = 0) #, color_components = TRUE
##use segment length as weights when caluclating spatial correlation
##not working because metric graph drop 3 segments -------------------------------
graph$set_edge_weights(weights = river_network$length_km)
_>Error in graph$set_edge_weights(weights = river_network$length_km) :
Beta Was this translation helpful? Give feedback.
All reactions