2013-03-26 63 views
1

我有一个有很多补丁(具有相同值的连续单元的块)的栅格地图。我需要做的是获得每个补丁的中心坐标(或接近中心)。 我对栅格包非常没有经验,但是只有当我知道地图中单元格的位置时,我才能得到坐标。有没有办法让坐标给出单元格的值呢?谢谢获取栅格地图中补丁的坐标(R中的栅格包)

+0

你是如何创建你创建/导入你的光栅?如果您展示了数据样本,用于创建栅格的代码和/或您尝试的内容,我会更简单地帮助您。 – plannapus 2013-03-26 08:51:14

+0

请参阅'?click'和可能还有'?zoom'。 – 2013-03-26 08:57:33

+0

作为一种替代方案,您可能想要使用ImageJ,它具有丛/找斑工具。 – 2013-03-26 11:40:34

回答

1

你可以采取每个补丁的坐标的平均值:

# some dummy data 
m <- matrix(c(
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 
    0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0, 
    0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,4,4,0, 
    0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,4,4,0, 
    0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,4,4,0, 
    0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0, 
    0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 
    0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 
    0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 
    0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0, 
    0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0, 
    0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 
    0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 
    0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, 
    0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0, 
    0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0, 
    0,0,2,3,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0, 
    0,0,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=20, byrow=T) 

# create a raster 
r <- raster(m) 

# convert raster to points 
p <- data.frame(rasterToPoints(r)) 

# filter out packground 
p <- p[p$layer > 0,] 

# for each patch calc mean coordinates 
sapply(split(p[, c("x", "y")], p$layer), colMeans) 
+0

它的工作原理!谢谢! – Oritteropus 2013-03-26 09:03:52

4

如果补丁你的意思是团块,光栅包可以让你找到并隔离,团块。服用clump()光栅包例子,扩展它:

library(raster) 
library(igraph) 
detach("package:coin", unload=TRUE) 
r <- raster(ncols=12, nrows=12) 
set.seed(0) 
r[] <- round(runif(ncell(r))*0.7) 
rc <- clump(r) 
clump_id <- getValues(rc)  
xy <- xyFromCell(rc,1:ncell(rc)) 
df <- data.frame(xy, clump_id, is_clump = rc[] %in% freq(rc, useNA = 'no')[,1]) 
df[df$is_clump == T, ] 

plot(r) 

r

plot(rc) 
text(df[df$is_clump == T, 1:2], labels = df[df$is_clump == T, 3]) 

rc

可能不会像有趣,你可以期望。

您与方向= 4

rc <- clump(r, directions = 4) 
clump_id <- getValues(rc)  
xy <- xyFromCell(rc,1:ncell(rc)) 
df <- data.frame(xy, clump_id, is_clump = rc[] %in% freq(rc, useNA = 'no')[,1]) 
df[df$is_clump == T, ] 

重来一次得到

rc4

,也许丛 '重心'

dfm <- ddply(df[df$is_clump == T, ], .(clump_id), summarise, xm = mean(x), ym = mean(y)) 
plot(rc) 
text(dfm[, 2:3], labels = dfm$clump_id) 

rc4mean

注意

如果您尝试使用clump()没有首先 分离modeltools库将有一个错误。模型工具是由硬币调用的,也可能是其他统计库。