Skip to content

Commit c3d2d3b

Browse files
committed
x and y from col/row
1 parent 073cf31 commit c3d2d3b

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: vaster
22
Title: Tools for Raster Grid Logic
3-
Version: 0.0.2.9002
3+
Version: 0.0.2.9003
44
Authors@R: c(person("Michael", "Sumner", email = "mdsumner@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-2471-7511")))
55
Description: Provides raster grid logic, the grid operations that don't require access to materialized data, i.e. most of them.
66
Grids are arrays with dimension and extent, and many operations are functions of just the dimension 'nrows', 'ncols' or

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# vaster dev
22

3-
* We now have internal functions `col_from_x_c()` and `row_from_y_c()` that use C under the hood, these currently return 0-based indexes.
3+
* We now have internal functions `col_from_x_c()` and `row_from_y_c()`, `x_from_col_c()` and `y_from_row_c()` that use C under the hood, these currently accept and return 0-based indexes.
4+
45

56
* Package now requires compilation, with the R C api.
67

R/C_versions.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ row_from_y_c <- function(dimension, extent, y) {
44
col_from_x_c <- function(dimension, extent, x) {
55
.Call("col_from_x_", as.integer(dimension[1L]), as.double(extent[1:2]), as.double(x), PACKAGE = "vaster")
66
}
7+
8+
x_from_col_c <- function(dimension, extent, col) {
9+
.Call("x_from_col_", as.integer(dimension[1L]), as.double(extent[1:2]), as.integer(col))
10+
}
11+
y_from_row_c <- function(dimension, extent, row) {
12+
.Call("y_from_row_", as.integer(dimension[2L]), as.double(extent[3:4]), as.integer(row))
13+
}

src/coordinates.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,31 @@ SEXP row_from_y_(SEXP nrow, SEXP ylim, SEXP py)
4242
return bin_from_float(nrow, ylim, py);
4343

4444
}
45+
46+
SEXP coord_from_bin(SEXP bins, SEXP range, SEXP index) {
47+
int nn = LENGTH(index);
48+
SEXP out;
49+
out = PROTECT(Rf_allocVector(REALSXP, nn));
50+
51+
int nbins = INTEGER(bins)[0];
52+
double res = (REAL(range)[1] - REAL(range)[0])/nbins;
53+
double cmin = REAL(range)[0];
54+
double* rout = REAL(out);
55+
int* rindex = INTEGER(index);
56+
for (int i = 0; i < nn; i++) {
57+
if ((rindex[i] >= 0) && (rindex[i] < nbins)) {
58+
rout[i] = cmin + res/2 + rindex[i] * res;
59+
} else {
60+
rout[i] = R_NaReal;
61+
}
62+
}
63+
UNPROTECT(1);
64+
return out;
65+
}
66+
SEXP x_from_col_(SEXP ncol, SEXP xlim, SEXP col) {
67+
return coord_from_bin(ncol, xlim, col);
68+
}
69+
70+
SEXP y_from_row_(SEXP nrow, SEXP ylim, SEXP row) {
71+
return coord_from_bin(nrow, ylim, row);
72+
}

src/init.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
/* .Call calls */
1111
extern SEXP col_from_x_(SEXP, SEXP, SEXP);
1212
extern SEXP row_from_y_(SEXP, SEXP, SEXP);
13+
extern SEXP x_from_col_(SEXP, SEXP, SEXP);
14+
extern SEXP y_from_row_(SEXP, SEXP, SEXP);
1315

1416
static const R_CallMethodDef CallEntries[] = {
1517
{"col_from_x_", (DL_FUNC) &col_from_x_, 3},
1618
{"row_from_y_", (DL_FUNC) &row_from_y_, 3},
19+
{"x_from_col_", (DL_FUNC) &x_from_col_, 3},
20+
{"y_from_row_", (DL_FUNC) &y_from_row_, 3},
1721
{NULL, NULL, 0}
1822
};
1923

0 commit comments

Comments
 (0)