I'm using the function gdal_rasterize
in R (package gdalUtils
) to convert a polygon shapefile into a raster. Here is my code:
r.test <- gdal_rasterize(src_datasource="df.shp", dst_filename="test.tif", a="class_raster", output_Raster=TRUE))
"class_raster"
are numeric values which should be transferred in the raster.
But I have this error message:
Error in .local(.Object, ...) :
`C:\Users\Documents\test.tif' does not exist in the file system,
and is not recognised as a supported dataset name.
In addition: Warning message:
running command '"C:\OSGeo4W64\bin\gdal_rasterize.exe" -a "class_raster" "df.shp" "test.tif"' had status 1
Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) :
Cannot create a RasterLayer object from this file. (file does not exist)
Here is a description of the shapefile:
> df
class : SpatialPolygonsDataFrame
features : 82077
extent : 265380.5, 462477.9, 4984446, 5081957 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0
variables : 1
names : class_raster
min values : 1
max values : 8
Update:
I created a file .tif before to call the function gdal_rasterize
as follows:
r.test <- raster(extent(df), resolution = 30)
projection(r.test) <- CRS("+proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")
> r.test
class : RasterLayer
dimensions : 3250, 6570, 21352500 (nrow, ncol, ncell)
resolution : 30, 30 (x, y)
extent : 265380.5, 462480.5, 4984457, 5081957 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0
> test <- writeRaster(r.test, filename="test.tif", format="GTiff", overwrite=TRUE)
Warning message:
In .local(x, filename, ...) : all cell values are NA
> gdal_rasterize(src_datasource="df.shp", dst_filename="test.tif", a="class_raster", output_Raster=TRUE)
class : RasterBrick
dimensions : 3250, 6570, 21352500, 1 (nrow, ncol, ncell, nlayers)
resolution : 30, 30 (x, y)
extent : 265380.5, 462480.5, 4984457, 5081957 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
data source : H:\test.tif
names : test
But I still have the message:
Warning message:
running command '"C:\OSGeo4W64\bin\gdal_rasterize.exe" -a "class_raster" "df" "test.tif"' had status 1
Here is a description of the raster with the function gdalinfo
> gdalinfo("H:/test.tif")
[1] "Driver: GTiff/GeoTIFF" "Files: H:/test.tif"
[3] "Size is 6570, 3250" "Coordinate System is:"
[5] "PROJCS[\"unnamed\"," " GEOGCS[\"NAD83\","
[7] " DATUM[\"North_American_Datum_1983\"," " SPHEROID[\"GRS 1980\",6378137,298.2572221010002,"
[9] " AUTHORITY[\"EPSG\",\"7019\"]]," " TOWGS84[0,0,0,0,0,0,0],"
[11] " AUTHORITY[\"EPSG\",\"6269\"]]," " PRIMEM[\"Greenwich\",0],"
[13] " UNIT[\"degree\",0.0174532925199433]," " AUTHORITY[\"EPSG\",\"4269\"]],"
[15] " PROJECTION[\"Transverse_Mercator\"]," " PARAMETER[\"latitude_of_origin\",0],"
[17] " PARAMETER[\"central_meridian\",-73.5]," " PARAMETER[\"scale_factor\",0.9999],"
[19] " PARAMETER[\"false_easting\",304800]," " PARAMETER[\"false_northing\",0],"
[21] " UNIT[\"metre\",1," " AUTHORITY[\"EPSG\",\"9001\"]]]"
[23] "Origin = (265380.523957236320000,5081957.489292523800000)" "Pixel Size = (30.000000000000000,-30.000000000000000)"
[25] "Metadata:" " AREA_OR_POINT=Area"
[27] "Image Structure Metadata:" " COMPRESSION=LZW"
[29] " INTERLEAVE=BAND" "Corner Coordinates:"
[31] "Upper Left ( 265380.524, 5081957.489) ( 74d 0'28.08\"W, 45d52'34.80\"N)" "Lower Left ( 265380.524, 4984457.489) ( 73d59'59.97\"W, 44d59'56.44\"N)"
[33] "Upper Right ( 462480.524, 5081957.489) ( 71d28' 9.73\"W, 45d51'33.89\"N)" "Lower Right ( 462480.524, 4984457.489) ( 71d30' 2.06\"W, 44d58'57.36\"N)"
[35] "Center ( 363930.524, 5033207.489) ( 72d44'39.21\"W, 45d26'10.68\"N)" "Band 1 Block=6570x1 Type=Float64, ColorInterp=Gray"
[37] " Min=1.#IO Max=-1.#IO " " Minimum=1.#IO, Maximum=-1.#IO, Mean=1.000, StdDev=1.000"
[39] " NoData Value=-1.6999999999999999e+308" " Metadata:"
[41] " STATISTICS_MAXIMUM=-1.#INF" " STATISTICS_MEAN=1.#SNAN"
[43] " STATISTICS_MINIMUM=1.#INF" " STATISTICS_STDDEV=1.#SNAN"
I have the version 1.0-7 of rgdal
.