1

I have updated a program because readShapePoly has been replaced by st_read. Changing from:

unzip(paste(mdir, "AU_Vic_POA_2011.zip", sep = ""))
boundary.shp <- readShapePoly("AU_Vic_Melbourne_POA_2011_boundary-wgs84")
# Create an owin:
boundary.w <- as(as(boundary.shp, "SpatialPolygons"), "owin")

To:

unzip(paste(mdir, "AU_Vic_POA_2011.zip", sep = ""))
boundary.shp <- st_read("AU_Vic_Melbourne_POA_2011_boundary-wgs84.shp")
# Create an owin:
boundary.w <- as(as(boundary.shp, "SpatialPolygons"), "owin")

I now get the error message:

Error in as(boundary.shp, "SpatialPolygons") : 
  no method or default for coercing “sf” to “SpatialPolygons”.

How do I fix this?

  • 1
    No. readShapePoly has not been "replaced by st_read". readShapePoly reads shapefiles into "sp" class objects. st_read reads shapefiles into "sf" class objects. Spatstat needs its own "owin" class objects so your question is how to convert between these different classes of objects. – Spacedman May 6 at 12:18
1

With package sf use as_Spatial(your.shp) instead as(your.shp,"SpatialPolygons") and use as.owin function from spatstat package

library(sf)
library(spatstat)

boundary.shp <- st_read("AU_Vic_Melbourne_POA_2011_boundary-wgs84.shp")

# Create an owin:
boundary.w <- as.owin(as_Spatial(boundary.shp))

# or
boundary.w <- as(as_Spatial(boundary.shp),"owin")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.