QGIS API Documentation 4.1.0-Master (3fcefe620d1)
Loading...
Searching...
No Matches
qgssensorthingsdataitems.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssensorthingsdataitems.cpp
3 ---------------------
4 begin : December 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
16
17#include "qgsdataprovider.h"
18#include "qgsprovidermetadata.h"
19#include "qgsproviderregistry.h"
23
24#include <QString>
25
26#include "moc_qgssensorthingsdataitems.cpp"
27
28using namespace Qt::StringLiterals;
29
31
32//
33// QgsSensorThingsRootItem
34//
35
36QgsSensorThingsRootItem::QgsSensorThingsRootItem( QgsDataItem *parent, QString name, QString path )
37 : QgsConnectionsRootItem( parent, name, path, u"sensorthings"_s )
38{
39 mCapabilities |= Qgis::BrowserItemCapability::Fast;
40 mIconName = u"mIconSensorThings.svg"_s;
41 populate();
42}
43
44QVector<QgsDataItem *> QgsSensorThingsRootItem::createChildren()
45{
46 QVector<QgsDataItem *> connections;
47 const auto connectionList = QgsSensorThingsProviderConnection::connectionList();
48 for ( const QString &connName : connectionList )
49 {
50 QgsDataItem *conn = new QgsSensorThingsConnectionItem( this, connName, mPath + '/' + connName );
51 connections.append( conn );
52 }
53 return connections;
54}
55
56//
57// QgsSensorThingsConnectionItem
58//
59
60QgsSensorThingsConnectionItem::QgsSensorThingsConnectionItem( QgsDataItem *parent, const QString &name, const QString &path )
61 : QgsDataCollectionItem( parent, name, path, u"sensorthings"_s )
62 , mConnName( name )
63{
64 mIconName = u"mIconConnect.svg"_s;
66}
67
68bool QgsSensorThingsConnectionItem::equal( const QgsDataItem *other )
69{
70 const QgsSensorThingsConnectionItem *o = qobject_cast<const QgsSensorThingsConnectionItem *>( other );
71 return ( type() == other->type() && o && mPath == o->mPath && mName == o->mName );
72}
73
74QVector<QgsDataItem *> QgsSensorThingsConnectionItem::createChildren()
75{
76 QVector<QgsDataItem *> children;
77
79 const QString uri = QgsSensorThingsProviderConnection::encodedLayerUri( connectionData );
80 const QVariantMap connectionUriParts = QgsProviderRegistry::instance()->decodeUri( QgsSensorThingsProvider::SENSORTHINGS_PROVIDER_KEY, uri );
81
82 const QgsSensorThingsUtils::ServiceCapabilities capabilities = QgsSensorThingsUtils::determineServiceCapabilities( connectionUriParts.value( u"url"_s ).toString() );
83
84 const QMetaEnum entities = QMetaEnum::fromType<Qgis::SensorThingsEntity>();
85 for ( qint32 i = 0, count = entities.keyCount(); i < count; i++ )
86 {
87 const Qgis::SensorThingsEntity entity = static_cast< Qgis::SensorThingsEntity >( entities.value( i ) );
88
89 if ( !capabilities.availableEntities.contains( entity ) )
90 continue;
91
92 QVariantMap entityUriParts = connectionUriParts;
93 entityUriParts.insert( u"entity"_s, qgsEnumValueToKey( entity ) );
94
96 {
97 children.append( new QgsSensorThingsEntityContainerItem( this, QgsSensorThingsUtils::displayString( entity, true ), mPath + '/' + qgsEnumValueToKey( entity ), entityUriParts, entity, mConnName ) );
98 }
99 else
100 {
101 children.append(
102 new QgsSensorThingsLayerEntityItem( this, QgsSensorThingsUtils::displayString( entity, true ), mPath + '/' + qgsEnumValueToKey( entity ), entityUriParts, QgsSensorThingsProvider::SENSORTHINGS_PROVIDER_KEY, Qgis::BrowserLayerType::TableLayer, entity, mConnName )
103 );
104 }
105 }
106
107 return children;
108}
109
110
111//
112// QgsSensorThingsEntityContainerItem
113//
114
115QgsSensorThingsEntityContainerItem::QgsSensorThingsEntityContainerItem(
116 QgsDataItem *parent, const QString &name, const QString &path, const QVariantMap &entityUriParts, Qgis::SensorThingsEntity entityType, const QString &connectionName
117)
118 : QgsDataCollectionItem( parent, name, path, u"sensorthings"_s )
119 , mEntityUriParts( entityUriParts )
120 , mEntityType( entityType )
121 , mConnectionName( connectionName )
122{
124 populate();
125}
126
127bool QgsSensorThingsEntityContainerItem::equal( const QgsDataItem *other )
128{
129 const QgsSensorThingsEntityContainerItem *o = qobject_cast<const QgsSensorThingsEntityContainerItem *>( other );
130 return ( type() == other->type() && o && mPath == o->mPath && mName == o->mName );
131}
132
133QVector<QgsDataItem *> QgsSensorThingsEntityContainerItem::createChildren()
134{
135 QVector<QgsDataItem *> children;
136
137 int sortKey = 1;
138 QList< Qgis::WkbType > compatibleTypes;
139 // we always expose "no geometry" types for these, even though they have a restricted fixed type
140 // according to the spec. This is because not all services respect the mandated geometry types!
141 switch ( QgsSensorThingsUtils::geometryTypeForEntity( mEntityType ) )
142 {
145 break;
148 break;
151 break;
154 break;
156 compatibleTypes << Qgis::WkbType::NoGeometry;
157 }
158
159 for ( const Qgis::WkbType wkbType : std::as_const( compatibleTypes ) )
160 {
161 QVariantMap geometryUriParts = mEntityUriParts;
162 QString name;
164 switch ( wkbType )
165 {
167 geometryUriParts.insert( u"geometryType"_s, u"point"_s );
168 name = tr( "Points" );
170 break;
172 geometryUriParts.insert( u"geometryType"_s, u"multipoint"_s );
173 name = tr( "MultiPoints" );
175 break;
177 geometryUriParts.insert( u"geometryType"_s, u"line"_s );
178 name = tr( "Lines" );
180 break;
182 geometryUriParts.insert( u"geometryType"_s, u"polygon"_s );
183 name = tr( "Polygons" );
185 break;
187 geometryUriParts.remove( u"geometryType"_s );
188 name = tr( "No Geometry" );
190 break;
191 default:
192 break;
193 }
194 children.append( new QgsSensorThingsLayerEntityItem( this, name, mPath + '/' + name, geometryUriParts, QgsSensorThingsProvider::SENSORTHINGS_PROVIDER_KEY, layerType, mEntityType, mConnectionName ) );
195 children.last()->setSortKey( sortKey++ );
196 }
197
198 return children;
199}
200
201//
202// QgsSensorThingsLayerEntityItem
203//
204
205QgsSensorThingsLayerEntityItem::QgsSensorThingsLayerEntityItem(
206 QgsDataItem *parent, const QString &name, const QString &path, const QVariantMap &uriParts, const QString &provider, Qgis::BrowserLayerType type, Qgis::SensorThingsEntity entityType, const QString &connectionName
207)
208 : QgsLayerItem( parent, name, path, QgsProviderRegistry::instance()->encodeUri( QgsSensorThingsProvider::SENSORTHINGS_PROVIDER_KEY, uriParts ), type, provider )
209 , mUriParts( uriParts )
210 , mEntityType( entityType )
211 , mConnectionName( connectionName )
212{
214}
215
216QString QgsSensorThingsLayerEntityItem::layerName() const
217{
218 QString baseName;
220 {
221 const QString geometryType = mUriParts.value( u"geometryType"_s ).toString();
222 QString geometryNamePart;
223 if ( geometryType.compare( "point"_L1, Qt::CaseInsensitive ) == 0 || geometryType.compare( "multipoint"_L1, Qt::CaseInsensitive ) == 0 )
224 {
225 geometryNamePart = tr( "Points" );
226 }
227 else if ( geometryType.compare( "line"_L1, Qt::CaseInsensitive ) == 0 )
228 {
229 geometryNamePart = tr( "Lines" );
230 }
231 else if ( geometryType.compare( "polygon"_L1, Qt::CaseInsensitive ) == 0 )
232 {
233 geometryNamePart = tr( "Polygons" );
234 }
235
236 if ( !geometryNamePart.isEmpty() )
237 {
238 baseName = u"%1 - %2 (%3)"_s.arg( mConnectionName, QgsSensorThingsUtils::displayString( mEntityType, true ), geometryNamePart );
239 }
240 else
241 {
242 baseName = u"%1 - %2"_s.arg( mConnectionName, QgsSensorThingsUtils::displayString( mEntityType, true ) );
243 }
244 }
245 else
246 {
247 baseName = u"%1 - %2"_s.arg( mConnectionName, QgsSensorThingsUtils::displayString( mEntityType, true ) );
248 }
249
250 return baseName;
251}
252
253//
254// QgsSensorThingsDataItemProvider
255//
256
257QString QgsSensorThingsDataItemProvider::name()
258{
259 return u"SensorThings"_s;
260}
261
262QString QgsSensorThingsDataItemProvider::dataProviderKey() const
263{
264 return u"sensorthings"_s;
265}
266
267Qgis::DataItemProviderCapabilities QgsSensorThingsDataItemProvider::capabilities() const
268{
270}
271
272QgsDataItem *QgsSensorThingsDataItemProvider::createDataItem( const QString &path, QgsDataItem *parentItem )
273{
274 if ( path.isEmpty() )
275 return new QgsSensorThingsRootItem( parentItem, QObject::tr( "SensorThings" ), u"sensorthings:"_s );
276
277 return nullptr;
278}
279
@ NetworkSources
Network/internet source.
Definition qgis.h:1049
@ Populated
Children created.
Definition qgis.h:990
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
Definition qgis.h:1005
@ Fast
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,...
Definition qgis.h:1004
QFlags< DataItemProviderCapability > DataItemProviderCapabilities
Capabilities for data item providers.
Definition qgis.h:1060
SensorThingsEntity
OGC SensorThings API entity types.
Definition qgis.h:6625
@ Point
Points.
Definition qgis.h:380
@ Line
Lines.
Definition qgis.h:381
@ Polygon
Polygons.
Definition qgis.h:382
@ Unknown
Unknown types.
Definition qgis.h:383
@ Null
No geometry.
Definition qgis.h:384
BrowserLayerType
Browser item layer types.
Definition qgis.h:1069
@ Point
Vector point layer.
Definition qgis.h:1073
@ Line
Vector line layer.
Definition qgis.h:1074
@ Polygon
Vector polygon layer.
Definition qgis.h:1075
@ TableLayer
Vector non-spatial layer.
Definition qgis.h:1076
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ Point
Point.
Definition qgis.h:296
@ MultiPoint
MultiPoint.
Definition qgis.h:300
@ MultiPolygon
MultiPolygon.
Definition qgis.h:302
@ NoGeometry
No geometry.
Definition qgis.h:312
@ MultiLineString
MultiLineString.
Definition qgis.h:301
A browser item that represents a root group of connections from a single data provider.
A browser item for collections of data.
Base class for all items in the model.
Definition qgsdataitem.h:50
Qgis::BrowserItemType type() const
A browser item that represents a layer that can be opened with one of the providers.
A registry / canonical manager of data providers.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
static QString encodedLayerUri(const Data &data)
Returns connection data encoded as a string containing a URI for a SensorThings vector data provider.
static QStringList connectionList()
Returns a list of the stored connection names.
static Data connection(const QString &name)
Returns connection details for the stored connection with the specified name.
static Qgis::GeometryType geometryTypeForEntity(Qgis::SensorThingsEntity type)
Returns the geometry type for if the specified entity type.
static QString displayString(Qgis::SensorThingsEntity type, bool plural=false)
Converts a Qgis::SensorThingsEntity type to a user-friendly translated string.
static bool entityTypeHasGeometry(Qgis::SensorThingsEntity type)
Returns true if the specified entity type can have geometry attached.
static ServiceCapabilities determineServiceCapabilities(const QString &uri, QgsFeedback *feedback=nullptr, const QString &authCfg=QString())
Retrieves general service capabilities for a SensorThings server.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7576
Represents decoded data of a SensorThings connection.
SensorThings service capabilities.
QSet< Qgis::SensorThingsEntity > availableEntities
Available SensorThings entities.