1 #include "csf.h"
2 #include "csfimpl.h"
3 
4 /* compute true world co-ordinate of a pixel
5  * RrowCol2Coords computes the true world co-ordinate from a
6  * row, column index.
7  * The row, column co-ordinate
8  * don't have to be on the map. They are just relative to upper left position.
9  * For example (row,col) = (-1,0) computes the (x,y) co-ordinate of
10  * the pixel that is right above upper left pixel.
11  *
12  * returns
13  *  0  if the co-ordinate is outside the map.
14  *  1 if inside.
15  * -1 in case of an error.
16  *
17  * Merrno
18  * ILL_CELLSIZE
19  */
RgetCoords(const MAP * m,int inCellPos,size_t row,size_t col,double * x,double * y)20 int RgetCoords(
21 	const MAP *m,	/* map handle */
22 	int inCellPos,  /* nonzero if you want the co-ordinate
23 			 * at the centre of the cell, 0 if you
24 			 * want the upper left co-ordinate of the cell
25 			 */
26 	size_t row,      /* Row number (relates to y position). */
27 	size_t col,      /* Column number (relates to x position). */
28 	double *x,      /* write-only. Returns x of true co-ordinate */
29 	double *y)      /* write-only. Returns y of true co-ordinate */
30 {
31 	return RrowCol2Coords(m,
32 		(double)row+(inCellPos ? 0.5 : 0.0),
33 		(double)col+(inCellPos ? 0.5 : 0.0),
34 		x,y);
35 }
36 
37 /* compute true world co-ordinate from row, column index
38  * RrowCol2Coords computes the true world co-ordinate from a
39  * row, column index. The row,column co-ordinate can be fractions.
40  * For example (row,col) = (0.5,0.5) computes the (x,y) co-ordinate of
41  * the centre of the upper left pixel. Secondly, the row and column co-ordinate
42  * don't have to be on the map. They are just relative to upper left position.
43  * For example (row,col) = (-0.5,0.5) computes the (x,y) co-ordinate of
44  * the centre of the pixel that is right above upper left pixel.
45  */
RasterRowCol2Coords(const CSF_RASTER_LOCATION_ATTRIBUTES * m,double row,double col,double * x,double * y)46 void RasterRowCol2Coords(
47 	const CSF_RASTER_LOCATION_ATTRIBUTES *m, /* raster handle */
48 	double row,  /* Row number (relates to y position). */
49 	double col,  /* Column number (relates to x position). */
50 	double *x,   /* write-only. x co-ordinate */
51 	double *y)   /* write-only. y co-ordinate */
52 {
53 	double cs     = m->cellSize;
54 	double c      = m->angleCos;
55 	double s      = m->angleSin;
56 	double yRow   = cs * row;
57 	double xCol   = cs * col;
58 	double xCol_t = xCol * c - yRow * s;
59 	double yRow_t = xCol * s + yRow * c;
60 
61 	*x = m->xUL + xCol_t;
62 	if (m->projection == PT_YINCT2B)
63 		*y = m->yUL + yRow_t;
64 	else  /* all other projections */
65 		*y = m->yUL - yRow_t;
66 }
67 
68 /* compute true world co-ordinate from row, column index
69  * RasterRowCol2Coords computes the true world co-ordinate from a
70  * row, column index. The row,column co-ordinate can be fractions.
71  * For example (row,col) = (0.5,0.5) computes the (x,y) co-ordinate of
72  * the centre of the upper left pixel. Secondly, the row and column co-ordinate
73  * don't have to be on the map. They are just relative to upper left position.
74  * For example (row,col) = (-0.5,0.5) computes the (x,y) co-ordinate of
75  * the centre of the pixel that is right above upper left pixel.
76  *
77  * returns
78  *  0  if the co-ordinate is outside the map.
79  *  1 if inside.
80  * -1 in case of an error.
81  *
82  * Merrno
83  * ILL_CELLSIZE
84  */
RrowCol2Coords(const MAP * m,double row,double col,double * x,double * y)85 int RrowCol2Coords(const MAP *m, /* map handle */
86 		    double row,  /* Row number (relates to y position). */
87 		    double col,  /* Column number (relates to x position). */
88 		    double *x,   /* write-only. x co-ordinate */
89 		    double *y)   /* write-only. y co-ordinate */
90 {
91 	if (m->raster.cellSize <= 0
92 	    || m->raster.cellSize != m->raster.cellSizeDupl )
93 	{
94 		M_ERROR(ILL_CELLSIZE);
95 		goto error;
96 	}
97 	RasterRowCol2Coords(&(m->raster),row,col,x,y);
98 	return( (m->raster.nrRows > row) && (m->raster.nrCols > col) &&
99 	        (row >= 0) && (col >= 0));
100 error:  return(-1);
101 }
102