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

ASANTOS @|ex@ndre@@nto@br @end|ng |rom y@hoo@com@br
Tue Nov 10 15:19:53 CET 2020


Thanks Ben,

Little things do matter, I changed cells<- xyFromCell(antscount, ants1) 
by cells <- unique(cellFromXY(antscount, geo.form)) and works!!

Final solution:

#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)
# 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(antscount)

# For 1 pixel neighborhood
neigh1 <- matrix(1L, nrow=3, ncol=3); neigh1[2,2] <- 0L
ants1<-which(values(antscount)> 0)
cells <- unique(cellFromXY(antscount, geo.form))
e1<-adjacent(antscount, cells, directions=neigh1, pairs=FALSE)
ng_coords1 <- xyFromCell(antscount, e1)
points(ng_coords1, col="red")


#Rasterize for 1 pixel neighborhood
ng_coords2<-rbind(ng_coords1,geo.form)
antscount.9<- rasterize(ng_coords2, raster(ants.res), fun='count', 
background=0)
values(antscount.9)[values(antscount.9) > 0] = 1
plot(antscount.9)

-- 
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/
--

Em 10/11/2020 09:24, Ben Tupper escreveu:
> Hi,
>
> Thanks for updating the example - it is much easier.  I'm still not
> sure of exactly what you are after, but my day is young and there is
> still coffee to be had.
>
> One thing I suggest is that you modify your computation of cells.  As
> you have it, you are computing a matrix of x and y locations.
>
>> cells<- xyFromCell(antscount, ants1)
>
> But I really think you want to compute the cell numbers (1,2,3,...
> left-to-right, top-to-bottom) from your original data locations (your
> geo.form). Also, if I have followed correctly, I think you are looking
> for a list of occupied cells regardless of how many ants in each cell.
>
>> cells <- unique(cellFromXY(antscount, geo.form))
> Then I get a series of plots that "look" right to me with little
> squares across the plot.
>
> Cheers,
> Ben
>
>
> On Mon, Nov 9, 2020 at 7:48 PM ASANTOS <alexandresantosbr using yahoo.com.br> wrote:
>> Dear Ben,
>>
>> Sorry about of lot of mistakes, you are sure some part of code are
>> confusing and/or with errors. I'll try again with some corrections:
>>
>> First, I have the rasterization process of ants presence (0 is absence)
>> in a 10x10 units raster:
>>
>> #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)
>> # 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(antscount)
>>
>> Now, I'd like to create 1 pixels around each pixel of ant presence
>> (Total of 9 pixels in each ant presence in antscount raster):
>>
>> # For 1 pixel neighborhood
>> neigh1 <- matrix(1L, nrow=3, ncol=3); neigh1[2,2] <- 0L
>> ants1<-which(values(antscount)> 0)
>> cells<- xyFromCell(antscount, ants1)
>> e1<-adjacent(antscount, cells, directions=neigh1, pairs=FALSE)
>> ng_coords1 <- xyFromCell(antscount, e1)
>> points(ng_coords1, col="red")
>>
>> I need that's this new create pixels has 1 as value too.
>>
>> #Rasterize for 1 pixel neighborhood
>> antscount.9<- rasterize(ng_coords1 , raster(ants.res), fun='count',
>> background=0)
>> plot(antscount.9)
>>
>> The problem is my ng_coords1 coordinates is wrong and just only in the
>> top of the antscount raster, despite xyFromCell(antscount, ants1)
>> condition. My goal is a new ant presence raster with 8 pixels
>> surrounding the neigourhood of each pixel (ant) in the original
>> antscount raster.
>>
>> 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/
>> --
>>
>> Em 09/11/2020 19:41, Ben Tupper escreveu:
>>> 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
>>>>
>
>



More information about the R-sig-Geo mailing list