[R-sig-Geo] Create pixels neighborhood in a raster

ASANTOS @|ex@ndre@@nto@br @end|ng |rom y@hoo@com@br
Mon Nov 9 18:45:56 CET 2020


Dear r-sig-geo Members,

I'd like to find any way to create 1 (total 9 pixels) and 2 pixels 
(total 25 pixels) surrounding the neighborhood of each pixel (ant) in my 
plot image (antscount).

In my example:

#Packages
library(spatstat)
library(raster)

#Selection of ants data set
data(ants)
geo.form<-cbind(x=ants$x,y=ants$y)

#Definition of raster resolution - 10 units
ants.w<-as.owin(ants)
ext <- as(extent(c(ants.w$xrange,ants.w$yrange)), "SpatialPolygons")
ants.res<-rasterToPoints(raster(ext, resolution = 10), spatial = TRUE)
coordinates(ants.res) <- ~ x + y
# coerce to SpatialPixelsDataFrame
gridded(ants.res) <- TRUE

#Rasterize
antscount<- rasterize(geo.form, raster(ants.res), fun='count', background=0)
values(antscount)[values(antscount) > 0] = 1

#Vizualize
plot(antscounts)

Now, the selection of neighborhood pixels sounds easy, something like:

# For 1 pixel neighborhood
neigh1 <- matrix(1L, nrow=3, ncol=3)
e1<-adjacent(antscounts, cells , directions=neigh1, pairs=FALSE)
ng_coords1 <- xyFromCell(antscounts, e1)

# For 2 pixel neighborhood
neigh2 <- matrix(1L, nrow=5, ncol=5)
e2<-adjacent(antscounts, cells , directions=neigh2, pairs=FALSE)
ng_coords5 <- xyFromCell(antscounts, e2)

But for the combination of all the information (0 and 1 new pixel 
values) and the new raster representation (antscounts) I don't have 
success. Please, any ideas?

Thanks in advanced,

Alexandre

-- 
Alexandre dos Santos
Geotechnologies and Spatial Statistics applied to Forest Entomology
Instituto Federal de Mato Grosso (IFMT) - Campus Caceres
Caixa Postal 244 (PO Box)
Avenida dos Ramires, s/n - Vila Real
Caceres - MT - CEP 78201-380 (ZIP code)
Phone: (+55) 65 99686-6970 / (+55) 65 3221-2674
Lattes CV: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
ResearchGate: www.researchgate.net/profile/Alexandre_Santos10
Publons: https://publons.com/researcher/3085587/alexandre-dos-santos/
--



More information about the R-sig-Geo mailing list