Skip to content

Commit

Permalink
QgsFeature and QgsGeometry serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed May 18, 2015
1 parent e633caf commit 6e3cbe3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/core/qgsfeature.cpp
Expand Up @@ -252,3 +252,32 @@ int QgsFeature::fieldNameIndex( const QString& fieldName ) const
}
return -1;
}

QDataStream& operator<<( QDataStream& out, const QgsFeature& feature )
{
out << feature.id();
out << feature.attributes();
if ( feature.geometry() )
{
out << *( feature.geometry() );
}
else
{
QgsGeometry geometry;
out << geometry;
}
out << feature.isValid();
return out;
}

QDataStream& operator>>( QDataStream& in, QgsFeature& feature )
{
QgsFeatureId id;
QgsGeometry* geometry = new QgsGeometry();
bool valid;
in >> id >> feature.attributes() >> *geometry >> valid;
feature.setFeatureId( id );
feature.setGeometry( geometry );
feature.setValid( valid );
return in;
}
10 changes: 10 additions & 0 deletions src/core/qgsfeature.h
Expand Up @@ -64,6 +64,11 @@ class QgsFeatureId
friend uint qHash( const QgsFeatureId &id );
};

/** Writes the feature id to stream out. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeatureId& featureId );
/** Reads a feature id from stream in into feature id. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeatureId& featureId );

inline uint qHash( const QgsFeatureId &id )
{
return qHash( id.mId );
Expand Down Expand Up @@ -313,6 +318,11 @@ class CORE_EXPORT QgsFeature

}; // class QgsFeature

/** Writes the feature to stream out. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeature& feature );
/** Reads a feature from stream in into feature. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeature& feature );

// key = feature id, value = changed attributes
typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;

Expand Down
17 changes: 17 additions & 0 deletions src/core/qgsgeometry.cpp
Expand Up @@ -6781,3 +6781,20 @@ bool QgsGeometry::compare( const QgsMultiPolygon &p1, const QgsMultiPolygon &p2,
}
return true;
}

QDataStream& operator<<( QDataStream& out, const QgsGeometry& geometry )
{
QByteArray byteArray = QByteArray::fromRawData(( char * )geometry.asWkb(), geometry.wkbSize() ); // does not copy data and does not take ownership
out << byteArray;
return out;
}

QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry )
{
QByteArray byteArray;
in >> byteArray;
char *data = new char[byteArray.size()];
memcpy( data, byteArray.data(), byteArray.size() );
geometry.fromWkb(( unsigned char* )data, byteArray.size() );
return in;
}
5 changes: 5 additions & 0 deletions src/core/qgsgeometry.h
Expand Up @@ -721,6 +721,11 @@ class CORE_EXPORT QgsGeometry

Q_DECLARE_METATYPE( QgsGeometry );

/** Writes the geometry to stream out. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsGeometry& geometry );
/** Reads a geometry from stream in into geometry. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry );

class CORE_EXPORT QgsWkbPtr
{
mutable unsigned char *mP;
Expand Down

0 comments on commit 6e3cbe3

Please sign in to comment.