Extract xy coordinates from raster of interesting neighborhood cells

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

Extract xy coordinates from raster of interesting neighborhood cells

R-sig-geo mailing list
Dear R-Sig-Geo Members,

     I've like to extract xy coordinates from raster of 24 neigborhood
cells that surround the 4 given points (pts_s) pixels and for this I used:

#Packages

require(raster)
require(sp)

## Create a raster
r <- raster(nc=30, nr=30)
r <- setValues(r, round(runif(ncell(r))* 255))
plot(r)

##Given interesting points coordinates
xd     <- c(-24.99270,45.12069,99.40321,73.64419)
yd  <- c(-45.435267,-88.369745,-7.086949,44.174530)
pts <- data.frame(xd,yd)
pts_s<- SpatialPoints(pts)
points(pts_s, col="red", pch=16)

## Find pixels center of each point (My idea is not to use the original
point coordinates, but coordinates of the center of interesting pixel).
N_cells <- cellFromXY(r, pts_s)


# Extract xy coordinates from raster of 24 neighborhood cells given
pixel number N_cells
e<-adjacent(r, N_cells , directions='bishop', id=TRUE)
coordinates(land_mask)[e[,1],] ## Doesn't return 24 neighborhood by
N_cells object
ng_coords<-coordinates(land_mask)[e[,1],]
#

#Visualization

plot(r)
points(pts_s, col="red", pch=16)
points(ng_coords, col="black", pch=16)
##

     But I don't have success because the ng_coords object is not a 24
neighborhood cells of each point that I search. There are solution for this?

Thanks in advance,

Alexandre

--
======================================================================
Alexandre dos Santos
Proteção Florestal
IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
Campus Cáceres
Caixa Postal 244
Avenida dos Ramires, s/n
Bairro: Distrito Industrial
Cáceres - MT                      CEP: 78.200-000
Fone: (+55) 65 99686-6970 (VIVO) (+55) 65 3221-2674 (FIXO)

         [hidden email]
Lattes: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
Researchgate: www.researchgate.net/profile/Alexandre_Santos10
LinkedIn: br.linkedin.com/in/alexandre-dos-santos-87961635
Mendeley:www.mendeley.com/profiles/alexandre-dos-santos6/

_______________________________________________
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: Extract xy coordinates from raster of interesting neighborhood cells

Ben Tupper
Hi,

I'm not sure why you expect 24 points - you have 4 locations and for each you want the 4 bishop's directions - so, I think at most you should expect 16 points.  See http://www.lpc.uottawa.ca/publications/moransi/moran.htm <http://www.lpc.uottawa.ca/publications/moransi/moran.htm> for where I expect 4 points for bishop's for each input location; perhaps you have a different idea of bishop's direction?  

Your second point [45.12069, -88.369745] is on to the southern edge of the raster - so it can't find points southward - only the 2 northward.

Finally, it may not make any difference at all, but your pts_s has no coordinate reference.  Below I show where I assign the same projection as your raster.  I also don't have 'land_mask' so I reused 'r' and used xyFromCell to backwards extract the adjacent cell coordinates.

Does that do the trick for you?

Cheers,
Ben

### START
require(raster)
require(sp)

## Create a raster
r <- raster(nc=30, nr=30)
r <- setValues(r, round(runif(ncell(r))* 255))
plot(r)

##Given interesting points coordinates
xd     <- c( -24.99270,   45.12069,  99.40321,  73.64419)
yd     <- c(-45.435267, -88.369745, -7.086949, 44.174530)
pts <- data.frame(xd,yd)
pts_s<- SpatialPoints(pts)
projection(pts_s) <- projection(r)
points(pts_s, col="red", pch=16)

## Find pixels center of each point
N_cells <- cellFromXY(r, pts_s)

e <- adjacent(r, N_cells , directions='bishop', id=TRUE, sorted = TRUE)
xy <- xyFromCell(r, e[,'to'], spatial = TRUE)


#Visualization

plot(r)
points(pts_s, col="red", pch=16)
points(xy, col="black", pch=16)
### END





