查找栅格砖中的哪些单元格包含任何图层中的数据

时间:2017-01-11 01:42:27

标签: r r-raster

我有一些网格化全球数据的大型栅格砖。我想在逐个单元的基础上执行一些计算要求高的计算。为了减少计算负荷,我想仅在包含砖的至少一个层中的数据的栅格单元上运行模型。但是,如何有效地查找包含某些数据的单元格呢?我知道我可以用循环做到这一点。像这样:

第一部分可重现的数据:

library(raster)
r.list = vector("list", 20)
set.seed(123)
for (i in 1:20) {
  r.list[[i]] = raster(vals = sample(c(rep(NA,10),1), 100, TRUE), nrows = 10, ncols = 10, ext = extent(c(0,25,0,25)))
}
r.brick = brick(r.list)

现在我们遍历砖块的每一层,找出哪些单元格有一些数据:

has.data.list = vector("list", 20)
for (i in 1:20) {
  has.data.list[[i]] = which(!is.na(values(raster(r.brick, layer=i))))
}
has.data = sort(unique(unlist(has.data.list)))

但这是相当不优雅的。是否有规范/有效的方法来获取包含某些数据的单元格向量?

1 个答案:

答案 0 :(得分:1)

如果指定na.rm = TRUE,则可能的一个技巧是利用max()仅在NA所有输入值为NA时返回has.data = which(!is.na(getValues(max(r.brick, na.rm=TRUE)))) 的事实。

因此,您可以计算栅格砖的最大值,并检查它与NA的不同之处。类似的东西:

public static void main(String[] args)
{
    Display display = new Display();
    final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
    Shell shell = new Shell(display);
    shell.setText("Images on the right side of the TableItem");
    shell.setLayout(new FillLayout());
    Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    int columnCount = 3;
    for (int i = 0; i < columnCount; i++)
    {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Column " + i);
    }
    int itemCount = 8;
    for (int i = 0; i < itemCount; i++)
    {
        TableItem item = new TableItem(table, SWT.NONE);
    }
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be
     * as efficient as possible.
     */
    Listener paintListener = event ->
    {
        switch (event.type)
        {
            case SWT.MeasureItem:
            {
                Rectangle rect1 = image.getBounds();
                event.width += rect1.width;
                event.height = Math.max(event.height, rect1.height + 2);
                break;
            }
            case SWT.PaintItem:
            {
                TableItem item = (TableItem) event.item;
                Rectangle rect2 = image.getBounds();
                Rectangle bounds = item.getBounds(event.index);
                int x = bounds.x + bounds.width - rect2.width;
                int offset = Math.max(0, (event.height - rect2.height) / 2);
                event.gc.drawImage(image, x, event.y + offset);
                break;
            }
        }
    };
    table.addListener(SWT.MeasureItem, paintListener);
    table.addListener(SWT.PaintItem, paintListener);

    for (int i = 0; i < columnCount; i++)
    {
        table.getColumn(i).pack();
    }
    shell.setSize(500, 200);
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch()) display.sleep();
    }
    if (image != null) image.dispose();
    display.dispose();
}
  

has.data

     

1 2 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20   21 22 23 24 25 26 27 28 29 30 31 33 34 35 37 38 39 40   41 42 44 46 47 48 49 50 51 52 53 54 55 56 57 59 60 61   62 64 66 67 68 69 70 71 74 75 76 77 78 79 80 82 83 85   86 87 89 90 91 92 93 95 96 97 98 99 100