Search code examples
rr-sfpolyline

Polygone to one polyline in R


I have a shape file from: https://earthworks.stanford.edu/catalog/stanford-yt100my8913 about Waters in Mozambique as Polygones.

I would like to have the Zambesze River as an Polyline. In the original file it is represented by two polygones. Which line (left, right, middle) exactly, I actually don't care but I want to have it as an single line.

I'm working with R. Reading Data as

River1 <- st_read("inwatera_moz.shp")
Subset_River1 <- "R. Zambeze"
River1<-  subset(River1,nam %in% Subset_River1)
plot(st_geometry(River1), col = "red", add = TRUE)

anyone have an Approach how I could manage?


Solution

  • If all you need is Zambezi river as a line (instead of a polygon) and your use case allows such a cavalier approach as you describe I then suggest using other data sources than the Stanford dataset.

    Open Street Map may be a good start. Consider this piece of code; it utilizes the {nominatimlite} to access the OSM data.

    library(sf)
    
    zambezi <- nominatimlite::geo_lite_sf("Zambezi river", points_only = F)
    
    st_geometry_type(zambezi)
    # [1] MULTILINESTRING
    # 18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE
    
    mapview::mapview(zambezi)
    

    zambezi river on map of East Africa