rspatialr-spspatstat

Error in as.owin function from spatstat package


I am conducting spatial point pattern analysis using the sp and spatstat packages.

The shapefile is available here.

I have converted the kelantan.shp to a SpatialPolygon object (kel_sp).

library(sp)
library(spatstat)
kel_map.owin <-as(kel_sp, "owin")

Error message:

Error in .make_numeric_version(x, strict, .standard_regexps()$valid_numeric_version) : invalid non-character version specification 'x' (type: double)

The code worked well before using a previous version of spatstat. It is no longer working after updating sp to version 2.1-4, and spatstat to version 3.1-1.

I riedt plotting the SpatialPolygon object and the boundaries appeared just fine. I tried creating the owin object using the extracted boundaries using the following code, but it only shows the rectangle boundaries instead of the polygon boundaries. The CRS is RSO Malaya metres and is the same for the polygon and the bbox.

kel_bbox <- bbox(kel_sp)
kel_owin <- owin(xrange = kel_bbox[1,], yrange = kel_map_bbox[2,])

Solution

  • Given the deprecation of sp et al., these issues are becoming increasingly more likely to occur. However, spatstat now accepts sf objects as an input to as.owin(). Indeed, I could not get it to work with an sp object.

    Note that you also need the maptools package installed and loaded for this to work. As maptools is also deprecated, I suspect an alternative to your intended workflow exists so it might be worth future-proofing by searching for updated methods. Either way, this works using R 4.4.1:

    install.packages("maptools", repos = "http://R-Forge.R-project.org")
    library(maptools) # maptools_1.1-9 
    library(sf) # sf_1.0-16
    library(spatstat) # spatstat_3.1-1
    
    # Load your shp as an sf from working directory (edit path if stored elsewhere)
    kel_sf <- st_read("data/kelantan.shp")
    
    # Create owin object
    kel_map.owin <- as.owin(kel_sf)
    
    kel_map.owin
    # window: polygonal boundary
    # enclosing rectangle: [371629.6, 519479.6] x [503028.2, 690232.8] units