3

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 filename, drivername, 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 SpatialPixelsDataFrame?

1

You can save R objects in R's own format using saveRDS, and load them back using readRDS. For example:

> z = runif(10)
> z
 [1] 0.93781501 0.04437402 0.04995689 0.01317236 0.83926320 0.72379189
 [7] 0.07266764 0.12712897 0.55884653 0.26702832

that's some random numbers - save them in a file:

> saveRDS(z, file="/tmp/zrandom.rds")

now remove the object and read the file back into another object:

> rm(z)
> z2 = readRDS(file="/tmp/zrandom.rds")
> z2
 [1] 0.93781501 0.04437402 0.04995689 0.01317236 0.83926320 0.72379189
 [7] 0.07266764 0.12712897 0.55884653 0.26702832

and I've got the same numbers back.

There are some cases where this doesn't work because the R object is storing references to other data that isn't saved, but I think SpatialPixelsDataFrames should work correctly.

The downside is that no other software, apart from R, can read these things.

You may wish to consider why you are using SpatialPixelsDataFrames and why you might want to use rasters, but this answers your question.

4
  • Thank you so very much. It worked great on my computer. What's even better is that the speed of reading data is so much faster than it used to be (in the past, it would take FOREVER to read a huge .shp or .tif file). You have no idea how much I have suffered from this problem. Besides, the reason I use raster of SpatialPixel is that many data are only saved in raster form, and in my study, I have to treat each pixel as single observation. I just have to thank you again. I apologize for my constant thank you but I am so happy and so overwhelmed right now. Wish you a good day! – abelxu73 Jan 16 at 2:43
  • Sorry again. The problem occurs again. If I save and read a huge SpatialPixelsDataFrame (1.4GB) as RDS consecutively, I can still get the original pixels dataframe. However, if I quit R, open it, and read the same .rds file again, I can only get a SpatialPointsDataFrame again. Is this the curse of big file? – abelxu73 Jan 16 at 4:07
  • I can save/read a small spatial pixels data frame and get it back as a spatial pixels data frame even after restarting R. Check that works for you (with a small SPxDF) and if it still fails with your large data write this up as a new question. – Spacedman Jan 16 at 11:21
  • I realized the format changed because I used spCbind to combine data, which would inevitably ctransform spatial pixel data frame into spaticl point data frame. I used cbind instaed and it worked perfectly well. Thank you again for your suggestion! – abelxu73 Jan 16 at 12:18

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.