Skip to content

Commit

Permalink
[Server][WFS] add QgsWfsParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
rldhont committed Oct 12, 2017
1 parent c0117df commit c8a4407
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/server/services/wfs/CMakeLists.txt
Expand Up @@ -10,12 +10,19 @@ SET (wfs_SRCS
qgswfsdescribefeaturetype.cpp
qgswfsgetfeature.cpp
qgswfstransaction.cpp
qgswfsparameters.cpp
)

SET (wfs_MOC_HDRS
qgswfsparameters.h
)

########################################################
# Build

ADD_LIBRARY (wfs MODULE ${wfs_SRCS})
QT5_WRAP_CPP(wfs_MOC_SRCS ${wfs_MOC_HDRS})

ADD_LIBRARY (wfs MODULE ${wfs_SRCS} ${wfs_MOC_SRCS} ${wfs_MOC_HDRS})


INCLUDE_DIRECTORIES(SYSTEM
Expand All @@ -31,10 +38,10 @@ INCLUDE_DIRECTORIES(
${CMAKE_BINARY_DIR}/src/analysis
${CMAKE_BINARY_DIR}/src/server
${CMAKE_CURRENT_BINARY_DIR}
../../../core
../../../core
../../../core/dxf
../../../core/expression
../../../core/geometry
../../../core/geometry
../../../core/metadata
../../../core/raster
../../../core/symbology
Expand Down
193 changes: 193 additions & 0 deletions src/server/services/wfs/qgswfsparameters.cpp
@@ -0,0 +1,193 @@
/***************************************************************************
qgswfsparameters.cpp
--------------------
begin : Sept 14, 2017
copyright : (C) 2017 by René-Luc Dhont
email : rldhont at 3liz dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgswfsparameters.h"
#include "qgsmessagelog.h"
#include <iostream>

namespace QgsWfs
{
QgsWfsParameters::QgsWfsParameters()
{
// Available version number
mVersions.append( QgsProjectVersion( 1, 0, 0 ) );
mVersions.append( QgsProjectVersion( 1, 1, 0 ) );

const Parameter pOutputFormat = { ParameterName::OUTPUTFORMAT,
QVariant::String,
QVariant( "" ),
QVariant()
};
save( pOutputFormat );

const Parameter pSrsName = { ParameterName::SRSNAME,
QVariant::String,
QVariant( "" ),
QVariant()
};
save( pSrsName );
}

QgsWfsParameters::QgsWfsParameters( const QgsServerRequest::Parameters &parameters )
{
load( parameters );
}

void QgsWfsParameters::load( const QgsServerRequest::Parameters &parameters )
{
mRequestParameters = parameters;

const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
foreach ( QString key, parameters.keys() )
{
const ParameterName name = ( ParameterName ) metaEnum.keyToValue( key.toStdString().c_str() );
if ( name >= 0 )
{
QVariant value( parameters[key] );
if ( value.canConvert( mParameters[name].mType ) )
{
mParameters[name].mValue = value;
}
else
{
raiseError( name );
}
}
}
}

void QgsWfsParameters::dump() const
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );

log( "WFS Request parameters:" );
for ( auto parameter : mParameters.toStdMap() )
{
const QString value = parameter.second.mValue.toString();

if ( ! value.isEmpty() )
{
const QString name = metaEnum.valueToKey( parameter.first );
log( " - " + name + " : " + value );
}
}

if ( !version().isEmpty() )
log( " - VERSION : " + version() );
}

void QgsWfsParameters::save( const Parameter &parameter )
{
mParameters[ parameter.mName ] = parameter;
}

QVariant QgsWfsParameters::value( ParameterName name ) const
{
return mParameters[name].mValue;
}

QVariant QgsWfsParameters::defaultValue( ParameterName name ) const
{
return mParameters[name].mDefaultValue;
}

QString QgsWfsParameters::outputFormatAsString() const
{
return value( ParameterName::OUTPUTFORMAT ).toString();
}

QgsWfsParameters::Format QgsWfsParameters::outputFormat() const
{
QString fStr = outputFormatAsString();

if ( fStr.isEmpty() )
{
if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
return Format::GML3;
else
return Format::GML2;
}

Format f = Format::NONE;
if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/2.1.2" ), Qt::CaseInsensitive ) == 0 )
f = Format::GML2;
else if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/3.1.1" ), Qt::CaseInsensitive ) == 0 )
f = Format::GML3;
else if ( fStr.compare( QLatin1String( "application/vnd.geo+json" ), Qt::CaseInsensitive ) == 0 )
f = Format::GeoJSON;
else if ( fStr.compare( QLatin1String( "gml2" ), Qt::CaseInsensitive ) == 0 )
f = Format::GML2;
else if ( fStr.compare( QLatin1String( "gml3" ), Qt::CaseInsensitive ) == 0 )
f = Format::GML3;
else if ( fStr.compare( QLatin1String( "geojson" ), Qt::CaseInsensitive ) == 0 )
f = Format::GeoJSON;

return f;
}

QString QgsWfsParameters::srsName() const
{
return value( ParameterName::SRSNAME ).toString();
}

QString QgsWfsParameters::version() const
{
// VERSION parameter is not managed with other parameters because
// there's a conflict with qgis VERSION defined in qgsconfig.h
if ( mRequestParameters.contains( "VERSION" ) )
return mRequestParameters["VERSION"];
else
return QString();
}

QgsProjectVersion QgsWfsParameters::versionAsNumber() const
{
QString vStr = version();
QgsProjectVersion version;

if ( vStr.isEmpty() )
version = QgsProjectVersion( 1, 1, 0 ); // default value
else if ( mVersions.contains( QgsProjectVersion( vStr ) ) )
version = QgsProjectVersion( vStr );

return version;
}

QString QgsWfsParameters::name( ParameterName name ) const
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
return metaEnum.valueToKey( name );
}

void QgsWfsParameters::log( const QString &msg ) const
{
QgsMessageLog::logMessage( msg, "Server", QgsMessageLog::INFO );
}

void QgsWfsParameters::raiseError( ParameterName paramName ) const
{
const QString value = mParameters[paramName].mValue.toString();
const QString param = name( paramName );
const QString type = QVariant::typeToName( mParameters[paramName].mType );
raiseError( param + " ('" + value + "') cannot be converted into " + type );
}

void QgsWfsParameters::raiseError( const QString &msg ) const
{
throw QgsBadRequestException( QStringLiteral( "Invalid WFS Parameter" ), msg );
}
}
145 changes: 145 additions & 0 deletions src/server/services/wfs/qgswfsparameters.h
@@ -0,0 +1,145 @@
/***************************************************************************
qgswfsparameters.h
------------------
begin : Sept 14, 2017
copyright : (C) 2017 by René-Luc Dhont
email : rldhont at 3liz dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSWFSPARAMETERS_H
#define QGSWFSPARAMETERS_H

#include <QMap>
#include <QObject>
#include <QMetaEnum>

#include "qgswfsserviceexception.h"
#include "qgsserverrequest.h"
#include "qgsprojectversion.h"

/**
* QgsWfsParameters provides an interface to retrieve and manipulate WFS
* parameters received from the client.
* \since QGIS 3.0
*/
namespace QgsWfs
{

class QgsWfsParameters
{
Q_GADGET

public:
enum ParameterName
{
OUTPUTFORMAT,
SRSNAME
};
Q_ENUM( ParameterName )

enum Format
{
NONE,
GML2,
GML3,
GeoJSON,
XSD
};

struct Parameter
{
ParameterName mName;
QVariant::Type mType;
QVariant mDefaultValue;
QVariant mValue;
};

/**
* Constructor.
* \param map of parameters where keys are parameters' names.
*/
QgsWfsParameters( const QgsServerRequest::Parameters &parameters );

/**
* Constructor.
*/
QgsWfsParameters();

/**
* Loads new parameters.
* \param map of parameters
*/
void load( const QgsServerRequest::Parameters &parameters );

/**
* Dumps parameters.
*/
void dump() const;

<<<<<<< HEAD
/** Returns VERSION parameter as a string or an empty string if not
=======
/**
* Returns REQUEST parameter as a string or an empty string if not
* defined.
* \returns request
*/
QString request() const;

/**
* Returns VERSION parameter as a string or an empty string if not
>>>>>>> 747f00d... QgsWfsParameters
* defined.
* \returns version
*/
QString version() const;

/**
* Returns VERSION parameter if defined or its default value.
* \returns version
*/
QgsProjectVersion versionAsNumber() const;

/** Returns OUTPUTFORMAT parameter as a string.
* \returns OUTPUTFORMAT parameter as string
*/
QString outputFormatAsString() const;

/**
* Returns format. If the OUTPUTFORMAT parameter is not used, then the
* default value is GML2 or GML3.
* \returns format
*/
Format outputFormat() const;

/** Returns SRSNAME parameter as a string.
* \returns SRSNAME parameter as string
*/
QString srsName() const;


private:
QString name( ParameterName name ) const;
void raiseError( ParameterName name ) const;
void raiseError( const QString &msg ) const;
QVariant value( ParameterName name ) const;
QVariant defaultValue( ParameterName name ) const;
void log( const QString &msg ) const;
void save( const Parameter &parameter );

QgsServerRequest::Parameters mRequestParameters;
QMap<ParameterName, Parameter> mParameters;
QList<QgsProjectVersion> mVersions;
};
}

#endif
14 changes: 11 additions & 3 deletions src/server/services/wfs/qgswfsserviceexception.h
Expand Up @@ -72,9 +72,17 @@ namespace QgsWfs
{}
};




/** \ingroup server
* \class QgsBadRequestException
* \brief Exception thrown in case of malformed request
*/
class QgsBadRequestException: public QgsServiceException
{
public:
QgsBadRequestException( const QString &code, const QString &message, const QString &locator = QString() )
: QgsServiceException( code, message, locator, 400 )
{}
};


} // namespace QgsWfs
Expand Down

0 comments on commit c8a4407

Please sign in to comment.