> On May 30, 2018, at 3:11 PM, ASANTOS via R-sig-Geo <[hidden email]> wrote:
>
> Dear R-Sig-Geo Members,
>
>     I've like to extract xy coordinates from raster of 24 neigborhood cells that surround the 4 given points (pts_s) pixels and for this I used:
>
> #Packages
>
> require(raster)
> require(sp)
>
> ## Create a raster
> r <- raster(nc=30, nr=30)
> r <- setValues(r, round(runif(ncell(r))* 255))
> plot(r)
>
> ##Given interesting points coordinates
> xd     <- c(-24.99270,45.12069,99.40321,73.64419)
> yd  <- c(-45.435267,-88.369745,-7.086949,44.174530)
> pts <- data.frame(xd,yd)
> pts_s<- SpatialPoints(pts)
> points(pts_s, col="red", pch=16)
>
> ## Find pixels center of each point (My idea is not to use the original point coordinates, but coordinates of the center of interesting pixel).
> N_cells <- cellFromXY(r, pts_s)
>
>
> # Extract xy coordinates from raster of 24 neighborhood cells given pixel number N_cells
> e<-adjacent(r, N_cells , directions='bishop', id=TRUE)
> coordinates(land_mask)[e[,1],] ## Doesn't return 24 neighborhood by N_cells object
> ng_coords<-coordinates(land_mask)[e[,1],]
> #
>
> #Visualization
>
> plot(r)
> points(pts_s, col="red", pch=16)
> points(ng_coords, col="black", pch=16)
> ##
>
>     But I don't have success because the ng_coords object is not a 24 neighborhood cells of each point that I search. There are solution for this?
>
> Thanks in advance,
>
> Alexandre
>
> --
> ======================================================================
> Alexandre dos Santos
> Proteção Florestal
> IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
> Campus Cáceres
> Caixa Postal 244
> Avenida dos Ramires, s/n
> Bairro: Distrito Industrial
> Cáceres - MT                      CEP: 78.200-000
> Fone: (+55) 65 99686-6970 (VIVO) (+55) 65 3221-2674 (FIXO)
>
>        [hidden email]
> Lattes: http://lattes.cnpq.br/1360403201088680
> OrcID: orcid.org/0000-0001-8232-6722
> Researchgate: www.researchgate.net/profile/Alexandre_Santos10
> LinkedIn: br.linkedin.com/in/alexandre-dos-santos-87961635
> Mendeley:www.mendeley.com/profiles/alexandre-dos-santos6/
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/






        [[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: Extract xy coordinates from raster of interesting neighborhood cells

Vijay Lulla
Or you can try something like this?

### START
require(raster)
require(sp)

r <- raster(nc=30, nr=30)
r <- setValues(r, round(runif(ncell(r))* 255))

xd     <- c(-24.99270,45.12069,99.40321,73.64419)
yd  <- c(-45.435267,-88.369745,-7.086949,44.174530)
pts <- data.frame(xd,yd)
pts_s<- SpatialPoints(pts)

N_cells <- cellFromXY(r, pts_s)
e<-adjacent(r, N_cells , directions='bishop', pairs=FALSE)

ng_coords <- xyFromCell(r, e)
plot(r)
points(pts, col='red', pch=16)
points(ng_coords, col='black', pch=16)


### Alternative to what Ben suggested!
## Set neighborhood matrix.  Focal cell is 0 while neighbors are 1!!
neigh <- matrix(1L, nrow=5, ncol=5); neigh[3,3] <- 0L
e1<-adjacent(r, N_cells , directions=neigh, pairs=FALSE)
ng_coords1 <- xyFromCell(r, e1)
plot(r);
points(pts, col='red', pch=16);
points(ng_coords, col='black', pch=16);
points(ng_coords1, col='blue', pch=16)
### END

HTH,
Vijay.

On Wed, May 30, 2018 at 4:22 PM, Ben Tupper <[hidden email]> wrote:

> Hi,
>
> I'm not sure why you expect 24 points - you have 4 locations and for each
> you want the 4 bishop's directions - so, I think at most you should expect
> 16 points.  See http://www.lpc.uottawa.ca/publications/moransi/moran.htm <
> http://www.lpc.uottawa.ca/publications/moransi/moran.htm> for where I
> expect 4 points for bishop's for each input location; perhaps you have a
> different idea of bishop's direction?
>
> Your second point [45.12069, -88.369745] is on to the southern edge of the
> raster - so it can't find points southward - only the 2 northward.
>
> Finally, it may not make any difference at all, but your pts_s has no
> coordinate reference.  Below I show where I assign the same projection as
> your raster.  I also don't have 'land_mask' so I reused 'r' and used
> xyFromCell to backwards extract the adjacent cell coordinates.
>
> Does that do the trick for you?
>
> Cheers,
> Ben
>
> ### START
> require(raster)
> require(sp)
>
> ## Create a raster
> r <- raster(nc=30, nr=30)
> r <- setValues(r, round(runif(ncell(r))* 255))
> plot(r)
>
> ##Given interesting points coordinates
> xd     <- c( -24.99270,   45.12069,  99.40321,  73.64419)
> yd     <- c(-45.435267, -88.369745, -7.086949, 44.174530)
> pts <- data.frame(xd,yd)
> pts_s<- SpatialPoints(pts)
> projection(pts_s) <- projection(r)
> points(pts_s, col="red", pch=16)
>
> ## Find pixels center of each point
> N_cells <- cellFromXY(r, pts_s)
>
> e <- adjacent(r, N_cells , directions='bishop', id=TRUE, sorted = TRUE)
> xy <- xyFromCell(r, e[,'to'], spatial = TRUE)
>
>
> #Visualization
>
> plot(r)
> points(pts_s, col="red", pch=16)
> points(xy, col="black", pch=16)
> ### END
>
>
>
>
>
> > On May 30, 2018, at 3:11 PM, ASANTOS via R-sig-Geo <
> [hidden email]> wrote:
> >
> > Dear R-Sig-Geo Members,
> >
> >     I've like to extract xy coordinates from raster of 24 neigborhood
> cells that surround the 4 given points (pts_s) pixels and for this I used:
> >
> > #Packages
> >
> > require(raster)
> > require(sp)
> >
> > ## Create a raster
> > r <- raster(nc=30, nr=30)
> > r <- setValues(r, round(runif(ncell(r))* 255))
> > plot(r)
> >
> > ##Given interesting points coordinates
> > xd     <- c(-24.99270,45.12069,99.40321,73.64419)
> > yd  <- c(-45.435267,-88.369745,-7.086949,44.174530)
> > pts <- data.frame(xd,yd)
> > pts_s<- SpatialPoints(pts)
> > points(pts_s, col="red", pch=16)
> >
> > ## Find pixels center of each point (My idea is not to use the original
> point coordinates, but coordinates of the center of interesting pixel).
> > N_cells <- cellFromXY(r, pts_s)
> >
> >
> > # Extract xy coordinates from raster of 24 neighborhood cells given
> pixel number N_cells
> > e<-adjacent(r, N_cells , directions='bishop', id=TRUE)
> > coordinates(land_mask)[e[,1],] ## Doesn't return 24 neighborhood by
> N_cells object
> > ng_coords<-coordinates(land_mask)[e[,1],]
> > #
> >
> > #Visualization
> >
> > plot(r)
> > points(pts_s, col="red", pch=16)
> > points(ng_coords, col="black", pch=16)
> > ##
> >
> >     But I don't have success because the ng_coords object is not a 24
> neighborhood cells of each point that I search. There are solution for this?
> >
> > Thanks in advance,
> >
> > Alexandre
> >
> > --
> > ======================================================================
> > Alexandre dos Santos
> > Proteção Florestal
> > IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
> > Campus Cáceres
> > Caixa Postal 244
> > Avenida dos Ramires, s/n
> > Bairro: Distrito Industrial
> > Cáceres - MT                      CEP: 78.200-000
> > Fone: (+55) 65 99686-6970 (VIVO) (+55) 65 3221-2674 (FIXO)
> >
> >        [hidden email]
> > Lattes: http://lattes.cnpq.br/1360403201088680
> > OrcID: orcid.org/0000-0001-8232-6722
> > Researchgate: www.researchgate.net/profile/Alexandre_Santos10
> > LinkedIn: br.linkedin.com/in/alexandre-dos-santos-87961635
> > Mendeley:www.mendeley.com/profiles/alexandre-dos-santos6/
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > [hidden email]
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



--
Vijay Lulla, Ph.D.

Assistant Professor,
Dept. of Geography, IUPUI
425 University Blvd, CA-207C.
Indianapolis, IN-46202
[hidden email]

<http://vijaylulla.com>
http://vijaylulla.com

        [[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: Extract xy coordinates from raster of interesting neighborhood cells

R-sig-geo mailing list
Thanks so much Vijay and Ben, now it works!!!

             Originally, I expect 24 for each 4 point (96 total).
Apologies, in my example preparation I changed the raster objects
'land_mask' and 'r' and the idea of my second point [45.12069,
-88.369745] is to force NA results.

             The code below is a solution that I expected:

### Alternative to what Ben suggested!
## Set neighborhood matrix.  Focal cell is 0 while neighbors are 1!!
neigh <- matrix(1L, nrow=5, ncol=5); neigh[3,3] <- 0L
e1<-adjacent(r, N_cells , directions=neigh, pairs=FALSE)
ng_coords1 <- xyFromCell(r, e1)
plot(r);
points(pts, col='red', pch=16);
points(ng_coords, col='black', pch=16);
points(ng_coords1, col='blue', pch=16)
### END

--
======================================================================
Alexandre dos Santos
Proteção Florestal
IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
Campus Cáceres
Caixa Postal 244
Avenida dos Ramires, s/n
Bairro: Distrito Industrial
Cáceres - MT                      CEP: 78.200-000
Fone: (+55) 65 99686-6970 (VIVO) (+55) 65 3221-2674 (FIXO)

         [hidden email]
Lattes: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
Researchgate: www.researchgate.net/profile/Alexandre_Santos10
LinkedIn: br.linkedin.com/in/alexandre-dos-santos-87961635
Mendeley:www.mendeley.com/profiles/alexandre-dos-santos6/
======================================================================

Em 30/05/2018 16:29, Vijay Lulla escreveu:

> Or you can try something like this?
>
> ### START
> require(raster)
> require(sp)
>
> r <- raster(nc=30, nr=30)
> r <- setValues(r, round(runif(ncell(r))* 255))
>
> xd     <- c(-24.99270,45.12069,99.40321,73.64419)
> yd  <- c(-45.435267,-88.369745,-7.086949,44.174530)
> pts <- data.frame(xd,yd)
> pts_s<- SpatialPoints(pts)
>
> N_cells <- cellFromXY(r, pts_s)
> e<-adjacent(r, N_cells , directions='bishop', pairs=FALSE)
>
> ng_coords <- xyFromCell(r, e)
> plot(r)
> points(pts, col='red', pch=16)
> points(ng_coords, col='black', pch=16)
>
>
[[elided Yahoo spam]]
[[elided Yahoo spam]]

> neigh <- matrix(1L, nrow=5, ncol=5); neigh[3,3] <- 0L
> e1<-adjacent(r, N_cells , directions=neigh, pairs=FALSE)
> ng_coords1 <- xyFromCell(r, e1)
> plot(r);
> points(pts, col='red', pch=16);
> points(ng_coords, col='black', pch=16);
> points(ng_coords1, col='blue', pch=16)
> ### END
>
> HTH,
> Vijay.
>
> On Wed, May 30, 2018 at 4:22 PM, Ben Tupper <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi,
>
>     I'm not sure why you expect 24 points - you have 4 locations and
>     for each you want the 4 bishop's directions - so, I think at most
>     you should expect 16 points.  See
>     http://www.lpc.uottawa.ca/publications/moransi/moran.htm
>     <http://www.lpc.uottawa.ca/publications/moransi/moran.htm>
>     <http://www.lpc.uottawa.ca/publications/moransi/moran.htm
>     <http://www.lpc.uottawa.ca/publications/moransi/moran.htm>> for
>     where I expect 4 points for bishop's for each input location;
>     perhaps you have a different idea of bishop's direction?
>
>     Your second point [45.12069, -88.369745] is on to the southern
>     edge of the raster - so it can't find points southward - only the
>     2 northward.
>
>     Finally, it may not make any difference at all, but your pts_s has
>     no coordinate reference.  Below I show where I assign the same
>     projection as your raster.  I also don't have 'land_mask' so I
>     reused 'r' and used xyFromCell to backwards extract the adjacent
>     cell coordinates.
>
>     Does that do the trick for you?
>
>     Cheers,
>     Ben
>
>     ### START
>     require(raster)
>     require(sp)
>
>     ## Create a raster
>     r <- raster(nc=30, nr=30)
>     r <- setValues(r, round(runif(ncell(r))* 255))
>     plot(r)
>
>     ##Given interesting points coordinates
>     xd     <- c( -24.99270,   45.12069,  99.40321, 73.64419)
>     yd     <- c(-45.435267, -88.369745, -7.086949, 44.174530)
>     pts <- data.frame(xd,yd)
>     pts_s<- SpatialPoints(pts)
>     projection(pts_s) <- projection(r)
>     points(pts_s, col="red", pch=16)
>
>     ## Find pixels center of each point
>     N_cells <- cellFromXY(r, pts_s)
>
>     e <- adjacent(r, N_cells , directions='bishop', id=TRUE, sorted =
>     TRUE)
>     xy <- xyFromCell(r, e[,'to'], spatial = TRUE)
>
>
>     #Visualization
>
>     plot(r)
>     points(pts_s, col="red", pch=16)
>     points(xy, col="black", pch=16)
>     ### END
>
>
>
>
>
>     > On May 30, 2018, at 3:11 PM, ASANTOS via R-sig-Geo
>     <[hidden email] <mailto:[hidden email]>> wrote:
>     >
>     > Dear R-Sig-Geo Members,
>     >
>     >     I've like to extract xy coordinates from raster of 24
>     neigborhood cells that surround the 4 given points (pts_s) pixels
>     and for this I used:
>     >
>     > #Packages
>     >
>     > require(raster)
>     > require(sp)
>     >
>     > ## Create a raster
>     > r <- raster(nc=30, nr=30)
>     > r <- setValues(r, round(runif(ncell(r))* 255))
>     > plot(r)
>     >
>     > ##Given interesting points coordinates
>     > xd     <- c(-24.99270,45.12069,99.40321,73.64419)
>     > yd  <- c(-45.435267,-88.369745,-7.086949,44.174530)
>     > pts <- data.frame(xd,yd)
>     > pts_s<- SpatialPoints(pts)
>     > points(pts_s, col="red", pch=16)
>     >
>     > ## Find pixels center of each point (My idea is not to use the
>     original point coordinates, but coordinates of the center of
>     interesting pixel).
>     > N_cells <- cellFromXY(r, pts_s)
>     >
>     >
>     > # Extract xy coordinates from raster of 24 neighborhood cells
>     given pixel number N_cells
>     > e<-adjacent(r, N_cells , directions='bishop', id=TRUE)
>     > coordinates(land_mask)[e[,1],] ## Doesn't return 24 neighborhood
>     by N_cells object
>     > ng_coords<-coordinates(land_mask)[e[,1],]
>     > #
>     >
>     > #Visualization
>     >
>     > plot(r)
>     > points(pts_s, col="red", pch=16)
>     > points(ng_coords, col="black", pch=16)
>     > ##
>     >
>     >     But I don't have success because the ng_coords object is not
>     a 24 neighborhood cells of each point that I search. There are
>     solution for this?
>     >
>     > Thanks in advance,
>     >
>     > Alexandre
>     >
>     > --
>     >
>     ======================================================================
>     > Alexandre dos Santos
>     > Proteção Florestal
>     > IFMT - Instituto Federal de Educação, Ciência e Tecnologia de
>     Mato Grosso
>     > Campus Cáceres
>     > Caixa Postal 244
>     > Avenida dos Ramires, s/n
>     > Bairro: Distrito Industrial
>     > Cáceres - MT                      CEP: 78.200-000
>     > Fone: (+55) 65 99686-6970 (VIVO) (+55) 65 3221-2674 (FIXO)
>     >
>     > [hidden email]
>     <mailto:[hidden email]>
>     > Lattes: http://lattes.cnpq.br/1360403201088680
>     <http://lattes.cnpq.br/1360403201088680>
>     > OrcID: orcid.org/0000-0001-8232-6722
>     <http://orcid.org/0000-0001-8232-6722>
>     > Researchgate: www.researchgate.net/profile/Alexandre_Santos10
>     <http://www.researchgate.net/profile/Alexandre_Santos10>
>     > LinkedIn: br.linkedin.com/in/alexandre-dos-santos-87961635
>     <http://br.linkedin.com/in/alexandre-dos-santos-87961635>
>     > Mendeley:www.mendeley.com/profiles/alexandre-dos-santos6/
>     <http://www.mendeley.com/profiles/alexandre-dos-santos6/>
>     >
>     > _______________________________________________
>     > R-sig-Geo mailing list
>     > [hidden email] <mailto:[hidden email]>
>     > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>     <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>     >
>
>     Ben Tupper
>     Bigelow Laboratory for Ocean Sciences
>     60 Bigelow Drive, P.O. Box 380
>     East Boothbay, Maine 04544
>     http://www.bigelow.org
>
>     Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>
>
>             [[alternative HTML version deleted]]
>
>     _______________________________________________
>     R-sig-Geo mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>     <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>
>
>
>
> --
> Vijay Lulla, Ph.D.
>
> Assistant Professor,
> Dept. of Geography, IUPUI
> 425 University Blvd, CA-207C.
> Indianapolis, IN-46202
> [hidden email] <mailto:[hidden email]>
>
> <http://vijaylulla.com>
> http://vijaylulla.com


        [[alternative HTML version deleted]]

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