Skip to content

Commit

Permalink
Convert added/changed geometries into the right before sending back t…
Browse files Browse the repository at this point in the history
…o provider
  • Loading branch information
mhugent committed Apr 27, 2016
1 parent b758a8c commit 679e8f2
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core/geometry/qgsabstractgeometryv2.h
Expand Up @@ -292,6 +292,11 @@ class CORE_EXPORT QgsAbstractGeometryV2
*/
virtual QgsAbstractGeometryV2* segmentize() const { return clone(); }

/** Returns the geometry converted to the more generic curve type.
E.g. QgsLineStringV2 -> QgsCompoundCurveV2, QgsPolygonV2 -> QgsCurvePolygonV2,
QgsMultiLineStringV2 -> QgsMultiCurveV2, QgsMultiPolygonV2 -> QgsMultiSurfaceV2*/
virtual QgsAbstractGeometryV2* toCurveType() const { return 0; }

/** Returns approximate angle at a vertex. This is usually the average angle between adjacent
* segments, and can be pictured as the orientation of a line following the curvature of the
* geometry at the specified vertex.
Expand Down
5 changes: 5 additions & 0 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -132,6 +132,11 @@ QgsAbstractGeometryV2* QgsGeometry::geometry() const

void QgsGeometry::setGeometry( QgsAbstractGeometryV2* geometry )
{
if ( d->geometry == geometry )
{
return;
}

detach( false );
if ( d->geometry )
{
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgslinestringv2.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgslinestringv2.h"
#include "qgsapplication.h"
#include "qgscompoundcurvev2.h"
#include "qgscoordinatetransform.h"
#include "qgsgeometryutils.h"
#include "qgsmaptopixel.h"
Expand Down Expand Up @@ -583,6 +584,13 @@ QPolygonF QgsLineStringV2::asQPolygonF() const
return points;
}

QgsAbstractGeometryV2* QgsLineStringV2::toCurveType() const
{
QgsCompoundCurveV2* compoundCurve = new QgsCompoundCurveV2();
compoundCurve->addCurve( clone() );
return compoundCurve;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests.
Expand Down
3 changes: 3 additions & 0 deletions src/core/geometry/qgslinestringv2.h
Expand Up @@ -132,6 +132,9 @@ class CORE_EXPORT QgsLineStringV2: public QgsCurveV2
*/
QPolygonF asQPolygonF() const;

/** Returns the geometry converted to QgsCompoundCurveV2*/
QgsAbstractGeometryV2* toCurveType() const override;

//reimplemented methods

virtual QString geometryType() const override { return "LineString"; }
Expand Down
11 changes: 11 additions & 0 deletions src/core/geometry/qgsmultilinestringv2.cpp
Expand Up @@ -20,6 +20,7 @@ email : marco.hugentobler at sourcepole dot com
#include "qgscompoundcurvev2.h"
#include "qgsgeometryutils.h"
#include "qgslinestringv2.h"
#include "qgsmulticurvev2.h"

QgsMultiLineStringV2* QgsMultiLineStringV2::clone() const
{
Expand Down Expand Up @@ -101,3 +102,13 @@ bool QgsMultiLineStringV2::addGeometry( QgsAbstractGeometryV2* g )
setZMTypeFromSubGeometry( g, QgsWKBTypes::MultiLineString );
return QgsGeometryCollectionV2::addGeometry( g );
}

QgsAbstractGeometryV2* QgsMultiLineStringV2::toCurveType() const
{
QgsMultiCurveV2* multiCurve = new QgsMultiCurveV2();
for ( int i = 0; i < mGeometries.size(); ++i )
{
multiCurve->addGeometry( mGeometries.at( i )->clone() );
}
return multiCurve;
}
3 changes: 3 additions & 0 deletions src/core/geometry/qgsmultilinestringv2.h
Expand Up @@ -42,6 +42,9 @@ class CORE_EXPORT QgsMultiLineStringV2: public QgsMultiCurveV2
/** Adds a geometry and takes ownership. Returns true in case of success*/
virtual bool addGeometry( QgsAbstractGeometryV2* g ) override;

/** Returns the geometry converted to QgsMultiCurveV2*/
QgsAbstractGeometryV2* toCurveType() const override;

protected:

virtual bool wktOmitChildType() const override { return true; }
Expand Down
10 changes: 10 additions & 0 deletions src/core/geometry/qgsmultipolygonv2.cpp
Expand Up @@ -117,3 +117,13 @@ bool QgsMultiPolygonV2::addGeometry( QgsAbstractGeometryV2* g )
setZMTypeFromSubGeometry( g, QgsWKBTypes::MultiPolygon );
return QgsGeometryCollectionV2::addGeometry( g );
}

QgsAbstractGeometryV2* QgsMultiPolygonV2::toCurveType() const
{
QgsMultiSurfaceV2* multiSurface = new QgsMultiSurfaceV2();
for ( int i = 0; i < mGeometries.size(); ++i )
{
multiSurface->addGeometry( mGeometries.at( i )->clone() );
}
return multiSurface;
}
3 changes: 3 additions & 0 deletions src/core/geometry/qgsmultipolygonv2.h
Expand Up @@ -43,6 +43,9 @@ class CORE_EXPORT QgsMultiPolygonV2: public QgsMultiSurfaceV2
/** Adds a geometry and takes ownership. Returns true in case of success*/
virtual bool addGeometry( QgsAbstractGeometryV2* g ) override;

/** Returns the geometry converted to QgsMultiSurfaceV2*/
QgsAbstractGeometryV2* toCurveType() const override;

