Skip to content
Permalink
Browse files

QgsFeature and QgsGeometry serialization

  • Loading branch information
blazek committed May 17, 2015
1 parent e633caf commit 6e3cbe3a3d21b80b5585a8988936fb0d74f3e248
Showing with 61 additions and 0 deletions.
  1. +29 −0 src/core/qgsfeature.cpp
  2. +10 −0 src/core/qgsfeature.h
  3. +17 −0 src/core/qgsgeometry.cpp
  4. +5 −0 src/core/qgsgeometry.h
@@ -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;
}
@@ -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 );
@@ -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;

@@ -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;
}
@@ -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;

0 comments on commit 6e3cbe3

Please sign in to comment.
You can’t perform that action at this time.