2

I keep getting the below error message when I try to use R in QGIS. For example Monte Carlo or Quadrant.

I use a point shape file with the x and y coordinates as centroids within the attribute table.

For example

##Point pattern analysis=group
##Layer=vector point
##Simulations=number 100
##Optional_plot_name=string
##showplots
library(spatstat)
library(maptools)
sp <- as(Layer, "SpatialPoints")
sp <- as(sp, "ppp")
e <- envelope(sp, Kest, nsim = Simulations)
>e
plot(e, main = Optional_plot_name)

Error Message

Error in as(Layer, "SpatialPoints") :
no method or default for coercing "sf" to "SpatialPoints"
Calls: as -> .class1 -> as

Info on the point shapefile as follows

Name
LandScanJoinCTAT
Path
G:\Article\GIS\LandScan\Master\LandScanJoinCTAT.shp
Storage
ESRI Shapefile
Comment

Encoding
System
Geometry
Point (Point)
CRS
EPSG:4326 - WGS 84 - Geographic
Extent
17.0590112291777913,-34.4981314092633582 : 32.5317232060707155,-22.4313408665917606
Unit
degrees
Feature count
234
3
  • 1
    There is no method for this. Instead use as(x, "Spatial") which will result in a SpatialPointsDataFrame, as it should. If you need aSpatialPoints object you can coerce using coordinates Jun 15, 2020 at 18:48
  • It looks like QGIS is passing vector data (here Layer) as sf objects instead of sp objects. I don't know when that happened or who wrote the analysis script being run here (wasn't me).
    – Spacedman
    Jun 15, 2020 at 18:58
  • @Spacedman The rsx code is still available here.
    – Kazuhito
    Jun 16, 2020 at 11:35

2 Answers 2

1

Latest version of Processing R Provider allows us to choose between sf and rgdal to load vector data, and sf is the default provider (from version 2.0, I guess).

You may follow the suggestion by @Jeffrey Evans, or add ##load_vector_using_rgdal option to use rgdal instead of sf; for instance -

##Point pattern analysis=group
##load_vector_using_rgdal 
##Layer=vector point
##Simulations=number 100
##Optional_plot_name=string
##showplots
library(spatstat)
library(maptools)
sp <- as(Layer, "SpatialPoints")
sp <- as(sp, "ppp")
e <- envelope(sp, Kest, nsim = Simulations)
>e
plot(e, main = Optional_plot_name)

It's the second line.

enter image description here

0

Thanks. I have used your advise and it seems to work. However I found another solution. I save the shape-files in csv via QGIS, then transfer the csv file into R and then use R to create another shape-file (similar to the original). I then open the R created shape-file in QGIS And it works fine. But only for point layers and files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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