[R-sig-Geo] Checking whether a file is a "good" raster
Roger Bivand
Roger.Bivand at nhh.no
Thu Sep 8 22:36:54 CEST 2011
On Thu, 8 Sep 2011, Jonathan Greenberg wrote:
> R-sig-geo'ers:
>
> I'm trying to write a raster catalog function, and one thing I'm
> running into is how to properly check if a file is a legitimate raster
> format -- I'm defining "legit" as GDALinfo recognizing it as a
> supported format, e.g. does not return:
>
> Error in .local(.Object, ...) :
> `/pathto/myfile' not recognised as a supported file format.
>
> I'm just looking for a TRUE/FALSE situation, at present (TRUE if
> GDALinfo returns a valid response, FALSE if it returns the error
> above). Thoughts?
> library(rgdal)
> o <- try(GDALinfo(system.file("external/test.ag", package="sp")[1]),
+ silent=TRUE)
> class(o)
[1] "GDALobj"
> o <- try(GDALinfo(system.file("vectors/ps_cant_31.MIF", package =
+ "rgdal")[1]), silent=TRUE)
> class(o)
[1] "try-error"
So wrap in try() and test that the output class is not "try-error". Also
see ?tryCatch.
Roger
>
> --j
>
>
>
--
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list