26#include <QCryptographicHash>
28#include <nlohmann/json.hpp>
32QgsSensorThingsSharedData::QgsSensorThingsSharedData(
const QString &uri )
34 const QVariantMap uriParts = QgsSensorThingsProviderMetadata().decodeUri( uri );
40 mMaximumPageSize = uriParts.value( QStringLiteral(
"pageSize" ), mMaximumPageSize ).toInt();
42 mFeatureLimit = uriParts.value( QStringLiteral(
"featureLimit" ) ).toInt();
43 mFilterExtent = uriParts.value( QStringLiteral(
"bounds" ) ).value<
QgsRectangle >();
44 mSubsetString = uriParts.value( QStringLiteral(
"sql" ) ).toString();
48 const QString geometryType = uriParts.value( QStringLiteral(
"geometryType" ) ).toString();
49 if ( geometryType.compare( QLatin1String(
"point" ), Qt::CaseInsensitive ) == 0 )
53 else if ( geometryType.compare( QLatin1String(
"multipoint" ), Qt::CaseInsensitive ) == 0 )
57 else if ( geometryType.compare( QLatin1String(
"line" ), Qt::CaseInsensitive ) == 0 )
61 else if ( geometryType.compare( QLatin1String(
"polygon" ), Qt::CaseInsensitive ) == 0 )
78 mRootUri = uriParts.value( QStringLiteral(
"url" ) ).toString();
81QUrl QgsSensorThingsSharedData::parseUrl(
const QUrl &url,
bool *isTestEndpoint )
84 *isTestEndpoint =
false;
86 QUrl modifiedUrl( url );
87 if ( modifiedUrl.toString().contains( QLatin1String(
"fake_qgis_http_endpoint" ) ) )
90 *isTestEndpoint =
true;
93 QString modifiedUrlString = modifiedUrl.toString();
95 modifiedUrlString = QUrl::fromPercentEncoding( modifiedUrlString.toUtf8() );
96 modifiedUrlString.replace( QLatin1String(
"fake_qgis_http_endpoint/" ), QLatin1String(
"fake_qgis_http_endpoint_" ) );
98 modifiedUrlString = modifiedUrlString.mid( QStringLiteral(
"http://" ).size() );
99 QString args = modifiedUrlString.indexOf(
'?' ) >= 0 ? modifiedUrlString.mid( modifiedUrlString.indexOf(
'?' ) ) : QString();
100 if ( modifiedUrlString.size() > 150 )
102 args = QCryptographicHash::hash( args.toUtf8(), QCryptographicHash::Md5 ).toHex();
106 args.replace( QLatin1String(
"?" ), QLatin1String(
"_" ) );
107 args.replace( QLatin1String(
"&" ), QLatin1String(
"_" ) );
108 args.replace( QLatin1String(
"$" ), QLatin1String(
"_" ) );
109 args.replace( QLatin1String(
"<" ), QLatin1String(
"_" ) );
110 args.replace( QLatin1String(
">" ), QLatin1String(
"_" ) );
111 args.replace( QLatin1String(
"'" ), QLatin1String(
"_" ) );
112 args.replace( QLatin1String(
"\"" ), QLatin1String(
"_" ) );
113 args.replace( QLatin1String(
" " ), QLatin1String(
"_" ) );
114 args.replace( QLatin1String(
":" ), QLatin1String(
"_" ) );
115 args.replace( QLatin1String(
"/" ), QLatin1String(
"_" ) );
116 args.replace( QLatin1String(
"\n" ), QLatin1String(
"_" ) );
121 if ( modifiedUrlString[1] ==
'/' )
123 modifiedUrlString = modifiedUrlString[0] +
":/" + modifiedUrlString.mid( 2 );
126 modifiedUrlString = modifiedUrlString.mid( 0, modifiedUrlString.indexOf(
'?' ) ) + args;
127 QgsDebugMsgLevel( QStringLiteral(
"Get %1 (after laundering)" ).arg( modifiedUrlString ), 2 );
128 modifiedUrl = QUrl::fromLocalFile( modifiedUrlString );
129 if ( !QFile::exists( modifiedUrlString ) )
131 QgsDebugError( QStringLiteral(
"Local test file %1 for URL %2 does not exist!!!" ).arg( modifiedUrlString, url.toString() ) );
144 return hasCachedAllFeatures() ? mFetchedFeatureExtent
145 : ( !mFilterExtent.isNull() ? mFilterExtent :
QgsRectangle( -180, -90, 180, 90 ) );
148long long QgsSensorThingsSharedData::featureCount(
QgsFeedback *feedback )
const
151 if ( mFeatureCount >= 0 )
152 return mFeatureCount;
158 QString countUri = QStringLiteral(
"%1?$top=0&$count=true" ).arg( mEntityBaseUri );
162 if ( !filterString.isEmpty() )
163 filterString = QStringLiteral(
"&$filter=" ) + filterString;
164 if ( !filterString.isEmpty() )
165 countUri += filterString;
167 const QUrl url = parseUrl( QUrl( countUri ) );
169 QNetworkRequest request( url );
171 mHeaders.updateNetworkRequest( request );
178 return mFeatureCount;
192 if ( !rootContent.contains(
"@iot.count" ) )
194 mError = QObject::tr(
"No '@iot.count' value in response" );
195 return mFeatureCount;
198 mFeatureCount = rootContent[
"@iot.count"].get<
long long>();
199 if ( mFeatureLimit > 0 && mFeatureCount > mFeatureLimit )
200 mFeatureCount = mFeatureLimit;
202 catch (
const json::parse_error &ex )
204 mError = QObject::tr(
"Error parsing response: %1" ).arg( ex.what() );
208 return mFeatureCount;
211QString QgsSensorThingsSharedData::subsetString()
const
213 return mSubsetString;
216bool QgsSensorThingsSharedData::hasCachedAllFeatures()
const
219 return mHasCachedAllFeatures
220 || ( mFeatureCount > 0 && mCachedFeatures.size() == mFeatureCount )
221 || ( mFeatureLimit > 0 && mCachedFeatures.size() >= mFeatureLimit );
229 QMap<QgsFeatureId, QgsFeature>::const_iterator it = mCachedFeatures.constFind(
id );
230 if ( it != mCachedFeatures.constEnd() )
236 if ( hasCachedAllFeatures() )
239 bool featureFetched =
false;
241 if ( mNextPage.isEmpty() )
245 int thisPageSize = mMaximumPageSize;
246 if ( mFeatureLimit > 0 && ( mCachedFeatures.size() + thisPageSize ) > mFeatureLimit )
247 thisPageSize = mFeatureLimit - mCachedFeatures.size();
249 mNextPage = QStringLiteral(
"%1?$top=%2&$count=false" ).arg( mEntityBaseUri ).arg( thisPageSize );
253 if ( !filterString.isEmpty() )
254 mNextPage += QStringLiteral(
"&$filter=" ) + filterString;
259 processFeatureRequest( mNextPage, feedback, [
id, &f, &featureFetched](
const QgsFeature & feature )
261 if ( feature.
id() ==
id )
264 featureFetched = true;
267 }, [&featureFetched,
this]
269 return !featureFetched && !hasCachedAllFeatures();
273 mHasCachedAllFeatures =
true;
276 return featureFetched;
285 if ( hasCachedAllFeatures() || mCachedExtent.contains( extentGeom ) )
288 return qgis::listToSet( mSpatialIndex.intersects( requestExtent ) );
294 if ( !filterString.isEmpty() )
295 filterString = QStringLiteral(
"&$filter=" ) + filterString;
296 int thisPageSize = mMaximumPageSize;
298 if ( !thisPage.isEmpty() )
301 const thread_local QRegularExpression topRe( QStringLiteral(
"\\$top=\\d+" ) );
302 const QRegularExpressionMatch match = topRe.match( queryUrl );
303 if ( match.hasMatch() )
305 if ( mFeatureLimit > 0 && ( mCachedFeatures.size() + thisPageSize ) > mFeatureLimit )
306 thisPageSize = mFeatureLimit - mCachedFeatures.size();
307 queryUrl = queryUrl.left( match.capturedStart( 0 ) ) + QStringLiteral(
"$top=%1" ).arg( thisPageSize ) + queryUrl.mid( match.capturedEnd( 0 ) );
312 queryUrl = QStringLiteral(
"%1?$top=%2&$count=false%3" ).arg( mEntityBaseUri ).arg( thisPageSize ).arg( filterString );
315 if ( thisPage.isEmpty() && mCachedExtent.intersects( extentGeom ) )
321 return qgis::listToSet( mSpatialIndex.intersects( requestExtent ) );
328 bool noMoreFeatures =
false;
329 bool hasFirstPage =
false;
330 const bool res = processFeatureRequest( queryUrl, feedback, [&ids, &alreadyFetchedIds](
const QgsFeature & feature )
332 if ( !alreadyFetchedIds.contains( feature.
id() ) )
333 ids.insert( feature.
id() );
345 noMoreFeatures =
true;
347 if ( noMoreFeatures && res && ( !feedback || !feedback->
isCanceled() ) )
352 nextPage = noMoreFeatures || !res ? QString() : queryUrl;
357void QgsSensorThingsSharedData::clearCache()
362 mCachedFeatures.clear();
363 mIotIdToFeatureId.clear();
368bool QgsSensorThingsSharedData::processFeatureRequest( QString &nextPage,
QgsFeedback *feedback,
const std::function<
void(
const QgsFeature & ) > &fetchedFeatureCallback,
const std::function<
bool ()> &continueFetchingCallback,
const std::function<
void ()> &onNoMoreFeaturesCallback )
373 const QString authcfg = mAuthCfg;
377 while ( continueFetchingCallback() )
386 const QUrl url = parseUrl( nextPage );
388 QNetworkRequest request( url );
414 if ( !rootContent.contains(
"value" ) )
417 mError = QObject::tr(
"No 'value' in response" );
424 const auto &values = rootContent[
"value"];
425 if ( values.empty() )
429 onNoMoreFeaturesCallback();
436 for (
const auto &featureData : values )
438 auto getString = [](
const basic_json<> &json,
const char *tag ) -> QVariant
440 if ( !json.contains( tag ) )
443 const auto &jObj = json[tag];
444 if ( jObj.is_number_integer() )
446 return QString::number( jObj.get<
int>() );
448 else if ( jObj.is_number_unsigned() )
450 return QString::number( jObj.get<
unsigned>() );
452 else if ( jObj.is_boolean() )
454 return QString::number( jObj.get<
bool>() );
456 else if ( jObj.is_number_float() )
458 return QString::number( jObj.get<
double>() );
461 return QString::fromStdString( json[tag].get<std::string >() );
464 auto getDateTime = [](
const basic_json<> &json,
const char *tag ) -> QVariant
466 if ( !json.contains( tag ) )
469 const auto &jObj = json[tag];
470 if ( jObj.is_string() )
472 const QString dateTimeString = QString::fromStdString( json[tag].get<std::string >() );
473 return QDateTime::fromString( dateTimeString, Qt::ISODateWithMs );
479 auto getVariantMap = [](
const basic_json<> &json,
const char *tag ) -> QVariant
481 if ( !json.contains( tag ) )
487 auto getStringList = [](
const basic_json<> &json,
const char *tag ) -> QVariant
489 if ( !json.contains( tag ) )
492 const auto &jObj = json[tag];
493 if ( jObj.is_string() )
495 return QStringList{ QString::fromStdString( json[tag].get<std::string >() ) };
497 else if ( jObj.is_array() )
500 for (
const auto &element : jObj )
502 if ( element.is_string() )
503 res.append( QString::fromStdString( element.get<std::string >() ) );
511 auto getDateTimeRange = [](
const basic_json<> &json,
const char *tag ) -> std::pair< QVariant, QVariant >
513 if ( !json.contains( tag ) )
514 return { QVariant(), QVariant() };
516 const auto &jObj = json[tag];
517 if ( jObj.is_string() )
519 const QString rangeString = QString::fromStdString( json[tag].get<std::string >() );
520 const QStringList rangeParts = rangeString.split(
'/' );
521 if ( rangeParts.size() == 2 )
525 QDateTime::fromString( rangeParts.at( 0 ), Qt::ISODateWithMs ),
526 QDateTime::fromString( rangeParts.at( 1 ), Qt::ISODateWithMs )
531 return { QVariant(), QVariant() };
535 const QString iotId = getString( featureData,
"@iot.id" ).toString();
536 auto existingFeatureIdIt = mIotIdToFeatureId.constFind( iotId );
537 if ( existingFeatureIdIt != mIotIdToFeatureId.constEnd() )
540 fetchedFeatureCallback( *mCachedFeatures.find( *existingFeatureIdIt ) );
545 feature.
setId( mNextFeatureId++ );
547 const QString selfLink = getString( featureData,
"@iot.selfLink" ).toString();
549 const QVariant properties = getVariantMap( featureData,
"properties" );
551 switch ( mEntityType )
561 << getString( featureData,
"name" )
562 << getString( featureData,
"description" )
572 << getString( featureData,
"name" )
573 << getString( featureData,
"description" )
583 << getDateTime( featureData,
"time" )
589 std::pair< QVariant, QVariant > phenomenonTime = getDateTimeRange( featureData,
"phenomenonTime" );
590 std::pair< QVariant, QVariant > resultTime = getDateTimeRange( featureData,
"resultTime" );
595 << getString( featureData,
"name" )
596 << getString( featureData,
"description" )
597 << getVariantMap( featureData,
"unitOfMeasurement" )
598 << getString( featureData,
"observationType" )
600 << phenomenonTime.first
601 << phenomenonTime.second
613 << getString( featureData,
"name" )
614 << getString( featureData,
"description" )
615 << getString( featureData,
"metadata" )
625 << getString( featureData,
"name" )
626 << getString( featureData,
"definition" )
627 << getString( featureData,
"description" )
634 std::pair< QVariant, QVariant > phenomenonTime = getDateTimeRange( featureData,
"phenomenonTime" );
635 std::pair< QVariant, QVariant > validTime = getDateTimeRange( featureData,
"validTime" );
640 << phenomenonTime.first
641 << phenomenonTime.second
642 << getString( featureData,
"result" )
643 << getDateTime( featureData,
"resultTime" )
644 << getStringList( featureData,
"resultQuality" )
647 << getVariantMap( featureData,
"parameters" )
657 << getString( featureData,
"name" )
658 << getString( featureData,
"description" )
668 if ( featureData.contains( mGeometryField.toLocal8Bit().constData() ) )
670 const auto &geometryPart = featureData[mGeometryField.toLocal8Bit().constData()];
671 if ( geometryPart.contains(
"geometry" ) )
678 mCachedFeatures.insert( feature.
id(), feature );
679 mIotIdToFeatureId.insert( iotId, feature.
id() );
680 mSpatialIndex.addFeature( feature );
683 fetchedFeatureCallback( feature );
685 if ( mFeatureLimit > 0 && mFeatureLimit <= mCachedFeatures.size() )
690 if ( rootContent.contains(
"@iot.nextLink" ) && ( mFeatureLimit == 0 || mFeatureLimit > mCachedFeatures.size() ) )
692 nextPage = QString::fromStdString( rootContent[
"@iot.nextLink"].get<std::string>() );
696 onNoMoreFeaturesCallback();
700 if ( !continueFetchingCallback() )
707 catch (
const json::parse_error &ex )
710 mError = QObject::tr(
"Error parsing response: %1" ).arg( ex.what() );
711 QgsDebugMsgLevel( QStringLiteral(
"Error parsing response: %1" ).arg( ex.what() ), 2 );
@ Sensor
A Sensor is an instrument that observes a property or phenomenon with the goal of producing an estima...
@ ObservedProperty
An ObservedProperty specifies the phenomenon of an Observation.
@ Invalid
An invalid/unknown entity.
@ FeatureOfInterest
In the context of the Internet of Things, many Observations’ FeatureOfInterest can be the Location of...
@ Datastream
A Datastream groups a collection of Observations measuring the same ObservedProperty and produced by ...
@ Observation
An Observation is the act of measuring or otherwise determining the value of a property.
@ Location
A Location entity locates the Thing or the Things it associated with. A Thing’s Location entity is de...
@ Thing
A Thing is an object of the physical world (physical things) or the information world (virtual things...
@ HistoricalLocation
A Thing’s HistoricalLocation entity set provides the times of the current (i.e., last known) and prev...
@ MultiPointZ
MultiPointZ.
@ MultiLineStringZ
MultiLineStringZ.
@ MultiPolygonZ
MultiPolygonZ.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "get" operation on the specified request.
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
This class represents a coordinate reference system (CRS).
Class for storing the component parts of a RDBMS data source URI (e.g.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
QgsHttpHeaders httpHeaders() const
Returns http headers.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setId(QgsFeatureId id)
Sets the feature id for this feature.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries, const QgsGeometryParameters ¶meters=QgsGeometryParameters())
Compute the unary union on a list of geometries.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsGeometry geometryFromGeoJson(const json &geometry)
Parses a GeoJSON "geometry" value to a QgsGeometry object.
static QVariant jsonToVariant(const json &value)
Converts a JSON value to a QVariant, in case of parsing error an invalid QVariant is returned.
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QByteArray content() const
Returns the reply content.
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
A rectangle specified with double values.
bool isNull() const
Test if the rectangle is null (holding no spatial information).
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
static QString combineFilters(const QStringList &filters)
Combines a set of SensorThings API filter operators.
static QString filterForWkbType(Qgis::SensorThingsEntity entityType, Qgis::WkbType wkbType)
Returns a filter string which restricts results to those matching the specified entityType and wkbTyp...
static bool entityTypeHasGeometry(Qgis::SensorThingsEntity type)
Returns true if the specified entity type can have geometry attached.
static QString geometryFieldForEntityType(Qgis::SensorThingsEntity type)
Returns the geometry field for a specified entity type.
static QgsFields fieldsForEntityType(Qgis::SensorThingsEntity type)
Returns the fields which correspond to a specified entity type.
static QString filterForExtent(const QString &geometryField, const QgsRectangle &extent)
Returns a filter string which restricts results to those within the specified extent.
A spatial index for QgsFeature objects.
@ Uncounted
Feature count not yet computed.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
#define QgsSetRequestInitiatorClass(request, _class)
QgsSQLStatement::Node * parse(const QString &str, QString &parserErrorMsg, bool allowFragments)