Skip to content

Commit

Permalink
move QgsMapToolFeature::avoidIntersections to QgsGeometry
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@12700 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Jan 7, 2010
1 parent 3d7b09c commit c0da2c6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 58 deletions.
9 changes: 9 additions & 0 deletions python/core/qgsgeometry.sip
Expand Up @@ -314,5 +314,14 @@ not disjoint with existing polygons of the feature*/
@return true in case of success and false else*/
bool convertToMultiType();

/** Modifies geometry to avoid intersections with the layers specified in project properties
* @return 0 in case of success,
* 1 if geometry is not of polygon type,
* 2 if avoid intersection would change the geometry type,
* 3 other error during intersection removal
* @note added in 1.5
*/
int avoidIntersections();

}; // class QgsGeometry

6 changes: 2 additions & 4 deletions src/app/gps/qgsgpsinformationwidget.cpp
Expand Up @@ -30,6 +30,7 @@
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsattributedialog.h"
#include "qgsgeometry.h"

//for avoid intersections static method
#include "qgsmaptooladdfeature.h"
Expand Down Expand Up @@ -449,9 +450,6 @@ void QgsGPSInformationWidget::displayGPSInformation( const QgsGPSInformation& in
mGPSTextEdit->append( "hdop: " + QString::number( info.hdop ) );
mGPSTextEdit->append( "vdop: " + QString::number( info.vdop ) );




// Avoid refreshing / panning if we havent moved
if ( mLastGpsPosition != myNewCenter )
{
Expand Down Expand Up @@ -768,7 +766,7 @@ void QgsGPSInformationWidget::on_mBtnCloseFeature_clicked( )
memcpy( &wkb[position], &y, sizeof( double ) );
f->setGeometryAndOwnership( &wkb[0], size );

int avoidIntersectionsReturn = QgsMapToolAddFeature::avoidIntersections( f->geometry() );
int avoidIntersectionsReturn = f->geometry()->avoidIntersections();
if ( avoidIntersectionsReturn == 1 )
{
//not a polygon type. Impossible to get there
Expand Down
46 changes: 1 addition & 45 deletions src/app/qgsmaptooladdfeature.cpp
Expand Up @@ -443,7 +443,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
}
f->setGeometryAndOwnership( &wkb[0], size );

int avoidIntersectionsReturn = avoidIntersections( f->geometry() );
int avoidIntersectionsReturn = f->geometry()->avoidIntersections();
if ( avoidIntersectionsReturn == 1 )
{
//not a polygon type. Impossible to get there
Expand Down Expand Up @@ -519,47 +519,3 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
}
}
}

int QgsMapToolAddFeature::avoidIntersections( QgsGeometry* g )
{
int returnValue = 0;

//check if g has polygon type
if ( !g || g->type() != QGis::Polygon )
{
return 1;
}

QGis::WkbType geomTypeBeforeModification = g->wkbType();

//read avoid intersections list from project properties
bool listReadOk;
QStringList avoidIntersectionsList = QgsProject::instance()->readListEntry( "Digitizing", "/AvoidIntersectionsList", &listReadOk );
if ( !listReadOk )
{
return true; //no intersections stored in project does not mean error
}

//go through list, convert each layer to vector layer and call QgsVectorLayer::removePolygonIntersections for each
QgsVectorLayer* currentLayer = 0;
QStringList::const_iterator aIt = avoidIntersectionsList.constBegin();
for ( ; aIt != avoidIntersectionsList.constEnd(); ++aIt )
{
currentLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( *aIt ) );
if ( currentLayer )
{
if ( currentLayer->removePolygonIntersections( g ) != 0 )
{
returnValue = 3;
}
}
}

//make sure the geometry still has the same type (e.g. no change from polygon to multipolygon)
if ( g->wkbType() != geomTypeBeforeModification )
{
return 2;
}

return returnValue;
}
10 changes: 1 addition & 9 deletions src/app/qgsmaptooladdfeature.h
Expand Up @@ -21,15 +21,7 @@ class QgsMapToolAddFeature: public QgsMapToolCapture
{
Q_OBJECT
public:
QgsMapToolAddFeature( QgsMapCanvas* canvas, enum CaptureMode tool );
QgsMapToolAddFeature( QgsMapCanvas* canvas, CaptureMode mode );
virtual ~QgsMapToolAddFeature();
void canvasReleaseEvent( QMouseEvent * e );

/**Modifies geometry to avoid intersections with the layers specified in project properties
@return 0 in case of success,
@return 1 if geometry is not of polygon type,
@return 2 if avoid intersection would change the geometry type, \
3 other error during intersection removal
@note Consider moving this into analysis lib since it is now used by QgsGpsInformation too. */
static int avoidIntersections( QgsGeometry* g );
};
48 changes: 48 additions & 0 deletions src/core/qgsgeometry.cpp
Expand Up @@ -27,6 +27,10 @@ email : morb at ozemail dot com dot au
#include "qgsrectangle.h"
#include "qgslogger.h"

#include "qgsmaplayerregistry.h"
#include "qgsvectorlayer.h"
#include "qgsproject.h"

#define DEFAULT_QUADRANT_SEGMENTS 8

#define CATCH_GEOS(r) \
Expand Down Expand Up @@ -6210,3 +6214,47 @@ bool QgsGeometry::deletePart( int partNum )

return TRUE;
}

int QgsGeometry::avoidIntersections()
{
int returnValue = 0;

//check if g has polygon type
if ( type() != QGis::Polygon )
{
return 1;
}

QGis::WkbType geomTypeBeforeModification = wkbType();

//read avoid intersections list from project properties
bool listReadOk;
QStringList avoidIntersectionsList = QgsProject::instance()->readListEntry( "Digitizing", "/AvoidIntersectionsList", &listReadOk );
if ( !listReadOk )
{
return true; //no intersections stored in project does not mean error
}

//go through list, convert each layer to vector layer and call QgsVectorLayer::removePolygonIntersections for each
QgsVectorLayer* currentLayer = 0;
QStringList::const_iterator aIt = avoidIntersectionsList.constBegin();
for ( ; aIt != avoidIntersectionsList.constEnd(); ++aIt )
{
currentLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( *aIt ) );
if ( currentLayer )
{
if ( currentLayer->removePolygonIntersections( this ) != 0 )
{
returnValue = 3;
}
}
}

//make sure the geometry still has the same type (e.g. no change from polygon to multipolygon)
if ( wkbType() != geomTypeBeforeModification )
{
return 2;
}

return returnValue;
}
8 changes: 8 additions & 0 deletions src/core/qgsgeometry.h
Expand Up @@ -353,6 +353,14 @@ class CORE_EXPORT QgsGeometry
@return true in case of success and false else*/
bool convertToMultiType();

/** Modifies geometry to avoid intersections with the layers specified in project properties
* @return 0 in case of success,
* 1 if geometry is not of polygon type,
* 2 if avoid intersection would change the geometry type,
* 3 other error during intersection removal
* @note added in 1.5
*/
int avoidIntersections();

private:
// Private variables
Expand Down

0 comments on commit c0da2c6

Please sign in to comment.