I am in the process of converting R scripts to Python and there are multiple uses of st_coordinates
. I know that geopandas
has similar functionality in get_coordinates
, but st_coordinates
apparently returns a column called M
. Does anyone know what this column represents?
I could not find any documentation relating to this, only on the L1, L2 and L3 columns that are returned. For reference, st_coordinates
is being passed an sf object that has only LineString geometries.
Due to computing restraints, I am unable to run R to try this code.
1
Basically, what you see is what you get. If sf::st_coordinates()
returns a column M
and/or Z
, your feature object has a M and/or Z dimension in addition to the regular X/Y dimensions.
?st_coordinates()
:
Matrix with coordinates (X, Y, possibly Z and/or M) in rows, possibly followed by integer indicators L1,…,L3 that point out to which structure the coordinate belongs […]
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
# starting with some simple wkt representation of a multilinestring
wkt <- "MULTILINESTRING((0 0, 0 1), (1 1, 1 2, 1 3))"
# some workaround to add M dimension to your multilinestring
x1 <- wkt |>
st_as_sfc() |>
st_zm(drop = FALSE, what = "Z") |>
st_as_text() |>
stringr::str_replace('MULTILINESTRING Z', 'MULTILINESTRING M') |>
st_as_sfc()
# result: XYM
x1
#> Geometry set for 1 feature
#> Geometry type: MULTILINESTRING
#> Dimension: XYM
#> Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 3
#> m_range: mmin: 0 mmax: 0
#> CRS: NA
#> MULTILINESTRING M ((0 0 0, 0 1 0), (1 1 0, 1 2 ...
# there it is, the M column
st_coordinates(x1)
#> X Y M L1 L2
#> [1,] 0 0 0 1 1
#> [2,] 0 1 0 1 1
#> [3,] 1 1 0 2 1
#> [4,] 1 2 0 2 1
#> [5,] 1 3 0 2 1
# same for z dimension
x2 <- st_zm(x1, drop = TRUE, what = "ZM") |> st_zm(drop = FALSE, what = "Z")
# result: XYZ
x2
#> Geometry set for 1 feature
#> Geometry type: MULTILINESTRING
#> Dimension: XYZ
#> Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 3
#> z_range: zmin: 0 zmax: 0
#> CRS: NA
#> MULTILINESTRING Z ((0 0 0, 0 1 0), (1 1 0, 1 2 ...
# and there is your Z
st_coordinates(x2)
#> X Y Z L1 L2
#> [1,] 0 0 0 1 1
#> [2,] 0 1 0 1 1
#> [3,] 1 1 0 2 1
#> [4,] 1 2 0 2 1
#> [5,] 1 3 0 2 1
Created on 2024-04-24 with reprex v2.1.0
1