Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
359 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¶meters ) | ||
{ | ||
load( parameters ); | ||
} | ||
|
||
void QgsWfsParameters::load( const QgsServerRequest::Parameters ¶meters ) | ||
{ | ||
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 ¶meter ) | ||
{ | ||
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¶meters ); | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
QgsWfsParameters(); | ||
|
||
/** | ||
* Loads new parameters. | ||
* \param map of parameters | ||
*/ | ||
void load( const QgsServerRequest::Parameters ¶meters ); | ||
|
||
/** | ||
* 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 ¶meter ); | ||
|
||
QgsServerRequest::Parameters mRequestParameters; | ||
QMap<ParameterName, Parameter> mParameters; | ||
QList<QgsProjectVersion> mVersions; | ||
}; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters