| TerraLib 4.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/kernel/TeCoverageInterpolatorNN.h
Go to the documentation of this file.

    00001 /************************************************************************************
    00002 TerraLib - a library for developing GIS applications.
    00003 Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio.
    00004
    00005 This code is part of the TerraLib library.
    00006 This library is free software; you can redistribute it and/or
    00007 modify it under the terms of the GNU Lesser General Public
    00008 License as published by the Free Software Foundation; either
    00009 version 2.1 of the License, or (at your option) any later version.
    00010
    00011 You should have received a copy of the GNU Lesser General Public
    00012 License along with this library.
    00013
    00014 The authors reassure the license terms regarding the warranties.
    00015 They specifically disclaim any warranties, including, but not limited to,
    00016 the implied warranties of merchantability and fitness for a particular purpose.
    00017 The library provided hereunder is on an "as is" basis, and the authors have no
    00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
    00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
    00020 indirect, special, incidental, or consequential damages arising out of the use
    00021 of this library and its documentation.
    00022 *************************************************************************************/
    00029 #ifndef  __TERRALIB_INTERNAL_COVERAGEINTERPOLATORNN_H
    00030 #define  __TERRALIB_INTERNAL_COVERAGEINTERPOLATORNN_H
    00031
    00032 #include "TeCoverageInterpolator.h"
    00033
    00038 template <class T>
    00039 class TeCoverageInterpolatorNN : public TeCoverageInterpolator<T>
    00040 {
    00041 public:
    00042
    00044
    00047     virtual void setDecoder(TeCoverageDecoder<T>* decoder)
    00048     {
    00049         decoder_ = decoder;
    00050
    00051         // Get global bounding box and total number of coverage elements
    00052         int numElements = decoder->getParameters().getNumElements();
    00053         TeBox& decoderBox = decoder->getParameters().getBoundingBox();
    00054         double boxArea = decoderBox.width() * decoderBox.height();
    00055
    00056         // Calculate the initial radius for nearest neighbour interpolation
    00057         // There is no need to be precise, this is just an heuristic.
    00058
    00059         // search area = total area / number of elements
    00060         // pi * r^2 = search area  -->  r = sqrt(search area / pi)
    00061         initialNeighbourhoodRadius_ = sqrt((boxArea / numElements) / 3.14159);
    00062     }
    00063
    00064
    00066
    00075     virtual void evaluate(const TeCoord2D& position, std::vector<double>& value)
    00076     {
    00077         TeGeomValuePair<T> nearestPoint = nearestNeighbour(position);
    00078         value = nearestPoint.value;
    00079     }
    00080
    00082
    00091     virtual void evaluate(TeRaster& raster, const int rasterBand, const int coverageDimension, const TeBox& box = TeBox())
    00092     {
    00093         // Setup iterators for traversing raster pixels
    00094         TeRaster::iterator it, end;
    00095         if (box == TeBox())
    00096         {
    00097             it = raster.begin();
    00098             end = raster.end();
    00099         }
    00100         else
    00101         {
    00102             it = raster.begin(TeMakePolygon(box), TeBBoxPixelInters, rasterBand);
    00103             end = raster.end(TeMakePolygon(box), TeBBoxPixelInters, rasterBand);
    00104         }
    00105
    00106         while(it != end)
    00107         {
    00108             // Get raster and coverage projections
    00109             TeProjection* coverageProj = decoder_->getParameters().getProjection();
    00110             TeProjection* rasterProj = raster.projection();
    00111             rasterProj->setDestinationProjection(coverageProj);
    00112
    00113             // Get pixel coordinate
    00114             TeCoord2D coord = raster.index2Coord(
    00115                     TeCoord2D(it.currentColumn(), it.currentLine()));
    00116
    00117             // Convert to the intermediate projection
    00118             coord = rasterProj->PC2LL(coord);
    00119
    00120             // Convert to the coverage projection
    00121             coord = coverageProj->LL2PC(coord);
    00122
    00123             // Find coverage value and set raster element
    00124             std::vector<double> value = std::vector<double>();
    00125             evaluate(coord, value);
    00126             raster.setElement(it.currentColumn(), it.currentLine(), value[coverageDimension], rasterBand);
    00127
    00128             it++;
    00129         }
    00130     }
    00131
    00132 protected:
    00133
    00134     double initialNeighbourhoodRadius_;
    00135
    00137
    00147     virtual TeGeomValuePair<T> nearestNeighbour(const TeCoord2D& position, double radius = 0)
    00148     {
    00149         if (!decoder_)
    00150         {
    00151             std::string errorMsg = "Couldn't evaluate position in coverage. Decoder is NULL.";
    00152             throw TeException(UNKNOWN_ERROR_TYPE, errorMsg, false);
    00153         }
    00154         if (!radius)
    00155         {
    00156             radius = initialNeighbourhoodRadius_;
    00157         }
    00158
    00159         TeBox box = TeBox(position.x_ - radius,
    00160                         position.y_ - radius,
    00161                         position.x_ + radius,
    00162                         position.y_ + radius);
    00163
    00164         std::vector<TeGeomValuePair<T> >& selected = std::vector<TeGeomValuePair<T> >();
    00165         decoder_->selectGeomValuePairs(TeMakePolygon(box), TeINTERSECTS, selected);
    00166
    00167         if (selected.size() > 0)
    00168         {
    00169             // Find nearest geometry, among the selected set
    00170             double minDist = TeMAXFLOAT;
    00171             double dist = TeMAXFLOAT;
    00172             std::vector<TeGeomValuePair<T> >::iterator itNearest = selected.begin();
    00173             for(std::vector<TeGeomValuePair<T> >::iterator it = selected.begin(); it != selected.end(); ++it)
    00174             {
    00175                 dist = distanceToGeom(position, it->geom);
    00176                 if (dist < minDist)
    00177                 {
    00178                     minDist = dist;
    00179                     itNearest = it;
    00180                 }
    00181             }
    00182
    00183             // Check if the distance is less than the radius
    00184             // Otherwise, there might be a closer geometry, outside the box
    00185             if (minDist <= radius)
    00186             {
    00187                 return (*itNearest);
    00188             }
    00189             else
    00190             {
    00191                 return nearestNeighbour(position, minDist);
    00192             }
    00193         }
    00194         else
    00195         {
    00196             // If coudn't find any geometries, search in a larger area
    00197             return nearestNeighbour(position, radius * 2);
    00198         }
    00199     }
    00200
    00202     double distanceToGeom(const TeCoord2D& position, TePoint point)
    00203     {
    00204         return TeDistance(position, point.location());
    00205     }
    00206
    00208     double distanceToGeom(const TeCoord2D& position, TeLine2D line)
    00209     {
    00210         double minDist = TeMAXFLOAT;
    00211         double dist = TeMAXFLOAT;
    00212
    00213         // Evaluate the distance to the line segment-by-segment
    00214         for (unsigned int i = 0; i < (line.size() - 1); ++i)
    00215         {
    00216             dist = TeMinimumDistance (line[i], line[i + 1], position, TeCoord2D(), 0);
    00217             if (dist < minDist)
    00218             {
    00219                 minDist = dist;
    00220             }
    00221         }
    00222         return minDist;
    00223     }
    00224
    00226     double distanceToGeom(const TeCoord2D& position, TePolygon poly)
    00227     {
    00228         // If the position is inside the polygon, return zero
    00229         if (TeWithin(position, poly))
    00230         {
    00231             return 0.0;
    00232         }
    00233
    00234         double minDist = TeMAXFLOAT;
    00235         double dist = TeMAXFLOAT;
    00236
    00237         // Evaluate the distance to each ring of the polygon segment-by-segment
    00238         for(TePolygon::iterator itPoly = poly.begin(); itPoly != poly.end(); ++itPoly)
    00239         {
    00240             TeLine2D line = *itPoly;
    00241
    00242             for (unsigned int i = 0; i < (line.size() - 1); ++i)
    00243             {
    00244                 dist = TeMinimumDistance (line[i], line[i + 1], position, TeCoord2D(), 0);
    00245                 if (dist < minDist)
    00246                 {
    00247                     minDist = dist;
    00248                 }
    00249             }
    00250         }
    00251         return minDist;
    00252     }
    00253
    00254 };
    00255
    00256 #endif // __TERRALIB_INTERNAL_COVERAGEINTERPOLATORNN_H

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines