3

I use a shapefile to create a regular grid using spsample, from sp package.

Two problems here: (1) I would like to define the size of my grid cells in meters, but I've checked the documentation of sp and it is not clear what is the default unit of measure ; (2) This code works to create hexagonal grid but I can't make this work to create a regular grid. Any ideas?

Here is a reproducible example:

# load libraries    
  library(sp)
  library(rgdal)

Download a shapefile

URL <- "ftp://dnrftp.dnr.ne.gov/pub/data/state/Legislative2010UTM.zip"
fil <- basename(URL)
if (!file.exists(fil)) download.file(URL, fil)

# unzip & get list of files
fils <- unzip(fil)

# find the shapefile in it
shp <- grep("shp$", fils, value=TRUE)

# get the first layer from it
lay <- ogrListLayers(shp)[1]

# read in the shapefile
leg <- readOGR(shp, lay)

plot(leg)

enter image description here

Define size of hexagon bins

HexPts <-spsample(leg,type="hexagonal", offset=c(0,0), cellsize=100000)

# Create hexagon grid as spatial polygons
HexPols <- HexPoints2SpatialPolygons(HexPts)


plot(HexPols, add=T)

enter image description here

4

Use spsample with cellsize:

pts = spsample(leg, cellsize=c(10000,10000), type="regular")
plot(leg)
points(pts)

check the coordinate spacing:

> coordinates(pts)[1:10,]
       x1      x2
17 244356 4437161
18 254356 4437161
19 264356 4437161

you see the x coordinate going by 10,000 (and if you look later on you'll see the y coordinate does too)

  • Thanks Barry. Do you what is the default unit of measure for the cell size? and is it possible to define the cell size in meters? – rafa.pereira Sep 24 '15 at 9:57
  • 2
    Its whatever the units of the map projection of your leg object is. And in this case, metres: proj4string(leg) gives: "+proj=utm +zone=14 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0""+proj=utm +zone=14 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0" - see "+units=m" there? – Spacedman Sep 24 '15 at 10:07

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.