@@ -731,20 +731,32 @@ int QgsGeos::mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult
731
731
return 0 ;
732
732
}
733
733
734
- GEOSGeometry* QgsGeos::createGeosCollection ( int typeId, QVector<GEOSGeometry*> geoms ) const
734
+ GEOSGeometry* QgsGeos::createGeosCollection ( int typeId, const QVector<GEOSGeometry*>& geoms )
735
735
{
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 ];
737
740
if ( !geomarr )
741
+ {
738
742
return 0 ;
743
+ }
739
744
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
+ }
743
755
GEOSGeometry *geom = 0 ;
744
756
745
757
try
746
758
{
747
- geom = GEOSGeom_createCollection_r ( geosinit.ctxt , typeId, geomarr, geoms. size () );
759
+ geom = GEOSGeom_createCollection_r ( geosinit.ctxt , typeId, geomarr, nNotNullGeoms );
748
760
}
749
761
catch ( GEOSException &e )
750
762
{
@@ -973,24 +985,13 @@ GEOSGeometry* QgsGeos::asGeos( const QgsAbstractGeometryV2* geom )
973
985
{
974
986
return createGeosPoint ( geom, coordDims );
975
987
}
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
- }
991
988
else if ( QgsWKBTypes::isMultiType ( geom->wkbType () ) )
992
989
{
993
990
int geosType = GEOS_MULTIPOINT;
991
+ if ( geom->geometryType () == " MultiPoint" )
992
+ {
993
+ geosType = GEOS_MULTIPOINT;
994
+ }
994
995
if ( geom->geometryType () == " MultiCurve" || geom->geometryType () == " MultiLineString" )
995
996
{
996
997
geosType = GEOS_MULTILINESTRING;
@@ -1010,41 +1011,12 @@ GEOSGeometry* QgsGeos::asGeos( const QgsAbstractGeometryV2* geom )
1010
1011
return 0 ;
1011
1012
}
1012
1013
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 )
1043
1016
{
1044
- Q_UNUSED ( e );
1045
- // delete?
1046
- return 0 ;
1017
+ geomVector[i] = asGeos ( c->geometryN ( i ) );
1047
1018
}
1019
+ return createGeosCollection ( geosType, geomVector );
1048
1020
}
1049
1021
1050
1022
return 0 ;
0 commit comments