read
The easiest way to implement a Geographic Information System is to use a projection where the Earth is divided on an equal latitude-longitude grid spacing. E.g. Plate Carrée.
Therefore, geographical coordinates represented by decimal values have to be mapped to pixels represented by integer values.
So, the conversion from geographic coordinates to pixels will be perfomed as following:
unsigned int px_left = static_cast<unsigned int>(round_to_nearest_int((geo_left - c_earth_left) / resolution));
unsigned int px_bottom = static_cast<unsigned int>(round_to_nearest_int((geo_bottom - c_earth_bottom) / resolution));
unsigned int px_right = static_cast<unsigned int>(round_to_nearest_int((geo_right - c_earth_left) / resolution) - 1);
unsigned int px_top = static_cast<unsigned int>(round_to_nearest_int((geo_top - c_earth_bottom) / resolution) - 1);
Where Earth boundaries are defined as constants:
const double c_earth_left = -180.0;
const double c_earth_bottom = -90.0;
const double c_earth_right = 180.0;
const double c_earth_top = 90.0;
The function to round a number to its nearest integer is implemented adding 0.5 and truncating the result.
int round_to_nearest_int ( double number )
{
return static_cast<int>(number + 0.5);
}
And the opposite, to convert from pixels to geographic coordinates:
double geo_left = c_earth_left + static_cast<double>(px_left) * resolution;
double geo_bottom = c_earth_bottom + static_cast<double>(px_bottom) * resolution;
double geo_right = c_earth_left + static_cast<double>(px_right + 1) * resolution;
double geo_top = c_earth_bottom + static_cast<double>(px_top + 1) * resolution;
References: