R: Read/write between GDAL grid maps and Spatial objects
readGDAL
R Documentation
Read/write between GDAL grid maps and Spatial objects
Description
The functions read or write GDAL grid maps. They will set
the spatial reference system if available. GDALinfo reports the
size and other parameters of the dataset. create2GDAL creates
a GDAL data set from a SpatialGridDataFrame object, in particular to
be able to save to GDAL driver formats that only permit copying rather
than creation.
file name of grid map; in create2GDAL provides a way
to pass through a file name with driver-required extension for sensitive
drivers
x
A GDALReadOnlyDataset object
offset
Number of rows and columns from the origin (usually the upper
left corner) to begin reading from; presently ordered (y,x) - this may change
region.dim
The number of rows and columns to read from the dataset;
presently ordered (y,x) - this may change
output.dim
The number of rows and columns to return in the created
object using GDAL's method to take care of image decimation / replication;
presently ordered (y,x) - this may change
band
if missing, all bands are read
p4s
PROJ4 string defining CRS, if default (NULL), the value is read
from the GDAL data set
half.cell
Used to adjust the intra-cell offset from corner to centre,
usually as default, but may be set to c=(0,0) if needed; presently ordered (y,x) - this may change
silent
logical; if TRUE, comment and non-fatal CPL driver errors
suppressed
OVERRIDE_PROJ_DATUM_WITH_TOWGS84
logical value, default NULL, which case the cached option set by set_OVERRIDE_PROJ_DATUM_WITH_TOWGS84 is used. Ignored if the GDAL version is less than “1.8.0” or if the CPLConfigOption variable is already set; see getProjectionRef for further details
...
arguments passed to either getRasterData, or
getRasterTable, depending on rotation angles (see below);
see the rgdal documentation for the available options (subsetting
etc.)
dataset
object of class SpatialGridDataFrame-class or
SpatialPixelsDataFrame-class
drivername, copy_drivername
GDAL driver name; if the chosen driver
does not support dataset creation, an attempt is made to use the
copy_drivername driver to create a dataset, and copyDatset
to copy to the target driver
type
GDAL write data type, one of: ‘Byte’, ‘Int16’,
‘Int32’, ‘Float32’, ‘Float64’; ‘UInt16’,
‘UInt32’ are available but have not been tests
mvFlag
default NA, missing value flag for output file; the default
value works for ‘Int32’, ‘Float32’, ‘Float64’, but
suitable in-range value that fits the data type should be used for other
data types, for example 255 for ‘Byte’, -32768 for ‘Int16’,
and so on; see Details below.
options
driver-specific options to be passed to the GDAL driver;
see Details below
setStatistics
default FALSE, if TRUE, attempt to set per-band
statistics in the output file (driver-dependent)
colorTables
default NULL, if not NULL, a list of length equal to the number of bands, with NULL components for bands with no color table, or either an integer matrix of red, green, blue and alpha values (0-255), or a character vector of colours. The number of colours permitted may vary with driver.
catNames
default NULL, if not NULL, a list of length equal to the number of bands, with NULL components for bands with no category names, or a string vector of category names
returnRAT
default FALSE, if TRUE, return a list with a Raster
Attribute Table or NULL for each band
returnCategoryNames
default FALSE, if TRUE, return a list with a character vector of CategoryNames or NULL for each band
returnStats
default TRUE, return band-wise statistics if avaliable (from 0.7-20 set to NA if not available)
returnColorTable
default FALSE; if TRUE return band-wise colour tables in a list attribute “ColorTables”
returnScaleOffset
default TRUE, return a matrix of bandwise scales and offsets
Details
In writeGDAL, if types other than ‘Int32’,
‘Float32’, ‘Float64’ are used, the “mvFlag”
argument should be used to set a no data value other than the default
NA. Note that the flag only replaces NA values in the data being exported
with the value of the argument - it does not mark data values equal to
“mvFlag” as missing. The value is stored in the file being written
in driver-specific ways, and may be used when the file is read. When the
default “mvFlag=NA” is used, no NoDataValue is written to the file,
and the input data is written as is.
Also in writeGDAL, the “options” argument may be
used to pass a character vector of one or more options to the
driver, for example ‘options=“INTERLEAVE=PIXEL”’,
or ‘options=c(“INTERLEAVE=PIXEL”,
“COMPRESS=DEFLATE”)’. Typical cases are given in the examples
below; it may also be necessary in some cases to escape quotation markes
if included in the string passed to the driver.
Value
read.GDAL returns the data in the file as a Spatial object.
Usually, GDAL maps will be north-south oriented, in which case the rgdal
function getRasterData is used to read the data, and an object
of class SpatialGridDataFrame-class is returned.
Some map formats supported by GDAL are not north-south oriented grids. If
this is the case, readGDAL returns the data as a set of point
data, being of class SpatialPointsDataFrame-class. If the points
are on a 45 or 90 degree rotated grid, you can try to enforce gridding
later on by e.g. using gridded-methods(x)=TRUE.
Warning
Some raster files may have an erroneous positive
y-axis resolution step, leading to the data being flipped on the
y-axis. readGDAL will issue a warning: Y axis resolution
positive, examine data for flipping, when the step is positive, but
this need not mean that the data are flipped. Examine a display of the
data compared with your knowledge of the file to determine whether this
is the case (one known case is interpolation files created under Qgis
up to February 2010 at least). To retreive the correct orientation,
use flipVertical.
Note
Non-fatal CPL errors may be displayed for some drivers, currently
for the AIG ArcInfo 9.3 binary raster driver using GDAL >= 1.6.2; the data
has been read correctly, but the contents of the info directory did not
meet the specifications used to reverse engineer the driver used in GDAL
(see http://trac.osgeo.org/gdal/ticket/3031)