Skip to content

Commit 9d38b71

Browse files
committedJun 4, 2015
Remove code duplication in geos import
1 parent 71ceab8 commit 9d38b71

File tree

2 files changed

+27
-55
lines changed

2 files changed

+27
-55
lines changed
 

‎src/core/geometry/qgsgeos.cpp

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -731,20 +731,32 @@ int QgsGeos::mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult
731731
return 0;
732732
}
733733

734-
GEOSGeometry* QgsGeos::createGeosCollection( int typeId, QVector<GEOSGeometry*> geoms ) const
734+
GEOSGeometry* QgsGeos::createGeosCollection( int typeId, const QVector<GEOSGeometry*>& geoms )
735735
{
736-
GEOSGeometry **geomarr = new GEOSGeometry*[ geoms.size()];
736+
int nNullGeoms = geoms.count( 0 );
737+
int nNotNullGeoms = geoms.size() - nNullGeoms;
738+
739+
GEOSGeometry **geomarr = new GEOSGeometry*[ nNotNullGeoms ];
737740
if ( !geomarr )
741+
{
738742
return 0;
743+
}
739744

740-
for ( int i = 0; i < geoms.size(); i++ )
741-
geomarr[i] = geoms[i];
742-
745+
int i = 0;
746+
QVector<GEOSGeometry*>::const_iterator geomIt = geoms.constBegin();
747+
for ( ; geomIt != geoms.constEnd(); ++geomIt )
748+
{
749+
if ( *geomIt )
750+
{
751+
geomarr[i] = *geomIt;
752+
++i;
753+
}
754+
}
743755
GEOSGeometry *geom = 0;
744756

745757
try
746758
{
747-
geom = GEOSGeom_createCollection_r( geosinit.ctxt, typeId, geomarr, geoms.size() );
759+
geom = GEOSGeom_createCollection_r( geosinit.ctxt, typeId, geomarr, nNotNullGeoms );
748760
}
749761
catch ( GEOSException &e )
750762
{
@@ -973,24 +985,13 @@ GEOSGeometry* QgsGeos::asGeos( const QgsAbstractGeometryV2* geom )
973985
{
974986
return createGeosPoint( geom, coordDims );
975987
}
976-
else if ( geom->geometryType() == "MultiPoint" )
977-
{
978-
const QgsGeometryCollectionV2* c = dynamic_cast<const QgsGeometryCollectionV2*>( geom );
979-
if ( !c )
980-
{
981-
return 0;
982-
}
983-
984-
GEOSGeometry **geomarr = new GEOSGeometry*[ c->numGeometries()];
985-
for ( int i = 0; i < c->numGeometries(); ++i )
986-
{
987-
geomarr[i] = createGeosPoint( c->geometryN( i ), coordDims );
988-
}
989-
return GEOSGeom_createCollection_r( geosinit.ctxt, GEOS_MULTIPOINT, geomarr, c->numGeometries() ); //todo: geos exceptions
990-
}
991988
else if ( QgsWKBTypes::isMultiType( geom->wkbType() ) )
992989
{
993990
int geosType = GEOS_MULTIPOINT;
991+
if ( geom->geometryType() == "MultiPoint" )
992+
{
993+
geosType = GEOS_MULTIPOINT;
994+
}
994995
if ( geom->geometryType() == "MultiCurve" || geom->geometryType() == "MultiLineString" )
995996
{
996997
geosType = GEOS_MULTILINESTRING;
@@ -1010,41 +1011,12 @@ GEOSGeometry* QgsGeos::asGeos( const QgsAbstractGeometryV2* geom )
10101011
return 0;
10111012
}
10121013

1013-
try
1014-
{
1015-
1016-
//GEOSGeometry **geomarr = new GEOSGeometry*[ c->numGeometries()];
1017-
QList< GEOSGeometry* > validGeoms;
1018-
GEOSGeometry* geosGeom = 0;
1019-
for ( int i = 0; i < c->numGeometries(); ++i )
1020-
{
1021-
geosGeom = asGeos( c->geometryN( i ) );
1022-
if ( geosGeom )
1023-
{
1024-
validGeoms.append( geosGeom );
1025-
}
1026-
}
1027-
1028-
if ( validGeoms.size() < 1 )
1029-
{
1030-
return 0;
1031-
}
1032-
1033-
GEOSGeometry **geomarr = new GEOSGeometry*[ validGeoms.size()];
1034-
for ( int i = 0; i < validGeoms.size(); ++i )
1035-
{
1036-
geomarr[i] = validGeoms.at( i );
1037-
}
1038-
1039-
return GEOSGeom_createCollection_r( geosinit.ctxt, geosType, geomarr, validGeoms.size() );
1040-
}
1041-
1042-
catch ( GEOSException &e )
1014+
QVector< GEOSGeometry* > geomVector( c->numGeometries() );
1015+
for ( int i = 0; i < c->numGeometries(); ++i )
10431016
{
1044-
Q_UNUSED( e );
1045-
//delete?
1046-
return 0;
1017+
geomVector[i] = asGeos( c->geometryN( i ) );
10471018
}
1019+
return createGeosCollection( geosType, geomVector );
10481020
}
10491021

10501022
return 0;

‎src/core/geometry/qgsgeos.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class CORE_EXPORT QgsGeos: public QgsGeometryEngine
112112
static int numberOfGeometries( GEOSGeometry* g );
113113
static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *geom );
114114
int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult ) const;
115-
GEOSGeometry* createGeosCollection( int typeId, QVector<GEOSGeometry*> geoms ) const;
115+
static GEOSGeometry* createGeosCollection( int typeId, const QVector<GEOSGeometry*>& geoms );
116116

117117
static GEOSGeometry* createGeosPoint( const QgsAbstractGeometryV2* point, int coordDims );
118118
static GEOSGeometry* createGeosLinestring( const QgsAbstractGeometryV2* curve );

0 commit comments

Comments
 (0)
Please sign in to comment.