Skip to content
Permalink
Browse files

Modernize some geometry code

  • Loading branch information
nyalldawson committed Sep 26, 2017
1 parent cc4fb39 commit ac7fede91a4d4e90f52de8cd6b794ee56f402aef
@@ -26,6 +26,7 @@
#include "qgslogger.h"
#include <QPainter>
#include <QPainterPath>
#include <memory>

QgsCircularString::QgsCircularString(): QgsCurve()
{
@@ -301,9 +302,8 @@ QString QgsCircularString::asWkt( int precision ) const
QDomElement QgsCircularString::asGML2( QDomDocument &doc, int precision, const QString &ns ) const
{
// GML2 does not support curves
QgsLineString *line = curveToLine();
std::unique_ptr< QgsLineString > line( curveToLine() );
QDomElement gml = line->asGML2( doc, precision, ns );
delete line;
return gml;
}

@@ -324,9 +324,8 @@ QDomElement QgsCircularString::asGML3( QDomDocument &doc, int precision, const Q
QString QgsCircularString::asJSON( int precision ) const
{
// GeoJSON does not support curves
QgsLineString *line = curveToLine();
std::unique_ptr< QgsLineString > line( curveToLine() );
QString json = line->asJSON( precision );
delete line;
return json;
}

@@ -23,7 +23,7 @@
#include "qgswkbptr.h"
#include <QPainter>
#include <QPainterPath>

#include <memory>

QgsCompoundCurve::QgsCompoundCurve(): QgsCurve()
{
@@ -263,9 +263,8 @@ QString QgsCompoundCurve::asWkt( int precision ) const
QDomElement QgsCompoundCurve::asGML2( QDomDocument &doc, int precision, const QString &ns ) const
{
// GML2 does not support curves
QgsLineString *line = curveToLine();
std::unique_ptr< QgsLineString > line( curveToLine() );
QDomElement gml = line->asGML2( doc, precision, ns );
delete line;
return gml;
}

@@ -286,9 +285,8 @@ QDomElement QgsCompoundCurve::asGML3( QDomDocument &doc, int precision, const QS
QString QgsCompoundCurve::asJSON( int precision ) const
{
// GeoJSON does not support curves
QgsLineString *line = curveToLine();
std::unique_ptr< QgsLineString > line( curveToLine() );
QString json = line->asJSON( precision );
delete line;
return json;
}

@@ -373,12 +371,11 @@ QgsLineString *QgsCompoundCurve::curveToLine( double tolerance, SegmentationTole
{
QList< QgsCurve * >::const_iterator curveIt = mCurves.constBegin();
QgsLineString *line = new QgsLineString();
QgsLineString *currentLine = nullptr;
std::unique_ptr< QgsLineString > currentLine;
for ( ; curveIt != mCurves.constEnd(); ++curveIt )
{
currentLine = ( *curveIt )->curveToLine( tolerance, toleranceType );
line->append( currentLine );
delete currentLine;
currentLine.reset( ( *curveIt )->curveToLine( tolerance, toleranceType ) );
line->append( currentLine.get() );
}
return line;
}
@@ -333,20 +333,19 @@ QDomElement QgsCurvePolygon::asGML2( QDomDocument &doc, int precision, const QSt
// GML2 does not support curves
QDomElement elemPolygon = doc.createElementNS( ns, QStringLiteral( "Polygon" ) );
QDomElement elemOuterBoundaryIs = doc.createElementNS( ns, QStringLiteral( "outerBoundaryIs" ) );
QgsLineString *exteriorLineString = exteriorRing()->curveToLine();
std::unique_ptr< QgsLineString > exteriorLineString( exteriorRing()->curveToLine() );
QDomElement outerRing = exteriorLineString->asGML2( doc, precision, ns );
outerRing.toElement().setTagName( QStringLiteral( "LinearRing" ) );
elemOuterBoundaryIs.appendChild( outerRing );
delete exteriorLineString;
elemPolygon.appendChild( elemOuterBoundaryIs );
std::unique_ptr< QgsLineString > interiorLineString;
for ( int i = 0, n = numInteriorRings(); i < n; ++i )
{
QDomElement elemInnerBoundaryIs = doc.createElementNS( ns, QStringLiteral( "innerBoundaryIs" ) );
QgsLineString *interiorLineString = interiorRing( i )->curveToLine();
interiorLineString.reset( interiorRing( i )->curveToLine() );
QDomElement innerRing = interiorLineString->asGML2( doc, precision, ns );
innerRing.toElement().setTagName( QStringLiteral( "LinearRing" ) );
elemInnerBoundaryIs.appendChild( innerRing );
delete interiorLineString;
elemPolygon.appendChild( elemInnerBoundaryIs );
}
return elemPolygon;
@@ -383,19 +382,18 @@ QString QgsCurvePolygon::asJSON( int precision ) const
// GeoJSON does not support curves
QString json = QStringLiteral( "{\"type\": \"Polygon\", \"coordinates\": [" );

QgsLineString *exteriorLineString = exteriorRing()->curveToLine();
std::unique_ptr< QgsLineString > exteriorLineString( exteriorRing()->curveToLine() );
QgsPointSequence exteriorPts;
exteriorLineString->points( exteriorPts );
json += QgsGeometryUtils::pointsToJSON( exteriorPts, precision ) + ", ";
delete exteriorLineString;

std::unique_ptr< QgsLineString > interiorLineString;
for ( int i = 0, n = numInteriorRings(); i < n; ++i )
{
QgsLineString *interiorLineString = interiorRing( i )->curveToLine();
interiorLineString.reset( interiorRing( i )->curveToLine() );
QgsPointSequence interiorPts;
interiorLineString->points( interiorPts );
json += QgsGeometryUtils::pointsToJSON( interiorPts, precision ) + ", ";
delete interiorLineString;
}
if ( json.endsWith( QLatin1String( ", " ) ) )
{
@@ -274,15 +274,14 @@ std::unique_ptr<QgsAbstractGeometry> QgsGeometryEditUtils::avoidIntersections( c
}


QgsAbstractGeometry *combinedGeometries = geomEngine->combine( nearGeometries );
std::unique_ptr< QgsAbstractGeometry > combinedGeometries( geomEngine->combine( nearGeometries ) );
qDeleteAll( nearGeometries );
if ( !combinedGeometries )
{
return nullptr;
}

std::unique_ptr< QgsAbstractGeometry > diffGeom( geomEngine->difference( combinedGeometries ) );
std::unique_ptr< QgsAbstractGeometry > diffGeom( geomEngine->difference( combinedGeometries.get() ) );

delete combinedGeometries;
return diffGeom;
}
@@ -708,14 +708,14 @@ GEOSGeometry *QgsGeos::linePointDifference( GEOSGeometry *GEOSsplitPoint ) const
{
int type = GEOSGeomTypeId_r( geosinit.ctxt, mGeos );

QgsMultiCurve *multiCurve = nullptr;
std::unique_ptr< QgsMultiCurve > multiCurve;
if ( type == GEOS_MULTILINESTRING )
{
multiCurve = qgsgeometry_cast<QgsMultiCurve *>( mGeometry->clone() );
multiCurve.reset( qgsgeometry_cast<QgsMultiCurve *>( mGeometry->clone() ) );
}
else if ( type == GEOS_LINESTRING )
{
multiCurve = new QgsMultiCurve();
multiCurve.reset( new QgsMultiCurve() );
multiCurve->addGeometry( mGeometry->clone() );
}
else
@@ -729,11 +729,10 @@ GEOSGeometry *QgsGeos::linePointDifference( GEOSGeometry *GEOSsplitPoint ) const
}


QgsAbstractGeometry *splitGeom = fromGeos( GEOSsplitPoint );
QgsPoint *splitPoint = qgsgeometry_cast<QgsPoint *>( splitGeom );
std::unique_ptr< QgsAbstractGeometry > splitGeom( fromGeos( GEOSsplitPoint ) );
QgsPoint *splitPoint = qgsgeometry_cast<QgsPoint *>( splitGeom.get() );
if ( !splitPoint )
{
delete splitGeom;
return nullptr;
}

@@ -765,8 +764,6 @@ GEOSGeometry *QgsGeos::linePointDifference( GEOSGeometry *GEOSsplitPoint ) const
}
}

delete splitGeom;
delete multiCurve;
return asGeos( &lines, mPrecision );
}

@@ -1698,13 +1695,13 @@ bool QgsGeos::isSimple( QString *errorMsg ) const

GEOSCoordSequence *QgsGeos::createCoordinateSequence( const QgsCurve *curve, double precision, bool forceClose )
{
bool segmentize = false;
std::unique_ptr< QgsLineString > segmentized;
const QgsLineString *line = qgsgeometry_cast<const QgsLineString *>( curve );

if ( !line )
{
line = curve->curveToLine();
segmentize = true;
segmentized.reset( curve->curveToLine() );
line = segmentized.get();
}

if ( !line )
@@ -1776,10 +1773,6 @@ GEOSCoordSequence *QgsGeos::createCoordinateSequence( const QgsCurve *curve, dou
}
CATCH_GEOS( nullptr )

if ( segmentize )
{
delete line;
}
return coordSeq;
}

@@ -64,23 +64,22 @@ QgsGeometry QgsInternalGeometryEngine::extrude( double x, double y ) const

if ( !linesToProcess.empty() )
{
std::unique_ptr< QgsLineString > secondline;
for ( QgsLineString *line : qgsAsConst( linesToProcess ) )
{
QTransform transform = QTransform::fromTranslate( x, y );

QgsLineString *secondline = line->reversed();
secondline.reset( line->reversed() );
secondline->transform( transform );

line->append( secondline );
line->append( secondline.get() );
line->addVertex( line->pointN( 0 ) );

polygon = new QgsPolygonV2();
polygon->setExteriorRing( line );

if ( multipolygon )
multipolygon->addGeometry( polygon );

delete secondline;
}

if ( multipolygon )
@@ -21,6 +21,7 @@ email : marco.hugentobler at sourcepole dot com
#include "qgsgeometryutils.h"
#include "qgslinestring.h"
#include "qgsmultipoint.h"
#include <memory>

QgsMultiCurve::QgsMultiCurve()
: QgsGeometryCollection()
@@ -64,13 +65,11 @@ QDomElement QgsMultiCurve::asGML2( QDomDocument &doc, int precision, const QStri
{
if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
{
QgsLineString *lineString = static_cast<const QgsCurve *>( geom )->curveToLine();
std::unique_ptr< QgsLineString > lineString( static_cast<const QgsCurve *>( geom )->curveToLine() );

QDomElement elemLineStringMember = doc.createElementNS( ns, QStringLiteral( "lineStringMember" ) );
elemLineStringMember.appendChild( lineString->asGML2( doc, precision, ns ) );
elemMultiLineString.appendChild( elemLineStringMember );

delete lineString;
}
}

@@ -103,11 +102,10 @@ QString QgsMultiCurve::asJSON( int precision ) const
{
if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
{
QgsLineString *lineString = static_cast<const QgsCurve *>( geom )->curveToLine();
std::unique_ptr< QgsLineString > lineString( static_cast<const QgsCurve *>( geom )->curveToLine() );
QgsPointSequence pts;
lineString->points( pts );
json += QgsGeometryUtils::pointsToJSON( pts, precision ) + ", ";
delete lineString;
}
}
if ( json.endsWith( QLatin1String( ", " ) ) )
@@ -168,7 +166,7 @@ QgsMultiCurve *QgsMultiCurve::reversed() const

QgsAbstractGeometry *QgsMultiCurve::boundary() const
{
QgsMultiPointV2 *multiPoint = new QgsMultiPointV2();
std::unique_ptr< QgsMultiPointV2 > multiPoint( new QgsMultiPointV2() );
for ( int i = 0; i < mGeometries.size(); ++i )
{
if ( QgsCurve *curve = qgsgeometry_cast<QgsCurve *>( mGeometries.at( i ) ) )
@@ -182,8 +180,7 @@ QgsAbstractGeometry *QgsMultiCurve::boundary() const
}
if ( multiPoint->numGeometries() == 0 )
{
delete multiPoint;
return nullptr;
}
return multiPoint;
return multiPoint.release();
}
@@ -94,19 +94,18 @@ QString QgsMultiPolygonV2::asJSON( int precision ) const

const QgsPolygonV2 *polygon = static_cast<const QgsPolygonV2 *>( geom );

QgsLineString *exteriorLineString = polygon->exteriorRing()->curveToLine();
std::unique_ptr< QgsLineString > exteriorLineString( polygon->exteriorRing()->curveToLine() );
QgsPointSequence exteriorPts;
exteriorLineString->points( exteriorPts );
json += QgsGeometryUtils::pointsToJSON( exteriorPts, precision ) + ", ";
delete exteriorLineString;

std::unique_ptr< QgsLineString > interiorLineString;
for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
{
QgsLineString *interiorLineString = polygon->interiorRing( i )->curveToLine();
interiorLineString.reset( polygon->interiorRing( i )->curveToLine() );
QgsPointSequence interiorPts;
interiorLineString->points( interiorPts );
json += QgsGeometryUtils::pointsToJSON( interiorPts, precision ) + ", ";
delete interiorLineString;
}
if ( json.endsWith( QLatin1String( ", " ) ) )
{
@@ -171,7 +170,7 @@ QgsMultiSurface *QgsMultiPolygonV2::toCurveType() const

QgsAbstractGeometry *QgsMultiPolygonV2::boundary() const
{
QgsMultiLineString *multiLine = new QgsMultiLineString();
std::unique_ptr< QgsMultiLineString > multiLine( new QgsMultiLineString() );
for ( int i = 0; i < mGeometries.size(); ++i )
{
if ( QgsPolygonV2 *polygon = qgsgeometry_cast<QgsPolygonV2 *>( mGeometries.at( i ) ) )
@@ -198,10 +197,9 @@ QgsAbstractGeometry *QgsMultiPolygonV2::boundary() const
}
if ( multiLine->numGeometries() == 0 )
{
delete multiLine;
return nullptr;
}
return multiLine;
return multiLine.release();
}

bool QgsMultiPolygonV2::wktOmitChildType() const

0 comments on commit ac7fede

Please sign in to comment.
You can’t perform that action at this time.