[英]How to use function crop(x, y, …) from raster package so that the output is information about y (raster) contained in x (shapefile, extent object)
我是R的新手,特别是在R中使用GIS,所以我希望我能解释一下。
我想从包栅格中获取函数crop(x,y ...)到merge / overlay(不确定要使用的正确表达式)带有shapefile的光栅文件。 从本质上讲,shapefile是一个5 x 5 km的瑞典网格,而栅格是瑞典的土地利用光栅。 通过使用裁剪功能,我想得到一个结果,对于shapefile中的每个5x5平方公里,该结果提供从光栅文件中提取的关于每个方格中土地利用类型的信息。
但是,当我使用crop然后write.csv从我裁剪的'crop(landuse,ekrut)'(见下面的代码),我基本上只是将ekrut(shapefile)作为输出csv文件,没有添加任何列土地利用光栅指示哪个方块具有哪个土地使用等级。
我很抱歉,但我不能在这里分享实际的shapefile,如果它会以某种方式产生影响,可以从这里下载土地利用数据: http : //gpt.vic-metria.nu/data/land/NMD/NMD2018_basskikt_ogeneraliserad_Sverige_v1_0.zip
library (raster)
library (rgdal)
library (sp)
这是GIS文件的坐标系/投影。 每个坐标系都有一个epsg代码( http://spatialreference.org/ )。
sweref.def <- "+init=epsg:3006"
# read in shapefile
ekrut <- readOGR (dsn = "//storage-al.slu.se/student$/nilc0001/Desktop/Nina/Ekrut",
layer = "ekrut_5x5_flat",
p4s = sweref.def)
ekrut
# class : SpatialPolygonsDataFrame
# features : 19192
# extent : 266333, 921700, 6131565, 7675329 (xmin, xmax, ymin, ymax)
# coord. ref. : +init=epsg:3006 +proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
# variables : 7
# names : AREA, PERIMETER, GGD_, GGD_ID, BK, Bk_num, BK_flat
# min values : 2.5e+07, 20000, 2, 0, 10A 0f, 012 77, 10A0f
# max values : 2.5e+07, 20000, 19208, 19230, 9J 9d, 329 48, 9J9d
#read in raster
landuse <- raster ("nmd2018bas_ogeneraliserad_v1_0.tif")
landuse
# class : RasterLayer
# dimensions : 157992, 71273, 11260563816 (nrow, ncol, ncell)
# resolution : 10, 10 (x, y)
# extent : 208450, 921180, 6091140, 7671060 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
# data source : //storage- al.slu.se/student$/nilc0001/Desktop/Nina/landuse/nmd2018bas_ogeneraliserad_v1_0.tif
# names : nmd2018bas_ogeneraliserad_v1_0
# values : 0, 128 (min, max)
# attributes :
# ID COUNT Opacity Klass
# from: 0 5204803484 0
# to : 255 0 0
#first few rows of attribute table of landuse
levels (landuse)
# [[1]]
# ID COUNT Opacity Klass
# 1 0 5204803484 0
# 2 1 0 255
# 3 2 382320369 255 Öppen våtmark
# 4 3 258249590 255 Åkermark
# crop and write csv
landuse.ekrut <- crop (landuse, ekrut)
write.csv (landuse.ekrut,"landuse.ek.csv")
就像我说的,当我使用crop然后write.csv从我裁剪的东西我基本上只是得到ekrut(shapefile)作为输出csv文件,没有添加landuse栅格的列表明哪个方块有哪个土地使用类。
我想有一个.csv,对于每个广场(其中有19 191个),给我一些关于这个广场上存在什么类型的土地利用的信息。
如果有更好的方法来解决这个问题,请说明。 我希望我的解释清楚!
谢谢。
即使您的代码正常工作,仍然无法将此输出导出为CSV
除非您使用majority
过滤器之类的东西。 这基本上是因为一个多边形可能包含多个土地利用。
一种方法如下:
library(raster)
library(stats)
#set.seed to have a reproducible example
set.seed(1523)
#making a raster layer
r <- raster(ncol=36, nrow=18, vals=floor(runif(648, 5.0, 7.5)))
rr <- aggregate(r, fact=3)
f.net <- rasterToPolygons(rr) # transform into a polygon
#simple ovelaying plot for visualisation
plot(r, main="Raster and the overlaying poly")
plot(f.net, col=rgb(1, 0, 0, alpha = 0.3), add=TRUE)
ext.r <- extract(r, f.net) # returns values per ploy
# a simple mode filter
mode <- function(x){
ta = table(x)
tam = max(ta)
mod = as.numeric(names(ta)[ta == tam])
mod = mod[1]
return(mod)
}
# each poly contains several values
head(ext.r)
# [[1]]
# [1] 6 5 5 6 5 6 5 6 5
#
# [[2]]
# [1] 5 5 6 7 6 7 6 5 7
#
# [[3]]
# [1] 5 5 7 6 6 7 6 6 7
#
# [[4]]
# [1] 6 7 5 5 7 6 5 6 5
#
# [[5]]
# [1] 5 6 5 5 5 5 6 5 5
#
# [[6]]
# [1] 7 5 6 6 6 5 5 6 5
# a mode filter can be applied to pas majority values to each poly
mode.ext.r <- lapply(ext.r, mode)
head(mode.ext.r)
# [[1]]
# [1] 5
#
# [[2]]
# [1] 5
#
# [[3]]
# [1] 6
#
# [[4]]
# [1] 5
#
# [[5]]
# [1] 5
#
# [[6]]
# [1] 5
write.table(mode.ext.r, file = 'mode_ext_r.csv', row.names = FALSE, na = '')
您可以提取每个多边形的所有类,为每个多边形创建一个表,然后将它们组合在一起。
建立Majid的示例数据。
library(raster)
set.seed(1523)
r <- raster(ncol=9, nrow=6, vals=sample(10, 9*6, replace=TRUE))
rr <- aggregate(raster(r), fact=3)
pols <- rasterToPolygons(rr) # transform into a polygon
plot(r); lines(pols); text(pols, 1:length(pols))
提取,创建表和组合
e <- extract(r, pols)
x <- lapply(e, table)
y <- lapply(1:length(x), function(i) data.frame(pol=i, as.data.frame(x[[i]])))
z <- do.call(rbind, y)
head(z, 10)
# pol Var1 Freq
#1 1 1 2
#2 1 2 2
#3 1 3 2
#4 1 6 2
#5 1 9 1
#6 2 1 1
#7 2 2 1
#8 2 4 2
#9 2 6 2
#10 2 7 1
并保存到csv
write.csv(z, "test.csv", row.names=FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.