Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE]: add base mechanisms for OGR feature style export
  • Loading branch information
mhugent committed Dec 7, 2012
1 parent cb7d1ec commit b0b9098
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/core/qgsvectorfilewriter.cpp
Expand Up @@ -24,6 +24,8 @@
#include "qgsmessagelog.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsvectorfilewriter.h"
#include "qgsrendererv2.h"
#include "qgssymbolv2.h"

#include <QFile>
#include <QSettings>
Expand Down Expand Up @@ -430,7 +432,7 @@ QString QgsVectorFileWriter::errorMessage()
return mErrorMessage;
}

bool QgsVectorFileWriter::addFeature( QgsFeature& feature )
bool QgsVectorFileWriter::addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer )
{
// create the feature
OGRFeatureH poFeature = OGR_F_Create( OGR_L_GetLayerDefn( mLayer ) );
Expand Down Expand Up @@ -564,6 +566,30 @@ bool QgsVectorFileWriter::addFeature( QgsFeature& feature )
}
}

//add OGR feature style type
if ( mExportFeatureStyle && renderer )
{
//concatenate ogr styles of all symbols
QgsSymbolV2List symbols = renderer->symbolsForFeature( feature );
QString styleString;
QString currentStyle;

QgsSymbolV2List::const_iterator symbolIt = symbols.constBegin();
for ( ; symbolIt != symbols.constEnd(); ++symbolIt )
{
currentStyle = ( *symbolIt )->ogrFeatureStyle();
if ( currentStyle.isEmpty() )
{
continue;
}
if ( symbolIt != symbols.constBegin() )
{
styleString.append( ";" );
}
styleString.append( currentStyle );
}
}

// put the created feature to layer
if ( OGR_L_CreateFeature( mLayer, poFeature ) != OGRERR_NONE )
{
Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsvectorfilewriter.h
Expand Up @@ -132,7 +132,7 @@ class CORE_EXPORT QgsVectorFileWriter
QString errorMessage();

/** add feature to the currently opened shapefile */
bool addFeature( QgsFeature& feature );
bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = 0 );

//! @note not available in python bindings
QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
Expand Down Expand Up @@ -168,6 +168,9 @@ class CORE_EXPORT QgsVectorFileWriter
/** map attribute indizes to OGR field indexes */
QMap<int, int> mAttrIdxToOgrIdx;

/** flag if OGR feature type style should be exported*/
bool mExportFeatureStyle;

private:
static bool driverMetadata( QString driverName, QString &longName, QString &trLongName, QString &glob, QString &ext );
};
Expand Down
2 changes: 2 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2.h
Expand Up @@ -54,6 +54,8 @@ class CORE_EXPORT QgsSymbolLayerV2
virtual void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const
{ Q_UNUSED( props ); element.appendChild( doc.createComment( QString( "SymbolLayerV2 %1 not implemented yet" ).arg( layerType() ) ) ); }

virtual QString ogrFeatureStyle() const { return QString(); }

virtual QgsStringMap properties() const = 0;

virtual void drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size ) = 0;
Expand Down
20 changes: 20 additions & 0 deletions src/core/symbology-ng/qgssymbolv2.cpp
Expand Up @@ -314,6 +314,26 @@ void QgsSymbolV2::toSld( QDomDocument &doc, QDomElement &element, QgsStringMap p
}
}

QString QgsSymbolV2::ogrFeatureStyle() const
{
QString styleString;
QString currentStyleString;
for ( QgsSymbolLayerV2List::const_iterator it = mLayers.constBegin(); it != mLayers.constEnd(); ++it )
{
currentStyleString = ( *it )->ogrFeatureStyle();
if ( currentStyleString.isEmpty() )
{
continue;
}

if ( it != mLayers.constBegin() )
{
styleString.append( ";" );
}
styleString.append( currentStyleString );
}
}

QgsSymbolLayerV2List QgsSymbolV2::cloneLayers() const
{
QgsSymbolLayerV2List lst;
Expand Down
3 changes: 3 additions & 0 deletions src/core/symbology-ng/qgssymbolv2.h
Expand Up @@ -113,6 +113,9 @@ class CORE_EXPORT QgsSymbolV2

void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;

/**Returns the OGR feature style string for the symbol*/
QString ogrFeatureStyle() const;

OutputUnit outputUnit() const { return mOutputUnit; }
void setOutputUnit( OutputUnit u ) { mOutputUnit = u; }

Expand Down

0 comments on commit b0b9098

Please sign in to comment.