1# Author: Robert J. Hijmans
2# Date :  October 2017
3# Version 1.0
4# License GPL v3
5
6setMethod("yFromRow", signature(object="SpatRaster", row="numeric"),
7	function(object, row) {
8		object@ptr$yFromRow(row - 1)
9	}
10)
11
12setMethod(xFromCol, signature(object="SpatRaster", col="numeric"),
13	function(object, col) {
14		object@ptr$xFromCol(col - 1)
15	}
16)
17
18setMethod(colFromX, signature(object="SpatRaster", x="numeric"),
19	function(object, x)	{
20		cols <- object@ptr$colFromX(x) + 1
21		cols[cols==0] <- NA
22		cols
23	}
24)
25
26setMethod(rowFromY, signature(object="SpatRaster", y="numeric"),
27	function(object, y)	{
28		rows <- object@ptr$rowFromY(y) + 1
29		rows[rows==0] <- NA
30		rows
31	}
32)
33
34setMethod(cellFromXY, signature(object="SpatRaster", xy="matrix"),
35	function(object, xy) {
36		stopifnot(ncol(xy) == 2)
37		#.checkXYnames(colnames(xy))
38		object@ptr$cellFromXY(xy[,1], xy[,2]) + 1
39	}
40)
41
42setMethod(cellFromRowCol, signature(object="SpatRaster", row="numeric", col="numeric"),
43	function(object, row, col) {
44		object@ptr$cellFromRowCol(row-1, col-1) + 1
45	}
46)
47
48setMethod(cellFromRowColCombine, signature(object="SpatRaster", row="numeric", col="numeric"),
49	function(object, row, col) {
50		object@ptr$cellFromRowColCombine(row-1, col-1) + 1
51	}
52)
53
54
55setMethod(xyFromCell, signature(object="SpatRaster", cell="numeric"),
56	function(object, cell) {
57		xy <- object@ptr$xyFromCell(cell-1)
58		xy <- do.call(cbind, xy)
59		colnames(xy) <- c("x", "y")
60		xy
61	}
62)
63
64setMethod(yFromCell, signature(object="SpatRaster", cell="numeric"),
65	function(object, cell) {
66		xyFromCell(object, cell)[,2]
67	}
68
69)
70
71setMethod(xFromCell, signature(object="SpatRaster", cell="numeric"),
72	function(object, cell) {
73		xyFromCell(object, cell)[,1]
74	}
75)
76
77setMethod(rowColFromCell, signature(object="SpatRaster", cell="numeric"),
78	function(object, cell) {
79		rc <- object@ptr$rowColFromCell(cell-1)
80		rc <- do.call(cbind, rc)
81		rc[rc < 0] <- NA
82		rc+1
83	}
84)
85
86setMethod(rowFromCell, signature(object="SpatRaster", cell="numeric"),
87	function(object, cell) {
88		rowColFromCell(object, cell)[,1]
89	}
90)
91
92setMethod(colFromCell, signature(object="SpatRaster", cell="numeric"),
93	function(object, cell) {
94		rowColFromCell(object, cell)[,2]
95	}
96)
97
98