Anyone have advice for retaining (or appending) attributes from a Spatial Polygons Data Frame after conversion to an owin object for pattern analysis in Spatstat?
Using spatstat to calculate point intensities across a tesselated surface. No issue with this except I lose the polygon labels when I convert the Spatial Polygons Data Frame to an owin object (and subsequently a tess object). See sample code. Thanks!
SF <- readOGR(dsn="D:/",layer="sf")
class(SF) # "SpatialPolygonsDataFrame"
# Convert SpatialPolygonsDataFrame to OWIN object for use in spatstat package
y <- as(SF, "SpatialPolygons")
p <- slot(y, "polygons")
v <- lapply(p, function(z) { SpatialPolygons(list(z)) })
winlist <- lapply(v, as.owin)
# establish window needed for creating tesseslated surface
SF2 <- as(SF, "owin")
# create tesselates surface
e <-tess(tiles = winlist, window = SF2)
## compute counts and density values, across tessellated area, of point data
# B - point pattern object created from *.csv of point locations
QB <- quadratcount(B, tess = e) # tally counts of 'biotic pseudo-variables' by tess tiles
Qi.B <- intensity(QB) # Compute intensity
# Plot results
plot(intensity(QB, image=TRUE), las=1, main=NULL) # plot tess units using intensity for legend
plot(B, pch=20, cex=0.5, col="white", add=TRUE)```
You will need to manually extract the polygon labels from the SpatialPolygonsDataFrame
object SF
as a character vector, and then assign them to the names of the tiles in the tessellation object e
.
To extract the polygon labels you can probably type nama <- rownames(SF)
but it depends which labels you require.
There are two ways to assign the tile names:
Assign names to the list of tiles before creating the tessellation. In the command e <- tess(tiles=winlist, window=SF2)
, if names(winlist)
is not NULL
then the names will be retained as the names of the tiles. So you could first do names(winlist) <- nama
and then e <- tess(tiles=winlist, window=SF2)
.
Assign tile names after creating the tessellation using "tilenames<-"
,as in tilenames(e) <- nama
where nama
is a character vector.
In either case, once the tiles of e
have names, the results of calculations such as Qi.B
will also have these names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.