1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/*
* rrowcol.c
*/
#include "csf.h"
#include "csfimpl.h"
#include <math.h> /* floor */
/* compute (fractional) row, column index from true world co-ordinate.
* RasterCoords2RowCol computes row, column index from true world co-ordinate.
* The row and column co-ordinate are returned as fractions (See parameters
* section).
* The x,y co-ordinate
* don't have to be on the map. They are just relative to upper left position.
*
* returns
* 0 if the co-ordinate is outside the map,
* 1 if inside,
* -1 in case of an error
*
* Merrno
* ILL_CELLSIZE
*/
void RasterCoords2RowCol(
const CSF_RASTER_LOCATION_ATTRIBUTES *m,
double x, /* x of true co-ordinate */
double y, /* y of true co-ordinate */
double *row, /* write-only. Row index (y-pos). floor(row) is row number,
* if (row >= 0) then fmod(row, 1) is in-pixel displacement from pixel-top,
* if (row <0) then fmod(row, 1) is in-pixel displacement from pixel-bottom.
*/
double *col) /* write-only. Column index (x-pos). floor(col) is column number,
* if (col >= 0) then fmod(col, 1) is in-pixel displacement from pixel-left,
* if (col <0) then fmod(col, 1) is in-pixel displacement from pixel-right.
*/
{
double cs = m->cellSize;
double xCol = (x - m->xUL) / cs;
double yRow = (((m->projection == PT_YINCT2B)
? (y - m->yUL)
: (m->yUL - y)) / cs);
/* rotate clockwise: */
double c = m->angleCos; /* cos(t) == cos(-t) */
double s = -(m->angleSin); /* -sin(t) == sin(-t) */
*col = xCol * c - yRow * s;
*row = xCol * s + yRow * c;
}
int RasterCoords2RowColChecked(
const CSF_RASTER_LOCATION_ATTRIBUTES *m,
double x, /* x of true co-ordinate */
double y, /* y of true co-ordinate */
double *row,
double *col)
{
double row_,col_; /* use copies, func( , , ,&dummy, &dummy) will fail */
RasterCoords2RowCol(m,x,y,&row_,&col_);
*row = row_;
*col = col_;
return( row_ >= 0 && col_ >= 0 &&
(m->nrRows > row_) && (m->nrCols > col_) );
}
/* compute (fractional) row, column index from true world co-ordinate.
* Rcoord2RowCol computes row, column index from true world co-ordinate.
* The row and column co-ordinate are returned as fractions (See parameters
* section).
* The x,y co-ordinate
* don't have to be on the map. They are just relative to upper left position.
*
* returns
* 0 if the co-ordinate is outside the map,
* 1 if inside,
* -1 in case of an error
*
* Merrno
* ILL_CELLSIZE
*/
int Rcoords2RowCol(
const MAP *m, /* map handle */
double x, /* x of true co-ordinate */
double y, /* y of true co-ordinate */
double *row, /* write-only. Row index (y-pos). floor(row) is row number,
* if (row >= 0) then fmod(row, 1) is in-pixel displacement from pixel-top,
* if (row <0) then fmod(row, 1) is in-pixel displacement from pixel-bottom.
*/
double *col) /* write-only. Column index (x-pos). floor(col) is column number,
* if (col >= 0) then fmod(col, 1) is in-pixel displacement from pixel-left,
* if (col <0) then fmod(col, 1) is in-pixel displacement from pixel-right.
*/
{
double row_,col_; /* use copies, func( , , ,&dummy, &dummy) will fail
* otherwise
*/
if (m->raster.cellSize <= 0
|| (m->raster.cellSize != m->raster.cellSizeDupl ) )
{ /* CW we should put this in Mopen */
M_ERROR(ILL_CELLSIZE);
goto error;
}
RasterCoords2RowCol(&(m->raster),x,y,&row_,&col_);
*row = row_;
*col = col_;
return( row_ >= 0 && col_ >= 0 &&
(m->raster.nrRows > row_) && (m->raster.nrCols > col_) );
error: return(-1);
}
/* compute row, column number of true world co-ordinate
* RgetRowCol computes row, column number of true world co-ordinate.
*
* returns
* 0 if the co-ordinate is outside the map,
* 1 if inside,
* -1 in case of an error
*
* Merrno
* ILL_CELLSIZE
*/
int RgetRowCol(
const MAP *m, /* map handle */
double x, /* x of true co-ordinate */
double y, /* y of true co-ordinate */
size_t *row, /* write-only. Row number (y-pos).
* Undefined if (x,y) is outside of map
*/
size_t *col) /* write-only. Column number (x-pos).
* Undefined if (x,y) is outside of map
*/
{
double row_d,col_d;
int result;
result = Rcoords2RowCol(m,x,y,&row_d,&col_d);
if (result > 0)
{
*row = (size_t)floor(row_d);
*col = (size_t)floor(col_d);
}
return(result);
}
|