Skip to content
Permalink
Browse files

Server: if there is a primary key, use the pk value(s) as gml id inst…

…ead of the feature id
  • Loading branch information
mhugent committed Feb 6, 2018
1 parent b6b394c commit 551547aea527ba4e7a281f60d0eac6239ecf3a06
@@ -104,7 +104,8 @@ class QgsWFSServer: public QgsOWSServer
protected:

void startGetFeature( QgsRequestHandler& request, const QString& format, int prec, QgsCoordinateReferenceSystem& crs, QgsRectangle* rect );
void setGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes );
void setGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes,
const QgsAttributeList& pkAttributes = QgsAttributeList());
void endGetFeature( QgsRequestHandler& request, const QString& format );

//method for transaction
@@ -114,10 +115,12 @@ class QgsWFSServer: public QgsOWSServer
QString createFeatureGeoJSON( QgsFeature* feat, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes ) /*const*/;

//methods to write GML2
QDomElement createFeatureGML2( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes ) /*const*/;
QDomElement createFeatureGML2( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes,
const QgsAttributeList& pkAttributes = QgsAttributeList()) /*const*/;

//methods to write GML3
QDomElement createFeatureGML3( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, QgsAttributeList attrIndexes, QSet<QString> excludedAttributes ) /*const*/;
QDomElement createFeatureGML3( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, QgsAttributeList attrIndexes, QSet<QString> excludedAttributes,
const QgsAttributeList& pkAttributes = QgsAttributeList() ) /*const*/;

void addTransactionResult( QDomDocument& responseDoc, QDomElement& responseElem, const QString& status, const QString& locator, const QString& message );
};
@@ -64,3 +64,24 @@ void QgsOWSServer::restoreLayerFilters( const QHash<QgsMapLayer*, QString>& filt
}
}
}

QString QgsOWSServer::featureGmlId( const QgsFeature* f, const QgsAttributeList& pkAttributes )
{
if ( !f )
{
return QString();
}

if ( pkAttributes.isEmpty() )
{
return QString::number( f->id() );
}

QString pkId;
QgsAttributeList::const_iterator it = pkAttributes.constBegin();
for ( ; it != pkAttributes.constEnd(); ++it )
{
pkId.append( f->attribute( *it ).toString() );
}
return pkId;
}
@@ -23,6 +23,7 @@
#include "qgsaccesscontrol.h"
#endif

#include "qgsfield.h"
#include <QHash>

class QgsMapLayer;
@@ -73,6 +74,13 @@ class QgsOWSServer
void applyAccessControlLayerFilters( QgsMapLayer* layer, QHash<QgsMapLayer*, QString>& originalLayerFilters ) const;
#endif

/** Creates gml id for feature. Returns primary key value or feature id in case there is no PK
* @param f feature
* @param pkAttributes list of attribute indices used as primary key
* @return pk as string or feature id
*/
static QString featureGmlId( const QgsFeature* f, const QgsAttributeList& pkAttributes );

};

