0
votes

Novice R programmer here... Looking for guidance on building a tess out of the polygons in a SpatialPolygonsDataFrame.

I am invoking quadratcount on points within a state boundary. Rather than using the default grid, I would like to use custom polygons to define the quadrats. Specifically, the county polygons which I have in shapefile format.

I take it from the documentation that the desired tesselation can be created out of a list of 'owin' objects. Where I'm getting jammed up is in taking my SpatialPolygonsDataFrame to generate that list.

I have confirmed that the polygons are read in correctly:

counties <- readOGR('/path/to/counties.shp', layer = "CountyBoundaries", GDAL1_integer64_policy = FALSE)
for(i in 1:nrow(counties)) { 
    plot(counties[i,])
}

Which generates a series of plots, one per county. That is, of course, only useful to know that my data isn't broken and that I can iterate over the polygons. What I think I need to do is make an owin out of each polygon in the SpatialPolygonsDataFrame and append that to myList for tess(tiles=myList). Not having much success in that approach.

My strong suspicion is that there's an easier way...

Many Thanks,

--gt

1

1 Answers

0
votes

Turns out my problem was in not fully understanding how lists are indexed in R. The following bit of code gives me the result I want.

I have no doubt that there is a better, vectorized, way to do it. But in the mean while:

# The point events are in a PPP: StateCrimes_ppp
counties <- readOGR('/path/to/counties.shp', layer = "CountyBoundaries", GDAL1_integer64_policy = FALSE)
tlist <- list()
for(i in 1:nrow(counties)) { 
    tlist[[i]] <-  as(counties[i,], 'owin')
}
QuadCount <- quadratcount(
    StateCrimes_ppp, 
    tess=tess(tiles=tlist)
)

plot(QuadCount, main=NULL)
plot(intensity(QuadCount, image=TRUE), main=NULL, las=1) 

If anybody sees how I've taken the long and hard way to solve a simple problem, I'd love to know a better, simpler, more elegant, or more R-like way to do this.

Thanks again,

--gt