Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active May 15, 2017 11:13
Show Gist options
  • Save mdsumner/19b36c829ec1cbedabff1bf619b4ec18 to your computer and use it in GitHub Desktop.
Save mdsumner/19b36c829ec1cbedabff1bf619b4ec18 to your computer and use it in GitHub Desktop.
## the projected extent
prj <- "+proj=sinu +a=6371007.181 +b=6371007.181"
ex <- extent(rgdal::project(cbind(c(-180, -180, 180, 180), c(90, 0, 0, -90)), prj))
#the MODIS tiles
mtiles <- raster(ex, ncols = 35, nrows = 17, crs = prj)
## for real use you wouldn't actually populate the grid here
## raster's cell logic can be used without ever filling the data
mtiles[] <- seq(ncell(mtiles))
#plot(setValues(mtiles, seq(ncell(mtiles))))
library(sf)
pmtiles <- st_as_sf(as(mtiles, "SpatialPolygonsDataFrame"))
pmtiles$row <- rowFromCell(mtiles, seq(ncell(mtiles)))
pmtiles$col <- colFromCell(mtiles, seq(ncell(mtiles)))
plot(pmtiles[c("row", "col")], asp = "")
plot(st_geometry(pmtiles), asp = "")
data("wrld_simpl", package = "maptools")
plot(spTransform(wrld_simpl, prj), add = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment