@@ -111,18 +111,21 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog* p )
111111
112112 // add the new count, sum, mean fields to the provider
113113 QList<QgsField> newFieldList;
114- QgsField countField ( mAttributePrefix + " count" , QVariant::Double, " double precision" );
115- QgsField sumField ( mAttributePrefix + " sum" , QVariant::Double, " double precision" );
116- QgsField meanField ( mAttributePrefix + " mean" , QVariant::Double, " double precision" );
114+ QString countFieldName = getUniqueFieldName ( mAttributePrefix + " count" );
115+ QString sumFieldName = getUniqueFieldName ( mAttributePrefix + " sum" );
116+ QString meanFieldName = getUniqueFieldName ( mAttributePrefix + " mean" );
117+ QgsField countField ( countFieldName, QVariant::Double, " double precision" );
118+ QgsField sumField ( sumFieldName, QVariant::Double, " double precision" );
119+ QgsField meanField ( meanFieldName, QVariant::Double, " double precision" );
117120 newFieldList.push_back ( countField );
118121 newFieldList.push_back ( sumField );
119122 newFieldList.push_back ( meanField );
120123 vectorProvider->addAttributes ( newFieldList );
121124
122125 // index of the new fields
123- int countIndex = vectorProvider->fieldNameIndex ( mAttributePrefix + " count " );
124- int sumIndex = vectorProvider->fieldNameIndex ( mAttributePrefix + " sum " );
125- int meanIndex = vectorProvider->fieldNameIndex ( mAttributePrefix + " mean " );
126+ int countIndex = vectorProvider->fieldNameIndex ( countFieldName );
127+ int sumIndex = vectorProvider->fieldNameIndex ( sumFieldName );
128+ int meanIndex = vectorProvider->fieldNameIndex ( meanFieldName );
126129
127130 if ( countIndex == -1 || sumIndex == -1 || meanIndex == -1 )
128131 {
@@ -362,4 +365,55 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeome
362365 CPLFree ( pixelData );
363366}
364367
368+ QString QgsZonalStatistics::getUniqueFieldName ( QString fieldName )
369+ {
370+ QgsVectorDataProvider* dp = mPolygonLayer ->dataProvider ();
371+
372+ if ( !dp->storageType ().contains ( " ESRI Shapefile" ) )
373+ {
374+ return fieldName;
375+ }
365376
377+ const QgsFields& providerFields = dp->fields ();
378+ QString shortName = fieldName.mid ( 0 , 10 );
379+
380+ bool found = false ;
381+ for ( int idx = 0 ; idx < providerFields.count (); ++idx )
382+ {
383+ if ( shortName == providerFields[idx].name () )
384+ {
385+ found = true ;
386+ break ;
387+ }
388+ }
389+
390+ if ( !found )
391+ {
392+ return shortName;
393+ }
394+
395+ int n = 1 ;
396+ shortName = QString ( " %1_%2" ).arg ( fieldName.mid ( 0 , 8 ) ).arg ( n );
397+ found = true ;
398+ while ( found )
399+ {
400+ found = false ;
401+ for ( int idx = 0 ; idx < providerFields.count (); ++idx )
402+ {
403+ if ( shortName == providerFields[idx].name () )
404+ {
405+ n += 1 ;
406+ if ( n < 9 )
407+ {
408+ shortName = QString ( " %1_%2" ).arg ( fieldName.mid ( 0 , 8 ) ).arg ( n );
409+ }
410+ else
411+ {
412+ shortName = QString ( " %1_%2" ).arg ( fieldName.mid ( 0 , 7 ) ).arg ( n );
413+ }
414+ found = true ;
415+ }
416+ }
417+ }
418+ return shortName;
419+ }
0 commit comments