[R-sig-Geo] Can I read only a portion of a raster? (With more code)

Ben Harrison harb at student.unimelb.edu.au
Thu Sep 26 08:35:13 CEST 2013


I thought I could use the extent of a raster to trim it, but it ignores 
the option. Is there another way to do it?

library(raster)

r <- raster(system.file("external/test.grd", package="raster"))
print(r)

# =-=-=-=- Results:
class       : RasterLayer
dimensions  : 115, 80, 9200  (nrow, ncol, ncell)
resolution  : 40, 40  (x, y)
extent      : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:28992 
+towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 
+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 
+k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs
data source : 
/home/blah/R/x86_64-pc-linux-gnu-library/3.0/raster/external/test.grd
names       : test
values      : 128.434, 1805.78  (min, max)
# =-=-=-=-

plot(r)

my.ext <- extent(179000, 181000, 330000, 333000)
# note: extent in relation to r's extent is smaller.
q <- raster(system.file("external/test.grd", package="raster"),
	    ext=my.ext)
print(q)

# =-=-=-=- Results:
class       : RasterLayer
dimensions  : 115, 80, 9200  (nrow, ncol, ncell)
resolution  : 40, 40  (x, y)
extent      : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:28992 
+towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 
+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 
+k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs
data source : 
/home/blah/R/x86_64-pc-linux-gnu-library/3.0/raster/external/test.grd
names       : test
values      : 128.434, 1805.78  (min, max)
# =-=-=-=-

plot(q)
all.equal(target=r, current=q)
[1] TRUE

It reads in the entire file. How can I read in only the portion I need?

Ben.



More information about the R-sig-Geo mailing list