/** RAII class to restore layer filters on destruction
@@ -603,7 +603,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
if ( featureCounter == 0 )
startGetFeature( request, format, layerPrec, layerCrs, &searchRect );

setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );

fid = "";
++featCounter;
@@ -642,7 +642,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format

if ( featureCounter >= startIndex )
{
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );
++featCounter;
}
++featureCounter;
@@ -696,7 +696,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format

if ( featureCounter >= startIndex )
{
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );
++featCounter;
}
++featureCounter;
@@ -714,7 +714,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format

if ( featureCounter >= startIndex )
{
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );
++featCounter;
}
++featureCounter;
@@ -977,7 +977,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
if ( featureCounter == 0 )
startGetFeature( request, format, layerPrec, layerCrs, &searchRect );

setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );
++featCounter;
++featureCounter;
}
@@ -1026,7 +1026,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format

if ( featureCounter >= startIndex )
{
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );
++featCounter;
}
++featureCounter;
@@ -1063,7 +1063,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
if ( featureCounter == 0 )
startGetFeature( request, format, layerPrec, layerCrs, &searchRect );

setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );

fid = "";
++featCounter;
@@ -1102,7 +1102,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format

if ( featureCounter >= startIndex )
{
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );
++featCounter;
}
++featureCounter;
@@ -1151,7 +1151,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format

if ( featureCounter >= startIndex )
{
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );
++featCounter;
}
++featureCounter;
@@ -1190,7 +1190,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format

if ( featureCounter >= startIndex )
{
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes );
setGetFeature( request, format, &feature, featCounter, layerPrec, layerCrs, attrIndexes, layerExcludedAttributes, provider->pkAttributeIndexes() );
++featCounter;
}
++featureCounter;
@@ -1363,7 +1363,8 @@ void QgsWFSServer::startGetFeature( QgsRequestHandler& request, const QString& f
fcString = "";
}

void QgsWFSServer::setGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes ) /*const*/
void QgsWFSServer::setGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes,
const QgsAttributeList& pkAttributes ) /*const*/
{
if ( !feat->isValid() )
return;
@@ -1389,12 +1390,12 @@ void QgsWFSServer::setGetFeature( QgsRequestHandler& request, const QString& for
QDomElement featureElement;
if ( format == "GML3" )
{
featureElement = createFeatureGML3( feat, gmlDoc, prec, crs, attrIndexes, excludedAttributes );
featureElement = createFeatureGML3( feat, gmlDoc, prec, crs, attrIndexes, excludedAttributes, pkAttributes );
gmlDoc.appendChild( featureElement );
}
else
{
featureElement = createFeatureGML2( feat, gmlDoc, prec, crs, attrIndexes, excludedAttributes );
featureElement = createFeatureGML2( feat, gmlDoc, prec, crs, attrIndexes, excludedAttributes, pkAttributes );
gmlDoc.appendChild( featureElement );
}

@@ -1963,14 +1964,16 @@ QString QgsWFSServer::createFeatureGeoJSON( QgsFeature* feat, int prec, QgsCoord
return exporter.exportFeature( f, QVariantMap(), id );
}

QDomElement QgsWFSServer::createFeatureGML2( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes ) /*const*/
QDomElement QgsWFSServer::createFeatureGML2( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes,
const QgsAttributeList& pkAttributes ) /*const*/
{
//gml:FeatureMember
QDomElement featureElement = doc.createElement( "gml:featureMember"/*wfs:FeatureMember*/ );

//qgs:%TYPENAME%
QDomElement typeNameElement = doc.createElement( "qgs:" + mTypeName /*qgs:%TYPENAME%*/ );
typeNameElement.setAttribute( "fid", mTypeName + "." + QString::number( feat->id() ) );
QString gmlId = featureGmlId( feat, pkAttributes );
typeNameElement.setAttribute( "fid", mTypeName + "." + gmlId );
featureElement.appendChild( typeNameElement );

if ( mWithGeom && mGeometryName != "NONE" )
@@ -2047,14 +2050,16 @@ QDomElement QgsWFSServer::createFeatureGML2( QgsFeature* feat, QDomDocument& doc
return featureElement;
}

QDomElement QgsWFSServer::createFeatureGML3( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes ) /*const*/
QDomElement QgsWFSServer::createFeatureGML3( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes,
const QgsAttributeList& pkAttributes ) /*const*/
{
//gml:FeatureMember
QDomElement featureElement = doc.createElement( "gml:featureMember"/*wfs:FeatureMember*/ );

//qgs:%TYPENAME%
QDomElement typeNameElement = doc.createElement( "qgs:" + mTypeName /*qgs:%TYPENAME%*/ );
typeNameElement.setAttribute( "gml:id", mTypeName + "." + QString::number( feat->id() ) );
QString gmlId = featureGmlId( feat, pkAttributes );
typeNameElement.setAttribute( "gml:id", mTypeName + "." + gmlId );
featureElement.appendChild( typeNameElement );

if ( mWithGeom && mGeometryName != "NONE" )
@@ -112,7 +112,8 @@ class QgsWFSServer: public QgsOWSServer
protected:

void startGetFeature( QgsRequestHandler& request, const QString& format, int prec, QgsCoordinateReferenceSystem& crs, QgsRectangle* rect );
void setGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes );
void setGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes,
const QgsAttributeList& pkAttributes = QgsAttributeList() );
void endGetFeature( QgsRequestHandler& request, const QString& format );

//method for transaction
@@ -122,10 +123,12 @@ class QgsWFSServer: public QgsOWSServer
QString createFeatureGeoJSON( QgsFeature* feat, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes ) /*const*/;

//methods to write GML2
QDomElement createFeatureGML2( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes ) /*const*/;
QDomElement createFeatureGML2( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes,
const QgsAttributeList& pkAttributes = QgsAttributeList() ) /*const*/;

//methods to write GML3
QDomElement createFeatureGML3( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes ) /*const*/;
QDomElement createFeatureGML3( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes, const QSet<QString>& excludedAttributes,
const QgsAttributeList& pkAttributes = QgsAttributeList() ) /*const*/;

void addTransactionResult( QDomDocument& responseDoc, QDomElement& responseElem, const QString& status, const QString& locator, const QString& message );
};
@@ -3312,7 +3312,8 @@ QDomElement QgsWMSServer::createFeatureGML(
{
//qgs:%TYPENAME%
QDomElement typeNameElement = doc.createElement( "qgs:" + typeName /*qgs:%TYPENAME%*/ );
typeNameElement.setAttribute( "fid", typeName + "." + QString::number( feat->id() ) );
QString gmlId = featureGmlId( feat, layer->dataProvider()->pkAttributeIndexes() );
typeNameElement.setAttribute( "fid", typeName + "." + gmlId );

const QgsCoordinateTransform* transform = nullptr;
if ( layer && layer->crs() != crs )

0 comments on commit 551547a

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