Reputation: 265
Whenever I rasterize my SpatialPolygonsDataFrame, I lose the attribute / data information part. The command "rasterize" is from the package "raster" in R.
I have the following RasterLayer (named "raster1")
class : RasterLayer
dimensions : 6000, 4800, 28800000 (nrow, ncol, ncell)
resolution : 0.00025, 0.00015 (x, y)
extent : 8.699875, 9.899875, 46.69993, 47.59993 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:3857 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs
My SpatialPolygonsDataFrame (named "bw1") has the following properties
class : SpatialPolygonsDataFrame
features : 7663
extent : 980075.6, 1076577, 5908811, 6023151 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:3857 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs
variables : 2
names : A, B
min values : 3231, 11
max values : 3955, 19
When I use a the command
bw1_raster<-rasterize(bw1,raster1,fun='last',field=c("A","B")
I get the new object "bw1_raster":
class : RasterLayer
dimensions : 6000, 4800, 28800000 (nrow, ncol, ncell)
resolution : 0.00025, 0.00015 (x, y)
extent : 8.699875, 9.899875, 46.69993, 47.59993 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:3857 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0
+lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs
data source : in memory
names : layer
values : NA, NA (min, max)
How do I get a raster object with attributes / layers "A" and "B"?
Upvotes: 0
Views: 1098
Reputation: 47051
The problem is that the coordinate reference system of the SpatialPolygons do not match that of the RasterLayer. It looks like they match, but that is probably because you changed the crs of the RasterLayer to an incorrect value. The RasterLayer almost certainly has a longlat crs, not merc.
You need to assign it the correct crs (or at least do not change it to a wrong one!).
Depending on what you want to achieve, you could use spTransform to transfrom the SpatialPolygons to the crs of the RasterLayer and try again.
Alternatively, you could do something along these lines
library(raster)
r <- raster(bw1, res=10000)
r <- rasterize(bw1, r)
Upvotes: 1