/
qgsamssourceselect.cpp
103 lines (90 loc) · 4.56 KB
/
qgsamssourceselect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/***************************************************************************
qgsamssourceselect.cpp
----------------------
begin : Nov 26, 2015
copyright : (C) 2015 by Sandro Mani
email : smani@sourcepole.ch
***************************************************************************/
/***************************************************************************
* *
* 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 "qgsamssourceselect.h"
#include "qgsarcgisrestutils.h"
#include "qgsamsprovider.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsowsconnection.h"
#include "qgsnetworkaccessmanager.h"
#include <QMessageBox>
QgsAmsSourceSelect::QgsAmsSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode widgetMode )
: QgsSourceSelectDialog( QStringLiteral( "ArcGisMapServer" ), QgsSourceSelectDialog::MapService, parent, fl )
{
if ( widgetMode == QgsProviderRegistry::WidgetMode::Embedded || widgetMode == QgsProviderRegistry::WidgetMode::Manager )
{
buttonBox->removeButton( buttonBox->button( QDialogButtonBox::Close ) );
}
// import/export of connections not supported yet
btnLoad->hide();
btnSave->hide();
}
bool QgsAmsSourceSelect::connectToService( const QgsOwsConnection &connection )
{
QString errorTitle, errorMessage;
QVariantMap serviceInfoMap = QgsArcGisRestUtils::getServiceInfo( connection.uri().param( QStringLiteral( "url" ) ), errorTitle, errorMessage );
if ( serviceInfoMap.isEmpty() )
{
QMessageBox::warning( this, tr( "Error" ), tr( "Failed to retrieve service capabilities:\n%1: %2" ).arg( errorTitle, errorMessage ) );
return false;
}
populateImageEncodings( serviceInfoMap[QStringLiteral( "supportedImageFormatTypes" )].toString().split( QStringLiteral( "," ) ) );
QStringList layerErrors;
foreach ( const QVariant &layerInfo, serviceInfoMap["layers"].toList() )
{
QVariantMap layerInfoMap = layerInfo.toMap();
if ( !layerInfoMap[QStringLiteral( "id" )].isValid() )
{
continue;
}
// Get layer info
QVariantMap layerData = QgsArcGisRestUtils::getLayerInfo( connection.uri().param( QStringLiteral( "url" ) ) + "/" + layerInfoMap[QStringLiteral( "id" )].toString(), errorTitle, errorMessage );
if ( layerData.isEmpty() )
{
layerErrors.append( QStringLiteral( "Layer %1: %2 - %3" ).arg( layerInfoMap[QStringLiteral( "id" )].toString(), errorTitle, errorMessage ) );
continue;
}
// insert the typenames, titles and abstracts into the tree view
QStandardItem *idItem = new QStandardItem( layerData[QStringLiteral( "id" )].toString() );
QStandardItem *nameItem = new QStandardItem( layerData[QStringLiteral( "name" )].toString() );
QStandardItem *abstractItem = new QStandardItem( layerData[QStringLiteral( "description" )].toString() );
abstractItem->setToolTip( layerData[QStringLiteral( "description" )].toString() );
QgsCoordinateReferenceSystem crs = QgsArcGisRestUtils::parseSpatialReference( serviceInfoMap[QStringLiteral( "spatialReference" )].toMap() );
if ( !crs.isValid() )
{
layerErrors.append( tr( "Layer %1: unable to parse spatial reference" ).arg( layerInfoMap[QStringLiteral( "id" )].toString() ) );
continue;
}
mAvailableCRS[layerData[QStringLiteral( "name" )].toString()] = QList<QString>() << crs.authid();
mModel->appendRow( QList<QStandardItem *>() << idItem << nameItem << abstractItem );
}
if ( !layerErrors.isEmpty() )
{
QMessageBox::warning( this, tr( "Error" ), tr( "Failed to query some layers:\n%1" ).arg( layerErrors.join( QStringLiteral( "\n" ) ) ) );
}
return true;
}
QString QgsAmsSourceSelect::getLayerURI( const QgsOwsConnection &connection,
const QString &layerTitle, const QString & /*layerName*/,
const QString &crs,
const QString & /*filter*/,
const QgsRectangle & /*bBox*/ ) const
{
QgsDataSourceUri ds = connection.uri();
ds.setParam( QStringLiteral( "layer" ), layerTitle );
ds.setParam( QStringLiteral( "crs" ), crs );
ds.setParam( QStringLiteral( "format" ), getSelectedImageEncoding() );
return ds.uri();
}