1

I am using R and have a SpatialGrid object similar to the example from this question:

Overlay a spatial polygon with a grid and check in which grid element specific coordinates are located

## read shapefile
library("rgdal")
shp <- readOGR("nybb_13a", "nybb")

proj4string(shp)  # units us-ft
# [1] "+proj=lcc +lat_1=40.66666666666666 +lat_2=41.03333333333333 
# +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000 +y_0=0 +datum=NAD83
# +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0"

### define coordinates and convert to SpatialPointsDataFrame
poi <- data.frame(x=c(919500, 959500, 1019500, 1049500, 1029500, 989500),
              y=c(130600, 150600, 180600, 198000, 248000, 218000),
              id="A", stringsAsFactors=F)
coordinates(poi) <- ~ x + y
proj4string(poi) <- proj4string(shp)

### define SpatialGrid object
bb <- bbox(shp)
cs <- c(3.28084, 3.28084)*6000  # cell size 6km x 6km (for illustration)
                            # 1 ft = 3.28084 m
cc <- bb[, 1] + (cs/2)  # cell offset
cd <- ceiling(diff(t(bb))/cs)  # number of cells per direction
grd <- GridTopology(cellcentre.offset=cc, cellsize=cs, cells.dim=cd)
grd
# cellcentre.offset 923018 129964
# cellsize           19685  19685
# cells.dim              8      8

sp_grd <- SpatialGridDataFrame(grd,
                           data=data.frame(id=1:prod(cd)),
                           proj4string=CRS(proj4string(shp)))

I have a SpatialGrid rather than SpatialGridDataFrame, so:

new_sp_grd <- SpatialGrid(grd,proj4string=CRS(proj4string(shp)))

I now want to calculate the centre coordinates of each grid cell, so that I can calculate the distance between this point and a variety of SpatialLines and SpatialPolygons objects.

Can anyone suggest how to do this? There is help for finding the centre of a polygon, but not a grid, and functions such as gCentroid from the rgeos package do not work on SpatialGrids.

  • wondering if this helps at all: stackoverflow.com/questions/37017854/… – hrbrmstr Jul 27 '16 at 14:38
  • AFAIK SpatialGrid contains a regular grid, so you can easily calculate each cell address by getting the value(X/Y) = bbox min X and Y + cellsize/2 offset. Then every cell in the grid will have the coordinates: X = valueX + cellsize*column and Y = valueY + cellsize*row – caiohamamura Jul 27 '16 at 15:11
  • Thanks for good informations. I have questions. The unit of cellsize in GridTopology is [ft]?? – CHOI Jul 8 '19 at 13:24
3

coordinates(new_sp_grd) returns the center coordinates of each cell. Here is a simple example:

library("sp")
grid <- SpatialGrid(GridTopology(c(0, 0), c(1, 1), c(5, 4)))

# get coordinates of center coordinates of each cell
grid_ctr <- coordinates(grid)

plot(grid, axes=TRUE)
points(grid_ctr, col="red", pch=16)

grid plot

0

Isn't this enough?

x <- 1:(cd[1,1])*cc['x']
y <- 1:(cd[1,2])*cc['y']

grid_cells_centre <- data.frame(x=rep(x, length(y)), y=rep(y, each=length(x)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.