2016-08-04 50 views
0

我是R新手,需要基于随机点的大型数据库制作栅格。你可以看到它们在比利时南部的分布。点分布栅格化随机空间点不起作用

enter image description here

我的需求是:

-The光栅具有相匹配的扩展全国。

- 我需要能够在尺寸的像素上播放,而不是在行数和列数上播放。

- 首先,像素值应该是所有落入点的平均值。然后,我还想要其他基本统计数据,如中位数,标准偏差等。

我跟着我在网上看到的有关“光栅化”命令,但我总是有相同的错误消息。

这里是我的代码:

#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) 

和错误消息我得到:

警告消息: 在is.na(场):is.na()适用于非(名单(几何(点[1]),data.frame(d),FALSE): erreur d'évaluationde l'argument'x'lors de lasélectiond'une mAthode pour la fonction'addAttrToGeom':错误(函数(类,fdef,mtable): 无法找到一个继承的方法为功能'几何'签名'“矩阵”'

你能帮我吗?

就在下面,你可以找到一个随机创建的数据库来尝试代码。感谢您阅读我。

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 
+0

您好,欢迎StackOverflow上。由于我们没有您的数据,我们无法重现您的问题。请尝试想出一个[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 – symbolrush

+0

嗨,我知道数据更容易,但它们是保密的......我刚刚创建了一个随机数据集(仅有100个点)来尝试代码,但它与原始数据库的特征相匹配。 –

+0

您不应该提供随机数字,而应提供生成这些数字的代码 – RobertH

回答

0

您的功能不正确。

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

mean(c)应该mean(x)

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

就这样,尝试

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

或用一些数据:

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))