[R-sig-Geo] how to get the value of a pixel and its 8 surrounding pixels from points?

Michael Sumner md@umner @end|ng |rom gm@||@com
Wed Nov 6 10:48:24 CET 2019


In raster try   cellFromXY() and adjacent()  which separates out the two
tasks, find a cell and find cell neighbours

On Wed., 6 Nov. 2019, 17:18 Bede-Fazekas Ákos, <bfalevlist using gmail.com> wrote:

> Dear Cristabel,
> function focal() of package raster is what you are searching for.
> focal(x, w = matrix(1/9, ncol = 3, nrow = 3), fun = sum)
> or
> focal(x, w = matrix(1, ncol = 3, nrow = 3), fun = mean)
>
> HTH,
> Ákos Bede-Fazekas
> Hungarian Academy of Sciences
>
> 2019.11.05. 17:10 keltezéssel, "Cristabel Durán Rangel" írta:
> >
> > I need to get the value of a pixel (I have coordinates for this
> > pixels) and its 8 surrounding pixel from the pixel/point. I am working
> > with NetCDF files. I am working with R.
> >
> > This is my code till now:
> >
> > nc <- nc_open(file, readunlim=FALSE)
> >
> > mylon <- ncvar_get(nc,"lon")
> > mylat <- ncvar_get(nc,"lat")
> >
> > my coordinates in real-world: lat 52.5935 lon 18.4467
> >
> > lat.coor <-mylat[which.min(abs(mylat-52.5935))]
> > lon.coor <- mylon[which.min(abs(mylon-18.4467))]
> >
> > var <- nc[lon.coor, lat.coor, ]
> >
> > In var are the values for my point. But I also need the values of the
> > 8 surrounding pixels to get an average.
> >
> > Thanks.
> >
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo using r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> _______________________________________________
> 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