Skip to content

Commit 8eb3553

Browse files
committedMar 26, 2017
Optimise QgsGeos conversions
Avoid some unnecessary creation of QgsPointV2
1 parent 314842d commit 8eb3553

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed
 

‎src/core/geometry/qgsgeometry.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,9 +1192,8 @@ QgsPolyline QgsGeometry::asPolyline() const
11921192
polyLine.resize( nVertices );
11931193
for ( int i = 0; i < nVertices; ++i )
11941194
{
1195-
QgsPointV2 pt = line->pointN( i );
1196-
polyLine[i].setX( pt.x() );
1197-
polyLine[i].setY( pt.y() );
1195+
polyLine[i].setX( line->xAt( i ) );
1196+
polyLine[i].setY( line->yAt( i ) );
11981197
}
11991198

12001199
if ( doSegmentation )

‎src/core/geometry/qgsgeos.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,7 @@ int QgsGeos::splitGeometry( const QgsLineString &splitLine,
417417
}
418418
else if ( splitLine.numPoints() == 1 )
419419
{
420-
QgsPointV2 pt = splitLine.pointN( 0 );
421-
splitLineGeos = createGeosPoint( &pt, 2, mPrecision );
420+
splitLineGeos = createGeosPointXY( splitLine.xAt( 0 ), splitLine.yAt( 0 ), false, 0, false, 0, 2, mPrecision );
422421
}
423422
else
424423
{
@@ -1544,33 +1543,31 @@ GEOSCoordSequence *QgsGeos::createCoordinateSequence( const QgsCurve *curve, dou
15441543
{
15451544
for ( int i = 0; i < numOutPoints; ++i )
15461545
{
1547-
const QgsPointV2 &pt = line->pointN( i % numPoints ); //todo: create method to get const point reference
1548-
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, i, qgsRound( pt.x() / precision ) * precision );
1549-
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, i, qgsRound( pt.y() / precision ) * precision );
1546+
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, i, qgsRound( line->xAt( i % numPoints ) / precision ) * precision );
1547+
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, i, qgsRound( line->yAt( i % numPoints ) / precision ) * precision );
15501548
if ( hasZ )
15511549
{
1552-
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 2, qgsRound( pt.z() / precision ) * precision );
1550+
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 2, qgsRound( line->zAt( i % numPoints ) / precision ) * precision );
15531551
}
15541552
if ( hasM )
15551553
{
1556-
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 3, pt.m() );
1554+
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 3, line->mAt( i % numPoints ) );
15571555
}
15581556
}
15591557
}
15601558
else
15611559
{
15621560
for ( int i = 0; i < numOutPoints; ++i )
15631561
{
1564-
const QgsPointV2 &pt = line->pointN( i % numPoints ); //todo: create method to get const point reference
1565-
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, i, pt.x() );
1566-
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, i, pt.y() );
1562+
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, i, line->xAt( i % numPoints ) );
1563+
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, i, line->yAt( i % numPoints ) );
15671564
if ( hasZ )
15681565
{
1569-
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 2, pt.z() );
1566+
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 2, line->zAt( i % numPoints ) );
15701567
}
15711568
if ( hasM )
15721569
{
1573-
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 3, pt.m() );
1570+
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 3, line->mAt( i % numPoints ) );
15741571
}
15751572
}
15761573
}
@@ -1590,6 +1587,14 @@ GEOSGeometry *QgsGeos::createGeosPoint( const QgsAbstractGeometry *point, int co
15901587
if ( !pt )
15911588
return nullptr;
15921589

1590+
return createGeosPointXY( pt->x(), pt->y(), pt->is3D(), pt->z(), pt->isMeasure(), pt->m(), coordDims, precision );
1591+
}
1592+
1593+
GEOSGeometry *QgsGeos::createGeosPointXY( double x, double y, bool hasZ, double z, bool hasM, double m, int coordDims, double precision )
1594+
{
1595+
Q_UNUSED( hasM );
1596+
Q_UNUSED( m );
1597+
15931598
GEOSGeometry *geosPoint = nullptr;
15941599

15951600
try
@@ -1602,26 +1607,26 @@ GEOSGeometry *QgsGeos::createGeosPoint( const QgsAbstractGeometry *point, int co
16021607
}
16031608
if ( precision > 0. )
16041609
{
1605-
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, 0, qgsRound( pt->x() / precision ) * precision );
1606-
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, 0, qgsRound( pt->y() / precision ) * precision );
1607-
if ( pt->is3D() )
1610+
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, 0, qgsRound( x / precision ) * precision );
1611+
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, 0, qgsRound( y / precision ) * precision );
1612+
if ( hasZ )
16081613
{
1609-
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, 0, 2, qgsRound( pt->z() / precision ) * precision );
1614+
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, 0, 2, qgsRound( z / precision ) * precision );
16101615
}
16111616
}
16121617
else
16131618
{
1614-
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, 0, pt->x() );
1615-
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, 0, pt->y() );
1616-
if ( pt->is3D() )
1619+
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, 0, x );
1620+
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, 0, y );
1621+
if ( hasZ )
16171622
{
1618-
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, 0, 2, pt->z() );
1623+
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, 0, 2, z );
16191624
}
16201625
}
16211626
#if 0 //disabled until geos supports m-coordinates
1622-
if ( pt->isMeasure() )
1627+
if ( hasM )
16231628
{
1624-
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, 0, 3, pt->m() );
1629+
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, 0, 3, m );
16251630
}
16261631
#endif
16271632
geosPoint = GEOSGeom_createPoint_r( geosinit.ctxt, coordSeq );
@@ -2165,10 +2170,8 @@ GEOSGeometry *QgsGeos::reshapeLine( const GEOSGeometry *line, const GEOSGeometry
21652170
if ( !_linestringEndpoints( line, x1, y1, x2, y2 ) )
21662171
return nullptr;
21672172

2168-
QgsPointV2 beginPoint( x1, y1 );
2169-
GEOSGeometry *beginLineVertex = createGeosPoint( &beginPoint, 2, precision );
2170-
QgsPointV2 endPoint( x2, y2 );
2171-
GEOSGeometry *endLineVertex = createGeosPoint( &endPoint, 2, precision );
2173+
GEOSGeometry *beginLineVertex = createGeosPointXY( x1, y1, false, 0, false, 0, 2, precision );
2174+
GEOSGeometry *endLineVertex = createGeosPointXY( x2, y2, false, 0, false, 0, 2, precision );
21722175

21732176
bool isRing = false;
21742177
if ( GEOSGeomTypeId_r( geosinit.ctxt, line ) == GEOS_LINEARRING
@@ -2225,10 +2228,8 @@ GEOSGeometry *QgsGeos::reshapeLine( const GEOSGeometry *line, const GEOSGeometry
22252228
GEOSCoordSeq_getY_r( geosinit.ctxt, currentCoordSeq, 0, &yBegin );
22262229
GEOSCoordSeq_getX_r( geosinit.ctxt, currentCoordSeq, currentCoordSeqSize - 1, &xEnd );
22272230
GEOSCoordSeq_getY_r( geosinit.ctxt, currentCoordSeq, currentCoordSeqSize - 1, &yEnd );
2228-
QgsPointV2 beginPoint( xBegin, yBegin );
2229-
GEOSGeometry *beginCurrentGeomVertex = createGeosPoint( &beginPoint, 2, precision );
2230-
QgsPointV2 endPoint( xEnd, yEnd );
2231-
GEOSGeometry *endCurrentGeomVertex = createGeosPoint( &endPoint, 2, precision );
2231+
GEOSGeometry *beginCurrentGeomVertex = createGeosPointXY( xBegin, yBegin, false, 0, false, 0, 2, precision );
2232+
GEOSGeometry *endCurrentGeomVertex = createGeosPointXY( xEnd, yEnd, false, 0, false, 0, 2, precision );
22322233

22332234
//check how many endpoints of the line merge result are on the (original) line
22342235
int nEndpointsOnOriginalLine = 0;

‎src/core/geometry/qgsgeos.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class CORE_EXPORT QgsGeos: public QgsGeometryEngine
190190

191191
static GEOSContextHandle_t getGEOSHandler();
192192

193+
193194
private:
194195
mutable GEOSGeometry *mGeos;
195196
const GEOSPreparedGeometry *mGeosPrepared = nullptr;
@@ -228,6 +229,7 @@ class CORE_EXPORT QgsGeos: public QgsGeometryEngine
228229
*/
229230
static GEOSGeometry *createGeosCollection( int typeId, const QVector<GEOSGeometry *> &geoms );
230231

232+
static GEOSGeometry *createGeosPointXY( double x, double y, bool hasZ, double z, bool hasM, double m, int coordDims, double precision );
231233
static GEOSGeometry *createGeosPoint( const QgsAbstractGeometry *point, int coordDims, double precision );
232234
static GEOSGeometry *createGeosLinestring( const QgsAbstractGeometry *curve, double precision );
233235
static GEOSGeometry *createGeosPolygon( const QgsAbstractGeometry *poly, double precision );

0 commit comments

Comments
 (0)
Please sign in to comment.