Add Z values to preexisting geometry #2447
Replies: 2 comments 3 replies
-
Hit this question too. The answer I was able to find is more complicated, and thus substantially slower, than copying elements of vector into an array of structs should be. But at least it works. library(dplyr)
sf1 = st_zm(sf1, drop = FALSE, what = "Z")
sf1xyz = st_coordinates(sf1)
sf1xyz[, "Z"] = external_z
sf1xyz = as_tibble(sf1xyz) %>% rowwise() %>% mutate(geom = list(st_point(c(X, Y, Z))))
sf1xyz = st_sfc(sf1xyz$geom)
sf1 = st_set_geometry(sf1, sf1xyz) Looks to me like there's three basic feature gaps introducing complexity and performance issues here.
A corollary issue is I didn't have luck finding a lighter weight way than dplyr's The way I encountered this in real world coding was merge = bind_rows(xyLayerWithCompoundCrs,
st_transform(xyzLayer1withProjectedCrs, crs(xyLayerWithCompoundCrs)),
st_transform(xyzLayer2withProjectedCrs, crs(xyLayerWithCompoundCrs)))
merge
Simple feature collection with 77049 features and 10 fields
Geometry type: POINT
Dimension: XY, XYZ
st_coordinates(merge)
Error in dimnames(x) <- dn :
length of 'dimnames' [2] not equal to array extent
In addition: Warning message:
In matrix(unlist(x, use.names = FALSE), nrow = length(x), byrow = TRUE, :
data length [211178] is not a sub-multiple or multiple of the number of rows [77049] Which illustrates a couple related issues,
Also, it's kinda odd it seems mandatory to store points' coordinates in an array of structs (AoS) instead of a structure of arrays (SoA). Usually SoA is preferred in new code for fewer memory allocations and SIMD friendliness. For example, AoS requires allocating hundreds of thousands of temporary point objects here just to copy three scalars into and out of each of them once. |
Beta Was this translation helpful? Give feedback.
-
The way to do this is to have the library(sf)
# Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
data.frame(x = 1:3, y = 1:3, z = 1:3, a = rnorm(3)) |>
st_as_sf(coords = c("x", "y", "z"))
# Simple feature collection with 3 features and 1 field
# Geometry type: POINT
# Dimension: XYZ
# Bounding box: xmin: 1 ymin: 1 xmax: 3 ymax: 3
# CRS: NA
# a geometry
# 1 -0.7473629 POINT Z (1 1 1)
# 2 -0.1859593 POINT Z (2 2 2)
# 3 -0.1143853 POINT Z (3 3 3) @twest820 thanks for your comments! On your point 4: does it work if the coordinates are |
Beta Was this translation helpful? Give feedback.
-
Hi,
Thank you for your work on this package. I have a relatively simple question with which I would appreciate your help.
I have an xy point sf object to which I am hoping to add height values from an external vector. I feel like this should be an easy operation but I have not been able to figure out the best way to do so. Any advice would be greatly appreciated.
Here is a demonstration:
Beta Was this translation helpful? Give feedback.
All reactions