QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsowssourcewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsowssourcewidget.cpp
3  --------------------------------------
4  Date : November 2021
5  Copyright : (C) 2021 by Samweli Mwakisambwe
6  Email : samweli at kartoza dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsowssourcewidget.h"
19 #include "qgsproviderregistry.h"
20 #include "qgsmapcanvas.h"
21 
22 #include <QNetworkRequest>
23 
24 
25 QgsOWSSourceWidget::QgsOWSSourceWidget( const QString &providerKey, QWidget *parent )
26  : QgsProviderSourceWidget( parent )
27  , mProviderKey( providerKey )
28 {
29  setupUi( this );
30 
31  QgsCoordinateReferenceSystem destinationCrs;
32  QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
33  mSpatialExtentBox->setOutputCrs( crs );
34 }
35 
36 
38 {
40 
41  QgsCoordinateReferenceSystem destinationCrs;
42  QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
43 
44  if ( mapCanvas() && mapCanvas()->mapSettings().destinationCrs().isValid() )
45  destinationCrs = mapCanvas()->mapSettings().destinationCrs();
46  else
47  destinationCrs = crs;
48 
49  mSpatialExtentBox->setOutputCrs( destinationCrs );
50  mSpatialExtentBox->setMapCanvas( mapCanvas() );
51 }
52 
53 
55 {
56  QgsCoordinateReferenceSystem destinationCrs;
57  QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
58  mSpatialExtentBox->setOutputCrs( crs );
59 
60  if ( mapCanvas() && mapCanvas()->mapSettings().destinationCrs().isValid() )
61  destinationCrs = mapCanvas()->mapSettings().destinationCrs();
62  else
63  destinationCrs = crs;
64  mSpatialExtentBox->setCurrentExtent( extent, destinationCrs );
65  mSpatialExtentBox->setOutputExtentFromCurrent();
66  mSpatialExtentBox->setMapCanvas( mapCanvas() );
67 }
68 
70 {
71  return mSpatialExtentBox->outputExtent();
72 }
73 
74 void QgsOWSSourceWidget::setSourceUri( const QString &uri )
75 {
76  mSourceParts = QgsProviderRegistry::instance()->decodeUri( mProviderKey, uri );
77  bool inverted = mSourceParts.value( QStringLiteral( "InvertAxisOrientation" ) ).toBool();
78 
79  QString bbox = mSourceParts.value( QStringLiteral( "bbox" ) ).toString();
81  if ( !bbox.isEmpty() )
82  {
83  QStringList coords = bbox.split( ',' );
84  extent = inverted ? QgsRectangle(
85  coords.takeAt( 1 ).toDouble(),
86  coords.takeAt( 0 ).toDouble(),
87  coords.takeAt( 2 ).toDouble(),
88  coords.takeAt( 3 ).toDouble() ) :
90  coords.takeAt( 0 ).toDouble(),
91  coords.takeAt( 1 ).toDouble(),
92  coords.takeAt( 2 ).toDouble(),
93  coords.takeAt( 3 ).toDouble() );
94  }
95  else
96  {
97  extent = QgsRectangle();
98  }
99 
100  setExtent( extent );
101  mSpatialExtentBox->setChecked( !extent.isNull() );
102 }
103 
105 {
106  QVariantMap parts = mSourceParts;
107 
108  QgsRectangle spatialExtent = extent();
109 
110  if ( mSpatialExtentBox->isChecked() && !spatialExtent.isNull() )
111  {
112  bool inverted = parts.value( QStringLiteral( "InvertAxisOrientation" ) ).toBool();
113 
114  QString bbox = QString( inverted ? "%2,%1,%4,%3" : "%1,%2,%3,%4" )
115  .arg( qgsDoubleToString( spatialExtent.xMinimum() ),
116  qgsDoubleToString( spatialExtent.yMinimum() ),
117  qgsDoubleToString( spatialExtent.xMaximum() ),
118  qgsDoubleToString( spatialExtent.yMaximum() ) );
119 
120  parts.insert( QStringLiteral( "bbox" ), bbox );
121  }
122 
123 
124  return QgsProviderRegistry::instance()->encodeUri( mProviderKey, parts );
125 }
126 
QgsProviderSourceWidget::setMapCanvas
virtual void setMapCanvas(QgsMapCanvas *mapCanvas)
Sets a map canvas associated with the widget.
Definition: qgsprovidersourcewidget.h:62
qgsmapcanvas.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:437
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsOWSSourceWidget::sourceUri
QString sourceUri() const override
Returns the source URI as currently defined by the widget.
Definition: qgsowssourcewidget.cpp:104
QgsProviderRegistry::encodeUri
QString encodeUri(const QString &providerKey, const QVariantMap &parts)
Reassembles a provider data source URI from its component paths (e.g.
Definition: qgsproviderregistry.cpp:564
QgsRectangle::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:2204
QgsProviderSourceWidget
Base class for widgets which allow customization of a provider's source URI.
Definition: qgsprovidersourcewidget.h:32
QgsRectangle::xMaximum
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:183
qgsproviderregistry.h
QgsProviderRegistry::decodeUri
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
Definition: qgsproviderregistry.cpp:555
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
QgsOWSSourceWidget::setExtent
void setExtent(const QgsRectangle &extent)
Sets the spatial extent in the widget extent box.
Definition: qgsowssourcewidget.cpp:54
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsProviderSourceWidget::mapCanvas
virtual QgsMapCanvas * mapCanvas()
Returns the map canvas associated with the widget.
Definition: qgsprovidersourcewidget.h:69
QgsMapSettings::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
Definition: qgsmapsettings.cpp:358
qgsowssourcewidget.h
QgsRectangle::yMaximum
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
QgsOWSSourceWidget::setSourceUri
void setSourceUri(const QString &uri) override
Sets the source uri to show in the widget.
Definition: qgsowssourcewidget.cpp:74
QgsOWSSourceWidget::QgsOWSSourceWidget
QgsOWSSourceWidget(const QString &providerKey, QWidget *parent=nullptr)
Constructor for QgsOWSSourceWidget with the specified provider key and parent widget.
Definition: qgsowssourcewidget.cpp:25
QgsOWSSourceWidget::setMapCanvas
void setMapCanvas(QgsMapCanvas *canvas) override
Sets a map canvas associated with the widget.
Definition: qgsowssourcewidget.cpp:37
QgsOWSSourceWidget::extent
QgsRectangle extent() const
Returns the spatial extent from the widget extent box.
Definition: qgsowssourcewidget.cpp:69
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:73
QgsRectangle::isNull
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:479