# Making an hexagonal grid using spsample

11 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Making an hexagonal grid using spsample

 Dear list members, I am trying to make an spatial hexagonal grid in R using the spsample function. I would like hexagons of 1 ha = 10000 sq meters. First I used a cell size of 62.04 m as the side of the hexagon. data(meuse.grid) gridded(meuse.grid) = ~x + y proj4string(meuse.grid) <- CRS("+init=epsg:28992") xx <- spsample(meuse.grid, type = "hexagonal", cellsize = 62.04) xx <- HexPoints2SpatialPolygons(xx) rgeos::gArea(xx, byid=TRUE) It gave me an area per hexagon = 3333.299 Second I used a cell size of 62.04 * 2 = 124.08 m as the long diagonal of the hexagon data(meuse.grid) gridded(meuse.grid) = ~x + y proj4string(meuse.grid) <- CRS("+init=epsg:28992") xx <- spsample(meuse.grid, type = "hexagonal", cellsize = 62.04*2) xx <- HexPoints2SpatialPolygons(xx) rgeos::gArea(xx, byid=TRUE) It gave me an area per hexagon = 13333.19 Both results are wrong, because I was expecting close to 10000. How can I make a spatial grid of 1 ha (or other size) in R? Manuel -- *Manuel Spínola, Ph.D.* Instituto Internacional en Conservación y Manejo de Vida Silvestre Universidad Nacional Apartado 1350-3000 Heredia COSTA RICA [hidden email] <[hidden email]> [hidden email] Teléfono: (506) 8706 - 4662 Personal website: Lobito de río Institutional website: ICOMVIS         [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Making an hexagonal grid using spsample

 Hi  Manuel, I do answer to the question "How can I make a spatial grid of 1 ha (or other size) in R?" You can use function hextess in spatstat library(spatstat) # some arbitrary area, with coordinates in hectometres   W <- Window(chorley) # As Rolf said, hexagons of 1ha should have side of 402.0673 metres, so, in hectometres: s <- 4.020673 plot(hextess(W, s)) plot(hexgrid(W, s), add=TRUE) Cheers, Marcelino El 03/03/2017 a las 20:03, Manuel Spínola escribió: > Dear list members, > > I am trying to make an spatial hexagonal grid in R using the spsample > function. I would like hexagons of 1 ha = 10000 sq meters. > > > First I used a cell size of 62.04 m as the side of the hexagon. > > data(meuse.grid) > gridded(meuse.grid) = ~x + y > proj4string(meuse.grid) <- CRS("+init=epsg:28992") > xx <- spsample(meuse.grid, type = "hexagonal", cellsize = 62.04) > xx <- HexPoints2SpatialPolygons(xx) > rgeos::gArea(xx, byid=TRUE) > > It gave me an area per hexagon = 3333.299 > > Second I used a cell size of 62.04 * 2 = 124.08 m as the long diagonal of > the hexagon > > data(meuse.grid) > gridded(meuse.grid) = ~x + y > proj4string(meuse.grid) <- CRS("+init=epsg:28992") > xx <- spsample(meuse.grid, type = "hexagonal", cellsize = 62.04*2) > xx <- HexPoints2SpatialPolygons(xx) > rgeos::gArea(xx, byid=TRUE) > > It gave me an area per hexagon = 13333.19 > > Both results are wrong, because I was expecting close to 10000. > > How can I make a spatial grid of 1 ha (or other size) in R? > > Manuel > -- Marcelino de la Cruz Rot Depto. de Biología y Geología Física y Química Inorgánica Universidad Rey Juan Carlos Móstoles España _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|
Report Content as Inappropriate

## Re: [FORGED] Re: Making an hexagonal grid using spsample

 On 04/03/17 08:38, Marcelino de la Cruz Rot wrote: > Hi  Manuel, > > I do answer to the question "How can I make a spatial grid of 1 ha (or > other size) in R?" > > You can use function hextess in spatstat > > library(spatstat) > > # some arbitrary area, with coordinates in hectometres >  W <- Window(chorley) > > # As Rolf said, hexagons of 1ha should have side of 402.0673 metres, so, > in hectometres: > > s <- 4.020673 > > plot(hextess(W, s)) > plot(hexgrid(W, s), add=TRUE) Marcelino, Actually I said hexagons of area *42* ha should have side length equal to 402.0673 metres. Moreover the Chorley data set has units of *kilometres* not hectometres, so that should be s <- 0.4020673.  Or, to avoid just a touch of round-off error, s <- sqrt(2*0.42)/3^0.75. Note that if you then do xxx <- hextess(W,s,trim=FALSE) unique(sapply(tiles(xxx),area.owin)) you get 0.42 --- i.e. 0.42 square kilometres, or 42 hectares. cheers, Rolf -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|
Report Content as Inappropriate

## Re: [FORGED] Re: Making an hexagonal grid using spsample

 My apologies for such a pair (or more) of embarrassing mistakes! I should read a bit more these days... Marcelino El 04/03/2017 a las 2:31, Rolf Turner escribió: > On 04/03/17 08:38, Marcelino de la Cruz Rot wrote: >> Hi  Manuel, >> >> I do answer to the question "How can I make a spatial grid of 1 ha (or >> other size) in R?" >> >> You can use function hextess in spatstat >> >> library(spatstat) >> >> # some arbitrary area, with coordinates in hectometres >>  W <- Window(chorley) >> >> # As Rolf said, hexagons of 1ha should have side of 402.0673 metres, so, >> in hectometres: >> >> s <- 4.020673 >> >> plot(hextess(W, s)) >> plot(hexgrid(W, s), add=TRUE) > > Marcelino, > > Actually I said hexagons of area *42* ha should have side length equal > to 402.0673 metres. > > Moreover the Chorley data set has units of *kilometres* not hectometres, > so that should be s <- 0.4020673.  Or, to avoid just a touch of > round-off error, s <- sqrt(2*0.42)/3^0.75. > > Note that if you then do > > xxx <- hextess(W,s,trim=FALSE) > unique(sapply(tiles(xxx),area.owin)) > > you get 0.42 --- i.e. 0.42 square kilometres, or 42 hectares. > > cheers, > > Rolf > -- Marcelino de la Cruz Rot Depto. de Biología y Geología Física y Química Inorgánica Universidad Rey Juan Carlos Móstoles España _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|
Report Content as Inappropriate

## Re: [FORGED] Re: Making an hexagonal grid using spsample

 Thank you very much to both. Manuel 2017-03-04 6:35 GMT-06:00 Marcelino de la Cruz Rot < [hidden email]>: > My apologies for such a pair (or more) of embarrassing mistakes! > > I should read a bit more these days... > > > Marcelino > > > > > El 04/03/2017 a las 2:31, Rolf Turner escribió: > >> On 04/03/17 08:38, Marcelino de la Cruz Rot wrote: >> >>> Hi  Manuel, >>> >>> I do answer to the question "How can I make a spatial grid of 1 ha (or >>> other size) in R?" >>> >>> You can use function hextess in spatstat >>> >>> library(spatstat) >>> >>> # some arbitrary area, with coordinates in hectometres >>>  W <- Window(chorley) >>> >>> # As Rolf said, hexagons of 1ha should have side of 402.0673 metres, so, >>> in hectometres: >>> >>> s <- 4.020673 >>> >>> plot(hextess(W, s)) >>> plot(hexgrid(W, s), add=TRUE) >>> >> >> Marcelino, >> >> Actually I said hexagons of area *42* ha should have side length equal to >> 402.0673 metres. >> >> Moreover the Chorley data set has units of *kilometres* not hectometres, >> so that should be s <- 0.4020673.  Or, to avoid just a touch of round-off >> error, s <- sqrt(2*0.42)/3^0.75. >> >> Note that if you then do >> >> xxx <- hextess(W,s,trim=FALSE) >> unique(sapply(tiles(xxx),area.owin)) >> >> you get 0.42 --- i.e. 0.42 square kilometres, or 42 hectares. >> >> cheers, >> >> Rolf >> >> > > -- > Marcelino de la Cruz Rot > Depto. de Biología y Geología > Física y Química Inorgánica > Universidad Rey Juan Carlos > Móstoles España > > -- *Manuel Spínola, Ph.D.* Instituto Internacional en Conservación y Manejo de Vida Silvestre Universidad Nacional Apartado 1350-3000 Heredia COSTA RICA [hidden email] <[hidden email]> [hidden email] Teléfono: (506) 8706 - 4662 Personal website: Lobito de río Institutional website: ICOMVIS         [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|
Report Content as Inappropriate

## Re: [FORGED] Re: Making an hexagonal grid using spsample

 In reply to this post by Marcelino de la Cruz Rot On 05/03/17 01:35, Marcelino de la Cruz Rot wrote: > My apologies for such a pair (or more) of embarrassing mistakes! > > I should read a bit more these days... I'm just happy to see that this sort of mistake happens to others as well as to my very good self! :-) cheers, Rolf -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Making an hexagonal grid using spsample

 In reply to this post by Manuel Spínola Hi, I understand you find a solution via hextess from spatstat. However, this is a solution with spsample from sp. Actually, the cellsize argument of spsample defines the distance between the center of consecutives hexagons. So it is the length of 2 medians of one of the 6 equilateral triangles composing the hexagon. You can obtain the correct cellsize with: - the formula that calculates the median length for a given equilateral triangle length size, - the formula that calculate the area of an hexagon with a given side length. library(sp) data(meuse.grid) gridded(meuse.grid) = ~x+y plot(meuse.grid) # targeted area in meters A <- 10000 # Corresponding cellsize : CS <- 2 * sqrt(A/((3*sqrt(3)/2))) * sqrt(3)/2 # hexagons HexPts <-spsample(x = meuse.grid, type = "hexagonal", cellsize = CS) HexPols <- HexPoints2SpatialPolygons(HexPts) plot(HexPols, add=TRUE) # check rgeos::gArea(HexPols, byid = T)[1:10] Since you asked me about getGridLayer via PM, this function of the cartography package uses spsample with its cellsize argument, maybe I should add a "targeted area" argument... _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Making an hexagonal grid using spsample

 In reply to this post by Manuel Spínola http://www.keittlab.org/On Fri, Mar 3, 2017 at 1:03 PM, Manuel Spínola <[hidden email]> wrote: > Dear list members, > > I am trying to make an spatial hexagonal grid in R using the spsample > function. I would like hexagons of 1 ha = 10000 sq meters. > > > First I used a cell size of 62.04 m as the side of the hexagon. > > data(meuse.grid) > gridded(meuse.grid) = ~x + y > proj4string(meuse.grid) <- CRS("+init=epsg:28992") > xx <- spsample(meuse.grid, type = "hexagonal", cellsize = 62.04) > xx <- HexPoints2SpatialPolygons(xx) > rgeos::gArea(xx, byid=TRUE) > > It gave me an area per hexagon = 3333.299 > > Second I used a cell size of 62.04 * 2 = 124.08 m as the long diagonal of > the hexagon > > data(meuse.grid) > gridded(meuse.grid) = ~x + y > proj4string(meuse.grid) <- CRS("+init=epsg:28992") > xx <- spsample(meuse.grid, type = "hexagonal", cellsize = 62.04*2) > xx <- HexPoints2SpatialPolygons(xx) > rgeos::gArea(xx, byid=TRUE) > > It gave me an area per hexagon = 13333.19 > > Both results are wrong, because I was expecting close to 10000. > > How can I make a spatial grid of 1 ha (or other size) in R? > You might search the list. I posted R code a long time ago that builds a hexagonal tessellation over a rectangle. I think Edzer used that to add the hexagonal option to spsample. THK > > Manuel > > -- > *Manuel Spínola, Ph.D.* > Instituto Internacional en Conservación y Manejo de Vida Silvestre > Universidad Nacional > Apartado 1350-3000 > Heredia > COSTA RICA > [hidden email] <[hidden email]> > [hidden email] > Teléfono: (506) 8706 - 4662 > Personal website: Lobito de río site/lobitoderio/> > Institutional website: ICOMVIS > >         [[alternative HTML version deleted]] > > _______________________________________________ > 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
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Making an hexagonal grid using spsample

 On Mon, Mar 6, 2017 at 2:18 PM, Tim Keitt <[hidden email]> wrote: > You might search the list https://stat.ethz.ch/pipermail/r-sig-geo/2007-March/001791.htmlhttp://www.keittlab.org/        [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Making an hexagonal grid using spsample

 In reply to this post by Timothée Giraud-2 Thank you very much Timothée. That works. Manuel 2017-03-06 9:42 GMT-06:00 Timothée Giraud <[hidden email]>: > Hi, > > I understand you find a solution via hextess from spatstat. > > However, this is a solution with spsample from sp. > > Actually, the cellsize argument of spsample defines the distance between > the center of consecutives hexagons. > So it is the length of 2 medians of one of the 6 equilateral triangles > composing the hexagon. > > You can obtain the correct cellsize with: > - the formula that calculates the median length for a given equilateral > triangle length size, > - the formula that calculate the area of an hexagon with a given side > length. > > > library(sp) > data(meuse.grid) > gridded(meuse.grid) = ~x+y > plot(meuse.grid) > > # targeted area in meters > A <- 10000 > # Corresponding cellsize : > CS <- 2 * sqrt(A/((3*sqrt(3)/2))) * sqrt(3)/2 > > # hexagons > HexPts <-spsample(x = meuse.grid, type = "hexagonal", cellsize = CS) > HexPols <- HexPoints2SpatialPolygons(HexPts) > > plot(HexPols, add=TRUE) > > # check > rgeos::gArea(HexPols, byid = T)[1:10] > > > > > Since you asked me about getGridLayer via PM, this function of the > cartography package uses spsample with its cellsize argument, maybe I > should add a "targeted area" argument... > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo> -- *Manuel Spínola, Ph.D.* Instituto Internacional en Conservación y Manejo de Vida Silvestre Universidad Nacional Apartado 1350-3000 Heredia COSTA RICA [hidden email] <[hidden email]> [hidden email] Teléfono: (506) 8706 - 4662 Personal website: Lobito de río Institutional website: ICOMVIS         [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo