-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsremotedatasourcebuilder.cpp
184 lines (162 loc) · 5.07 KB
/
qgsremotedatasourcebuilder.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/***************************************************************************
qgsremotedatasourcebuilder.cpp
------------------------------
begin : July, 2008
copyright : (C) 2008 by Marco Hugentobler
email : marco dot hugentobler at karto dot baug dot ethz dot 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 "qgsremotedatasourcebuilder.h"
#include "qgsftptransaction.h"
#include "qgshttptransaction.h"
#include "qgslogger.h"
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"
#include <QDomElement>
#include <QFile>
#include <QTemporaryFile>
QgsRemoteDataSourceBuilder::QgsRemoteDataSourceBuilder(): QgsMSLayerBuilder()
{
}
QgsRemoteDataSourceBuilder::~QgsRemoteDataSourceBuilder()
{
}
QgsMapLayer* QgsRemoteDataSourceBuilder::createMapLayer( const QDomElement& elem, const QString& layerName, QList<QTemporaryFile*>& filesToRemove, QList<QgsMapLayer*>& layersToRemove, bool allowCaching ) const
{
QgsDebugMsg( "entering." );
QgsMapLayer* theLayer = 0;
if ( elem.tagName() == "RemoteRDS" )
{
theLayer = rasterLayerFromRemoteRDS( elem, layerName, filesToRemove, layersToRemove, allowCaching );
}
else if ( elem.tagName() == "RemoteVDS" )
{
theLayer = vectorLayerFromRemoteVDS( elem, layerName, filesToRemove, layersToRemove, allowCaching );
}
else
{
return 0;
}
return theLayer;
}
QgsRasterLayer* QgsRemoteDataSourceBuilder::rasterLayerFromRemoteRDS( const QDomElement& remoteRDSElem,
const QString& layerName,
QList<QTemporaryFile*> &filesToRemove,
QList<QgsMapLayer*> &layersToRemove,
bool allowCaching ) const
{
Q_UNUSED( layerName );
Q_UNUSED( allowCaching );
QgsDebugMsg( "entering." );
//load file with QgsHttpTransaction or QgsFtpTransaction
QByteArray fileContents;
QString uri = remoteRDSElem.text();
QgsRasterLayer* rl = 0;
if ( loadData( uri, fileContents ) != 0 )
{
return 0;
}
QTemporaryFile* tmpFile = new QTemporaryFile();
if ( tmpFile->open() )
{
tmpFile->write( fileContents );
tmpFile->flush();
}
else
{
QgsDebugMsg( "Error, creation of temp file failed" );
delete tmpFile;
return 0;
}
//create rasterlayer
rl = new QgsRasterLayer( tmpFile->fileName(), layerNameFromUri( tmpFile->fileName() ) );
layersToRemove.push_back( rl );
filesToRemove.push_back( tmpFile );
clearRasterSymbology( rl );
return rl;
}
QgsVectorLayer* QgsRemoteDataSourceBuilder::vectorLayerFromRemoteVDS( const QDomElement& remoteVDSElem,
const QString& layerName,
QList<QTemporaryFile*>& filesToRemove,
QList<QgsMapLayer*>& layersToRemove,
bool allowCaching ) const
{
Q_UNUSED( layerName );
Q_UNUSED( allowCaching );
QString providerString;
QString formatString = remoteVDSElem.attribute( "format" );
if ( formatString.compare( "gml", Qt::CaseInsensitive ) == 0 )
{
providerString = "WFS";
}
else
{
providerString = formatString;
}
//load file with QgsHttpTransaction
QByteArray fileContents;
QString uri = remoteVDSElem.text();
QgsVectorLayer* vl = 0;
if ( loadData( uri, fileContents ) != 0 )
{
return 0;
}
//store content into temporary file
QTemporaryFile* tmpFile = new QTemporaryFile();
if ( tmpFile->open() )
{
tmpFile->write( fileContents );
tmpFile->flush();
}
else
{
delete tmpFile;
return 0;
}
//create vector layer
//SOS has a special datasource key...
if ( formatString.compare( "SOS", Qt::CaseInsensitive ) == 0 )
{
QString url = "url=" + tmpFile->fileName() + " method=FILE xml=";
vl = new QgsVectorLayer( url, layerNameFromUri( tmpFile->fileName() ), providerString );
}
else
{
vl = new QgsVectorLayer( tmpFile->fileName(), layerNameFromUri( tmpFile->fileName() ), providerString );
}
if ( !( vl->isValid() ) )
{
QgsDebugMsg( "vl is not valid" );
}
layersToRemove.push_back( vl );
filesToRemove.push_back( tmpFile );
return vl;
}
int QgsRemoteDataSourceBuilder::loadData( const QString& url, QByteArray& data ) const
{
if ( url.startsWith( "http", Qt::CaseInsensitive ) )
{
QgsHttpTransaction http( url );
if ( !http.getSynchronously( data ) )
{
QgsDebugMsg( "Error, loading from http failed" );
return 1; //no success
}
}
else if ( url.startsWith( "ftp", Qt::CaseInsensitive ) )
{
QgsFtpTransaction ftp;
if ( ftp.get( url, data ) != 0 )
{
return 1;
}
}
return 0;
}