Subversion Repositories public

Rev

Rev 159 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/***************************************************************************
 *   Copyright (C) 2007, 2008 by Andreas Theofilu                          *
 *   andreas@theosys.at                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation version 3 of the License.                *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <iostream>
#include "transform.h"

using std::cout;
using std::cerr;
using std::clog;
using std::endl;

transform::transform(double _llat, double _llon, double _rlat, double _rlon)
{
        llat = _llat;
        llon = _llon;
        rlat = _rlat;
        rlon = _rlon;
        MapLatCtr = (rlat - llat) / 2.0 + llat;
        MapLonCtr = (llon - rlon) / 2.0 + rlon;
        MapXCtr = MapYCtr = 0;
        xscale = yscale = 0.0;
        width = height = 0;
}

transform::~transform()
{
}

/*
 * Set the dimensions in pixels of the image and calculate the scaling.
 */
void transform::setDimensions(int _width, int _height)
{
        width = _width;
        height = _height;
        MapXCtr = width / 2;
        MapYCtr = height / 2;
        xscale = 1.0 / ((double)width - 1.0);
        yscale = -1.0 / ((double)height - 1.0);
        PixPerLatDeg = width / (llat - rlat);
        PixPerLonDeg = height / (rlon - llon);
}

/*
 * Set the scale of the image.
 * This function makes only a rudimentary check of the given values
 * and overwrites the previous values if no errors were found.
 */
void transform::setScale(double scx, double scy)
{
        if (scx <= 0.0 || scx > 1.0 || scy >= 0.0 || scy < -1.0)
           return;

        xscale = scx;
        yscale = scy;
}

/*
 * Return the current scale of X andd Y dimensions
 */
void transform::getScale(double *scx, double *scy)
{
        *scx = xscale;
        *scy = yscale;
}

/*
 * Calculate the pixel position on a raster map out of the
 * geo coordinates given in radian.
 */
bool transform::latLonToPix(double plat, double plon, int *px, int *py)
{
int x,y;
double lat, lon, xdist, ydist, dist, az, pixdist;

        lat = plat;
        lon = plon;
        ydist = PixPerLatDeg * (lat - MapLatCtr);
        xdist = PixPerLonDeg * (lon - MapLonCtr);
        az = atan(ydist / xdist);

        if (xdist < 0.0)
           az += M_PI;

        dist = (xdist * xdist) + (ydist * ydist);
        pixdist = sqrt(dist);
        x = (int)(MapXCtr + pixdist * cos(az));
        y = (int)(MapYCtr - pixdist * sin(az));
        *px = x;
        *py = y;
        return true;
}

/*
 * Calculate the offset inside an image. This value can be used in a
 * buffer as an index, for example.
 */
long transform::getOffset(double plat, double plon)
{
int x, y;

        latLonToPix(plat, plon, &x, &y);
        return ((long)width * (long)y + (long)x);
}