how to get the value of a pixel and its 8 surrounding pixels from points?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

how to get the value of a pixel and its 8 surrounding pixels from points?

"Cristabel Durán Rangel"

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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: how to get the value of a pixel and its 8 surrounding pixels from points?

Bede-Fazekas Ákos
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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: how to get the value of a pixel and its 8 surrounding pixels from points?

Barry Rowlingson-3
On Wed, Nov 6, 2019 at 6:18 AM Bede-Fazekas Ákos <[hidden email]>
wrote:

> Dear Cristabel,
> function focal() of package raster is what you are searching for.
>

No it isn't. That operates over the whole raster, which is massive overkill
for querying the eight pixels around a single point.


>
> > 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.
>
>
The 8 pixels around X[i,j] are:

X[i-1,j-1], X[i, j-1], X[i+1,j-1]
X[i-1,j], X[i+1,j]
X[i-1,j+1], X[i,j+1], X[i+1,j+1]

as long as those cells aren't now outside the matrix.

So write a function that generates those offsets given i,j:

 > n8 =
function(i,j){cbind(i+c(-1,0,1,-1,1,-1,0,1),j+c(-1,-1,-1,0,0,1,1,1))}

which returns this:

 > n8(23,12)
     [,1] [,2]
[1,]   22   11
[2,]   23   11
[3,]   24   11
[4,]   22   12
[5,]   24   12
[6,]   22   13
[7,]   23   13
[8,]   24   13

Then for a test matrix:

> m = matrix(1:10000,100,100)

where i,j = 23, 12:

> m[23,12]
[1] 1123

the 8 neighbours are:

> m[n8(23,12)]
[1] 1022 1023 1024 1122 1124 1222 1223 1224
>

Now I'm not sure if you can do exactly that with your netcdf object because
netcdf is a structured flexible format, and you've shown `nc[lon, lat, ]`
where `nc` is your netcdf object rather than a variable got from it and I
can't get that to work for my test NCDF. (I have sea surface temp in an
netcdf which I get with `sst = nc_getvar(nc, "sst")` for example). But the
mechanism is the same - compute the index of the 8 neighbour pixels and
extract.

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: how to get the value of a pixel and its 8 surrounding pixels from points?

Michael Sumner-2
In reply to this post by Bede-Fazekas Ákos
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, <[hidden email]> 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
> > [hidden email]
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo