I have met a small yet puzzling problem about saving and reading Spatial Pixel Data Frame.
I created a raster with over 10 million grids (resolution = 25000).
adm0 <- readOGR(dsn = path.expand("C:/Users/Abel/Desktop/district"), layer = "administrative0") proj.crs <- proj4string(CRS("+proj=utm +zone=33 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")) adm0.newproj <- spTransform(adm0, proj.crs) grid_2.5km <- raster(adm0.newproj, res = 2500) # 13143792 grids
In order to add data into the raster, I further transformed this raster into
SpatialPixelsDataFrame (I did not use
SpatialPolygons because my computer is incapable of dealing with vector of this size).
grid_2.5km<- 1:ncell(grid_2.5km) grid_2.5km.sp <- as(grid_2.5km, "SpatialPolygonsDataFrame")
At first I used
writeOGR to save this grid file and
readOGR to read it. But turns out by doing so all grids would be transformed to
SpatialPoint, which is definitely not what I want.
I also tried
writeGDAL. But I have no idea about the set the
type and many other parameters.
writeGDAL(grid_2.5km.sp, "C:/Users/Abel/Desktop/divide/grid_2.5km.tif", drivername = "Gtiff", type = "Float32" )
How can I save this
SpatialPixelsDataFrame correctly and keep its original object, and how should I read this file, letting the opened file still having the object as