Skip to content
Prev 1312 / 29559 Next

Problem in converting SpatialPolygonsDataFrame to owin object

On Fri, 15 Sep 2006, Adrian Baddeley wrote:

            
Yes, correct. The real culprit is the very messy shapefile (which was made 
available to me offline), which not only appears to be of 7 contiguous 
counties, but when dissolved using:

library(spgpc)
tcma_outer <- unionSpatialPolygons(as(tcma, "SpatialPolygons"), rep(1, 7))

has a single outer boundary and as many as 428 internal slivers. My 
immediate suggestion to the questioner would be to locate a better data 
source, such as:

http://www.census.gov/geo/cob/bdy/co/co00shp/co27_d00_shp.zip

In R:

library(rgdal)
tcma <- readOGR(".", "DG_tcma")
library(maptools)
mn <- readShapePoly("co27_d00.shp", proj4string=CRS("+proj=longlat +datum=WGS84"))
mn_FIPS <- paste(as.character(mn$STATE), as.character(mn$COUNTY), sep="")
match(as.character(tcma$COUNTY), mn_FIPS)
mn_subset <- mn[match(as.character(tcma$COUNTY), mn_FIPS),]
mn_subset_utm15N <- spTransform(mn_subset, CRS(proj4string(tcma)))
plot(mn_subset_utm15N, lwd=3, axes=TRUE)
plot(tcma, add=TRUE, border="red")
library(spgpc)
tcma_outer <- unionSpatialPolygons(as(mn_subset_utm15N, "SpatialPolygons"), rep(1, 7))
plot(tcma_outer)
library(spspatstat)
tcma_owin <- as(as(tcma_outer, "SpatialPolygons"), "owin")
plot(tcma_owin)

The key problem was the very messy shapefile, followed by not having 
understood that owin objects cannot be contiguous. The wrapper packages 
are on the sourceforge site. 

Hope this helps,

Roger