lucarraro / rivnet

An R-package allowing seamless extraction of river networks from Digital Elevation Models data
https://lucarraro.github.io/rivnet/
Other
13 stars 1 forks source link

How to access spatial data from "river" class? #1

Open mauriciovancine opened 1 year ago

mauriciovancine commented 1 year ago

Dear @lucarraro,

Congratulations on the package.

I tested it here, and it worked great (after spending some time compiling TrauDEM on GNU/Linux).

I would like to know if there is a way to access the spatial data of the “river” class?

Thanks.

Yours sincerely.

lucarraro commented 1 year ago

Thanks for your interest!

"river" objects are lists containing spatial information at various aggregation levels. Essentially, sub-list "FD" contains information at the pixel level; "RN" pertains to pixels belonging to the channelized river network; "AG" corresponds to the reach level; and "SC" identifies the subcatchment level.

You can read more about the structure of "river" objects in the documentation of functions OCNet::create_OCN and OCNet::landscape_OCN. The output of function extract_river is equivalent to that of OCNet::landscape_OCN. Note that, in the CRAN version of OCNet, the objects produced are simply lists and there is no definition of a S4 class--but their structure is actually the same and I plan to introduce the "river" class in OCNet with the next CRAN update. The current GitHub version of OCNet already features the "river" class.

mauriciovancine commented 1 year ago

Thank you so much @lucarraro .

I will test here.

Best regards.

andrew-caudillo commented 8 months ago

@mauriciovancine , I ran into the same issue but with the catchments ("CM" in the river class). It required some digging into how the object is stored, but it is pretty easy and fast to take the information and make it into an object that the package sf can recognize. This manipulation of the geometry allows one to plot the result in things like leaflet or mapview with other spatial objects. Below is the code I used to get the catchment. note that r is my river object.

o<-unlist(r$CM)

this is synonymous with the LIKE

operator in SQL. We want only values

whose columns contain X or Y countour

xs<-o[ grepl( "XContour" , names( o ) ) ] ys<-o[ grepl( "YContour" , names( o ) ) ]

make a little df

raw_shed_coords<-data.frame(xs,ys)

change the little df names

names(raw_shed_coords)<-c('X','Y');row.names(raw_shed_coords)<-NULL

make the shed into an sf object

sf_shed <- raw_shed_coords %>% st_as_sf(coords = c("X", "Y"), crs = 4269) %>% summarise(geometry = st_combine(geometry)) %>% st_cast("POLYGON")

fill the list