0

I'm trying to create an object of class 'sf' that is single multipolygon out of a raster. I'm following the procedure in this SE post: https://gis.stackexchange.com/a/357792/191148, but I want to keep the object as an sf because I want to use other st_ functions on it later (e.g., st_intersects).

From what I understand, I need to use st_combine to take the set of polygons and combine them into one. When I use st_make_valid on the result it returns without error, but the resulting sf is still invalid.

Not sure if there is something wrong with the data or if I've made a mistake somewhere. Also open to different approaches so long as they are efficient enough to handle rasters of much higher resolution.

Here's the code:

# Package for getting bathymetry (water depth) data
library(marmap)

library(sf) library(stars) library(raster)

Get bathymetry data (need internet connection for this)

bathy <- getNOAA.bathy(lon1 = -73, lon2 = -50, lat1 = -37, lat2 = 50, resolution = 10)

Convert to raster

bathy_rast <- as.raster(bathy)

Use land cutoff value to identify the value where land begins. Anything that isn't land

(i.e., below -10) is NA, anything at or above land is 1

bathy_rast@data@values <- ifelse(bathy_rast@data@values < -10, NA, 1)

This executes fine with no errors

land_poly <- bathy_rast |> stars::st_as_stars() |> sf::st_as_sf(as_points = FALSE, merge = TRUE) |> sf::st_combine() |> sf::st_make_valid()

This should return TRUE, but is FALSE

sf::st_is_valid(land_poly)

Edit

Here's the full WKT of the object land_poly:

