简体   繁体   English

栅格化随机空间点不起作用

[英]Rasterize random spatial points doesn't work

I'm new with R and I need to make a Raster based on a large database of random points. 我是R的新手,我需要根据大型随机点数据库制作一个Raster。 You can see the distribution of them on the southern half of Belgium. 您可以看到它们在比利时南半部的分布。 point distribution 点分布

在此处输入图片说明

My needs are : 我的需求是:

-The raster has to match the extend of the country. -栅格必须与国家/地区相符。

-I need to be able to play on the size of the pixels rather than on the number of rows and columns. -我需要能够播放像素的大小 ,而不是行和列的数量。

-At first, the pixel value should be the mean value of all points falling inside. -首先,像素值应为落入内部的所有点的平均值。 Then I also want other basic statistics like median, standard deviation, etc. 然后,我还需要其他基本统计信息,例如中位数,标准差等。

I followed what I saw on the net about "rasterize" command but I always have the same error message. 我遵循在网上看到的有关“栅格化”命令的内容,但始终有相同的错误消息。

Here is my code : 这是我的代码:

#create function for raster calc 
fun_ras<-function(x){c(length(x),mean(c))}

# set wd and loading packages
library(raster)
library(rgdal)
library(maptools)

# reading database
BDD <- read.csv2("~/R/scriptsR/BDD_derniere_version.csv")

# charge shapefile on witch the extend will be defined
EX <- readShapeSpatial(fn = "Reg_Agric_RW", proj4string = CRS("+init=epsg:31370"))
# Make the database a spatialpointsdataframe 
coordinates(BDD)<- ~ coord_x+coord_y
proj4string(BDD)<- CRS("+init=epsg:31370") 

# plot (result is the map above) 
plot(EX, axes = TRUE, las=2)
plot(BDD,add=TRUE,col='red')

# Create an empty grid of required extend and pixel size 
grille<-extend(raster(),EX,value=NA)
projection(grille)<-CRS("+init=epsg:31370")
res(grille) = sqrt(3)

# Assign values to raster
RAST<-rasterize(BDD,grille,BDD$phkcl,fun = fun_ras)
plot(RAST)

And the error message I get : 和我得到的错误信息:

Warning message: In is.na(field) : is.na() applied to non-(list or vector) of type 'S4' Error in addAttrToGeom(geometry(dots[ 1 ]), data.frame(d), FALSE) : erreur d'évaluation de l'argument 'x' lors de la sélection d'une méthode pour la fonction 'addAttrToGeom' : Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'geometry' for signature '"matrix"' 警告消息:在is.na(field)中:is.na()应用于类型为“ S4”的非(列表或矢量)addAttrToGeom(geometry(dots [dots [ 1 ])),data.frame(d),FALSE中的错误):参数'x'的错误评估'addAttrToGeom'的功能选择'addAttrToGeom':(函数(类,fdef,mtable)的错误:无法为函数'geometry找到继承的方法'用于签名'“ matrix”'

Can you help me with that ? 你能帮我吗?

Just below you can find a randomly created DB to try the code. 在下面,您可以找到一个随机创建的数据库来尝试代码。 Thanks for reading me. 感谢您的阅读。

Object_ID;coord_x;coord_y;phkcl
1;117300;132300;8,820924815
2;111966;124063;9,956132806
3;112093;124117;7,200693488
4;112093;124117;4,681636614
5;78600;149400;5,082282047
6;79000;149500;1,68962794
7;139886;150047;2,330266083
8;145905;141454;9,173647156
9;97078;135221;2,195583634
10;97110;135268;6,111852348
11;101268;133203;1,103343387
12;101593;133083;3,255404464
13;102186;133388;0,558961603
14;102286;133652;7,339728115
15;103085;134037;3,694454146
16;103604;134163;4,825037287
17;106300;134800;5,314536385
18;106400;133850;5,198687281
19;106400;132450;5,176775115
20;106700;135100;9,451347469
21;106854;134476;2,18238857
22;106934;134262;1,441814562
23;107072;134397;3,294001808
24;107100;134900;0,326928888
25;107160;132169;6,442982335
26;107500;133950;0,204386523
27;107900;132550;4,44092199
28;108450;130650;1,834331129
29;108700;131900;6,215733241
30;108700;131900;9,216165015
31;139159;146432;1,347052569
32;110100;133400;8,503910347
33;118119;134708;1,463121228
34;118393;134797;9,544489997
35;141186;145434;8,848701457
36;141303;148274;3,867873941
37;140790;149107;4,859392138
38;142174;147614;5,391746188
39;142308;152940;7,818416216
40;141834;147294;3,996244438
41;142449;143127;2,65462876
42;145099;146806;2,500204413
43;145112;147182;6,731960216
44;145379;149868;2,064895872
45;143811;149209;3,574042593
46;143841;147402;1,307497542
47;143942;149818;9,359814992
48;143944;149839;7,800701068
49;143962;147282;6,806606928
50;143969;148612;6,046161109
51;143980;147246;5,976350979
52;144041;148569;4,404409454
53;144668;148739;9,141859663
54;146014;145129;4,584515956
55;146018;145128;8,439605309
56;146142;145505;4,2956544
57;146221;145462;8,633782378
58;146263;150271;5,958452708
59;146349;145168;4,71636601
60;146352;145339;0,220649424
61;146392;145645;4,155722048
62;146542;153460;6,250098446
63;146545;146311;5,811902205
64;146611;146226;7,309251549
65;147100;151842;7,258990339
66;147113;152663;9,107590126
67;147198;151887;2,32100404
68;149290;150703;8,560092216
69;149344;150770;9,938811209
70;149344;150770;5,787478429
71;148990;122971;2,583192828
72;89864;158673;5,299541772
73;79465;148922;6,038002095
74;148388;123963;3,192033439
75;148432;124468;8,085579794
76;148536;123631;2,027891366
77;148648;123694;5,397160821
78;51326;166144;1,072041674
79;51424;166163;7,190299142
80;94300;131850;1,520360916
81;106100;133600;7,43384938
82;106350;135450;2,710186103
83;106400;132600;9,698695261
84;106500;134100;3,931355396
85;106800;134650;4,687253969
86;107050;134000;5,724228038
87;107312;133228;5,973770237
88;107767;127098;4,408539793
89;112905;128428;9,884175679
90;116250;129000;1,405425006
91;105850;134150;0,359823103
92;105900;133300;9,692940279
93;118143;135279;1,268076954
94;149300;103700;6,738560056
95;52188;163000;3,667692753
96;76057;139944;9,787384332
97;76282;150985;3,825164433
98;76300;152200;3,029343061
99;76366;151495;8,911580441
100;76406;151244;4,929952604

Your function is not correct. 您的功能不正确。

fun_ras<-function(x){c(length(x),mean(c))}

mean(c) should be mean(x) mean(c)应为mean(x)

fun_ras<-function(x, ...){ c(length(x), mean(x)) }

With that, try 这样,尝试

grille <- raster(EX, res=10000) # res=sqrt(3), or 1.7 meter, seems too small!
RAST <- rasterize(BDD, grille, BDD$phkcl, fun = fun_ras)

Or with some data: 或带有一些数据:

library(raster)
bel <- getData('GADM', country='BEL', level=1)
bel <- bel[bel$NAME_1 == 'Wallonie', ]
xy  <- spsample(bel, 1000, 'random')
v <- runif(1000)
r <- raster(bel, res=.1)
fun_ras <- function(x, ...){c(length(x), mean(x))}
r <- rasterize(xy, r, v, fun=fun_ras)
plot(r, addfun=function()lines(bel))

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

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