35 #include <QTextStream>
43 #include <ogr_srs_api.h>
44 #include <cpl_error.h>
47 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
48 #define TO8(x) (x).toUtf8().constData()
49 #define TO8F(x) (x).toUtf8().constData()
51 #define TO8(x) (x).toLocal8Bit().constData()
52 #define TO8F(x) QFile::encodeName( x ).constData()
57 const QString &theVectorFileName,
58 const QString &theFileEncoding,
62 const QString& driverName,
63 const QStringList &datasourceOptions,
64 const QStringList &layerOptions,
72 , mSymbologyExport( symbologyExport )
74 QString vectorFileName = theVectorFileName;
75 QString fileEncoding = theFileEncoding;
76 QStringList layOptions = layerOptions;
77 QStringList dsOptions = datasourceOptions;
79 QString ogrDriverName;
80 if ( driverName ==
"MapInfo MIF" )
82 ogrDriverName =
"MapInfo File";
84 else if ( driverName ==
"SpatiaLite" )
86 ogrDriverName =
"SQLite";
87 if ( !dsOptions.contains(
"SPATIALITE=YES" ) )
89 dsOptions.append(
"SPATIALITE=YES" );
92 else if ( driverName ==
"DBF file" )
94 ogrDriverName =
"ESRI Shapefile";
95 if ( !layOptions.contains(
"SHPT=NULL" ) )
97 layOptions.append(
"SHPT=NULL" );
103 ogrDriverName = driverName;
107 OGRSFDriverH poDriver;
110 poDriver = OGRGetDriverByName( ogrDriverName.toLocal8Bit().data() );
112 if ( poDriver == NULL )
116 .arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
121 if ( ogrDriverName ==
"ESRI Shapefile" )
123 if ( layOptions.join(
"" ).toUpper().indexOf(
"ENCODING=" ) == -1 )
128 if ( driverName ==
"ESRI Shapefile" && !vectorFileName.endsWith(
".shp", Qt::CaseInsensitive ) )
130 vectorFileName +=
".shp";
132 else if ( driverName ==
"DBF file" && !vectorFileName.endsWith(
".dbf", Qt::CaseInsensitive ) )
134 vectorFileName +=
".dbf";
137 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM < 1700
139 QSet<QString> fieldNames;
140 for (
int i = 0; i < fields.
count(); ++i )
142 QString name = fields[i].name().left( 10 );
143 if ( fieldNames.contains( name ) )
145 mErrorMessage =
QObject::tr(
"trimming attribute name '%1' to ten significant characters produces duplicate column name." )
146 .arg( fields[i].name() );
156 else if ( driverName ==
"KML" )
158 if ( !vectorFileName.endsWith(
".kml", Qt::CaseInsensitive ) )
160 vectorFileName +=
".kml";
163 if ( fileEncoding.compare(
"UTF-8", Qt::CaseInsensitive ) != 0 )
166 fileEncoding =
"UTF-8";
169 QFile::remove( vectorFileName );
179 QStringList allExts = exts.split(
" ", QString::SkipEmptyParts );
181 foreach ( QString ext, allExts )
183 if ( vectorFileName.endsWith(
"." + ext, Qt::CaseInsensitive ) )
192 vectorFileName +=
"." + allExts[0];
196 QFile::remove( vectorFileName );
199 char **options = NULL;
200 if ( !dsOptions.isEmpty() )
202 options =
new char *[ dsOptions.size()+1 ];
203 for (
int i = 0; i < dsOptions.size(); i++ )
205 options[i] = CPLStrdup( dsOptions[i].toLocal8Bit().data() );
207 options[ dsOptions.size()] = NULL;
211 mDS = OGR_Dr_CreateDataSource( poDriver,
TO8F( vectorFileName ), options );
215 for (
int i = 0; i < dsOptions.size(); i++ )
216 CPLFree( options[i] );
225 .arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
232 mCodec = QTextCodec::codecForName( fileEncoding.toLocal8Bit().constData() );
235 QgsDebugMsg(
"error finding QTextCodec for " + fileEncoding );
238 QString enc = settings.value(
"/UI/encoding",
"System" ).toString();
239 mCodec = QTextCodec::codecForName( enc.toLocal8Bit().constData() );
242 QgsDebugMsg(
"error finding QTextCodec for " + enc );
243 mCodec = QTextCodec::codecForLocale();
252 QString srsWkt = srs->
toWkt();
254 ogrRef = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
258 QString layerName = QFileInfo( vectorFileName ).baseName();
259 OGRwkbGeometryType wkbType =
static_cast<OGRwkbGeometryType
>( geometryType );
261 if ( !layOptions.isEmpty() )
263 options =
new char *[ layOptions.size()+1 ];
264 for (
int i = 0; i < layOptions.size(); i++ )
266 options[i] = CPLStrdup( layOptions[i].toLocal8Bit().data() );
268 options[ layOptions.size()] = NULL;
272 CPLSetConfigOption(
"SHAPE_ENCODING",
"" );
274 mLayer = OGR_DS_CreateLayer(
mDS,
TO8F( layerName ), ogrRef, wkbType, options );
278 for (
int i = 0; i < layOptions.size(); i++ )
279 CPLFree( options[i] );
285 if ( !settings.value(
"/qgis/ignoreShapeEncoding",
true ).toBool() )
287 CPLSetConfigOption(
"SHAPE_ENCODING", 0 );
292 if ( ogrDriverName ==
"ESRI Shapefile" )
294 QString layerName = vectorFileName.left( vectorFileName.indexOf(
".shp", Qt::CaseInsensitive ) );
295 QFile prjFile( layerName +
".qpj" );
296 if ( prjFile.open( QIODevice::WriteOnly ) )
298 QTextStream prjStream( &prjFile );
299 prjStream << srs->
toWkt().toLocal8Bit().data() << endl;
304 QgsDebugMsg(
"Couldn't open file " + layerName +
".qpj" );
308 OSRDestroySpatialReference( ogrRef );
314 .arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
319 OGRFeatureDefnH defn = OGR_L_GetLayerDefn(
mLayer );
324 QgsDebugMsg(
"creating " + QString::number( fields.
size() ) +
" fields" );
329 for (
int fldIdx = 0; fldIdx < fields.
count(); ++fldIdx )
331 const QgsField& attrField = fields[fldIdx];
333 OGRFieldType ogrType = OFTString;
334 int ogrWidth = attrField.
length();
335 int ogrPrecision = attrField.
precision();
336 switch ( attrField.
type() )
338 case QVariant::LongLong:
340 ogrWidth = ogrWidth > 0 && ogrWidth <= 21 ? ogrWidth : 21;
344 case QVariant::String:
346 if ( ogrWidth <= 0 || ogrWidth > 255 )
351 ogrType = OFTInteger;
352 ogrWidth = ogrWidth > 0 && ogrWidth <= 10 ? ogrWidth : 10;
356 case QVariant::Double:
364 case QVariant::DateTime:
365 ogrType = OFTDateTime;
371 .arg( attrField.
name() );
376 QString name( attrField.
name() );
378 if ( ogrDriverName ==
"SQLite" && name.compare(
"ogc_fid", Qt::CaseInsensitive ) == 0 )
381 for ( i = 0; i < 10; i++ )
383 name = QString(
"ogc_fid%1" ).arg( i );
386 for ( j = 0; j < fields.
size() && name.compare( fields[j].name(), Qt::CaseInsensitive ) != 0; j++ )
389 if ( j == fields.
size() )
404 OGRFieldDefnH fld = OGR_Fld_Create(
mCodec->fromUnicode( name ), ogrType );
407 OGR_Fld_SetWidth( fld, ogrWidth );
410 if ( ogrPrecision >= 0 )
412 OGR_Fld_SetPrecision( fld, ogrPrecision );
417 " type " + QString( QVariant::typeToName( attrField.
type() ) ) +
418 " width " + QString::number( ogrWidth ) +
419 " precision " + QString::number( ogrPrecision ) );
420 if ( OGR_L_CreateField(
mLayer, fld,
true ) != OGRERR_NONE )
424 .arg( attrField.
name() )
425 .arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
427 OGR_Fld_Destroy( fld );
430 OGR_Fld_Destroy( fld );
432 int ogrIdx = OGR_FD_GetFieldIndex( defn,
mCodec->fromUnicode( name ) );
435 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM < 1700
438 int fieldCount = OGR_FD_GetFieldCount( defn );
440 OGRFieldDefnH fdefn = OGR_FD_GetFieldDefn( defn, fieldCount - 1 );
443 const char *fieldName = OGR_Fld_GetNameRef( fdefn );
445 if ( attrField.
name().left( strlen( fieldName ) ) == fieldName )
447 ogrIdx = fieldCount - 1;
452 ogrIdx = OGR_FD_GetFieldCount( defn ) - 1;
459 .arg( attrField.
name() )
460 .arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
479 *newFilename = vectorFileName;
484 return OGR_G_CreateGeometry(( OGRwkbGeometryType ) wkbType );
509 QString currentStyle;
511 QgsSymbolV2List::const_iterator symbolIt = symbols.constBegin();
512 for ( ; symbolIt != symbols.constEnd(); ++symbolIt )
514 int nSymbolLayers = ( *symbolIt )->symbolLayerCount();
515 for (
int i = 0; i < nSymbolLayers; ++i )
518 QMap< QgsSymbolLayerV2*, QString >::const_iterator it = mSymbolLayerTable.find(( *symbolIt )->symbolLayer( i ) );
519 if ( it == mSymbolLayerTable.constEnd() )
527 currentStyle = ( *symbolIt )->symbolLayer( i )->ogrFeatureStyle( mmsf, musf );
531 if ( symbolIt != symbols.constBegin() || i != 0 )
533 styleString.append(
";" );
535 styleString.append( currentStyle );
539 OGR_F_SetStyleString( poFeature, currentStyle.toLocal8Bit().data() );
547 OGR_F_SetStyleString( poFeature, styleString.toLocal8Bit().data() );
558 OGR_F_Destroy( poFeature );
564 OGRFeatureH poFeature = OGR_F_Create( OGR_L_GetLayerDefn(
mLayer ) );
569 QgsDebugMsg( QString(
"feature id %1 too large." ).arg( fid ) );
570 OGRErr err = OGR_F_SetFID( poFeature, static_cast<long>( fid ) );
571 if ( err != OGRERR_NONE )
573 QgsDebugMsg( QString(
"Failed to set feature id to %1: %2 (OGR error: %3)" )
575 .arg( err ).arg( CPLGetLastErrorMsg() )
581 for (
int fldIdx = 0; fldIdx <
mFields.
count(); ++fldIdx )
585 QgsDebugMsg( QString(
"no ogr field for field %1" ).arg( fldIdx ) );
589 const QVariant& attrValue = feature.
attribute( fldIdx );
592 if ( !attrValue.isValid() || attrValue.isNull() )
595 switch ( attrValue.type() )
598 OGR_F_SetFieldInteger( poFeature, ogrField, attrValue.toInt() );
600 case QVariant::Double:
601 OGR_F_SetFieldDouble( poFeature, ogrField, attrValue.toDouble() );
603 case QVariant::LongLong:
604 case QVariant::String:
605 OGR_F_SetFieldString( poFeature, ogrField,
mCodec->fromUnicode( attrValue.toString() ).data() );
608 OGR_F_SetFieldDateTime( poFeature, ogrField,
609 attrValue.toDate().year(),
610 attrValue.toDate().month(),
611 attrValue.toDate().day(),
614 case QVariant::DateTime:
615 OGR_F_SetFieldDateTime( poFeature, ogrField,
616 attrValue.toDateTime().date().year(),
617 attrValue.toDateTime().date().month(),
618 attrValue.toDateTime().date().day(),
619 attrValue.toDateTime().time().hour(),
620 attrValue.toDateTime().time().minute(),
621 attrValue.toDateTime().time().second(),
624 case QVariant::Invalid:
630 .arg( QMetaType::typeName( attrValue.type() ) )
631 .arg( attrValue.toString() );
665 .arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
668 OGR_F_Destroy( poFeature );
672 OGRErr err = OGR_G_ImportFromWkb( mGeom2, const_cast<unsigned char *>( geom->
asWkb() ), geom->
wkbSize() );
673 if ( err != OGRERR_NONE )
676 .arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
679 OGR_F_Destroy( poFeature );
684 OGR_F_SetGeometryDirectly( poFeature, mGeom2 );
688 OGRErr err = OGR_G_ImportFromWkb(
mGeom, const_cast<unsigned char *>( geom->
asWkb() ), geom->
wkbSize() );
689 if ( err != OGRERR_NONE )
692 .arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
695 OGR_F_Destroy( poFeature );
700 OGR_F_SetGeometry( poFeature,
mGeom );
708 if ( OGR_L_CreateFeature( layer, feature ) != OGRERR_NONE )
710 mErrorMessage =
QObject::tr(
"Feature creation error (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
713 OGR_F_Destroy( feature );
723 OGR_G_DestroyGeometry(
mGeom );
728 OGR_DS_Destroy(
mDS );
734 const QString& fileName,
735 const QString& fileEncoding,
737 const QString& driverName,
739 QString *errorMessage,
740 const QStringList &datasourceOptions,
741 const QStringList &layerOptions,
742 bool skipAttributeCreation,
743 QString *newFilename,
745 double symbologyScale )
750 int shallTransform =
false;
757 if ( destCRS && destCRS->
isValid() )
761 shallTransform =
true;
766 outputCRS = &layer->
crs();
789 errorMessage->clear();
817 shallTransform =
false;
839 int n = 0, errors = 0;
851 bool transactionsEnabled =
true;
853 if ( OGRERR_NONE != OGR_L_StartTransaction( writer->
mLayer ) )
855 QgsDebugMsg(
"Error when trying to enable transactions on OGRLayer." );
856 transactionsEnabled =
false;
862 if ( onlySelected && !ids.contains( fet.
id() ) )
865 if ( shallTransform )
879 QString msg =
QObject::tr(
"Failed to transform a point while drawing a feature with ID '%1'. Writing stopped. (Exception: %2)" )
880 .arg( fet.
id() ).arg( e.
what() );
888 if ( allAttr.size() < 1 && skipAttributeCreation )
896 if ( err !=
NoError && errorMessage )
898 if ( errorMessage->isEmpty() )
900 *errorMessage =
QObject::tr(
"Feature write errors:" );
910 *errorMessage +=
QObject::tr(
"Stopping after %1 errors" ).arg( errors );
920 if ( transactionsEnabled )
922 if ( OGRERR_NONE != OGR_L_CommitTransaction( writer->
mLayer ) )
924 QgsDebugMsg(
"Error while committing transaction on OGRLayer." );
931 if ( shallTransform )
936 if ( errors > 0 && errorMessage && n > 0 )
938 *errorMessage +=
QObject::tr(
"\nOnly %1 of %2 features written." ).arg( n - errors ).arg( n );
947 QFileInfo fi( theFileName );
951 const char *suffixes[] = {
".shp",
".shx",
".dbf",
".prj",
".qix",
".qpj" };
952 for ( std::size_t i = 0; i <
sizeof( suffixes ) /
sizeof( *suffixes ); i++ )
954 filter << fi.completeBaseName() + suffixes[i];
958 foreach ( QString
file, dir.entryList( filter ) )
960 if ( !QFile::remove( dir.canonicalPath() +
"/" +
file ) )
972 QMap<QString, QString> resultMap;
975 int const drvCount = OGRGetDriverCount();
977 for (
int i = 0; i < drvCount; ++i )
979 OGRSFDriverH drv = OGRGetDriver( i );
982 QString drvName = OGR_Dr_GetName( drv );
983 if ( OGR_Dr_TestCapability( drv,
"CreateDataSource" ) != 0 )
986 if ( filterString.isEmpty() )
989 resultMap.insert( filterString, drvName );
999 QMap<QString, QString> resultMap;
1002 int const drvCount = OGRGetDriverCount();
1004 QStringList writableDrivers;
1005 for (
int i = 0; i < drvCount; ++i )
1007 OGRSFDriverH drv = OGRGetDriver( i );
1010 QString drvName = OGR_Dr_GetName( drv );
1011 if ( OGR_Dr_TestCapability( drv,
"CreateDataSource" ) != 0 )
1014 if ( drvName ==
"MapInfo File" )
1016 writableDrivers <<
"MapInfo MIF";
1018 else if ( drvName ==
"SQLite" )
1025 QString option =
"SPATIALITE=YES";
1026 char **options =
new char *[2];
1027 options[0] = CPLStrdup( option.toLocal8Bit().data() );
1029 OGRSFDriverH poDriver;
1031 poDriver = OGRGetDriverByName( drvName.toLocal8Bit().data() );
1034 OGRDataSourceH ds = OGR_Dr_CreateDataSource( poDriver,
TO8F( QString(
"/vsimem/spatialitetest.sqlite" ) ), options );
1037 writableDrivers <<
"SpatiaLite";
1038 OGR_Dr_DeleteDataSource( poDriver,
TO8F( QString(
"/vsimem/spatialitetest.sqlite" ) ) );
1039 OGR_DS_Destroy( ds );
1042 CPLFree( options[0] );
1045 else if ( drvName ==
"ESRI Shapefile" )
1047 writableDrivers <<
"DBF file";
1049 writableDrivers << drvName;
1054 foreach ( QString drvName, writableDrivers )
1062 resultMap.insert( trLongName, drvName );
1071 QString filterString;
1073 QMap< QString, QString>::const_iterator it = driverFormatMap.constBegin();
1074 for ( ; it != driverFormatMap.constEnd(); ++it )
1076 if ( filterString.isEmpty() )
1077 filterString +=
";;";
1079 filterString += it.key();
1081 return filterString;
1090 if ( !
driverMetadata( driverName, longName, trLongName, glob, exts ) || trLongName.isEmpty() || glob.isEmpty() )
1093 return trLongName +
" [OGR] (" + glob.toLower() +
" " + glob.toUpper() +
")";
1098 if ( codecName ==
"System" )
1099 return QString(
"LDID/0" );
1101 QRegExp re = QRegExp( QString(
"(CP|windows-|ISO[ -])(.+)" ), Qt::CaseInsensitive );
1102 if ( re.exactMatch( codecName ) )
1104 QString c = re.cap( 2 ).replace(
"-" ,
"" );
1106 c.toInt( &isNumber );
1115 if ( driverName.startsWith(
"AVCE00" ) )
1117 longName =
"Arc/Info ASCII Coverage";
1118 trLongName =
QObject::tr(
"Arc/Info ASCII Coverage" );
1122 else if ( driverName.startsWith(
"BNA" ) )
1124 longName =
"Atlas BNA";
1129 else if ( driverName.startsWith(
"CSV" ) )
1131 longName =
"Comma Separated Value";
1132 trLongName =
QObject::tr(
"Comma Separated Value" );
1136 else if ( driverName.startsWith(
"ESRI" ) )
1138 longName =
"ESRI Shapefile";
1143 else if ( driverName.startsWith(
"DBF file" ) )
1145 longName =
"DBF File";
1150 else if ( driverName.startsWith(
"FMEObjects Gateway" ) )
1152 longName =
"FMEObjects Gateway";
1157 else if ( driverName.startsWith(
"GeoJSON" ) )
1159 longName =
"GeoJSON";
1164 else if ( driverName.startsWith(
"GeoRSS" ) )
1166 longName =
"GeoRSS";
1171 else if ( driverName.startsWith(
"GML" ) )
1173 longName =
"Geography Markup Language [GML]";
1174 trLongName =
QObject::tr(
"Geography Markup Language [GML]" );
1178 else if ( driverName.startsWith(
"GMT" ) )
1180 longName =
"Generic Mapping Tools [GMT]";
1181 trLongName =
QObject::tr(
"Generic Mapping Tools [GMT]" );
1185 else if ( driverName.startsWith(
"GPX" ) )
1187 longName =
"GPS eXchange Format [GPX]";
1188 trLongName =
QObject::tr(
"GPS eXchange Format [GPX]" );
1192 else if ( driverName.startsWith(
"Interlis 1" ) )
1194 longName =
"INTERLIS 1";
1196 glob =
"*.itf *.xml *.ili";
1199 else if ( driverName.startsWith(
"Interlis 2" ) )
1201 longName =
"INTERLIS 2";
1203 glob =
"*.itf *.xml *.ili";
1206 else if ( driverName.startsWith(
"KML" ) )
1208 longName =
"Keyhole Markup Language [KML]";
1209 trLongName =
QObject::tr(
"Keyhole Markup Language [KML]" );
1213 else if ( driverName.startsWith(
"MapInfo File" ) )
1215 longName =
"Mapinfo TAB";
1221 else if ( driverName.startsWith(
"MapInfo MIF" ) )
1223 longName =
"Mapinfo MIF";
1228 else if ( driverName.startsWith(
"DGN" ) )
1230 longName =
"Microstation DGN";
1235 else if ( driverName.startsWith(
"S57" ) )
1237 longName =
"S-57 Base file";
1242 else if ( driverName.startsWith(
"SDTS" ) )
1244 longName =
"Spatial Data Transfer Standard [SDTS]";
1245 trLongName =
QObject::tr(
"Spatial Data Transfer Standard [SDTS]" );
1249 else if ( driverName.startsWith(
"SQLite" ) )
1251 longName =
"SQLite";
1257 else if ( driverName.startsWith(
"SpatiaLite" ) )
1259 longName =
"SpatiaLite";
1264 else if ( driverName.startsWith(
"DXF" ) )
1266 longName =
"AutoCAD DXF";
1271 else if ( driverName.startsWith(
"Geoconcept" ) )
1273 longName =
"Geoconcept";
1275 glob =
"*.gxt *.txt";
1278 else if ( driverName.startsWith(
"FileGDB" ) )
1280 longName =
"ESRI FileGDB";
1313 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1700
1314 mSymbolLayerTable.clear();
1315 OGRStyleTableH ogrStyleTable = OGR_STBL_Create();
1316 OGRStyleMgrH styleManager = OGR_SM_Create( ogrStyleTable );
1319 int nTotalLevels = 0;
1321 QgsSymbolV2List::iterator symbolIt = symbolList.begin();
1322 for ( ; symbolIt != symbolList.end(); ++symbolIt )
1327 int nLevels = ( *symbolIt )->symbolLayerCount();
1328 for (
int i = 0; i < nLevels; ++i )
1330 mSymbolLayerTable.insert(( *symbolIt )->symbolLayer( i ), QString::number( nTotalLevels ) );
1331 OGR_SM_AddStyle( styleManager, QString::number( nTotalLevels ).toLocal8Bit(),
1332 ( *symbolIt )->symbolLayer( i )->ogrFeatureStyle( mmsf, musf ).toLocal8Bit() );
1336 OGR_DS_SetStyleTableDirectly( ds, ogrStyleTable );
1352 QHash< QgsSymbolV2*, QList<QgsFeature> > features;
1381 QString msg =
QObject::tr(
"Failed to transform, writing stopped. (Exception: %1)" )
1385 *errorMessage = msg;
1392 if ( !featureSymbol )
1397 QHash< QgsSymbolV2*, QList<QgsFeature> >::iterator it = features.find( featureSymbol );
1398 if ( it == features.end() )
1400 it = features.insert( featureSymbol, QList<QgsFeature>() );
1402 it.value().append( fet );
1408 for (
int i = 0; i < symbols.count(); i++ )
1414 if ( level < 0 || level >= 1000 )
1417 while ( level >= levels.count() )
1419 levels[level].append( item );
1424 int nTotalFeatures = 0;
1427 for (
int l = 0; l < levels.count(); l++ )
1430 for (
int i = 0; i < level.count(); i++ )
1433 QHash< QgsSymbolV2*, QList<QgsFeature> >::iterator levelIt = features.find( item.
symbol() );
1434 if ( levelIt == features.end() )
1443 int llayer = item.
layer();
1444 QList<QgsFeature>& featureList = levelIt.value();
1445 QList<QgsFeature>::iterator featureIt = featureList.begin();
1446 for ( ; featureIt != featureList.end(); ++featureIt )
1456 QString styleString = levelIt.key()->symbolLayer( llayer )->ogrFeatureStyle( mmsf, musf );
1457 if ( !styleString.isEmpty() )
1459 OGR_F_SetStyleString( ogrFeature, styleString.toLocal8Bit().data() );
1465 OGR_F_Destroy( ogrFeature );
1472 if ( nErrors > 0 && errorMessage )
1474 *errorMessage +=
QObject::tr(
"\nOnly %1 of %2 features written." ).arg( nTotalFeatures - nErrors ).arg( nTotalFeatures );
1491 return 1000 / scaleDenominator;
1508 return scaleDenominator / 1000;
1565 for (
int i = 0; i < rendererAttributes.size(); ++i )
1570 attList.push_back( vl->
fieldNameIndex( rendererAttributes.at( i ) ) );
QgsFeatureId id() const
Get the feature id for this feature.
static WkbType singleType(WkbType type)
const QString & name() const
Gets the name of the field.
Wrapper for iterator of features from vector data provider or vector layer.
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
static QMap< QString, QString > supportedFiltersAndFormats()
Returns map with format filter string as key and OGR format key as value.
SymbologyExport symbologyExport() const
QList< QgsSymbolV2 * > QgsSymbolV2List
void createSymbolLayerTable(QgsVectorLayer *vl, const QgsCoordinateTransform *ct, OGRDataSourceH ds)
bool writeFeature(OGRLayerH layer, OGRFeatureH feature)
WriterError mError
contains error value if construction was not successful
size_t wkbSize() const
Returns the size of the WKB in asWkb().
static QString fileFilterString()
Returns filter string that can be used for dialogs.
QSet< QgsFeatureId > QgsFeatureIds
SymbologyExport mSymbologyExport
double mSymbologyScaleDenominator
Scale for symbology export (e.g.
static void warning(const QString &msg)
Goes to qWarning.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
QgsGeometry * geometry() const
Get the geometry object associated with this feature.
void setRendererScale(double scale)
int precision() const
Gets the precision of the field.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
static void registerOgrDrivers()
Register OGR drivers ensuring this only happens once.
static bool deleteShapeFile(QString theFileName)
Delete a shapefile (and its accompanying shx / dbf / prf)
Container of fields for a vector layer.
double mmScaleFactor(double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits)
void addRendererAttributes(QgsVectorLayer *vl, QgsAttributeList &attList)
Adds attributes needed for classification.
A geometry is the spatial representation of a feature.
WkbType
Used for symbology operations.
virtual QList< QString > usedAttributes()=0
A convenience class for writing vector files to disk.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
static void logMessage(QString message, QString tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
QGis::WkbType wkbType() const
Returns the WKBType or WKBUnknown in case of error.
virtual void stopRender(QgsRenderContext &context)=0
double ANALYSIS_EXPORT max(double x, double y)
returns the maximum of two doubles or the first argument if both are equal
virtual QgsSymbolV2List symbols()=0
for symbol levels
double mapUnitScaleFactor(double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits)
void startRender(QgsVectorLayer *vl) const
const QgsFeatureIds & selectedFeaturesIds() const
Return reference to identifiers of selected features.
#define FID_TO_NUMBER(fid)
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
~QgsVectorFileWriter()
close opened shapefile for writing
QList< int > QgsAttributeList
OGRFeatureH createFeature(QgsFeature &feature)
bool addFeature(QgsFeature &feature, QgsFeatureRendererV2 *renderer=0, QGis::UnitType outputUnit=QGis::Meters)
add feature to the currently opened shapefile
int count() const
Return number of items.
QGis::WkbType wkbType() const
Returns type of wkb (point / linestring / polygon etc.)
void stopRender(QgsVectorLayer *vl) const
QGis::WkbType mWkbType
geometry type which is being used
Encapsulate a field in an attribute table or data source.
static bool driverMetadata(QString driverName, QString &longName, QString &trLongName, QString &glob, QString &ext)
QMap< int, int > mAttrIdxToOgrIdx
map attribute indizes to OGR field indexes
WriterError exportFeaturesSymbolLevels(QgsVectorLayer *layer, QgsFeatureIterator &fit, const QgsCoordinateTransform *ct, QString *errorMessage=0)
Writes features considering symbol level order.
QList< QgsSymbolV2LevelItem > QgsSymbolV2Level
int renderingPass() const
static QMap< QString, QString > ogrDriverList()
Returns driver list that can be used for dialogs.
QString errorMessage()
retrieves error message
QgsRenderContext renderContext() const
Contains information about the context of a rendering operation.
static QString convertCodecNameForEncodingOption(const QString &codecName)
Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile.
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
WriterError hasError()
checks whether there were any errors in constructor
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
QgsAttributeList pendingAllAttributesList()
returns list of attributes
QgsVectorFileWriter(const QString &vectorFileName, const QString &fileEncoding, const QgsFields &fields, QGis::WkbType geometryType, const QgsCoordinateReferenceSystem *srs, const QString &driverName="ESRI Shapefile", const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QString *newFilename=0, SymbologyExport symbologyExport=NoSymbology)
create shapefile and initialize it
virtual QgsSymbolV2List symbolsForFeature(QgsFeature &feat)
return list of symbols used for rendering the feature.
void setSymbologyScaleDenominator(double d)
bool usingSymbolLevels() const
Class for storing a coordinate reference system (CRS)
int length() const
Gets the length of the field.
int size() const
Return number of items.
UnitType
Map units that qgis supports.
int transform(const QgsCoordinateTransform &ct)
Transform this geometry as described by CoordinateTranasform ct.
QList< QgsSymbolV2Level > QgsSymbolV2LevelOrder
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
virtual void startRender(QgsRenderContext &context, const QgsVectorLayer *vlayer)=0
static WriterError writeAsVectorFormat(QgsVectorLayer *layer, const QString &fileName, const QString &fileEncoding, const QgsCoordinateReferenceSystem *destCRS, const QString &driverName="ESRI Shapefile", bool onlySelected=false, QString *errorMessage=0, const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), bool skipAttributeCreation=false, QString *newFilename=0, SymbologyExport symbologyExport=NoSymbology, double symbologyScale=1.0)
Write contents of vector layer to an (OGR supported) vector formt.
Custom exception class for Coordinate Reference System related exceptions.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
QgsSymbolLayerV2 * symbolLayer(int layer)
virtual int capabilities()
returns bitwise OR-ed capabilities of the renderer
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
virtual QgsSymbolV2 * symbolForFeature(QgsFeature &feature)=0
to be overridden
QgsFeatureRequest & setFlags(Flags flags)
Set flags that affect how features will be fetched.
const unsigned char * asWkb() const
Returns the buffer containing this geometry in WKB format.
QgsFeatureRendererV2 * symbologyRenderer(QgsVectorLayer *vl) const
OGRGeometryH createEmptyGeometry(QGis::WkbType wkbType)
static QString filterForDriver(const QString &driverName)
Creates a filter for an OGR driver key.
void * OGRSpatialReferenceH
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
QGis::UnitType mapUnits() const