Andres
Andres

Reputation: 59

Error loading a .shp file, using the function "as.owin"

I am working with a ".shp" file with class "SpatialPolygonsDataFrame", and I am trying to apply it, a function named "as.owin" as shown in the next code. But the "as.owin" function returned the next error.

polygon <- readOGR(dsn = "C:/Users/Datos Geoespaciales/recintos_provinciales_inspire_peninbal_etrs89",layer = "recintos_provinciales_inspire_peninbal_etrs89", encoding = "UTF-8")
im <- as.owin(polygon, fatal = TRUE)

"Error in as.owin.SpatialPolygons(abc, fatal = TRUE) : Only projected coordinates may be converted to spatstat class objects"

I tried also with the function "as.owin.SpatialPolygons", but the error still the same

im <- as.owin.SpatialPolygons(polygon, fatal = TRUE)

"Error in as.owin.SpatialPolygons(abc, fatal = TRUE) : Only projected coordinates may be converted to spatstat class objects"

According to some information I found on internet, I loaded the .shp file using the function readShapeSpatial instead, and the error changed.

polygon <- readShapeSpatial("C:/Users/Datos Geoespaciales/recintos_provinciales_inspire_peninbal_etrs89.shp")
im <- as.owin(polygon, fatal = TRUE)

"Error: 'spatstat.options' is not an exported object from 'namespace:spatstat'"

I tried also changing the CRS of the .shp file and the error still the same

utm <- "+proj=utm +zone=32 +datum=WGS84"
polygon <- spTransform(test_test, utm)
im <- as.owin(polygon, fatal = TRUE)

"Error: 'spatstat.options' is not an exported object from 'namespace:spatstat'"

I am not sure, what am I doing wrong?, I would appreciate any assitance.

Upvotes: 0

Views: 770

Answers (1)

dieghernan
dieghernan

Reputation: 3402

I have followed a slightly different approach, that is reading and projecting the shapefile with sf, convert it to Spatial* and create an owin. See reprex:

library(sf)
library(spatstat)
library(maptools)

# With sf
polygon <- st_read("recintos_provinciales_inspire_peninbal_etrs89.shp")
st_is_longlat(polygon)

#> [1] TRUE

# sf to sp
polygon_sp <- as(polygon, "Spatial")
# Error
im <- as.owin(polygon_sp, fatal = TRUE)

#> Error in as.owin.SpatialPolygons(polygon_sp, fatal = TRUE) : 
#>  Only projected coordinates may be converted to spatstat class objects

# Project EPSG:25830
polygon_utm <- st_transform(polygon, 25830)

st_is_longlat(polygon_utm)
#> [1] FALSE

# sf to sp
polygon_utm_sp <- as(polygon_utm, "Spatial")
# No error
im_utm <- as.owin(polygon_utm_sp, fatal = TRUE)

im_utm

#> window: polygonal boundary
#> enclosing rectangle: [-14129.5, 1126923.3] x [3892590, 4859517] units

Upvotes: 1

Related Questions