protected:

virtual bool wktOmitChildType() const override { return true; }
Expand Down
12 changes: 12 additions & 0 deletions src/core/geometry/qgspolygonv2.cpp
Expand Up @@ -240,3 +240,15 @@ QgsPolygonV2* QgsPolygonV2::surfaceToPolygon() const
{
return clone();
}

QgsAbstractGeometryV2* QgsPolygonV2::toCurveType() const
{
QgsCurvePolygonV2* curvePolygon = new QgsCurvePolygonV2();
curvePolygon->setExteriorRing( mExteriorRing->clone() );
int nInteriorRings = mInteriorRings.size();
for ( int i = 0; i < nInteriorRings; ++i )
{
curvePolygon->addInteriorRing( mInteriorRings.at( i )->clone() );
}
return curvePolygon;
}
3 changes: 3 additions & 0 deletions src/core/geometry/qgspolygonv2.h
Expand Up @@ -50,6 +50,9 @@ class CORE_EXPORT QgsPolygonV2: public QgsCurvePolygonV2

QgsPolygonV2* surfaceToPolygon() const override;

/** Returns the geometry converted to QgsCurvePolygonV2*/
QgsAbstractGeometryV2* toCurveType() const override;

void addInteriorRing( QgsCurveV2* ring ) override;
//overridden to handle LineString25D rings
virtual void setExteriorRing( QgsCurveV2* ring ) override;
Expand Down
83 changes: 83 additions & 0 deletions src/core/qgsvectorlayereditbuffer.cpp
Expand Up @@ -15,7 +15,13 @@
#include "qgsvectorlayereditbuffer.h"

#include "qgsgeometry.h"
#include "qgscurvepolygonv2.h"
#include "qgscurvev2.h"
#include "qgsgeometrycollectionv2.h"
#include "qgsgeometryfactory.h"
#include "qgsgeometryutils.h"
#include "qgslogger.h"
#include "qgspolygonv2.h"
#include "qgsvectorlayerundocommand.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
Expand Down Expand Up @@ -270,6 +276,23 @@ bool QgsVectorLayerEditBuffer::commitChanges( QStringList& commitErrors )
int cap = provider->capabilities();
bool success = true;

//convert added features to a type accepted by the backend
QgsFeatureMap::iterator addedIt = mAddedFeatures.begin();
for ( ; addedIt != mAddedFeatures.end(); ++addedIt )
{
QgsGeometry* geom = addedIt.value().geometry();
if ( geom )
{
geom->setGeometry( outputGeometry( geom->geometry() ) );
}
}

QgsGeometryMap::iterator changedIt = mChangedGeometries.begin();
for ( ; changedIt != mChangedGeometries.end(); ++changedIt )
{
changedIt.value().setGeometry( outputGeometry( changedIt.value().geometry() ) );
}

// geometry updates attribute updates
// yes no => changeGeometryValues
// no yes => changeAttributeValues
Expand Down Expand Up @@ -666,3 +689,63 @@ void QgsVectorLayerEditBuffer::updateLayerFields()
{
L->updateFields();
}

QgsAbstractGeometryV2* QgsVectorLayerEditBuffer::outputGeometry( QgsAbstractGeometryV2* geom ) const
{
if ( !geom || !L )
{
delete geom;
return nullptr;
}

QgsWKBTypes::Type providerGeomType = QGis::fromOldWkbType( L->dataProvider()->geometryType() );

if ( geom->wkbType() == providerGeomType )
{
return geom;
}

QgsAbstractGeometryV2* outputGeom = geom;

//convert to multitype if necessary
if ( QgsWKBTypes::isMultiType( providerGeomType ) && !QgsWKBTypes::isMultiType( geom->wkbType() ) )
{
outputGeom = QgsGeometryFactory::geomFromWkbType( providerGeomType );
QgsGeometryCollectionV2* geomCollection = dynamic_cast<QgsGeometryCollectionV2*>( outputGeom );
if ( geomCollection )
{
geomCollection->addGeometry( geom );
}
}

//convert to curved type if necessary
if ( !QgsWKBTypes::isCurvedType( outputGeom->wkbType() ) && QgsWKBTypes::isCurvedType( providerGeomType ) )
{
QgsAbstractGeometryV2* curveGeom = outputGeom->toCurveType();
if ( curveGeom )
{
outputGeom = curveGeom;
}
}

//convert to linear type from curved type
if ( QgsWKBTypes::isCurvedType( outputGeom->wkbType() ) && !QgsWKBTypes::isCurvedType( providerGeomType ) )
{
QgsAbstractGeometryV2* segmentizedGeom = outputGeom->segmentize();
if ( segmentizedGeom )
{
outputGeom = segmentizedGeom;
}
}

//set z/m types
if ( QgsWKBTypes::hasZ( providerGeomType ) )
{
outputGeom->addZValue();
}
if ( QgsWKBTypes::hasM( providerGeomType ) )
{
outputGeom->addMValue();
}
return outputGeom;
}
2 changes: 2 additions & 0 deletions src/core/qgsvectorlayereditbuffer.h
Expand Up @@ -156,6 +156,8 @@ class CORE_EXPORT QgsVectorLayerEditBuffer : public QObject

void updateLayerFields();

QgsAbstractGeometryV2* outputGeometry( QgsAbstractGeometryV2* geom ) const;

protected:
QgsVectorLayer* L;
friend class QgsVectorLayer;
Expand Down

0 comments on commit 679e8f2

Please sign in to comment.