[R-sig-Geo] as.im in spatstat::ppm not working as before

Roozbeh Valavi v@|@v|@r @end|ng |rom gm@||@com
Sat May 11 10:21:34 CEST 2024


This might also help:

library(spatstat)
library(terra)

.raster_to_im <- function(x){
        r <- as.data.frame(x, xy = TRUE)
         im <- spatstat.geom::as.im(r) return(im)
}

 # to generalise the spatstat.geom::as.im to SpatRaster and RasterLayer
as.im.SpatRaster <- function(x){ .raster_to_im(x) } as.im.RasterLayer <-
function(x){ .raster_to_im(x) }


I haven’t used it in a while so there might be a direct support in the
spatstat.geom now!


Roozbeh Valavi,
Biodiversity Modeller & Spatial Ecologist
Senior Research Scientist | CSIRO Environment, VIC, Australia
*Twitter*: @ValaviRoozbeh | ResearchGate
<https://www.researchgate.net/profile/Roozbeh-Valavi> | Google Scholar
<https://scholar.google.co.uk/citations?user=3m0jCHwAAAAJ&hl=en&oi=ao>


On Wed, 8 May 2024 at 7:41 PM, Roger Bivand <Roger.Bivand using nhh.no> wrote:

> as.im() was in maptools, which was retired last year. If you need to
> recreate old work, install maptools from source from the CRAN package
> archive
> https://cran.r-project.org/src/contrib/Archive/maptools/.
>
> For non-archival work, update the workflow. See also
> https://github.com/r-spatial/evolution/issues/8.
>
> Hope this clarifies,
>
> Roger
>
> ---
> Roger Bivand
> Emeritus Professor
> Department of Economics
> Norwegian School of Economics, Bergen, Norway
>
> ________________________________
> Fra: R-sig-Geo <r-sig-geo-bounces using r-project.org> på vegne av Edzer
> Pebesma <edzer.pebesma using uni-muenster.de>
> Sendt: onsdag, mai 8, 2024 9:28:54 a.m.
> Til: r-sig-geo using r-project.org <r-sig-geo using r-project.org>
> Emne: Re: [R-sig-Geo] as.im in spatstat::ppm not working as before
>
> r.grid.world.SGDF.F[[1]] is a numerical vector, not even a matrix, so
> clearly as.im.default doesn't know what it should do with it.
>
> Maybe there was once a method as.im for r.grid.world.SGDF.F, which is of
> class SpatialGridDataFrame, but no longer - software evolves.
>
> A way you could approach this is converting that object to a stars object:
>
>  > library(stars)
> Loading required package: abind
> Loading required package: sf
> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
>  > as.im(st_as_stars(r.grid.world.SGDF.F))
> Error: Only projected coordinates may be converted to spatstat class
> objects
>
> and that points you to another issue you have: spatstat works with data
> in Cartesian coordiantes (R2), not with geodetic coordinates (S2). So
> that's a helpful error message.
>
>
> On 08/05/2024 01:49, Alexandre Santos via R-sig-Geo wrote:
> > Dear Members,
> >
> > I'll calculate some old codes for a paper review, and I'm astonished
> that as.im() for fitting the Poisson model is not working as before. In
> my example:
> >
> > library(raster)
> > library(spatstat)
> >
> >
> > # Elevation
> > r.grid.world <- raster('
> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fraw.githubusercontent.com%2FLeprechault%2Ftrash%2Fmain%2Fwc2.1_10m_elev.tif&data=05%7C02%7CRoger.Bivand%40nhh.no%7Cd2a2b64634a7462f860708dc6f3082d5%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C638507501339842669%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=cyducyKF16yIdtfuykXUSifCR8clFtxDhiG1eY4waFU%3D&reserved=0
> <
> https://raw.githubusercontent.com/Leprechault/trash/main/wc2.1_10m_elev.tif
> >')
> > proj4string(r.grid.world) <- CRS("+proj=longlat +datum=WGS84 +no_defs
> +ellps=WGS84 +towgs84=0,0,0")
> > r.grid.world.SGDF.F <- as(r.grid.world, "SpatialGridDataFrame")
> >
> >
> > #Fitting Poisson model
> > fit.c <- ppm(nztrees, ~ elev,
> > covariates=list(elev=as.im(r.grid.world.SGDF.F[[1]])))
> > #
> > # Error in as.im.default(r.grid.world.SGDF.F[[1]]) :
> > #   Can't convert X to a pixel image
> >
> > I tried as matrix and rotate, and the final real-valued pixel image
> didn't work as a covariate in the model.
> >
> > Please, any help with it?
> >
> > Thanks in advance
> >
> > Alexandre
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo using r-project.org
> >
> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=05%7C02%7CRoger.Bivand%40nhh.no%7Cd2a2b64634a7462f860708dc6f3082d5%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C638507501339851113%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=LeMCUv%2F8%2FJCZFGehLYaPgDEGRfsPeZZbkV5aCia8l60%3D&reserved=0
> <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>
> --
> Edzer Pebesma (he/him)
> Universität Münster, Institute for Geoinforma
> <https://www.google.com/maps/search/versit%C3%A4t+M%C3%BCnster,+Institute+for+Geoinforma?entry=gmail&source=g>
> tics
> Heisenbergstrasse 2, 48149 Münster, Germany
> Phone: +49 251 8333081
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
>
> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=05%7C02%7CRoger.Bivand%40nhh.no%7Cd2a2b64634a7462f860708dc6f3082d5%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C638507501339856786%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=6H%2F9HQ%2BV1nyf2AdWabq0eoG3b8zqCQEC3ZC%2BFoRYvUI%3D&reserved=0
> <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list