|
1 |
#include "qgswfsserver.h"
|
|
2 |
#include "qgsconfigparser.h"
|
|
3 |
#include "qgscrscache.h"
|
|
4 |
#include "qgsfield.h"
|
|
5 |
#include "qgsgeometry.h"
|
|
6 |
#include "qgsmaplayer.h"
|
|
7 |
#include "qgsmaplayerregistry.h"
|
|
8 |
#include "qgsmaprenderer.h"
|
|
9 |
#include "qgsmaptopixel.h"
|
|
10 |
#include "qgspallabeling.h"
|
|
11 |
#include "qgsproject.h"
|
|
12 |
#include "qgsrasterlayer.h"
|
|
13 |
#include "qgsscalecalculator.h"
|
|
14 |
#include "qgscoordinatereferencesystem.h"
|
|
15 |
#include "qgsvectordataprovider.h"
|
|
16 |
#include "qgsvectorlayer.h"
|
|
17 |
#include "qgsfilter.h"
|
|
18 |
#include "qgslogger.h"
|
|
19 |
#include "qgsmapserviceexception.h"
|
|
20 |
#include "qgssldparser.h"
|
|
21 |
#include "qgssymbol.h"
|
|
22 |
#include "qgssymbolv2.h"
|
|
23 |
#include "qgsrenderer.h"
|
|
24 |
#include "qgslegendmodel.h"
|
|
25 |
#include "qgscomposerlegenditem.h"
|
|
26 |
#include "qgslogger.h"
|
|
27 |
#include <QImage>
|
|
28 |
#include <QPainter>
|
|
29 |
#include <QStringList>
|
|
30 |
#include <QTextStream>
|
|
31 |
#include <QDir>
|
|
32 |
|
|
33 |
//for printing
|
|
34 |
#include "qgscomposition.h"
|
|
35 |
#include <QBuffer>
|
|
36 |
#include <QPrinter>
|
|
37 |
#include <QSvgGenerator>
|
|
38 |
#include <QUrl>
|
|
39 |
#include <QPaintEngine>
|
|
40 |
|
|
41 |
QgsWFSServer::QgsWFSServer( QMap<QString, QString> parameters )
|
|
42 |
: mParameterMap( parameters )
|
|
43 |
, mConfigParser( 0 )
|
|
44 |
{
|
|
45 |
}
|
|
46 |
|
|
47 |
QgsWFSServer::~QgsWFSServer()
|
|
48 |
{
|
|
49 |
}
|
|
50 |
|
|
51 |
QgsWFSServer::QgsWFSServer()
|
|
52 |
{
|
|
53 |
}
|
|
54 |
|
|
55 |
QDomDocument QgsWFSServer::getCapabilities()
|
|
56 |
{
|
|
57 |
QgsDebugMsg( "Entering." );
|
|
58 |
QDomDocument doc;
|
|
59 |
//wfs:WFS_Capabilities element
|
|
60 |
QDomElement wfsCapabilitiesElement = doc.createElement( "WFS_Capabilities"/*wms:WFS_Capabilities*/ );
|
|
61 |
wfsCapabilitiesElement.setAttribute( "xmlns", "http://www.opengis.net/wfs" );
|
|
62 |
wfsCapabilitiesElement.setAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
|
|
63 |
wfsCapabilitiesElement.setAttribute( "xsi:schemaLocation", "http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/wfs.xsd" );
|
|
64 |
wfsCapabilitiesElement.setAttribute( "xmlns:ogc", "http://www.opengis.net/ogc" );
|
|
65 |
wfsCapabilitiesElement.setAttribute( "xmlns:gml", "http://www.opengis.net/gml" );
|
|
66 |
wfsCapabilitiesElement.setAttribute( "xmlns:ows", "http://www.opengis.net/ows" );
|
|
67 |
wfsCapabilitiesElement.setAttribute( "xmlns:xlink", "http://www.w3.org/1999/xlink" );
|
|
68 |
wfsCapabilitiesElement.setAttribute( "version", "1.0.0" );
|
|
69 |
wfsCapabilitiesElement.setAttribute( "updateSequence", "0" );
|
|
70 |
doc.appendChild( wfsCapabilitiesElement );
|
|
71 |
|
|
72 |
if ( mConfigParser )
|
|
73 |
{
|
|
74 |
mConfigParser->serviceCapabilities( wfsCapabilitiesElement, doc );
|
|
75 |
}
|
|
76 |
|
|
77 |
//wfs:Capability element
|
|
78 |
QDomElement capabilityElement = doc.createElement( "Capability"/*wfs:Capability*/ );
|
|
79 |
wfsCapabilitiesElement.appendChild( capabilityElement );
|
|
80 |
|
|
81 |
//wfs:Request element
|
|
82 |
QDomElement requestElement = doc.createElement( "Request"/*wfs:Request*/ );
|
|
83 |
capabilityElement.appendChild( requestElement );
|
|
84 |
//wfs:GetCapabilities
|
|
85 |
QDomElement getCapabilitiesElement = doc.createElement( "GetCapabilities"/*wfs:GetCapabilities*/ );
|
|
86 |
requestElement.appendChild( getCapabilitiesElement );
|
|
87 |
QDomElement capabilitiesFormatElement = doc.createElement( "Format" );/*wfs:Format*/
|
|
88 |
getCapabilitiesElement.appendChild( capabilitiesFormatElement );
|
|
89 |
QDomText capabilitiesFormatText = doc.createTextNode( "text/xml" );
|
|
90 |
capabilitiesFormatElement.appendChild( capabilitiesFormatText );
|
|
91 |
|
|
92 |
QDomElement dcpTypeElement = doc.createElement( "DCPType"/*wfs:DCPType*/ );
|
|
93 |
getCapabilitiesElement.appendChild( dcpTypeElement );
|
|
94 |
QDomElement httpElement = doc.createElement( "HTTP"/*wfs:HTTP*/ );
|
|
95 |
dcpTypeElement.appendChild( httpElement );
|
|
96 |
|
|
97 |
//Prepare url
|
|
98 |
//Some client requests already have http://<SERVER_NAME> in the REQUEST_URI variable
|
|
99 |
QString hrefString;
|
|
100 |
QString requestUrl = getenv( "REQUEST_URI" );
|
|
101 |
QUrl mapUrl( requestUrl );
|
|
102 |
mapUrl.setHost( QString( getenv( "SERVER_NAME" ) ) );
|
|
103 |
mapUrl.removeQueryItem( "REQUEST" );
|
|
104 |
mapUrl.removeQueryItem( "VERSION" );
|
|
105 |
mapUrl.removeQueryItem( "SERVICE" );
|
|
106 |
hrefString = mapUrl.toString();
|
|
107 |
|
|
108 |
//only Get supported for the moment
|
|
109 |
QDomElement getElement = doc.createElement( "Get"/*wfs:Get*/ );
|
|
110 |
httpElement.appendChild( getElement );
|
|
111 |
QDomElement olResourceElement = doc.createElement( "OnlineResource"/*wfs:OnlineResource*/ );
|
|
112 |
olResourceElement.setAttribute( "xlink:type", "simple" );
|
|
113 |
requestUrl.truncate( requestUrl.indexOf( "?" ) + 1 );
|
|
114 |
olResourceElement.setAttribute( "xlink:href", hrefString );
|
|
115 |
getElement.appendChild( olResourceElement );
|
|
116 |
|
|
117 |
//wfs:DescribeFeatureType
|
|
118 |
QDomElement describeFeatureTypeElement = doc.createElement( "DescribeFeatureType"/*wfs:DescribeFeatureType*/ );
|
|
119 |
requestElement.appendChild( describeFeatureTypeElement );
|
|
120 |
QDomElement schemaDescriptionLanguageElement = doc.createElement( "SchemaDescriptionLanguage"/*wfs:SchemaDescriptionLanguage*/ );
|
|
121 |
describeFeatureTypeElement.appendChild( schemaDescriptionLanguageElement );
|
|
122 |
QDomElement xmlSchemaElement = doc.createElement( "XMLSCHEMA"/*wfs:XMLSCHEMA*/ );
|
|
123 |
schemaDescriptionLanguageElement.appendChild( xmlSchemaElement );
|
|
124 |
QDomElement describeFeatureTypeDhcTypeElement = dcpTypeElement.cloneNode().toElement();//this is the same as for 'GetCapabilities'
|
|
125 |
describeFeatureTypeElement.appendChild( describeFeatureTypeDhcTypeElement );
|
|
126 |
|
|
127 |
//wfs:GetFeature
|
|
128 |
QDomElement getFeatureElement = doc.createElement( "GetFeature"/*wfs:GetFeature*/ );
|
|
129 |
requestElement.appendChild( getFeatureElement );
|
|
130 |
QDomElement getFeatureFormatElement = doc.createElement( "ResultFormat" );/*wfs:ResultFormat*/
|
|
131 |
getFeatureElement.appendChild( getFeatureFormatElement );
|
|
132 |
QDomElement gmlFormatElement = doc.createElement( "GML2" );/*wfs:GML2*/
|
|
133 |
getFeatureFormatElement.appendChild( gmlFormatElement );
|
|
134 |
QDomElement geojsonFormatElement = doc.createElement( "GeoJSON" );/*wfs:GeoJSON*/
|
|
135 |
getFeatureFormatElement.appendChild( geojsonFormatElement );
|
|
136 |
QDomElement getFeatureDhcTypeElement = dcpTypeElement.cloneNode().toElement();//this is the same as for 'GetCapabilities'
|
|
137 |
getFeatureElement.appendChild( getFeatureDhcTypeElement );
|
|
138 |
|
|
139 |
//wfs:FeatureTypeList element
|
|
140 |
QDomElement featureTypeListElement = doc.createElement( "FeatureTypeList"/*wfs:FeatureTypeList*/ );
|
|
141 |
capabilityElement.appendChild( featureTypeListElement );
|
|
142 |
//wfs:Operations element
|
|
143 |
QDomElement operationsElement = doc.createElement( "Operations"/*wfs:Operations*/ );
|
|
144 |
featureTypeListElement.appendChild( operationsElement );
|
|
145 |
//wfs:Query element
|
|
146 |
QDomElement queryElement = doc.createElement( "Query"/*wfs:Query*/ );
|
|
147 |
operationsElement.appendChild( queryElement );
|
|
148 |
/*
|
|
149 |
* Adding layer liste in featureTypeListElement
|
|
150 |
*/
|
|
151 |
if ( mConfigParser )
|
|
152 |
{
|
|
153 |
mConfigParser->featureTypeList( featureTypeListElement, doc );
|
|
154 |
}
|
|
155 |
|
|
156 |
/*
|
|
157 |
* Adding ogc:Filter_Capabilities in capabilityElement
|
|
158 |
*/
|
|
159 |
//ogc:Filter_Capabilities element
|
|
160 |
QDomElement filterCapabilitiesElement = doc.createElement( "ogc:Filter_Capabilities"/*ogc:Filter_Capabilities*/ );
|
|
161 |
capabilityElement.appendChild( filterCapabilitiesElement );
|
|
162 |
QDomElement spatialCapabilitiesElement = doc.createElement( "ogc:Spatial_Capabilities"/*ogc:Spatial_Capabilities*/ );
|
|
163 |
filterCapabilitiesElement.appendChild( spatialCapabilitiesElement );
|
|
164 |
QDomElement spatialOperatorsElement = doc.createElement( "ogc:Spatial_Operators"/*ogc:Spatial_Operators*/ );
|
|
165 |
spatialCapabilitiesElement.appendChild( spatialOperatorsElement );
|
|
166 |
QDomElement ogcBboxElement = doc.createElement( "ogc:BBOX"/*ogc:BBOX*/ );
|
|
167 |
spatialOperatorsElement.appendChild( ogcBboxElement );
|
|
168 |
QDomElement scalarCapabilitiesElement = doc.createElement( "ogc:Scalar_Capabilities"/*ogc:Scalar_Capabilities*/ );
|
|
169 |
filterCapabilitiesElement.appendChild( scalarCapabilitiesElement );
|
|
170 |
QDomElement comparisonOperatorsElement = doc.createElement( "ogc:Comparison_Operators"/*ogc:Comparison_Operators*/ );
|
|
171 |
scalarCapabilitiesElement.appendChild( comparisonOperatorsElement );
|
|
172 |
QDomElement simpleComparisonsElement = doc.createElement( "ogc:Simple_Comparisons"/*ogc:Simple_Comparisons*/ );
|
|
173 |
comparisonOperatorsElement.appendChild( simpleComparisonsElement );
|
|
174 |
return doc;
|
|
175 |
}
|
|
176 |
|
|
177 |
QDomDocument QgsWFSServer::describeFeatureType()
|
|
178 |
{
|
|
179 |
QgsDebugMsg( "Entering." );
|
|
180 |
QDomDocument doc;
|
|
181 |
//xsd:schema
|
|
182 |
QDomElement schemaElement = doc.createElement( "schema"/*xsd:schema*/ );
|
|
183 |
schemaElement.setAttribute( "xmlns", "http://www.w3.org/2001/XMLSchema" );
|
|
184 |
schemaElement.setAttribute( "xmlns:xsd", "http://www.w3.org/2001/XMLSchema" );
|
|
185 |
schemaElement.setAttribute( "xmlns:ogc", "http://www.opengis.net/ogc" );
|
|
186 |
schemaElement.setAttribute( "xmlns:gml", "http://www.opengis.net/gml" );
|
|
187 |
schemaElement.setAttribute( "xmlns:qgs", "http://www.qgis.org/gml" );
|
|
188 |
schemaElement.setAttribute( "targetNamespace", "http://www.qgis.org/gml" );
|
|
189 |
doc.appendChild( schemaElement );
|
|
190 |
|
|
191 |
//xsd:import
|
|
192 |
QDomElement importElement = doc.createElement( "import"/*xsd:import*/ );
|
|
193 |
importElement.setAttribute( "namespace", "http://www.opengis.net/gml" );
|
|
194 |
importElement.setAttribute( "schemaLocation", "http://schemas.opengis.net/gml/2.1.2/feature.xsd" );
|
|
195 |
schemaElement.appendChild( importElement );
|
|
196 |
|
|
197 |
//read TYPENAME
|
|
198 |
QString typeName;
|
|
199 |
QMap<QString, QString>::const_iterator type_name_it = mParameterMap.find( "TYPENAME" );
|
|
200 |
if ( type_name_it != mParameterMap.end() )
|
|
201 |
{
|
|
202 |
typeName = type_name_it.value();
|
|
203 |
}
|
|
204 |
else
|
|
205 |
{
|
|
206 |
return doc;
|
|
207 |
}
|
|
208 |
|
|
209 |
QStringList nonIdentifiableLayers = mConfigParser->identifyDisabledLayers();
|
|
210 |
QMap< QString, QMap< int, QString > > aliasInfo = mConfigParser->layerAliasInfo();
|
|
211 |
QMap< QString, QSet<QString> > hiddenAttributes = mConfigParser->hiddenAttributes();
|
|
212 |
|
|
213 |
QList<QgsMapLayer*> layerList;
|
|
214 |
QgsMapLayer* currentLayer = 0;
|
|
215 |
|
|
216 |
layerList = mConfigParser->mapLayerFromStyle( typeName, "" );
|
|
217 |
currentLayer = layerList.at( 0 );
|
|
218 |
|
|
219 |
QgsVectorLayer* layer = dynamic_cast<QgsVectorLayer*>( currentLayer );
|
|
220 |
if ( layer )
|
|
221 |
{
|
|
222 |
//is there alias info for this vector layer?
|
|
223 |
QMap< int, QString > layerAliasInfo;
|
|
224 |
QMap< QString, QMap< int, QString > >::const_iterator aliasIt = aliasInfo.find( currentLayer->id() );
|
|
225 |
if ( aliasIt != aliasInfo.constEnd() )
|
|
226 |
{
|
|
227 |
layerAliasInfo = aliasIt.value();
|
|
228 |
}
|
|
229 |
|
|
230 |
//hidden attributes for this layer
|
|
231 |
QSet<QString> layerHiddenAttributes;
|
|
232 |
QMap< QString, QSet<QString> >::const_iterator hiddenIt = hiddenAttributes.find( currentLayer->id() );
|
|
233 |
if ( hiddenIt != hiddenAttributes.constEnd() )
|
|
234 |
{
|
|
235 |
layerHiddenAttributes = hiddenIt.value();
|
|
236 |
}
|
|
237 |
|
|
238 |
//do a select with searchRect and go through all the features
|
|
239 |
QgsVectorDataProvider* provider = layer->dataProvider();
|
|
240 |
if ( !provider )
|
|
241 |
{
|
|
242 |
return doc;
|
|
243 |
}
|
|
244 |
|
|
245 |
typeName = typeName.replace( QString(" "), QString("_") );
|
|
246 |
|
|
247 |
//xsd:element
|
|
248 |
QDomElement elementElem = doc.createElement( "element"/*xsd:element*/ );
|
|
249 |
elementElem.setAttribute( "name", typeName );
|
|
250 |
elementElem.setAttribute( "type", "qgs:" + typeName + "Type" );
|
|
251 |
elementElem.setAttribute( "substitutionGroup", "gml:_Feature" );
|
|
252 |
schemaElement.appendChild( elementElem );
|
|
253 |
|
|
254 |
//xsd:complexType
|
|
255 |
QDomElement complexTypeElem = doc.createElement( "complexType"/*xsd:complexType*/ );
|
|
256 |
complexTypeElem.setAttribute( "name", typeName + "Type" );
|
|
257 |
schemaElement.appendChild( complexTypeElem );
|
|
258 |
|
|
259 |
//xsd:complexType
|
|
260 |
QDomElement complexContentElem = doc.createElement( "complexContent"/*xsd:complexContent*/ );
|
|
261 |
complexTypeElem.appendChild( complexContentElem );
|
|
262 |
|
|
263 |
//xsd:extension
|
|
264 |
QDomElement extensionElem = doc.createElement( "extension"/*xsd:extension*/ );
|
|
265 |
extensionElem.setAttribute( "base", "gml:AbstractFeatureType" );
|
|
266 |
complexContentElem.appendChild( extensionElem );
|
|
267 |
|
|
268 |
//xsd:sequence
|
|
269 |
QDomElement sequenceElem = doc.createElement( "sequence"/*xsd:sequence*/ );
|
|
270 |
extensionElem.appendChild( sequenceElem );
|
|
271 |
|
|
272 |
//xsd:element
|
|
273 |
QDomElement geomElem = doc.createElement( "element"/*xsd:element*/ );
|
|
274 |
geomElem.setAttribute( "name", "geometry" );
|
|
275 |
geomElem.setAttribute( "type", "gml:GeometryPropertyType" );
|
|
276 |
geomElem.setAttribute( "minOccurs", "0" );
|
|
277 |
geomElem.setAttribute( "maxOccurs", "1" );
|
|
278 |
sequenceElem.appendChild( geomElem );
|
|
279 |
|
|
280 |
const QgsFieldMap& fields = provider->fields();
|
|
281 |
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
|
|
282 |
{
|
|
283 |
|
|
284 |
QString attributeName = it.value().name();
|
|
285 |
//skip attribute if it has edit type 'hidden'
|
|
286 |
if ( layerHiddenAttributes.contains( attributeName ) )
|
|
287 |
{
|
|
288 |
continue;
|
|
289 |
}
|
|
290 |
|
|
291 |
//check if the attribute name should be replaced with an alias
|
|
292 |
QMap<int, QString>::const_iterator aliasIt = layerAliasInfo.find( it.key() );
|
|
293 |
if ( aliasIt != layerAliasInfo.constEnd() )
|
|
294 |
{
|
|
295 |
attributeName = aliasIt.value();
|
|
296 |
}
|
|
297 |
|
|
298 |
//xsd:element
|
|
299 |
QDomElement geomElem = doc.createElement( "element"/*xsd:element*/ );
|
|
300 |
geomElem.setAttribute( "name", attributeName );
|
|
301 |
if ( it.value().type() == 2 )
|
|
302 |
geomElem.setAttribute( "type", "integer" );
|
|
303 |
else if ( it.value().type() == 6 )
|
|
304 |
geomElem.setAttribute( "type", "double" );
|
|
305 |
else
|
|
306 |
geomElem.setAttribute( "type", "string" );
|
|
307 |
|
|
308 |
sequenceElem.appendChild( geomElem );
|
|
309 |
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
return doc;
|
|
314 |
}
|
|
315 |
|
|
316 |
int QgsWFSServer::getFeature( QByteArray& result, const QString& format )
|
|
317 |
{
|
|
318 |
QgsDebugMsg( "Info format is:" + infoFormat );
|
|
319 |
|
|
320 |
//read TYPENAME
|
|
321 |
QString typeName;
|
|
322 |
QMap<QString, QString>::const_iterator type_name_it = mParameterMap.find( "TYPENAME" );
|
|
323 |
if ( type_name_it != mParameterMap.end() )
|
|
324 |
{
|
|
325 |
typeName = type_name_it.value();
|
|
326 |
}
|
|
327 |
else
|
|
328 |
{
|
|
329 |
return 1;
|
|
330 |
}
|
|
331 |
|
|
332 |
QStringList nonIdentifiableLayers = mConfigParser->identifyDisabledLayers();
|
|
333 |
QMap< QString, QMap< int, QString > > aliasInfo = mConfigParser->layerAliasInfo();
|
|
334 |
QMap< QString, QSet<QString> > hiddenAttributes = mConfigParser->hiddenAttributes();
|
|
335 |
|
|
336 |
QList<QgsMapLayer*> layerList;
|
|
337 |
QgsMapLayer* currentLayer = 0;
|
|
338 |
|
|
339 |
layerList = mConfigParser->mapLayerFromStyle( typeName, "" );
|
|
340 |
currentLayer = layerList.at( 0 );
|
|
341 |
|
|
342 |
QgsVectorLayer* layer = dynamic_cast<QgsVectorLayer*>( currentLayer );
|
|
343 |
if ( layer )
|
|
344 |
{
|
|
345 |
//is there alias info for this vector layer?
|
|
346 |
QMap< int, QString > layerAliasInfo;
|
|
347 |
QMap< QString, QMap< int, QString > >::const_iterator aliasIt = aliasInfo.find( currentLayer->id() );
|
|
348 |
if ( aliasIt != aliasInfo.constEnd() )
|
|
349 |
{
|
|
350 |
layerAliasInfo = aliasIt.value();
|
|
351 |
}
|
|
352 |
|
|
353 |
//hidden attributes for this layer
|
|
354 |
QSet<QString> layerHiddenAttributes;
|
|
355 |
QMap< QString, QSet<QString> >::const_iterator hiddenIt = hiddenAttributes.find( currentLayer->id() );
|
|
356 |
if ( hiddenIt != hiddenAttributes.constEnd() )
|
|
357 |
{
|
|
358 |
layerHiddenAttributes = hiddenIt.value();
|
|
359 |
}
|
|
360 |
|
|
361 |
//do a select with searchRect and go through all the features
|
|
362 |
QgsVectorDataProvider* provider = layer->dataProvider();
|
|
363 |
if ( !provider )
|
|
364 |
{
|
|
365 |
return 2;
|
|
366 |
}
|
|
367 |
|
|
368 |
QgsFeature feature;
|
|
369 |
QgsAttributeMap featureAttributes;
|
|
370 |
const QgsFieldMap& fields = provider->fields();
|
|
371 |
|
|
372 |
//map extent
|
|
373 |
QgsRectangle searchRect = layer->extent();
|
|
374 |
|
|
375 |
//read FEATUREDID
|
|
376 |
bool fidOk = false;
|
|
377 |
QString fid;
|
|
378 |
QMap<QString, QString>::const_iterator fidIt = mParameterMap.find( "FEATUREID" );
|
|
379 |
if ( fidIt != mParameterMap.end() ) {
|
|
380 |
fidOk = true;
|
|
381 |
fid = fidIt.value();
|
|
382 |
}
|
|
383 |
|
|
384 |
//read FILTER
|
|
385 |
bool filterOk = false;
|
|
386 |
QDomDocument filter;
|
|
387 |
QMap<QString, QString>::const_iterator filterIt = mParameterMap.find( "FILTER" );
|
|
388 |
if ( filterIt != mParameterMap.end() ) {
|
|
389 |
try {
|
|
390 |
QString errorMsg;
|
|
391 |
if ( !filter.setContent( filterIt.value(), true, &errorMsg ) )
|
|
392 |
{
|
|
393 |
QgsDebugMsg( "soap request parse error" );
|
|
394 |
QgsDebugMsg( "error message: " + errorMsg );
|
|
395 |
QgsDebugMsg( "the xml string was:" );
|
|
396 |
QgsDebugMsg( filterIt.value() );
|
|
397 |
}
|
|
398 |
else
|
|
399 |
{
|
|
400 |
filterOk = true;
|
|
401 |
}
|
|
402 |
} catch(QgsMapServiceException& e ) {
|
|
403 |
filterOk = false;
|
|
404 |
}
|
|
405 |
}
|
|
406 |
|
|
407 |
|
|
408 |
bool conversionSuccess;
|
|
409 |
double minx, miny, maxx, maxy;
|
|
410 |
bool bboxOk = false;
|
|
411 |
//read BBOX
|
|
412 |
QMap<QString, QString>::const_iterator bbIt = mParameterMap.find( "BBOX" );
|
|
413 |
if ( bbIt == mParameterMap.end() )
|
|
414 |
{
|
|
415 |
minx = 0; miny = 0; maxx = 0; maxy = 0;
|
|
416 |
}
|
|
417 |
else
|
|
418 |
{
|
|
419 |
bboxOk = true;
|
|
420 |
QString bbString = bbIt.value();
|
|
421 |
minx = bbString.section( ",", 0, 0 ).toDouble( &conversionSuccess );
|
|
422 |
if ( !conversionSuccess ) {bboxOk = false;}
|
|
423 |
miny = bbString.section( ",", 1, 1 ).toDouble( &conversionSuccess );
|
|
424 |
if ( !conversionSuccess ) {bboxOk = false;}
|
|
425 |
maxx = bbString.section( ",", 2, 2 ).toDouble( &conversionSuccess );
|
|
426 |
if ( !conversionSuccess ) {bboxOk = false;}
|
|
427 |
maxy = bbString.section( ",", 3, 3 ).toDouble( &conversionSuccess );
|
|
428 |
if ( !conversionSuccess ) {bboxOk = false;}
|
|
429 |
}
|
|
430 |
|
|
431 |
QList<QgsFeature> featureList;
|
|
432 |
if ( fidOk )
|
|
433 |
{
|
|
434 |
provider->featureAtId( fid.toInt(), feature, true, provider->attributeIndexes() );
|
|
435 |
featureList.append(feature);
|
|
436 |
}
|
|
437 |
else if ( filterOk )
|
|
438 |
{
|
|
439 |
provider->select( provider->attributeIndexes(), searchRect, true, true );
|
|
440 |
try {
|
|
441 |
QgsFilter* mFilter = QgsFilter::createFilterFromXml( filter.firstChild().toElement().firstChild().toElement(), layer );
|
|
442 |
while ( provider->nextFeature( feature ) )
|
|
443 |
{
|
|
444 |
if ( mFilter )
|
|
445 |
{
|
|
446 |
if ( mFilter->evaluate( feature ) )
|
|
447 |
{
|
|
448 |
featureList.append(feature);
|
|
449 |
}
|
|
450 |
}
|
|
451 |
else
|
|
452 |
{
|
|
453 |
featureList.append(feature);
|
|
454 |
}
|
|
455 |
}
|
|
456 |
delete mFilter;
|
|
457 |
} catch(QgsMapServiceException& e ) {
|
|
458 |
while ( provider->nextFeature( feature ) )
|
|
459 |
{
|
|
460 |
featureList.append(feature);
|
|
461 |
}
|
|
462 |
}
|
|
463 |
}
|
|
464 |
else
|
|
465 |
{
|
|
466 |
if ( bboxOk )
|
|
467 |
searchRect.set( minx, miny, maxx, maxy );
|
|
468 |
provider->select( provider->attributeIndexes(), searchRect, true, true );
|
|
469 |
while ( provider->nextFeature( feature ) )
|
|
470 |
{
|
|
471 |
featureList.append(feature);
|
|
472 |
}
|
|
473 |
}
|
|
474 |
QList<QgsFeature>::const_iterator featureIt = featureList.constBegin();
|
|
475 |
|
|
476 |
if ( format == "GeoJSON" )
|
|
477 |
{
|
|
478 |
int featureCounter = 0;
|
|
479 |
|
|
480 |
QString fcString;
|
|
481 |
fcString.append("{\"type\": \"FeatureCollection\",\n");
|
|
482 |
fcString.append(" \"features\": [\n");
|
|
483 |
for ( ; featureIt != featureList.constEnd(); ++featureIt )
|
|
484 |
{
|
|
485 |
feature = *featureIt;
|
|
486 |
if (featureCounter == 0)
|
|
487 |
fcString.append(" {\"type\": \"Feature\",\n");
|
|
488 |
else
|
|
489 |
fcString.append(" ,{\"type\": \"Feature\",\n");
|
|
490 |
|
|
491 |
fcString.append(" \"id\": ");
|
|
492 |
fcString.append( QString::number( feature.id() ) );
|
|
493 |
fcString.append(",\n");
|
|
494 |
|
|
495 |
QgsGeometry* geom = feature.geometry();
|
|
496 |
if ( geom )
|
|
497 |
{
|
|
498 |
fcString.append(" \"geometry\": ");
|
|
499 |
fcString.append(geom->exportToGeoJSON());
|
|
500 |
fcString.append(",\n");
|
|
501 |
}
|
|
502 |
|
|
503 |
fcString.append(" \"properties\": {\n");
|
|
504 |
|
|
505 |
//read all attribute values from the feature
|
|
506 |
featureAttributes = feature.attributeMap();
|
|
507 |
int attributeCounter = 0;
|
|
508 |
for ( QgsAttributeMap::const_iterator it = featureAttributes.begin(); it != featureAttributes.end(); ++it )
|
|
509 |
{
|
|
510 |
|
|
511 |
QString attributeName = fields[it.key()].name();
|
|
512 |
//skip attribute if it has edit type 'hidden'
|
|
513 |
if ( layerHiddenAttributes.contains( attributeName ) )
|
|
514 |
{
|
|
515 |
continue;
|
|
516 |
}
|
|
517 |
|
|
518 |
//check if the attribute name should be replaced with an alias
|
|
519 |
QMap<int, QString>::const_iterator aliasIt = layerAliasInfo.find( it.key() );
|
|
520 |
if ( aliasIt != layerAliasInfo.constEnd() )
|
|
521 |
{
|
|
522 |
attributeName = aliasIt.value();
|
|
523 |
}
|
|
524 |
|
|
525 |
if (attributeCounter == 0)
|
|
526 |
fcString.append(" \"");
|
|
527 |
else
|
|
528 |
fcString.append(" ,\"");
|
|
529 |
fcString.append(attributeName);
|
|
530 |
fcString.append("\": ");
|
|
531 |
if (it->type() == 6 || it->type() == 2)
|
|
532 |
{
|
|
533 |
fcString.append( it->toString());
|
|
534 |
}
|
|
535 |
else
|
|
536 |
{
|
|
537 |
fcString.append("\"");
|
|
538 |
fcString.append( it->toString());
|
|
539 |
fcString.append("\"");
|
|
540 |
}
|
|
541 |
fcString.append("\n");
|
|
542 |
++attributeCounter;
|
|
543 |
}
|
|
544 |
|
|
545 |
fcString.append(" }\n");
|
|
546 |
|
|
547 |
fcString.append(" }");
|
|
548 |
fcString.append("\n");
|
|
549 |
|
|
550 |
++featureCounter;
|
|
551 |
}
|
|
552 |
fcString.append(" ]\n");
|
|
553 |
fcString.append("}");
|
|
554 |
|
|
555 |
result = fcString.toUtf8();
|
|
556 |
}
|
|
557 |
else
|
|
558 |
{
|
|
559 |
QDomDocument gmlDoc;
|
|
560 |
//wfs:FeatureCollection
|
|
561 |
QDomElement fcElement = gmlDoc.createElement( "wfs:FeatureCollection"/*wfs:FeatureCollection*/ );
|
|
562 |
fcElement.setAttribute( "xmlns", "http://www.opengis.net/wfs" );
|
|
563 |
fcElement.setAttribute( "xmlns:wfs", "http://www.opengis.net/wfs" );
|
|
564 |
fcElement.setAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
|
|
565 |
fcElement.setAttribute( "xsi:schemaLocation", "http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/wfs.xsd" );
|
|
566 |
fcElement.setAttribute( "xmlns:ogc", "http://www.opengis.net/ogc" );
|
|
567 |
fcElement.setAttribute( "xmlns:gml", "http://www.opengis.net/gml" );
|
|
568 |
fcElement.setAttribute( "xmlns:ows", "http://www.opengis.net/ows" );
|
|
569 |
fcElement.setAttribute( "xmlns:xlink", "http://www.w3.org/1999/xlink" );
|
|
570 |
fcElement.setAttribute( "xmlns:qgs", "http://www.qgis.org/gml" );
|
|
571 |
gmlDoc.appendChild( fcElement );
|
|
572 |
|
|
573 |
for ( ; featureIt != featureList.constEnd(); ++featureIt )
|
|
574 |
{
|
|
575 |
feature = *featureIt;
|
|
576 |
//gml:FeatureMember
|
|
577 |
QDomElement featureElement = gmlDoc.createElement( "gml:featureMember"/*wfs:FeatureMember*/ );
|
|
578 |
fcElement.appendChild(featureElement);
|
|
579 |
|
|
580 |
//qgs:%TYPENAME%
|
|
581 |
QDomElement typeNameElement = gmlDoc.createElement( "qgs:"+typeName.replace( QString(" "), QString("_") )/*qgs:%TYPENAME%*/ );
|
|
582 |
typeNameElement.setAttribute( "fid", QString::number( feature.id() ) );
|
|
583 |
featureElement.appendChild(typeNameElement);
|
|
584 |
|
|
585 |
//read all attribute values from the feature
|
|
586 |
featureAttributes = feature.attributeMap();
|
|
587 |
for ( QgsAttributeMap::const_iterator it = featureAttributes.begin(); it != featureAttributes.end(); ++it )
|
|
588 |
{
|
|
589 |
|
|
590 |
QString attributeName = fields[it.key()].name();
|
|
591 |
//skip attribute if it has edit type 'hidden'
|
|
592 |
if ( layerHiddenAttributes.contains( attributeName ) )
|
|
593 |
{
|
|
594 |
continue;
|
|
595 |
}
|
|
596 |
|
|
597 |
//check if the attribute name should be replaced with an alias
|
|
598 |
QMap<int, QString>::const_iterator aliasIt = layerAliasInfo.find( it.key() );
|
|
599 |
if ( aliasIt != layerAliasInfo.constEnd() )
|
|
600 |
{
|
|
601 |
attributeName = aliasIt.value();
|
|
602 |
}
|
|
603 |
|
|
604 |
QDomElement fieldElem = gmlDoc.createElement( "qgs:"+attributeName );
|
|
605 |
QDomText fieldText = gmlDoc.createTextNode( it->toString() );
|
|
606 |
fieldElem.appendChild( fieldText );
|
|
607 |
typeNameElement.appendChild( fieldElem );
|
|
608 |
}
|
|
609 |
|
|
610 |
//add geometry column (as gml)
|
|
611 |
QDomElement geomElem = gmlDoc.createElement( "qgs:geometry" );
|
|
612 |
QDomElement gmlElem = createGeometryElem( feature.geometry(), gmlDoc );
|
|
613 |
if ( !gmlElem.isNull() )
|
|
614 |
{
|
|
615 |
QgsCoordinateReferenceSystem layerCrs = layer->crs();
|
|
616 |
if ( layerCrs.isValid() )
|
|
617 |
{
|
|
618 |
gmlElem.setAttribute( "srsName", layerCrs.authid() );
|
|
619 |
}
|
|
620 |
geomElem.appendChild( gmlElem );
|
|
621 |
typeNameElement.appendChild( geomElem );
|
|
622 |
}
|
|
623 |
}
|
|
624 |
|
|
625 |
result = gmlDoc.toByteArray();
|
|
626 |
}
|
|
627 |
|
|
628 |
}
|
|
629 |
else
|
|
630 |
{
|
|
631 |
return 2;
|
|
632 |
}
|
|
633 |
return 0;
|
|
634 |
}
|
|
635 |
|
|
636 |
QDomElement QgsWFSServer::createGeometryElem( QgsGeometry* geom, QDomDocument& doc ) /*const*/
|
|
637 |
{
|
|
638 |
if ( !geom )
|
|
639 |
{
|
|
640 |
return QDomElement();
|
|
641 |
}
|
|
642 |
|
|
643 |
QDomElement geomElement;
|
|
644 |
|
|
645 |
QString geomTypeName;
|
|
646 |
QGis::WkbType wkbType = geom->wkbType();
|
|
647 |
switch ( wkbType )
|
|
648 |
{
|
|
649 |
case QGis::WKBPoint:
|
|
650 |
case QGis::WKBPoint25D:
|
|
651 |
geomElement = createPointElem( geom, doc );
|
|
652 |
break;
|
|
653 |
case QGis::WKBMultiPoint:
|
|
654 |
case QGis::WKBMultiPoint25D:
|
|
655 |
geomElement = createMultiPointElem( geom, doc );
|
|
656 |
break;
|
|
657 |
case QGis::WKBLineString:
|
|
658 |
case QGis::WKBLineString25D:
|
|
659 |
geomElement = createLineStringElem( geom, doc );
|
|
660 |
break;
|
|
661 |
case QGis::WKBMultiLineString:
|
|
662 |
case QGis::WKBMultiLineString25D:
|
|
663 |
geomElement = createMultiLineStringElem( geom, doc );
|
|
664 |
break;
|
|
665 |
case QGis::WKBPolygon:
|
|
666 |
case QGis::WKBPolygon25D:
|
|
667 |
geomElement = createPolygonElem( geom, doc );
|
|
668 |
break;
|
|
669 |
case QGis::WKBMultiPolygon:
|
|
670 |
case QGis::WKBMultiPolygon25D:
|
|
671 |
geomElement = createMultiPolygonElem( geom, doc );
|
|
672 |
break;
|
|
673 |
default:
|
|
674 |
return QDomElement();
|
|
675 |
}
|
|
676 |
return geomElement;
|
|
677 |
}
|
|
678 |
|
|
679 |
QDomElement QgsWFSServer::createLineStringElem( QgsGeometry* geom, QDomDocument& doc ) const
|
|
680 |
{
|
|
681 |
if ( !geom )
|
|
682 |
{
|
|
683 |
return QDomElement();
|
|
684 |
}
|
|
685 |
|
|
686 |
QDomElement lineStringElem = doc.createElement( "gml:LineString" );
|
|
687 |
QDomElement coordElem = createCoordinateElem( geom->asPolyline(), doc );
|
|
688 |
lineStringElem.appendChild( coordElem );
|
|
689 |
return lineStringElem;
|
|
690 |
}
|
|
691 |
|
|
692 |
QDomElement QgsWFSServer::createMultiLineStringElem( QgsGeometry* geom, QDomDocument& doc ) const
|
|
693 |
{
|
|
694 |
if ( !geom )
|
|
695 |
{
|
|
696 |
return QDomElement();
|
|
697 |
}
|
|
698 |
|
|
699 |
QDomElement multiLineStringElem = doc.createElement( "gml:MultiLineString" );
|
|
700 |
QgsMultiPolyline multiline = geom->asMultiPolyline();
|
|
701 |
|
|
702 |
QgsMultiPolyline::const_iterator multiLineIt = multiline.constBegin();
|
|
703 |
for ( ; multiLineIt != multiline.constEnd(); ++multiLineIt )
|
|
704 |
{
|
|
705 |
QgsGeometry* lineGeom = QgsGeometry::fromPolyline( *multiLineIt );
|
|
706 |
if ( lineGeom )
|
|
707 |
{
|
|
708 |
QDomElement lineStringMemberElem = doc.createElement( "gml:lineStringMember" );
|
|
709 |
QDomElement lineElem = createLineStringElem( lineGeom, doc );
|
|
710 |
lineStringMemberElem.appendChild( lineElem );
|
|
711 |
multiLineStringElem.appendChild( lineStringMemberElem );
|
|
712 |
}
|
|
713 |
delete lineGeom;
|
|
714 |
}
|
|
715 |
|
|
716 |
return multiLineStringElem;
|
|
717 |
}
|
|
718 |
|
|
719 |
QDomElement QgsWFSServer::createPointElem( QgsGeometry* geom, QDomDocument& doc ) const
|
|
720 |
{
|
|
721 |
if ( !geom )
|
|
722 |
{
|
|
723 |
return QDomElement();
|
|
724 |
}
|
|
725 |
|
|
726 |
QDomElement pointElem = doc.createElement( "gml:Point" );
|
|
727 |
QgsPoint p = geom->asPoint();
|
|
728 |
QVector<QgsPoint> v;
|
|
729 |
v.append( p );
|
|
730 |
QDomElement coordElem = createCoordinateElem( v, doc );
|
|
731 |
pointElem.appendChild( coordElem );
|
|
732 |
return pointElem;
|
|
733 |
}
|
|
734 |
|
|
735 |
QDomElement QgsWFSServer::createMultiPointElem( QgsGeometry* geom, QDomDocument& doc ) const
|
|
736 |
{
|
|
737 |
if ( !geom )
|
|
738 |
{
|
|
739 |
return QDomElement();
|
|
740 |
}
|
|
741 |
|
|
742 |
QDomElement multiPointElem = doc.createElement( "gml:MultiPoint" );
|
|
743 |
QgsMultiPoint multiPoint = geom->asMultiPoint();
|
|
744 |
|
|
745 |
QgsMultiPoint::const_iterator multiPointIt = multiPoint.constBegin();
|
|
746 |
for ( ; multiPointIt != multiPoint.constEnd(); ++multiPointIt )
|
|
747 |
{
|
|
748 |
QgsGeometry* pointGeom = QgsGeometry::fromPoint( *multiPointIt );
|
|
749 |
if ( pointGeom )
|
|
750 |
{
|
|
751 |
QDomElement multiPointMemberElem = doc.createElement( "gml:pointMember" );
|
|
752 |
QDomElement pointElem = createPointElem( pointGeom, doc );
|
|
753 |
multiPointMemberElem.appendChild( pointElem );
|
|
754 |
multiPointElem.appendChild( multiPointMemberElem );
|
|
755 |
}
|
|
756 |
}
|
|
757 |
return multiPointElem;
|
|
758 |
}
|
|
759 |
|
|
760 |
QDomElement QgsWFSServer::createPolygonElem( QgsGeometry* geom, QDomDocument& doc ) const
|
|
761 |
{
|
|
762 |
if ( !geom )
|
|
763 |
{
|
|
764 |
return QDomElement();
|
|
765 |
}
|
|
766 |
|
|
767 |
QDomElement polygonElem = doc.createElement( "gml:Polygon" );
|
|
768 |
QgsPolygon poly = geom->asPolygon();
|
|
769 |
for ( int i = 0; i < poly.size(); ++i )
|
|
770 |
{
|
|
771 |
QString boundaryName;
|
|
772 |
if ( i == 0 )
|
|
773 |
{
|
|
774 |
boundaryName = "outerBoundaryIs";
|
|
775 |
}
|
|
776 |
else
|
|
777 |
{
|
|
778 |
boundaryName = "innerBoundaryIs";
|
|
779 |
}
|
|
780 |
QDomElement boundaryElem = doc.createElementNS( "http://www.opengis.net/gml", boundaryName );
|
|
781 |
QDomElement ringElem = doc.createElement( "gml:LinearRing" );
|
|
782 |
QDomElement coordElem = createCoordinateElem( poly.at( i ), doc );
|
|
783 |
ringElem.appendChild( coordElem );
|
|
784 |
boundaryElem.appendChild( ringElem );
|
|
785 |
polygonElem.appendChild( boundaryElem );
|
|
786 |
}
|
|
787 |
return polygonElem;
|
|
788 |
}
|
|
789 |
|
|
790 |
QDomElement QgsWFSServer::createMultiPolygonElem( QgsGeometry* geom, QDomDocument& doc ) const
|
|
791 |
{
|
|
792 |
if ( !geom )
|
|
793 |
{
|
|
794 |
return QDomElement();
|
|
795 |
}
|
|
796 |
QDomElement multiPolygonElem = doc.createElement( "gml:MultiPolygon" );
|
|
797 |
QgsMultiPolygon multipoly = geom->asMultiPolygon();
|
|
798 |
|
|
799 |
QgsMultiPolygon::const_iterator polyIt = multipoly.constBegin();
|
|
800 |
for ( ; polyIt != multipoly.constEnd(); ++polyIt )
|
|
801 |
{
|
|
802 |
QgsGeometry* polygonGeom = QgsGeometry::fromPolygon( *polyIt );
|
|
803 |
if ( polygonGeom )
|
|
804 |
{
|
|
805 |
QDomElement polygonMemberElem = doc.createElement( "gml:polygonMember" );
|
|
806 |
QDomElement polygonElem = createPolygonElem( polygonGeom, doc );
|
|
807 |
delete polygonGeom;
|
|
808 |
polygonMemberElem.appendChild( polygonElem );
|
|
809 |
multiPolygonElem.appendChild( polygonMemberElem );
|
|
810 |
}
|
|
811 |
}
|
|
812 |
return multiPolygonElem;
|
|
813 |
}
|
|
814 |
|
|
815 |
QDomElement QgsWFSServer::createCoordinateElem( const QVector<QgsPoint> points, QDomDocument& doc ) const
|
|
816 |
{
|
|
817 |
QDomElement coordElem = doc.createElement( "gml:coordinates" );
|
|
818 |
coordElem.setAttribute( "cs", "," );
|
|
819 |
coordElem.setAttribute( "ts", " " );
|
|
820 |
|
|
821 |
//precision 4 for meters / feet, precision 8 for degrees
|
|
822 |
int precision = 8;
|
|
823 |
/*
|
|
824 |
if ( mSourceCRS.mapUnits() == QGis::Meters
|
|
825 |
|| mSourceCRS.mapUnits() == QGis::Feet )
|
|
826 |
{
|
|
827 |
precision = 4;
|
|
828 |
}
|
|
829 |
*/
|
|
830 |
|
|
831 |
QString coordString;
|
|
832 |
QVector<QgsPoint>::const_iterator pointIt = points.constBegin();
|
|
833 |
for ( ; pointIt != points.constEnd(); ++pointIt )
|
|
834 |
{
|
|
835 |
if ( pointIt != points.constBegin() )
|
|
836 |
{
|
|
837 |
coordString += " ";
|
|
838 |
}
|
|
839 |
coordString += QString::number( pointIt->x(), 'f', precision );
|
|
840 |
coordString += ",";
|
|
841 |
coordString += QString::number( pointIt->y(), 'f', precision );
|
|
842 |
}
|