POLYGON ((-63.16667 46.16667, -63.16667 46, -62.83333 46, -62.83333 45.66667, -62.66667 45.66667, -62.66667 45.83333, -63.66667 45.83333, -63.66667 46.16667, -63.16667 46.16667), (-64 50, -64 49.83333, -63.16667 49.83333, -63.16667 49.66667, -62.33333 49.66667, -62.33333 49.5, -62 49.5, -62 49.33333, -61.66667 49.33333, -61.66667 49, -62.66667 49, -62.66667 49.16667, -63.16667 49.16667, -63.16667 49.33333, -63.66667 49.33333, -63.66667 49.66667, -64.33333 49.66667, -64.33333 50, -64 50), (-61.5 47.66667, -61.5 47.5, -61.66667 47.5, -61.66667 47.66667, -61.5 47.66667), (-61.66667 47.5, -61.66667 47.16667, -62 47.16667, -62 47.5, -61.66667 47.5), (-56.16667 47, -56.16667 46.83333, -56.33333 46.83333, -56.33333 47, -56.16667 47), (-56.66667 50, -56.66667 49.83333, -56.5 49.83333, -56.5 50, -55.5 50, -55.5 49.83333, -55.66667 49.83333, -55.66667 49.5, -55 49.5, -55 49.66667, -54.16667 49.66667, -54.16667 49.5, -53.66667 49.5, -53.66667 49.33333, -53.5 49.33333, -53.5 49, -53.66667 49, -53.66667 48.66667, -53 48.66667, -53 48.33333, -53.5 48.33333, -53.5 48, -53.66667 48, -53.66667 47.66667, -53.5 47.66667, -53.5 47.83333, -53.33333 47.83333, -53.33333 48, -53.16667 48, -53.16667 48.16667, -53 48.16667, -53 47.66667, -52.66667 47.66667, -52.66667 47.33333, -52.83333 47.33333, -52.83333 47, -53 47, -53 46.66667, -53.5 46.66667, -53.5 46.5, -53.66667 46.5, -53.66667 47, -53.83333 47, -53.83333 46.83333, -54.33333 46.83333, -54.33333 47, -54.16667 47, -54.16667 47.16667, -54 47.16667, -54 47.5, -54.16667 47.5, -54.16667 47.33333, -54.83333 47.33333, -54.83333 47.16667, -55 47.16667, -55 47, -55.33333 47, -55.33333 46.83333, -55.83333 46.83333, -55.83333 47, -55.66667 47, -55.66667 47.16667, -55.33333 47.16667, -55.33333 47.33333, -55.16667 47.33333, -55.16667 47.5, -55 47.5, -55 47.66667, -55.33333 47.66667, -55.33333 47.5, -56 47.5, -56 47.66667, -56.83333 47.66667, -56.83333 47.5, -57 47.5, -57 47.66667, -59.33333 47.66667, -59.33333 47.83333, -59.5 47.83333, -59.5 48, -59.16667 48, -59.16667 48.16667, -59 48.16667, -59 48.5, -58.5 48.5, -58.5 48.33333, -59.16667 48.33333, -59.16667 48.66667, -59 48.66667, -59 48.83333, -58.66667 48.83333, -58.66667 49, -58.33333 49, -58.33333 49.5, -58.16667 49.5, -58.16667 49.66667, -57.83333 49.66667, -57.83333 50, -56.66667 50), (-53.16667 47.66667, -53.16667 47.5, -53 47.5, -53 47.66667, -53.16667 47.66667), (-66.66667 44.83333, -66.66667 44.5, -66.83333 44.5, -66.83333 44.83333, -66.66667 44.83333), (-59.66667 44, -59.66667 43.83333, -60 43.83333, -60 44, -59.66667 44), (-70 42, -70 41.83333, -69.83333 41.83333, -69.83333 41.5, -70.16667 41.5, -70.16667 42, -70 42), (-67 50, -67 49.66667, -67.16667 49.66667, -67.16667 49.5, -67.33333 49.5, -67.33333 49.33333, -67.83333 49.33333, -67.83333 49.16667, -68.16667 49.16667, -68.16667 49, -68.83333 49, -68.83333 48.83333, -69 48.83333, -69 48.5, -69.33333 48.5, -69.33333 48.33333, -69.66667 48.33333, -69.66667 48, -69.33333 48, -69.33333 48.16667, -69.16667 48.16667, -69.16667 48.33333, -68.66667 48.33333, -68.66667 48.5, -68.33333 48.5, -68.33333 48.66667, -67.83333 48.66667, -67.83333 48.83333, -67.5 48.83333, -67.5 49, -66.5 49, -66.5 49.16667, -65.66667 49.16667, -65.66667 49.33333, -65.33333 49.33333, -65.33333 49.16667, -64.66667 49.16667, -64.66667 49, -64.16667 49, -64.16667 48.33333, -64.66667 48.33333, -64.66667 48.16667, -65 48.16667, -65 48, -65.66667 48, -65.66667 47.66667, -65.33333 47.66667, -65.33333 47.83333, -64.66667 47.83333, -64.66667 48, -64.33333 48, -64.33333 47.83333, -64.5 47.83333, -64.5 47.66667, -64.83333 47.66667, -64.83333 46.83333, -64.66667 46.83333, -64.66667 46.5, -64.5 46.5, -64.5 46.33333, -64.33333 46.33333, -64.33333 46.66667, -64.5 46.66667, -64.5 46.83333, -64.33333 46.83333, -64.33333 47, -63.83333 47, -63.83333 46.66667, -63.66667 46.66667, -63.66667 46.5, -63.16667 46.5, -63.16667 46.33333, -62.83333 46.33333, -62.83333 46.5, -62 46.5, -62 46.33333, -62.33333 46.33333, -62.33333 46, -62.5 46, -62.5 45.66667, -62.16667 45.66667, -62.16667 45.83333, -61.83333 45.83333, -61.83333 45.66667, -61.66667 45.66667, -61.66667 46.16667, -61.5 46.16667, -61.5 46.33333, -61 46.33333, -61 46.83333, -60.83333 46.83333, -60.83333 47, -60.33333 47, -60.33333 46.33333, -59.83333 46.33333, -59.83333 46.16667, -59.66667 46.16667, -59.66667 46, -59.83333 46, -59.83333 45.83333, -60.16667 45.83333, -60.16667 45.66667, -60.83333 45.66667, -60.83333 45.5, -61.16667 45.5, -61.16667 45.33333, -61 45.33333, -61 45.16667, -61.33333 45.16667, -61.33333 45, -62.33333 45, -62.33333 44.83333, -62.66667 44.83333, -62.66667 44.66667, -63.33333 44.66667, -63.33333 44.5, -63.5 44.5, -63.5 44.33333, -63.83333 44.33333, -63.83333 44.5, -64 44.5, -64 44.33333, -64.33333 44.33333, -64.33333 44.16667, -64.5 44.16667, -64.5 44, -64.66667 44, -64.66667 43.83333, -64.83333 43.83333, -64.83333 43.66667, -65.33333 43.66667, -65.33333 43.5, -65.83333 43.5, -65.83333 43.33333, -66 43.33333, -66 43.66667, -66.16667 43.66667, -66.16667 44, -66.33333 44, -66.33333 44.33333, -66.16667 44.33333, -66.16667 44.5, -66 44.5, -66 44.66667, -65.66667 44.66667, -65.66667 44.83333, -65.5 44.83333, -65.5 45, -65 45, -65 45.16667, -64.83333 45.16667, -64.83333 45.33333, -65 45.33333, -65 45.5, -65.33333 45.5, -65.33333 45.33333, -65.66667 45.33333, -65.66667 45.16667, -66.16667 45.16667, -66.16667 45, -67 45, -67 44.66667, -67.33333 44.66667, -67.33333 44.5, -67.83333 44.5, -67.83333 44.33333, -68.33333 44.33333, -68.33333 44.16667, -68.5 44.16667, -68.5 44, -68.66667 44, -68.66667 44.33333, -69 44.33333, -69 44.16667, -68.83333 44.16667, -68.83333 44, -69.16667 44, -69.16667 43.83333, -69.66667 43.83333, -69.66667 43.66667, -70.33333 43.66667, -70.33333 43.33333, -70.5 43.33333, -70.5 43, -70.83333 43, -70.83333 42.66667, -70.66667 42.66667, -70.66667 42.5, -70.83333 42.5, -70.83333 42.33333, -70.66667 42.33333, -70.66667 42, -70.5 42, -70.5 41.83333, -70.33333 41.83333, -70.33333 41.33333, -70.83333 41.33333, -70.83333 41.5, -70.66667 41.5, -70.66667 41.66667, -71 41.66667, -71 41.5, -71.5 41.5, -71.5 41.33333, -72.33333 41.33333, -72.33333 41.16667, -73 41.16667, -73 50, -67 50), (-66.16667 46, -66.16667 45.83333, -66 45.83333, -66 46, -66.16667 46), (-69.83333 41.33333, -69.83333 41.16667, -70.33333 41.16667, -70.33333 41.33333, -69.83333 41.33333), (-72.33333 41, -72.33333 40.83333, -72.83333 40.83333, -72.83333 40.66667, -73 40.66667, -73 41, -72.33333 41), (-71.66667 21.83333, -71.66667 21.66667, -71.83333 21.66667, -71.83333 21.83333, -71.66667 21.83333), (-71.66667 21.5, -71.66667 21.16667, -72 21.16667, -72 21.33333, -72.16667 21.33333, -72.16667 21.5, -71.66667 21.5), (-69.66667 20.83333, -69.66667 20.66667, -69.5 20.66667, -69.5 20.5, -69.66667 20.5, -69.66667 20.33333, -69.83333 20.33333, -69.83333 20.83333, -69.66667 20.83333), (-68.83333 20.16667, -68.83333 20, -69 20, -69 20.16667, -68.83333 20.16667), (-72.83333 18.83333, -72.83333 18.66667, -73 18.66667, -73 18.83333, -72.83333 18.83333), (-64.33333 18.66667, -64.33333 18.33333, -64.83333 18.33333, -64.83333 18.5, -64.66667 18.5, -64.66667 18.66667, -64.33333 18.66667), (-72.66667 20, -72.66667 19.83333, -72.33333 19.83333, -72.33333 19.66667, -71.66667 19.66667, -71.66667 20, -71.5 20, -71.5 19.83333, -70.66667 19.83333, -70.66667 19.66667, -69.83333 19.66667, -69.83333 19.33333, -69.16667 19.33333, -69.16667 19, -68.66667 19, -68.66667 18.83333, -68.5 18.83333, -68.5 18.66667, -68.33333 18.66667, -68.33333 18.33333, -69.66667 18.33333, -69.66667 18.5, -70 18.5, -70 18.33333, -70.33333 18.33333, -70.33333 18.16667, -70.5 18.16667, -70.5 18.33333, -71 18.33333, -71 18, -71.16667 18, -71.16667 17.83333, -71.33333 17.83333, -71.33333 17.66667, -71.66667 17.66667, -71.66667 18, -71.83333 18, -71.83333 18.16667, -72 18.16667, -72 18.33333, -72.5 18.33333, -72.5 18.16667, -73 18.16667, -73 18.33333, -72.66667 18.33333, -72.66667 18.5, -72.5 18.5, -72.5 18.83333, -72.66667 18.83333, -72.66667 19, -72.83333 19, -72.83333 19.5, -73 19.5, -73 20, -72.66667 20), (-66.83333 18.5, -66.83333 18.33333, -65.66667 18.33333, -65.66667 18.16667, -65.83333 18.16667, -65.83333 18, -66.16667 18, -66.16667 17.83333, -66.5 17.83333, -66.5 18, -67 18, -67 17.83333, -67.16667 17.83333, -67.16667 18.33333, -67 18.33333, -67 18.5, -66.83333 18.5), (-62.66667 18.33333, -62.66667 18.16667, -62.83333 18.16667, -62.83333 18, -62.66667 18, -62.66667 17.83333, -63 17.83333, -63 18.33333, -62.66667 18.33333), (-62.5 17.16667, -62.5 17, -62.66667 17, -62.66667 17.16667, -62.5 17.16667), (-61.66667 17.16667, -61.66667 17, -61.83333 17, -61.83333 17.16667, -61.66667 17.16667), (-62.16667 16.83333, -62.16667 16.66667, -62.33333 16.66667, -62.33333 16.83333, -62.16667 16.83333), (-61.16667 16, -61.16667 15.83333, -61.33333 15.83333, -61.33333 16, -61.16667 16), (-61.16667 15.66667, -61.16667 15.33333, -61.5 15.33333, -61.5 15.66667, -61.16667 15.66667), (-61 15, -61 14.66667, -61.16667 14.66667, -61.16667 15, -61 15), (-60.83333 14.66667, -60.83333 14.33333, -61 14.33333, -61 14.66667, -60.83333 14.66667), (-61 13.33333, -61 13.16667, -61.16667 13.16667, -61.16667 13.33333, -61 13.33333), (-59.5 13.16667, -59.5 13, -59.66667 13, -59.66667 13.16667, -59.5 13.16667), (-61.33333 12.66667, -61.33333 12.5, -61.5 12.5, -61.5 12.66667, -61.33333 12.66667), (-60.5 11.33333, -60.5 11.16667, -60.66667 11.16667, -60.66667 11, -60.83333 11, -60.83333 11.33333, -60.5 11.33333), (-71 12.33333, -71 11.66667, -70.33333 11.66667, -70.33333 12.16667, -69.83333 12.16667, -69.83333 12, -69.66667 12, -69.66667 11.66667, -69.5 11.66667, -69.5 11.5, -69.33333 11.5, -69.33333 11.66667, -69.16667 11.66667, -69.16667 11.5, -68.66667 11.5, -68.66667 11.33333, -68.5 11.33333, -68.5 11.16667, -68.33333 11.16667, -68.33333 10.66667, -68.16667 10.66667, -68.16667 10.5, -67 10.5, -67 10.66667, -66 10.66667, -66 10.33333, -65.5 10.33333, -65.5 10.16667, -64.83333 10.16667, -64.83333 10.33333, -64.33333 10.33333, -64.33333 11, -64 11, -64 11.16667, -63.66667 11.16667, -63.66667 11.33333, -63.5 11.33333, -63.5 11, -63.16667 11, -63.16667 10.83333, -61.83333 10.83333, -61.83333 10.66667, -61.66667 10.66667, -61.66667 10.83333, -61 10.83333, -61 10, -61.33333 10, -61.33333 9.666667, -61 9.666667, -61 9.5, -60.83333 9.5, -60.83333 9.333333, -60.66667 9.333333, -60.66667 9.166667, -60.5 9.166667, -60.5 9, -60.33333 9, -60.33333 8.666667, -59.83333 8.666667, -59.83333 8.5, -59.66667 8.5, -59.66667 8.333333, -59.16667 8.333333, -59.16667 8.166667, -59 8.166667, -59 8, -58.83333 8, -58.83333 7.833333, -58.66667 7.833333, -58.66667 7.666667, -58.33333 7.666667, -58.33333 7.166667, -58.16667 7.166667, -58.16667 7, -57.83333 7, -57.83333 6.833333, -57.66667 6.833333, -57.66667 6.5, -57.33333 6.5, -57.33333 6.333333, -57 6.333333, -57 6.166667, -56.5 6.166667, -56.5 6, -55.66667 6, -55.66667 6.166667, -54.66667 6.166667, -54.66667 6, -54 6, -54 5.833333, -53.66667 5.833333, -53.66667 5.666667, -53 5.666667, -53 5.5, -52.66667 5.5, -52.66667 5.333333, -52.5 5.333333, -52.5 5.166667, -52.33333 5.166667, -52.33333 5, -51.83333 5, -51.83333 4.833333, -51.66667 4.833333, -51.66667 4.666667, -51.5 4.666667, -51.5 4.5, -51.33333 4.5, -51.33333 4.333333, -51 4.333333, -51 3.833333, -50.83333 3.833333, -50.83333 3.333333, -50.66667 3.333333, -50.66667 3, -50.33333 3, -50.33333 2.666667, -50.16667 2.666667, -50.16667 2.5, -50 2.5, -50 -30, -50.16667 -30, -50.16667 -30.33333, -50.33333 -30.33333, -50.33333 -31, -50.66667 -31, -50.66667 -31.33333, -50.83333 -31.33333, -50.83333 -31.5, -51 -31.5, -51 -31.66667, -51.16667 -31.66667, -51.16667 -31.83333, -51.33333 -31.83333, -51.33333 -32, -51.66667 -32, -51.66667 -32.16667, -51.83333 -32.16667, -51.83333 -32.33333, -52.16667 -32.33333, -52.16667 -32.66667, -52.33333 -32.66667, -52.33333 -33.33333, -52.5 -33.33333, -52.5 -33.5, -52.83333 -33.5, -52.83333 -33.66667, -53.16667 -33.66667, -53.16667 -33.83333, -53.33333 -33.83333, -53.33333 -34, -53.5 -34, -53.5 -34.33333, -53.66667 -34.33333, -53.66667 -34.5, -53.83333 -34.5, -53.83333 -34.66667, -54 -34.66667, -54 -34.83333, -54.33333 -34.83333, -54.33333 -35, -55.83333 -35, -55.83333 -35.16667, -56.16667 -35.16667, -56.16667 -35.33333, -56.33333 -35.33333, -56.33333 -35.5, -56.5 -35.5, -56.5 -35.66667, -56.66667 -35.66667, -56.66667 -36.33333, -56.5 -36.33333, -56.5 -37, -73 -37, -73 -36.33333, -72.83333 -36.33333, -72.83333 -36, -72.66667 -36, -72.66667 -35.66667, -72.5 -35.66667, -72.5 -35.5, -72.33333 -35.5, -72.33333 -35.16667, -72.16667 -35.16667, -72.16667 -34.83333, -72 -34.83333, -72 -34.16667, -71.83333 -34.16667, -71.83333 -33.83333, -71.66667 -33.83333, -71.66667 -33, -71.5 -33, -71.5 -32, -71.66667 -32, -71.66667 -30.33333, -71.33333 -30.33333, -71.33333 -29.5, -71.5 -29.5, -71.5 -29, -71.33333 -29, -71.33333 -28.66667, -71.16667 -28.66667, -71.16667 -28.16667, -71 -28.16667, -71 -27.16667, -70.83333 -27.16667, -70.83333 -26.83333, -70.66667 -26.83333, -70.66667 -25.66667, -70.5 -25.66667, -70.5 -23.83333, -70.33333 -23.83333, -70.33333 -23.66667, -70.5 -23.66667, -70.5 -23.16667, -70.33333 -23.16667, -70.33333 -22, -70.16667 -22, -70.16667 -21, -70.33333 -21, -70.33333 -20.66667, -70.16667 -20.66667, -70.16667 -19.83333, -70.33333 -19.83333, -70.33333 -18.33333, -70.83333 -18.33333, -70.83333 -18.16667, -71 -18.16667, -71 -18, -71.16667 -18, -71.16667 -17.83333, -71.33333 -17.83333, -71.33333 -17.66667, -71.5 -17.66667, -71.5 -17.5, -71.66667 -17.5, -71.66667 -17.33333, -71.83333 -17.33333, -71.83333 -17.16667, -72.16667 -17.16667, -72.16667 -17, -72.33333 -17, -72.33333 -16.83333, -72.83333 -16.83333, -72.83333 -16.66667, -73 -16.66667, -73 11.66667, -72.5 11.66667, -72.5 11.83333, -72.33333 11.83333, -72.33333 12.16667, -72.16667 12.16667, -72.16667 12.33333, -71 12.33333), (-52.33333 -2, -52.33333 -2.166667, -52.16667 -2.166667, -52.16667 -2, -52.33333 -2), (-53 -32.66667, -53 -32.83333, -52.83333 -32.83333, -52.83333 -32.66667, -53 -32.66667), (-53.5 -33, -53.5 -33.16667, -53.16667 -33.16667, -53.16667 -33, -53.5 -33), (-53.66667 -33.16667, -53.66667 -33.66667, -53.33333 -33.66667, -53.33333 -33.5, -53.5 -33.5, -53.5 -33.16667, -53.66667 -33.16667), (-57 -36.33333, -57 -36.5, -56.83333 -36.5, -56.83333 -36.33333, -57 -36.33333), (-57 -36.66667, -57 -36.83333, -56.83333 -36.83333, -56.83333 -36.66667, -57 -36.66667))
Will Hipson
  • 123
  • 5
  • Take out the geometry as WKT after st_combine() and st_make_valid(). What do you have, or do you have anything? – user30184 Sep 28 '21 at 12:50
  • Here's the result of st_geometry after st_make_valid.

    Geometry set for 1 feature Geometry type: POLYGON Dimension: XY Bounding box: xmin: -73 ymin: -37 xmax: -50 ymax: 50 CRS: +proj=longlat +datum=WGS84 +no_defs POLYGON ((-63.16667 46.16667, -63.16667 46, -62...

    If I look at the object after st_combine the WKT is a MULTIPOLYGON

    – Will Hipson Sep 28 '21 at 13:03
  • 1
    Add the full WKT into your question so that others can test it with their IsValid. – user30184 Sep 28 '21 at 13:28
  • Seems to work for me. I get land_poly as a MULTIPOLYGON, not a POLYGON so I don't know why your WKT is that. – Spacedman Sep 28 '21 at 14:23
  • Strangely enough, it works if I convert it to WKT string and then back to an sf:
        stars::st_as_stars() |> 
        sf::st_as_sf(as_points = FALSE, merge = TRUE) |> 
        sf::st_combine() |> 
        sf::st_as_text() |> 
        sf::st_as_sfc() |> 
        sf::st_make_valid() |> 
        sf::st_as_sf(crs = 4326)
    
    # Returns TRUE
    sf::st_is_valid(land_poly)```
    
    Seems like an awful lot of extra work though...
    
    – Will Hipson Sep 28 '21 at 14:47

0 Answers0