简体   繁体   English

R-栅格化SpatialPolygonsDataFrame-删除变量/属性

[英]R - Rasterize a SpatialPolygonsDataFrame - variables/attributes dropped

Whenever I rasterize my SpatialPolygonsDataFrame, I lose the attribute / data information part. 每当我栅格化SpatialPolygonsDataFrame时,都会丢失属性/数据信息部分。 The command "rasterize" is from the package "raster" in R. 命令“ rasterize”来自R中的软件包“ raster”。

I have the following RasterLayer (named "raster1") 我有以下RasterLayer(名为“ 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 我的SpatialPolygonsDataFrame(名为“ bw1”)具有以下属性

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": 我得到了新对象“ 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"? 如何获得具有属性/图层“ A”和“ B”的栅格对象?

The problem is that the coordinate reference system of the SpatialPolygons do not match that of the RasterLayer. 问题在于SpatialPolygon的坐标参考系统与RasterLayer的坐标参考系统不匹配。 It looks like they match, but that is probably because you changed the crs of the RasterLayer to an incorrect value. 看起来它们匹配,但这可能是因为您将RasterLayer的crs更改为错误的值。 The RasterLayer almost certainly has a longlat crs, not merc. RasterLayer几乎可以肯定有一个longlat crs,而不是merc。

You need to assign it the correct crs (or at least do not change it to a wrong one!). 您需要为其分配正确的crs(或至少不要将其更改为错误的crs!)。

Depending on what you want to achieve, you could use spTransform to transfrom the SpatialPolygons to the crs of the RasterLayer and try again. 根据您要实现的目标,可以使用spTransform将SpatialPolygons转换为RasterLayer的crs,然后重试。

Alternatively, you could do something along these lines 或者,您可以按照以下方式进行操作

library(raster)
r <- raster(bw1, res=10000)
r <- rasterize(bw1, r)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM