ropensci / maptools

CRAN Task View for mapping tools
44 stars 13 forks source link

Converting raster to PPP using as.ppp() requires maptools, which is no longer on CRAN #26

Open foreignsand opened 10 months ago

foreignsand commented 10 months ago

I am attempting to import a bitmap image into R, rasterize it, and calculate the area of discrete regions in the image using spatstats. Importing the bitmap into R and converting it to a raster isn't a problem:

library(bmp)
library(raster)
bmp_01 = read.bmp("20231031_110019.bmp")
rast_01 = raster(bmp_01)
plot(rast_01)

20231031_110019.bmp.zip

From what I've found googling, if I can convert the raster to an owin object or ppp (hypothetical name ppp_01), I can then calculate the area of the discrete ovoid regions in the object using the following code:

islands_dd <- dilation(ppp_01, 0.5, polygonal=FALSE)
islands_image_P <- connected(islands_dd)
islands_B <- tess(image = islands_image_P)
answer <- tile.areas(islands_B)

So I have at least two possibilities:

  1. Convert the raster into an object that can be manipulated with the above code. From what I've read, maptools is required to convert objects like rasters into a ppp or im or owin object. The maptools package is no longer available from CRAN and I am unfortunately unable to install maptools even with:
require('devtools')
install_github("ropensci/maptools")
  1. Find another way to calculate the area of the discrete 'islands' in my raster.

If you can help me figure out how to get my desired result, I would be SO grateful!

Many thanks, foreignsand

jstep818 commented 10 months ago

I am also running into a similar issue when trying to convert a raster to an "im" object in R 4.3.2 and spatstat 3.0-7. I'm trying to use a raster as the mu variable in rMatClust and previously the code below worked (R version 4.2.2, spatstat 3.0-2) but now I'm getting an error that X can't be converted to a pixel image. I assume this is because the previous version of "as.im" relied on maptools which is no longer available.

rMatClust(10, 10, mu = as.im(myraster)

I have found a workaround to convert the raster to a matrix, then invert and transpose it so it is correctly aligned, and then use spatstat.geom::as.im() to convert it, and I use the calls "xcol" and "yrow" in as.im() to define the extent of the window. However, this modified code produces no point outputs when using the same parameters as the original method.

I've reverted to an old version of R to get my code to run. Is there any workaround to convert raster or terra objects to pixel images?

Thank you Julia S