|
1 |
| - |
| 1 | + |
2 | 2 |
|
3 | 3 | .readCellsGDAL <- function(x, cells, layers) {
|
| 4 | + |
| 5 | + nl <- nlayers(x) |
| 6 | + if (nl == 1) { |
| 7 | + if (inherits(x, 'RasterLayer')) { |
| 8 | + layers <- bandnr(x) |
| 9 | + } else { |
| 10 | + layers <- 1 |
| 11 | + } |
| 12 | + } |
| 13 | + laysel <- length(layers) |
| 14 | + |
| 15 | + colrow <- matrix(ncol=2+laysel, nrow=length(cells)) |
| 16 | + colrow[,1] <- colFromCell(x, cells) |
| 17 | + colrow[,2] <- rowFromCell(x, cells) |
| 18 | + colrow[,3] <- NA |
| 19 | + rows <- sort(unique(colrow[,2])) |
| 20 | + |
| 21 | + nc <- x@ncols |
| 22 | + con <- rgdal::GDAL.open(x@file@name, silent=TRUE) |
| 23 | + |
| 24 | + if (laysel == 1) { |
| 25 | + for (i in 1:length(rows)) { |
| 26 | + offs <- c(rows[i]-1, 0) |
| 27 | + v <- rgdal::getRasterData(con, offset=offs, region.dim=c(1, nc), band = layers) |
| 28 | + thisrow <- colrow[colrow[,2] == rows[i], , drop=FALSE] |
| 29 | + colrow[colrow[,2]==rows[i], 3] <- v[thisrow[,1]] |
| 30 | + } |
| 31 | + } else { |
| 32 | + for (i in 1:length(rows)) { |
| 33 | + thisrow <- colrow[colrow[,2] == rows[i], , drop=FALSE] |
| 34 | + if (nrow(thisrow) == 1) { |
| 35 | + offs <- c(rows[i]-1, thisrow[,1]-1) |
| 36 | + v <- as.vector( rgdal::getRasterData(con, offset=offs, region.dim=c(1, 1)) ) |
| 37 | + colrow[colrow[,2]==rows[i], 2+(1:laysel)] <- v[layers] |
| 38 | + |
| 39 | + } else { |
| 40 | + offs <- c(rows[i]-1, 0) |
| 41 | + v <- rgdal::getRasterData(con, offset=offs, region.dim=c(1, nc)) |
| 42 | + v <- do.call(cbind, lapply(1:nl, function(i) v[,,i])) |
| 43 | + |
| 44 | + colrow[colrow[,2]==rows[i], 2+(1:laysel)] <- v[thisrow[,1], layers] |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + rgdal::closeDataset(con) |
| 49 | + colnames(colrow)[2+(1:laysel)] <- names(x)[layers] |
| 50 | + colrow[, 2+(1:laysel)] |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +...readCellsGDAL <- function(x, cells, layers) { |
4 | 57 | # new version by kendonB via mdsumner
|
5 | 58 | # https://github.com/mdsumner/raster-rforge/pull/16/files#diff-5cf48e61a52c5d9bc1d671a341f80d77
|
6 | 59 |
|
| 60 | +# reverted --- too slow |
| 61 | + |
7 | 62 | nl <- nlayers(x)
|
8 | 63 | if (nl == 1) {
|
9 | 64 | if (inherits(x, 'RasterLayer')) {
|
|
57 | 112 | colnames(colrow)[2+(1:laysel)] <- names(x)[layers]
|
58 | 113 | colrow[, 2+(1:laysel), drop = laysel == 1]
|
59 | 114 | }
|
60 |
| - |
61 |
| - |
62 |
| - |
63 |
| -.readCellsGDAL_old <- function(x, cells, layers) { |
64 |
| - |
65 |
| - nl <- nlayers(x) |
66 |
| - if (nl == 1) { |
67 |
| - if (inherits(x, 'RasterLayer')) { |
68 |
| - layers <- bandnr(x) |
69 |
| - } else { |
70 |
| - layers <- 1 |
71 |
| - } |
72 |
| - } |
73 |
| - laysel <- length(layers) |
74 |
| - |
75 |
| - colrow <- matrix(ncol=2+laysel, nrow=length(cells)) |
76 |
| - colrow[,1] <- colFromCell(x, cells) |
77 |
| - colrow[,2] <- rowFromCell(x, cells) |
78 |
| - colrow[,3] <- NA |
79 |
| - rows <- sort(unique(colrow[,2])) |
80 |
| - |
81 |
| - nc <- x@ncols |
82 |
| - con <- rgdal::GDAL.open(x@file@name, silent=TRUE) |
83 |
| - |
84 |
| - if (laysel == 1) { |
85 |
| - for (i in 1:length(rows)) { |
86 |
| - offs <- c(rows[i]-1, 0) |
87 |
| - v <- rgdal::getRasterData(con, offset=offs, region.dim=c(1, nc), band = layers) |
88 |
| - thisrow <- colrow[colrow[,2] == rows[i], , drop=FALSE] |
89 |
| - colrow[colrow[,2]==rows[i], 3] <- v[thisrow[,1]] |
90 |
| - } |
91 |
| - } else { |
92 |
| - for (i in 1:length(rows)) { |
93 |
| - thisrow <- colrow[colrow[,2] == rows[i], , drop=FALSE] |
94 |
| - if (nrow(thisrow) == 1) { |
95 |
| - offs <- c(rows[i]-1, thisrow[,1]-1) |
96 |
| - v <- as.vector( rgdal::getRasterData(con, offset=offs, region.dim=c(1, 1)) ) |
97 |
| - colrow[colrow[,2]==rows[i], 2+(1:laysel)] <- v[layers] |
98 |
| - |
99 |
| - } else { |
100 |
| - offs <- c(rows[i]-1, 0) |
101 |
| - v <- rgdal::getRasterData(con, offset=offs, region.dim=c(1, nc)) |
102 |
| - v <- do.call(cbind, lapply(1:nl, function(i) v[,,i])) |
103 |
| - |
104 |
| - colrow[colrow[,2]==rows[i], 2+(1:laysel)] <- v[thisrow[,1], layers] |
105 |
| - } |
106 |
| - } |
107 |
| - } |
108 |
| - rgdal::closeDataset(con) |
109 |
| - colnames(colrow)[2+(1:laysel)] <- names(x)[layers] |
110 |
| - colrow[, 2+(1:laysel)] |
111 |
| -} |
112 |
| - |
113 |
| - |
|
0 commit comments