@@ -1759,16 +1759,12 @@ OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature )
1759
1759
}
1760
1760
1761
1761
// attribute handling
1762
- for ( int fldIdx = 0 ; fldIdx < mFields . count (); ++fldIdx )
1762
+ for ( QMap< int , int >::const_iterator it = mAttrIdxToOgrIdx . constBegin (); it != mAttrIdxToOgrIdx . constEnd (); ++it )
1763
1763
{
1764
- if ( !mAttrIdxToOgrIdx .contains ( fldIdx ) )
1765
- {
1766
- QgsDebugMsg ( QString ( " no ogr field for field %1" ).arg ( fldIdx ) );
1767
- continue ;
1768
- }
1764
+ int fldIdx = it.key ();
1765
+ int ogrField = it.value ();
1769
1766
1770
1767
const QVariant& attrValue = feature.attribute ( fldIdx );
1771
- int ogrField = mAttrIdxToOgrIdx [ fldIdx ];
1772
1768
1773
1769
if ( !attrValue.isValid () || attrValue.isNull () )
1774
1770
continue ;
@@ -1950,6 +1946,16 @@ OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature )
1950
1946
return poFeature;
1951
1947
}
1952
1948
1949
+ void QgsVectorFileWriter::resetMap ( const QgsAttributeList &attributes )
1950
+ {
1951
+ QMap<int , int > omap ( mAttrIdxToOgrIdx );
1952
+ mAttrIdxToOgrIdx .clear ();
1953
+ for ( int i = 0 ; i < attributes.size (); i++ )
1954
+ {
1955
+ mAttrIdxToOgrIdx .insert ( attributes[i], omap[i] );
1956
+ }
1957
+ }
1958
+
1953
1959
bool QgsVectorFileWriter::writeFeature ( OGRLayerH layer, OGRFeatureH feature )
1954
1960
{
1955
1961
if ( OGR_L_CreateFeature ( layer, feature ) != OGRERR_NONE )
@@ -1998,7 +2004,8 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
1998
2004
const QgsRectangle* filterExtent,
1999
2005
QgsWKBTypes::Type overrideGeometryType,
2000
2006
bool forceMulti,
2001
- bool includeZ )
2007
+ bool includeZ,
2008
+ QgsAttributeList attributes )
2002
2009
{
2003
2010
QgsCoordinateTransform* ct = nullptr ;
2004
2011
if ( destCRS && layer )
@@ -2007,7 +2014,7 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
2007
2014
}
2008
2015
2009
2016
QgsVectorFileWriter::WriterError error = writeAsVectorFormat ( layer, fileName, fileEncoding, ct, driverName, onlySelected,
2010
- errorMessage, datasourceOptions, layerOptions, skipAttributeCreation, newFilename, symbologyExport, symbologyScale, filterExtent, overrideGeometryType, forceMulti, includeZ );
2017
+ errorMessage, datasourceOptions, layerOptions, skipAttributeCreation, newFilename, symbologyExport, symbologyScale, filterExtent, overrideGeometryType, forceMulti, includeZ, attributes );
2011
2018
delete ct;
2012
2019
return error;
2013
2020
}
@@ -2028,7 +2035,8 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
2028
2035
const QgsRectangle* filterExtent,
2029
2036
QgsWKBTypes::Type overrideGeometryType,
2030
2037
bool forceMulti,
2031
- bool includeZ )
2038
+ bool includeZ,
2039
+ QgsAttributeList attributes )
2032
2040
{
2033
2041
if ( !layer )
2034
2042
{
@@ -2061,7 +2069,27 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
2061
2069
destWkbType = QgsWKBTypes::multiType ( destWkbType );
2062
2070
}
2063
2071
2064
- QgsFields fields = skipAttributeCreation ? QgsFields () : layer->fields ();
2072
+ if ( skipAttributeCreation )
2073
+ attributes.clear ();
2074
+ else if ( attributes.isEmpty () )
2075
+ {
2076
+ Q_FOREACH ( int idx, layer->attributeList () )
2077
+ {
2078
+ const QgsField &fld = layer->fields ()[idx];
2079
+ if ( layer->providerType () == " oracle" && fld.typeName ().contains ( " SDO_GEOMETRY" ) )
2080
+ continue ;
2081
+ attributes.append ( idx );
2082
+ }
2083
+ }
2084
+
2085
+ QgsFields fields;
2086
+ if ( !attributes.isEmpty () )
2087
+ {
2088
+ Q_FOREACH ( int attrIdx, attributes )
2089
+ {
2090
+ fields.append ( layer->fields ()[attrIdx] );
2091
+ }
2092
+ }
2065
2093
2066
2094
if ( layer->providerType () == " ogr" && layer->dataProvider () )
2067
2095
{
@@ -2116,6 +2144,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
2116
2144
new QgsVectorFileWriter ( fileName, fileEncoding, fields, QGis::fromNewWkbType ( destWkbType ), outputCRS, driverName, datasourceOptions, layerOptions, newFilename, symbologyExport );
2117
2145
writer->setSymbologyScaleDenominator ( symbologyScale );
2118
2146
2147
+
2119
2148
if ( newFilename )
2120
2149
{
2121
2150
QgsDebugMsg ( " newFilename = " + *newFilename );
@@ -2136,18 +2165,17 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
2136
2165
errorMessage->clear ();
2137
2166
}
2138
2167
2139
- QgsAttributeList allAttr = skipAttributeCreation ? QgsAttributeList () : layer->attributeList ();
2140
2168
QgsFeature fet;
2141
2169
2142
2170
// add possible attributes needed by renderer
2143
- writer->addRendererAttributes ( layer, allAttr );
2171
+ writer->addRendererAttributes ( layer, attributes );
2144
2172
2145
2173
QgsFeatureRequest req;
2146
2174
if ( layer->wkbType () == QGis::WKBNoGeometry )
2147
2175
{
2148
2176
req.setFlags ( QgsFeatureRequest::NoGeometry );
2149
2177
}
2150
- req.setSubsetOfAttributes ( allAttr );
2178
+ req.setSubsetOfAttributes ( attributes );
2151
2179
if ( onlySelected )
2152
2180
req.setFilterFids ( layer->selectedFeaturesIds () );
2153
2181
QgsFeatureIterator fit = layer->getFeatures ( req );
@@ -2190,6 +2218,8 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
2190
2218
transactionsEnabled = false ;
2191
2219
}
2192
2220
2221
+ writer->resetMap ( attributes );
2222
+
2193
2223
// write all features
2194
2224
while ( fit.nextFeature ( fet ) )
2195
2225
{
@@ -2219,7 +2249,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
2219
2249
if ( fet.constGeometry () && filterExtent && !fet.constGeometry ()->intersects ( *filterExtent ) )
2220
2250
continue ;
2221
2251
2222
- if ( allAttr .size () < 1 && skipAttributeCreation )
2252
+ if ( attributes .size () < 1 && skipAttributeCreation )
2223
2253
{
2224
2254
fet.initAttributes ( 0 );
2225
2255
}
0 commit comments