Skip to content

Commit 596af6a

Browse files
authored
Merge pull request #2465 from agila5/spatstat
as.ppp.sf accepts ncol(X) > 1
2 parents dc02f71 + fc8d909 commit 596af6a

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* `gdal_utils()` `ogrinfo` has an argument `read_only` which, when `TRUE` (or `options` includes `"-ro"`), opens a datasource in read-only mode (#2460; `sf` did this before 1.0-17); by default a datasource is opened in update (read-write) mode (since sf 1.0-17; #2420)
66

7+
* the `sf` -> `ppp` conversion accepts a data.frame of marks instead of just 1 column #2450, by @agila5
8+
79
# version 1.0-18
810

911
* support `POLYGON FULL` simple feature geometry, representing the entire Earth surface, as used by `s2geometry`; #2441

R/spatstat.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,12 @@ as.ppp.sf = function(X, ...) {
144144
if (st_dimension(X[1,]) == 2)
145145
X = X[-1,]
146146
st_geometry(X) = NULL # remove geometry column
147-
if (ncol(X) > 1)
148-
warning("only first attribute column is used for marks")
149147

150-
if (ncol(X) == 0)
148+
if (ncol(X) == 0) {
151149
pp
152-
else
153-
spatstat.geom::setmarks(pp, X[1])
150+
} else {
151+
spatstat.geom::setmarks(pp, X)
152+
}
154153
}
155154

156155
as.owin.POLYGON = function(W, ..., fatal, check_polygons = TRUE) {

tests/spatstat.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,28 @@ as.psp(sf, marks = 5:1)
108108
(x = st_as_sf(as.psp(sf)))
109109
(y = st_as_sfc(as.psp(sf)))
110110
all.equal(st_geometry(x), y)
111+
112+
# Test sf -> ppp conversion when the conversion involves more than 1 column of mark(s)
113+
# (https://github.com/r-spatial/sf/issues/2450)
114+
reference_ppp <- ppp(
115+
x = c(0.25, 0.75),
116+
y = c(0.25, 0.75),
117+
# We consider a data.frame of marks which includes several types of columns
118+
# (and also a list column)
119+
marks = data.frame(
120+
a = TRUE, b = 1L, c = pi, d = I(list(list(1, 2), list("A", "B", "C"))),
121+
#NB: row.names should always defined as a vector with character character
122+
#since they are converted as characters when applying st_as_sf (see line
123+
#below) which mixes NA and not-NA row.names
124+
row.names = c("point1", "point2")
125+
)
126+
)
127+
# The st_as_sf conversion returns an sf object where the first row is the Window
128+
# and the other rows are the points
129+
tmp <- st_as_sf(reference_ppp)
130+
pts <- tmp[tmp$label == "point", 1:4]
131+
target_ppp <- as.ppp(pts)
132+
Window(target_ppp) <- owin()
133+
all.equal(reference_ppp, target_ppp)
111134
}
112135
## IGNORE_RDIFF_END

0 commit comments

Comments
 (0)