Quantum GIS API Documentation  1.8
src/core/qgslabelsearchtree.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgslabelsearchtree.cpp
00003     ---------------------
00004     begin                : November 2010
00005     copyright            : (C) 2010 by Marco Hugentobler
00006     email                : marco dot hugentobler at sourcepole dot ch
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 #include "qgslabelsearchtree.h"
00016 #include "labelposition.h"
00017 
00018 bool searchCallback( QgsLabelPosition* pos, void* context )
00019 {
00020   QList<QgsLabelPosition*>* list = static_cast< QList<QgsLabelPosition*>* >( context );
00021   list->push_back( pos );
00022   return true;
00023 }
00024 
00025 QgsLabelSearchTree::QgsLabelSearchTree()
00026 {
00027 }
00028 
00029 QgsLabelSearchTree::~QgsLabelSearchTree()
00030 {
00031   clear();
00032 }
00033 
00034 void QgsLabelSearchTree::label( const QgsPoint& p, QList<QgsLabelPosition*>& posList )
00035 {
00036   double c_min[2]; c_min[0] = p.x() - 0.1; c_min[1] = p.y() - 0.1;
00037   double c_max[2]; c_max[0] = p.x() + 0.1; c_max[1] = p.y() + 0.1;
00038 
00039   QList<QgsLabelPosition*> searchResults;
00040   mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );
00041 
00042   //tolerance +-0.1 could be high in case of degree crs, so check if p is really contained in the results
00043   posList.clear();
00044   QList<QgsLabelPosition*>::const_iterator resultIt = searchResults.constBegin();
00045   for ( ; resultIt != searchResults.constEnd(); ++resultIt )
00046   {
00047     if (( *resultIt )->labelRect.contains( p ) )
00048     {
00049       posList.push_back( *resultIt );
00050     }
00051   }
00052 }
00053 
00054 bool QgsLabelSearchTree::insertLabel( LabelPosition* labelPos, int featureId, const QString& layerName, bool diagram )
00055 {
00056   if ( !labelPos )
00057   {
00058     return false;
00059   }
00060 
00061   double c_min[2];
00062   double c_max[2];
00063   labelPos->getBoundingBox( c_min, c_max );
00064 
00065   QVector<QgsPoint> cornerPoints;
00066   for ( int i = 0; i < 4; ++i )
00067   {
00068     cornerPoints.push_back( QgsPoint( labelPos->getX( i ), labelPos->getY( i ) ) );
00069   }
00070   QgsLabelPosition* newEntry = new QgsLabelPosition( featureId, labelPos->getAlpha(), cornerPoints, QgsRectangle( c_min[0], c_min[1], c_max[0], c_max[1] ),
00071       labelPos->getWidth(), labelPos->getHeight(), layerName, labelPos->getUpsideDown(), diagram );
00072   mSpatialIndex.Insert( c_min, c_max, newEntry );
00073   return true;
00074 }
00075 
00076 void QgsLabelSearchTree::clear()
00077 {
00078   RTree<QgsLabelPosition*, double, 2, double>::Iterator indexIt;
00079   mSpatialIndex.GetFirst( indexIt );
00080   while ( !mSpatialIndex.IsNull( indexIt ) )
00081   {
00082     delete mSpatialIndex.GetAt( indexIt );
00083     mSpatialIndex.GetNext( indexIt );
00084   }
00085   mSpatialIndex.RemoveAll();
00086 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines