Basic question
Working with the sf
library in R, how can I add M-values to a geometry that doesn't already have them?
Or put differently, given an sf
feature without M-values, how can I create a new object with the same coordinates as the original, but which also contains M-values (for example, just a bunch of zeroes)?
Example & code
Suppose I have an object called ml
which contains a MultiLineString, as shown below:
library(sf)
wkt = "MULTILINESTRING((0 0, 0 1), (1 1, 1 2, 1 3))"
ml = st_as_sfc(wkt,
crs='epsg:4326')
Notice that the ml
object was created from a WKT string that specified the geometry as a MultiLineString
, not a MultiLineString M
.
So how can I generate a new geometry that has the same coordinates as ml
but also has M-values enabled? The M-values could be all zero - what matters aren't the individual M-values themselves. Instead, what matters is that, at the end of this process, I have a MultiLineString M
geometry.
What I've tried
I tried using the st_cast
function, but that doesn't seem to work:
st_cast(ml, "MULTILINESTRING M")
# Error in which_sfc_col(to) : st_cast for MULTILINESTRING M not supported
Manually editing the original WKT string is also a possibility here, but that won't really solve my problem since what I've described here is just a little small-scale version of the real bigger problem I'm trying to solve which involves a large feature with many, many coordinates. I want to avoid manually editing that thing as much as I can.
st_zm(your_object, drop = F, what = "Z")
- but for M dimension only dropping is currently implemented, not addingMULTILINESTRING Z
intoMULTILINESTRING M
and create a new geometry.