as.ppp.Rd
Functions to convert between spatstats planar point pattern (ppp
)
format and sps SpatialPoints
and SpatialPointsDataFrame
as
well as one-way conversion from SpatialGridDataFrame
to ppp
.
S4-style as()
coercion can be used as well.
as.ppp.SpatialPoints(X)
as.ppp.SpatialPointsDataFrame(X)
as.SpatialPoints.ppp(from)
as.SpatialPointsDataFrame.ppp(from)
as.SpatialGridDataFrame.ppp(from)
signature(from = "SpatialPoints", to = "ppp")
signature(from = "SpatialPointsDataFrame", to = "ppp")
signature(from = "ppp", to = "SpatialGridDataFrame")
signature(from = "ppp", to = "SpatialPointsDataFrame")
signature(from = "ppp", to = "SpatialPoints")
object to coerce from
The main conversion is between sps
SpatialPoints
/SpatialPointsDataFrame
and spatstats
ppp
. Conversion between SpatialGridDataFrame
and ppp
should
rarely be used; using as.owin.SpatialGridDataFrame
is more transparent.
The ppp
format requires an observation window which is the sampling
region. The sp formats contain no such information and by default the
bounding box of the points is simply used. This is almost never the correct
thing to do! Rather, information about the sampling region should be converted
into spatstats owin
format and assigned as the
observation window. Usually conversion from ppp
to sp format simply
discards the owin
. However, as.SpatialGridDataFrame.ppp
actually
first discards the points(!), second checks that the corresponding owin
is in a grid format (matrix of TRUE/FALSE for inside/outside sampling region),
and finally converts the TRUE/FALSE grid to a SpatialGridDataFrame
.
In spatstat all spatial objects are assumed to be planar. This means
that spatstat is not designed to work directly with geographic
(longitude and latitude) coordinates. If a sp object is declared to
have geographic (unprojected) coordinates maptools refuses to convert
directly to spatstat format. Rather, these should be projected first
using e.g. spTransform
. If you know what you are doing, and
really want to force coercion, you can overwrite the proj4string
of the
sp object with NA
, proj4string(x) <- CRS(NA)
, which
will fool the system to think that the data is in local planar coordinates.
This is probably not a good idea!
run <- FALSE
if (require("spatstat.geom", quietly=TRUE)) run <- TRUE
if (run) {
## Convert SpatialPointsDataFrame into a marked ppp
data(meuse)
coordinates(meuse) = ~x+y
meuse_ppp <- as(meuse, "ppp")
meuse_ppp # Window is the bounding rectangle
}
#> Warning: some mark values are NA in the point pattern x
#> Marked planar point pattern: 155 points
#> Mark variables:
#> cadmium copper lead zinc elev dist om ffreq soil lime landuse dist.m
#> window: rectangle = [178605, 181390] x [329714, 333611] units
if (run) {
plot(meuse_ppp, which.marks = "zinc")
}
if (run) {
## Convert SpatialPoints into an unmarked ppp
meuse2 <- as(meuse, "SpatialPoints")
as(meuse2, "ppp")
}
#> Planar point pattern: 155 points
#> window: rectangle = [178605, 181390] x [329714, 333611] units
if (run) {
## Get sampling region in grid format and assign it as observation window
data(meuse.grid)
gridded(meuse.grid) <- ~x+y
mg_owin <- as(meuse.grid, "owin")
Window(meuse_ppp) <- mg_owin
meuse_ppp # Window is now a binary image mask (TRUE/FALSE grid)
}
#> Warning: as.matrix.SpatialGridDataFrame uses first column;
#> use subset or [] for other columns
#> Warning: some mark values are NA in the point pattern x
#> Marked planar point pattern: 155 points
#> Mark variables:
#> cadmium copper lead zinc elev dist om ffreq soil lime landuse dist.m
#> window: binary image mask
#> 104 x 78 pixel array (ny, nx)
#> enclosing rectangle: [178440, 181560] x [329600, 333760] units
if (run) {
plot(meuse_ppp, which.marks = "zinc")
}
if (run) {
## Convert marked ppp back to SpatialPointsDataFrame
rev_ppp_SPDF <- as.SpatialPointsDataFrame.ppp(meuse_ppp)
summary(rev_ppp_SPDF)
}
#> Object of class SpatialPointsDataFrame
#> Coordinates:
#> min max
#> mx 178605 181390
#> my 329714 333611
#> Is projected: NA
#> proj4string : [NA]
#> Number of points: 155
#> Data attributes:
#> marks.cadmium marks.copper marks.lead marks.zinc
#> Min. : 0.200 Min. : 14.00 Min. : 37.0 Min. : 113.0
#> 1st Qu.: 0.800 1st Qu.: 23.00 1st Qu.: 72.5 1st Qu.: 198.0
#> Median : 2.100 Median : 31.00 Median :123.0 Median : 326.0
#> Mean : 3.246 Mean : 40.32 Mean :153.4 Mean : 469.7
#> 3rd Qu.: 3.850 3rd Qu.: 49.50 3rd Qu.:207.0 3rd Qu.: 674.5
#> Max. :18.100 Max. :128.00 Max. :654.0 Max. :1839.0
#>
#> marks.elev marks.dist marks.om marks.ffreq marks.soil
#> Min. : 5.180 Min. :0.00000 Min. : 1.000 1:84 1:97
#> 1st Qu.: 7.546 1st Qu.:0.07569 1st Qu.: 5.300 2:48 2:46
#> Median : 8.180 Median :0.21184 Median : 6.900 3:23 3:12
#> Mean : 8.165 Mean :0.24002 Mean : 7.478
#> 3rd Qu.: 8.955 3rd Qu.:0.36407 3rd Qu.: 9.000
#> Max. :10.520 Max. :0.88039 Max. :17.000
#> NA's :2
#> marks.lime marks.landuse marks.dist.m
#> 0:111 W :50 Min. : 10.0
#> 1: 44 Ah :39 1st Qu.: 80.0
#> Am :22 Median : 270.0
#> Fw :10 Mean : 290.3
#> Ab : 8 3rd Qu.: 450.0
#> (Other):25 Max. :1000.0
#> NA's : 1
if (run) {
## Convert marked ppp back to SpatialPoints (discarding marks)
rev_ppp_SP <- as.SpatialPoints.ppp(meuse_ppp)
summary(rev_ppp_SP)
}
#> Object of class SpatialPoints
#> Coordinates:
#> min max
#> mx 178605 181390
#> my 329714 333611
#> Is projected: NA
#> proj4string : [NA]
#> Number of points: 155
if (run) {
## Convert marked ppp back to SpatialGridDataFrame (extracting the window grid)
rev_ppp_SGDF <- as.SpatialGridDataFrame.ppp(meuse_ppp)
summary(rev_ppp_SGDF)
}
#> Object of class SpatialGridDataFrame
#> Coordinates:
#> min max
#> [1,] 178440 181560
#> [2,] 329600 333760
#> Is projected: NA
#> proj4string : [NA]
#> Grid attributes:
#> cellcentre.offset cellsize cells.dim
#> 1 178460 40 78
#> 2 329620 40 104
#> Data attributes:
#> mask
#> Mode:logical
#> TRUE:3103
#> NA's:5009