Inspired by this GIS exchange question.
Requires function shapewriter.
Creates polygon fishnet based on input shapefile. Rows and columns counts have to be specified. Length and height of each zone is calculated within the function in meter using “x/y shapefile extent” / “given number of rows and cols”.

fishnet <- function(boundingbox, NumberofZonesX, NumberofZonesY, outname=NULL) {
  
  require(raster)
  require(sp)
  require(Grid2Polygons)
  
  # calculate extends
  ext = raster::extent(boundingbox)
  bb <- sp::bbox(boundingbox)
  # calculates cell size in x and y (in m) based on chosen 'Number of Zones'
  cs <- c(floor((ext@xmax-ext@xmin)/NumberofZonesX),
          floor((ext@ymax-ext@ymin)/NumberofZonesY))  
  # calculates cell offsets
  cc <- bb[, 1] + (cs/2)
  # sets cells per direction for Grid creation
  cd <- c(NumberofZonesX, NumberofZonesY)  
  # creates spatialGrid
  grd <- sp::GridTopology(cellcentre.offset=cc, cellsize=cs, cells.dim=cd)
  
  # converts into SpGridDaFrame
  sp_grd <- sp::SpatialGridDataFrame(grd,
                                     data=data.frame(id=1:prod(cd)),
                                     proj4string=CRS(proj4string(d)))
  # converting to Polygon
  sp_grd = Grid2Polygons::Grid2Polygons(sp_grd)
  
  # return dataframe and write shapefile
  if (!is.null(outname)) {
    shapewriter(sp_grd,outname)
  }
  return(sp_grd)
}
data = shapereader("shape.shp")

# calculate fishnet with 7 rows and 7 columns
fishnet(data, NumberofZonesX = 7, NumberofZonesY = 7, "Desktop/Fishnet7x7.shp")

Green: 10x10; Red: 7x7
auswahl_002

Patrick Schratz

Patrick Schratz

PhD Student at University of Jena

I have experience in working with all kind of image analysis, GIS software and programming languages such as R and Python. Currently I use statistical and machine learning methods to improve the understanding of forest decline within tree plantations in northern Spain.