Skip to content

Commit 08c80b4

Browse files
committed
handle truncating of long fieldnames (mainly for shapefiles)
1 parent 573efab commit 08c80b4

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

‎src/analysis/vector/qgszonalstatistics.cpp‎

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

‎src/analysis/vector/qgszonalstatistics.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ANALYSIS_EXPORT QgsZonalStatistics
5454
void statisticsFromPreciseIntersection( void* band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
5555
double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double& sum, double& count );
5656

57+
QString getUniqueFieldName( QString fieldName );
5758

5859
QString mRasterFilePath;
5960
/**Raster band to calculate statistics from (defaults to 1)*/

0 commit comments

Comments
 (0)