Crop Raster Images in R

E020N40The maptools package has a pruneMap() function t0 crop map objects in R.  In practice, the function extracts data from SpatialPolygon or SpatialLine objects given a boundary box or specific area of interest.  Unfortunately, there is no equivalent function for high resolution, large data, raster images, which are common in many Earth Science applications.  The following post defines a custom function to crop raster images in R and to extract data from SpatialGridDataFrames.  The function is tested using a raster image from the Shuttle Radar Topography Mission (SRTM; shown at left).  The resulting data is then mapped using the image() function in R.

Function Data, Use and Definition

Digitial elevation data from the SRTM is packaged by the U.S. Geological Survey as GOTOPO30 data.  The pruneDEM() function is used to ingest GOTOPO30 data into R given a path and file name.  The function then extracts data using a boundary box defined by min/max coordinates.  The result is a SpatialGridDataFrame. that can be used as a land surface definition for GIS mapping, engineering design, or spatial weather modeling.

Working with Raster Images in R

The following example ingests a large tile of digital elevation data, which can be downloaded below.  pruneDEM() will ingest the raster file and extract data given a boundary box.  Function output is then plotted with custom colors and axes legends.

[sdm-download id=”3769″ fancy=”0″]


This entry was posted in GDAL, GIS, R Data Import, R Graphics, Spatial Analysis. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

one + 13 =