|
QGIS API Documentation
master-28efcda
|
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 void QgsLabelSearchTree::labelsInRect( const QgsRectangle& r, QList<QgsLabelPosition*>& posList ) 00055 { 00056 double c_min[2]; c_min[0] = r.xMinimum(); c_min[1] = r.yMinimum(); 00057 double c_max[2]; c_max[0] = r.xMaximum(); c_max[1] = r.yMaximum(); 00058 00059 QList<QgsLabelPosition*> searchResults; 00060 mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults ); 00061 00062 posList.clear(); 00063 QList<QgsLabelPosition*>::const_iterator resultIt = searchResults.constBegin(); 00064 for ( ; resultIt != searchResults.constEnd(); ++resultIt ) 00065 { 00066 posList.push_back( *resultIt ); 00067 } 00068 } 00069 00070 bool QgsLabelSearchTree::insertLabel( LabelPosition* labelPos, int featureId, const QString& layerName, const QString& labeltext, const QFont& labelfont, bool diagram, bool pinned ) 00071 { 00072 if ( !labelPos ) 00073 { 00074 return false; 00075 } 00076 00077 double c_min[2]; 00078 double c_max[2]; 00079 labelPos->getBoundingBox( c_min, c_max ); 00080 00081 QVector<QgsPoint> cornerPoints; 00082 for ( int i = 0; i < 4; ++i ) 00083 { 00084 cornerPoints.push_back( QgsPoint( labelPos->getX( i ), labelPos->getY( i ) ) ); 00085 } 00086 QgsLabelPosition* newEntry = new QgsLabelPosition( featureId, labelPos->getAlpha(), cornerPoints, QgsRectangle( c_min[0], c_min[1], c_max[0], c_max[1] ), 00087 labelPos->getWidth(), labelPos->getHeight(), layerName, labeltext, labelfont, labelPos->getUpsideDown(), diagram, pinned ); 00088 mSpatialIndex.Insert( c_min, c_max, newEntry ); 00089 return true; 00090 } 00091 00092 void QgsLabelSearchTree::clear() 00093 { 00094 RTree<QgsLabelPosition*, double, 2, double>::Iterator indexIt; 00095 mSpatialIndex.GetFirst( indexIt ); 00096 while ( !mSpatialIndex.IsNull( indexIt ) ) 00097 { 00098 delete mSpatialIndex.GetAt( indexIt ); 00099 mSpatialIndex.GetNext( indexIt ); 00100 } 00101 mSpatialIndex.RemoveAll(); 00102 }