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

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Tue Nov 10 00:41:20 CET 2020


Hi,

Your example isn't fully reproducible.  Or maybe it just has some little
errors.  I can't get past this spot...

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
>

You have requested that raster::rasterToPoints() return a spatial points
object, so there shouldn't be a need for sp::coordinates() to cast to a
spatial object.

Also, I think you are invoking "antscounts" and "antscount" as the same
variable.  Maybe?  It's not clear to me.

Finally, when you call raster::adjacent() you haven't defined the value for
the cells argument when computing e1 and e2.  What do you want them to be?

I wish I could be more helpful.

Cheers,
Ben

On Mon, Nov 9, 2020 at 12:46 PM ASANTOS via R-sig-Geo <
r-sig-geo using r-project.org> wrote:

> 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/
> --
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>


-- 
Ben Tupper
Bigelow Laboratory for Ocean Science
East Boothbay, Maine
http://www.bigelow.org/
https://eco.bigelow.org

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list