简体   繁体   English

栅格化R中的空间多边形,得到具有NA值的栅格

[英]Rasterize spatialpolygons in R giving raster with NA values

I am having issues converting this spatialpolygondataframe to a raster. 我在将此空间多边形数据帧转换为栅格时遇到问题。 When I do ther conversion, the raster has NA as its values. 当我进行转换时,栅格将NA作为其值。 As shown below: 如下所示:

 DL3
[1]
class       : SpatialPolygonsDataFrame 
features    : 126 
extent      : -15.04001, 46.1036, 3.759985, 31.71804  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +towgs84=0,0,0 +ellps=WGS84 
variables   : 1
names       :  LFRP 
min values  :    14 
max values  : 335.2 

This is how I rasterize it: 这是我光栅化的方式:

##TO CONVERT TO RASTER
FunR<-function(r){
ext<-raster(extent(r))
crs(ext)<-crs(r)
D<-rasterize(r,ext,field=1,update=T)
D}

DL4<-lapply(DL3,FunR)
DL4
[1]
class       : RasterLayer 
dimensions  : 45, 40, 1800  (nrow, ncol, ncell)
resolution  : 1.52859, 0.6212901  (x, y)
extent      : -15.04001, 46.1036, 3.759985, 31.71804  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +towgs84=0,0,0 +ellps=WGS84 
data source : in memory
names       : layer 
values      : NA, NA  (min, max)

What can I be doing wrongly? 我做错了什么? I need help with a method to ensure the values in the dataframe reflect in the raster, please. 我需要一种方法的帮助,以确保数据框中的值反映在栅格中。

The principle works, as illustrated below. 该原理起作用,如下所示。

library(raster)
SPP <- shapefile(system.file("external/lux.shp", package="raster"))
r <- raster(SPP, ncol=40, nrow=45)    
SPP2 <- rasterize(SPP, r, "ID_1")

As this does not work for you, I assume that your polygons are very small relative to the raster cell size such that none of the cells is covered. 由于这对您不起作用,因此我认为您的多边形相对于栅格像元大小非常小,因此没有像元被覆盖。 Can you try with much smaller grid cells? 您可以尝试使用更小的网格单元吗? If indeed the polygons are that small, it might make more sense to use their centroids ( coordinates(SPP) ) and rasterize these (as points). 如果确实如此小的多边形,则使用其质心( coordinates(SPP) )并将其栅格化(作为点)可能更有意义。

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

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