28 #include "nlohmann/json.hpp"
35 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
36 const QStringList parts { bbox.split(
',', QString::SplitBehavior::SkipEmptyParts ) };
38 const QStringList parts { bbox.split(
',', Qt::SplitBehaviorFlags::SkipEmptyParts ) };
42 if ( parts.count() == 4 || parts.count() == 6 )
44 const auto hasZ { parts.count() == 6 };
45 auto toDouble = [ & ](
const int i ) ->
double
49 return parts[i].toDouble( &ok );
55 toDouble( 3 ), toDouble( 4 ) );
60 toDouble( 2 ), toDouble( 3 ) );
74 QList< QgsMapLayerServerProperties::WmsDimensionInfo > dimensions { serverProperties->
wmsDimensions() };
76 dimensions.erase( std::remove_if( dimensions.begin(),
80 return dim.name.toLower() != QStringLiteral(
"time" )
81 && dim.name.toLower() != QStringLiteral(
"date" ) ;
82 } ), dimensions.end() );
85 if ( dimensions.isEmpty() )
87 const auto constFields { layer->
fields() };
88 for (
const auto &f : constFields )
90 if ( f.isDateOrTime() )
93 QStringLiteral(
"time" ) :
94 QStringLiteral(
"date" ), f.name() ) );
103 template<
typename T,
class T2> T QgsServerApiUtils::parseTemporalInterval(
const QString &interval )
105 auto parseDate = [ ](
const QString & date ) -> T2
108 if ( date == QLatin1String(
".." ) || date.isEmpty() )
114 T2 result { T2::fromString( date, Qt::DateFormat::ISODate ) };
115 if ( !result.isValid() )
122 const QStringList parts { interval.split(
'/' ) };
123 if ( parts.length() != 2 )
127 T result { parseDate( parts[0] ), parseDate( parts[1] ) };
129 if ( result.isEmpty() )
139 return QgsServerApiUtils::parseTemporalInterval<QgsDateRange, QDate>( interval );
144 return QgsServerApiUtils::parseTemporalInterval<QgsDateTimeRange, QDateTime>( interval );
150 QStringList conditions;
153 if ( dimensions.isEmpty() )
159 auto fieldTypeFromName = [ & ](
const QString & fieldName,
const QgsVectorLayer * layer ) -> QVariant::Type
164 return QVariant::Type::Invalid;
171 auto refFieldCast = [ & ](
const QString & fieldName, QVariant::Type queryType, QVariant::Type fieldType ) -> QString
174 const auto fieldRealType { fieldTypeFromName( fieldName, layer ) };
175 if ( fieldRealType == QVariant::Type::Invalid )
182 if ( fieldRealType == QVariant::Type::String )
185 if ( fieldType != queryType || fieldType == QVariant::Type::Date )
192 .arg( queryType == QVariant::Type::Date ? QStringLiteral(
"to_date" ) : QStringLiteral(
"to_datetime" ) );
195 else if ( fieldType == queryType || fieldType == QVariant::Type::Date )
202 .arg( queryType == QVariant::Type::Date ? QStringLiteral(
"to_date" ) : QStringLiteral(
"to_datetime" ) );
207 auto quoteValue = [ ](
const QString & value ) -> QString
209 if ( value.length() == 10 )
220 auto makeFilter = [ "eValue ](
const QString & fieldBegin,
const QString & fieldEnd,
221 const QString & fieldBeginCasted,
const QString & fieldEndCasted,
222 const QString & queryBegin,
const QString & queryEnd ) -> QString
227 if ( ! queryBegin.isEmpty() && ! queryEnd.isEmpty() )
230 if ( ! fieldEndCasted.isEmpty() )
232 result = QStringLiteral(
"( %1 IS NULL OR %2 <= %6 ) AND ( %4 IS NULL OR %5 >= %3 )" )
235 quoteValue( queryBegin ),
238 quoteValue( queryEnd ) );
242 result = QStringLiteral(
"( %1 IS NULL OR ( %2 <= %3 AND %3 <= %4 ) )" )
244 quoteValue( queryBegin ),
246 quoteValue( queryEnd ) );
250 else if ( ! queryBegin.isEmpty() )
252 if ( ! fieldEndCasted.isEmpty() )
254 result = QStringLiteral(
"( %1 IS NULL OR %2 >= %3 )" ).arg( fieldEnd, fieldEndCasted, quoteValue( queryBegin ) );
258 result = QStringLiteral(
"( %1 IS NULL OR %2 >= %3 )" ).arg( fieldBegin, fieldBeginCasted, quoteValue( queryBegin ) );
263 result = QStringLiteral(
"( %1 IS NULL OR %2 <= %3 )" ).arg( fieldBegin, fieldBeginCasted, quoteValue( queryEnd ) );
269 QString testType { interval };
270 if ( interval.contains(
'/' ) )
272 const QStringList parts { interval.split(
'/' ) };
274 if ( testType.isEmpty() || testType == QLatin1String(
".." ) )
280 const bool inputQueryIsDateTime { testType.length() > 10 };
281 const QVariant::Type queryType { inputQueryIsDateTime ? QVariant::Type::DateTime : QVariant::Type::Date };
284 if ( interval.contains(
'/' ) )
286 if ( ! inputQueryIsDateTime )
290 for (
const auto &dimension : std::as_const( dimensions ) )
294 const QVariant::Type fieldType { dimension.name.toLower() == QLatin1String(
"time" ) ? QVariant::Type::DateTime : QVariant::Type::Date };
296 const auto fieldBeginCasted { refFieldCast( dimension.fieldName, queryType, fieldType ) };
297 if ( fieldBeginCasted.isEmpty() )
306 const auto fieldEndCasted { refFieldCast( dimension.endFieldName, queryType, fieldType ) };
307 if ( ! dateInterval.begin().isValid( ) && ! dateInterval.end().isValid( ) )
313 conditions.push_back( makeFilter( fieldBegin,
317 dateInterval.begin().toString( Qt::DateFormat::ISODate ),
318 dateInterval.end().toString( Qt::DateFormat::ISODate ) ) );
326 for (
const auto &dimension : std::as_const( dimensions ) )
330 const QVariant::Type fieldType { dimension.name.toLower() == QLatin1String(
"time" ) ? QVariant::Type::DateTime : QVariant::Type::Date };
332 const auto fieldfBeginCasted { refFieldCast( dimension.fieldName, queryType, fieldType ) };
333 if ( fieldfBeginCasted.isEmpty() )
341 const auto fieldEndCasted { refFieldCast( dimension.endFieldName, queryType, fieldType ) };
342 if ( ! dateTimeInterval.begin().isValid( ) && ! dateTimeInterval.end().isValid( ) )
352 if ( fieldType == QVariant::Type::Date )
354 beginQuery = dateTimeInterval.begin().date().toString( Qt::DateFormat::ISODate );
355 endQuery = dateTimeInterval.end().date().toString( Qt::DateFormat::ISODate );
359 beginQuery = dateTimeInterval.begin().toString( Qt::DateFormat::ISODate );
360 endQuery = dateTimeInterval.end().toString( Qt::DateFormat::ISODate );
362 conditions.push_back( makeFilter( fieldBegin,
375 for (
const auto &dimension : std::as_const( dimensions ) )
378 const bool fieldIsDateTime { dimension.name.toLower() == QLatin1String(
"time" ) };
379 const QVariant::Type fieldType { fieldIsDateTime ? QVariant::Type::DateTime : QVariant::Type::Date };
381 const auto fieldRefBegin { refFieldCast( dimension.fieldName, queryType, fieldType ) };
382 if ( fieldRefBegin.isEmpty() )
389 const auto fieldRefEnd { refFieldCast( dimension.endFieldName, queryType, fieldType ) };
396 if ( ! inputQueryIsDateTime || ! fieldIsDateTime )
398 QString castedInterval { interval };
400 if ( inputQueryIsDateTime )
402 castedInterval = QDate::fromString( castedInterval, Qt::DateFormat::ISODate ).toString( Qt::DateFormat::ISODate );
408 QString castedInterval { interval };
410 if ( ! inputQueryIsDateTime )
412 castedInterval = QDateTime::fromString( castedInterval, Qt::DateFormat::ISODate ).toString( Qt::DateFormat::ISODate );
417 if ( ! fieldRefEnd.isEmpty() )
419 condition = QStringLiteral(
"( %1 IS NULL OR %2 <= %3 ) AND ( %5 IS NULL OR %3 <= %4 )" ).arg(
428 condition = QStringLiteral(
"( %1 IS NULL OR %2 = %3 )" )
434 conditions.push_back( condition );
438 if ( ! conditions.isEmpty() )
440 expression.
setExpression( conditions.join( QLatin1String(
" AND " ) ) );
447 auto extent { layer->
extent() };
448 if ( layer->
crs().
authid() != QLatin1String(
"EPSG:4326" ) )
454 return {{ extent.xMinimum(), extent.yMinimum(), extent.xMaximum(), extent.yMaximum() }};
462 QgsDateTimeRange result;
474 QDateTime min { minVal.toDateTime() };
475 QDateTime max { maxVal.toDateTime() };
476 if ( ! dimInfo.endFieldName.isEmpty() )
485 QDateTime minEnd { minVal.toDateTime() };
486 QDateTime maxEnd { maxVal.toDateTime() };
487 if ( minEnd.isValid() )
489 min = std::min<QDateTime>( min, minEnd );
491 if ( maxEnd.isValid() )
493 max = std::max<QDateTime>( max, maxEnd );
501 if ( dimensions.isEmpty() )
509 QgsDateTimeRange extent;
511 for (
const auto &dimension : dimensions )
516 extent = range( dimension );
521 extent.extend( range( dimension ) );
524 json ret = json::array();
525 const QString beginVal { extent.begin().toString( Qt::DateFormat::ISODate ) };
526 const QString endVal { extent.end().toString( Qt::DateFormat::ISODate ) };
528 if ( beginVal.isEmpty() && endVal.isEmpty() )
530 ret.push_back( {
nullptr,
nullptr } );
532 else if ( beginVal.isEmpty() )
534 ret.push_back( {
nullptr, endVal.toStdString() } );
536 else if ( endVal.isEmpty() )
538 ret.push_back( { beginVal.toStdString(),
nullptr } );
542 ret.push_back( { beginVal.toStdString(), endVal.toStdString() } );
546 catch ( std::exception &ex )
548 const QString errorMessage { QStringLiteral(
"Error creating temporal extent: %1" ).arg( ex.what() ) };
568 const auto parts { QUrl( bboxCrs ).path().split(
'/' ) };
569 if ( parts.count() == 6 )
581 return publishedWfsLayers< QgsVectorLayer * >( context );
586 QString result { QUrl( value ).toString() };
587 return result.replace(
'\'', QLatin1String(
"\'" ) );
593 QStringList result { { QStringLiteral(
"http://www.opengis.net/def/crs/OGC/1.3/CRS84" )}};
597 for (
const QString &crsId : outputCrsList )
600 if ( ! crsUri.isEmpty() )
602 result.push_back( crsUri );
611 const auto parts {
crs.
authid().split(
':' ) };
612 if ( parts.length() == 2 )
614 if ( parts[0] == QLatin1String(
"EPSG" ) )
615 return QStringLiteral(
"http://www.opengis.net/def/crs/EPSG/9.6.2/%1" ).arg( parts[1] ) ;
616 else if ( parts[0] == QLatin1String(
"OGC" ) )
618 return QStringLiteral(
"http://www.opengis.net/def/crs/OGC/1.3/%1" ).arg( parts[1] ) ;
622 QgsMessageLog::logMessage( QStringLiteral(
"Error converting published CRS to URI %1: (not OGC or EPSG)" ).arg(
crs.
authid() ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
627 QgsMessageLog::logMessage( QStringLiteral(
"Error converting published CRS to URI: %1" ).arg(
crs.
authid() ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
634 QList<QPair<QString, QString> > qi;
635 QString result { path };
636 const auto constItems { QUrlQuery( requestUrl ).queryItems() };
637 for (
const auto &i : constItems )
639 if ( i.first.compare( QStringLiteral(
"MAP" ), Qt::CaseSensitivity::CaseInsensitive ) == 0 )
646 if ( ! path.endsWith(
'?' ) )
650 result.append( QStringLiteral(
"MAP=%1" ).arg( qi.first().second ) );