wfs-server.patch

For branch 1.8 and trunk : This patch adds a public method to get QgsGeometry in GeoJSON format. This pacth adds WFS 1.0.0 service capabilities to QGIS Server This pacth extends WFS 1.0.0 service capabilities with GeoJSON output format. - Michael Douchin, 2012-02-27 08:58 AM

Download (54.2 KB)

View differences:

src/core/qgsgeometry.cpp
4167 4167
  }
4168 4168
}
4169 4169

  
4170
QString QgsGeometry::exportToGeoJSON()
4171
{
4172
  QgsDebugMsg( "entered." );
4173

  
4174
  // TODO: implement with GEOS
4175
  if ( mDirtyWkb )
4176
  {
4177
    exportGeosToWkb();
4178
  }
4179

  
4180
  if ( !mGeometry )
4181
  {
4182
    QgsDebugMsg( "WKB geometry not available!" );
4183
    return QString::null;
4184
  }
4185

  
4186
  QGis::WkbType wkbType;
4187
  bool hasZValue = false;
4188
  double *x, *y;
4189

  
4190
  QString mWkt; // TODO: rename
4191

  
4192
  // Will this really work when mGeometry[0] == 0 ???? I (gavin) think not.
4193
  //wkbType = (mGeometry[0] == 1) ? mGeometry[1] : mGeometry[4];
4194
  memcpy( &wkbType, &( mGeometry[1] ), sizeof( int ) );
4195

  
4196
  switch ( wkbType )
4197
  {
4198
    case QGis::WKBPoint25D:
4199
    case QGis::WKBPoint:
4200
    {
4201
      mWkt += "{ \"type\": \"Point\", \"coordinates\": [";
4202
      x = ( double * )( mGeometry + 5 );
4203
      mWkt += QString::number( *x, 'f', 6 );
4204
      mWkt += ", ";
4205
      y = ( double * )( mGeometry + 5 + sizeof( double ) );
4206
      mWkt += QString::number( *y, 'f', 6 );
4207
      mWkt += "] }";
4208
      return mWkt;
4209
    }
4210

  
4211
    case QGis::WKBLineString25D:
4212
      hasZValue = true;
4213
    case QGis::WKBLineString:
4214
    {
4215
      QgsDebugMsg( "LINESTRING found" );
4216
      unsigned char *ptr;
4217
      int *nPoints;
4218
      int idx;
4219

  
4220
      mWkt += "{ \"type\": \"LineString\", \"coordinates\": [ ";
4221
      // get number of points in the line
4222
      ptr = mGeometry + 5;
4223
      nPoints = ( int * ) ptr;
4224
      ptr = mGeometry + 1 + 2 * sizeof( int );
4225
      for ( idx = 0; idx < *nPoints; ++idx )
4226
      {
4227
        if ( idx != 0 )
4228
        {
4229
          mWkt += ", ";
4230
        }
4231
        mWkt += "[";
4232
        x = ( double * ) ptr;
4233
        mWkt += QString::number( *x, 'f', 6 );
4234
        mWkt += ", ";
4235
        ptr += sizeof( double );
4236
        y = ( double * ) ptr;
4237
        mWkt += QString::number( *y, 'f', 6 );
4238
        ptr += sizeof( double );
4239
        if ( hasZValue )
4240
        {
4241
          ptr += sizeof( double );
4242
        }
4243
        mWkt += "]";
4244
      }
4245
      mWkt += " ] }";
4246
      return mWkt;
4247
    }
4248

  
4249
    case QGis::WKBPolygon25D:
4250
      hasZValue = true;
4251
    case QGis::WKBPolygon:
4252
    {
4253
      QgsDebugMsg( "POLYGON found" );
4254
      unsigned char *ptr;
4255
      int idx, jdx;
4256
      int *numRings, *nPoints;
4257

  
4258
      mWkt += "{ \"type\": \"Polygon\", \"coordinates\": [ ";
4259
      // get number of rings in the polygon
4260
      numRings = ( int * )( mGeometry + 1 + sizeof( int ) );
4261
      if ( !( *numRings ) )  // sanity check for zero rings in polygon
4262
      {
4263
        return QString();
4264
      }
4265
      int *ringStart; // index of first point for each ring
4266
      int *ringNumPoints; // number of points in each ring
4267
      ringStart = new int[*numRings];
4268
      ringNumPoints = new int[*numRings];
4269
      ptr = mGeometry + 1 + 2 * sizeof( int ); // set pointer to the first ring
4270
      for ( idx = 0; idx < *numRings; idx++ )
4271
      {
4272
        if ( idx != 0 )
4273
        {
4274
          mWkt += ", ";
4275
        }
4276
        mWkt += "[ ";
4277
        // get number of points in the ring
4278
        nPoints = ( int * ) ptr;
4279
        ringNumPoints[idx] = *nPoints;
4280
        ptr += 4;
4281

  
4282
        for ( jdx = 0; jdx < *nPoints; jdx++ )
4283
        {
4284
          if ( jdx != 0 )
4285
          {
4286
            mWkt += ", ";
4287
          }
4288
          mWkt += "[";
4289
          x = ( double * ) ptr;
4290
          mWkt += QString::number( *x, 'f', 6 );
4291
          mWkt += ", ";
4292
          ptr += sizeof( double );
4293
          y = ( double * ) ptr;
4294
          mWkt += QString::number( *y, 'f', 6 );
4295
          ptr += sizeof( double );
4296
          if ( hasZValue )
4297
          {
4298
            ptr += sizeof( double );
4299
          }
4300
          mWkt += "]";
4301
        }
4302
        mWkt += " ]";
4303
      }
4304
      mWkt += " ] }";
4305
      delete [] ringStart;
4306
      delete [] ringNumPoints;
4307
      return mWkt;
4308
    }
4309

  
4310
    case QGis::WKBMultiPoint25D:
4311
      hasZValue = true;
4312
    case QGis::WKBMultiPoint:
4313
    {
4314
      unsigned char *ptr;
4315
      int idx;
4316
      int *nPoints;
4317

  
4318
      mWkt += "{ \"type\": \"MultiPoint\", \"coordinates\": [ ";
4319
      nPoints = ( int* )( mGeometry + 5 );
4320
      ptr = mGeometry + 5 + sizeof( int );
4321
      for ( idx = 0; idx < *nPoints; ++idx )
4322
      {
4323
        ptr += ( 1 + sizeof( int ) );
4324
        if ( idx != 0 )
4325
        {
4326
          mWkt += ", ";
4327
        }
4328
        mWkt += "[";
4329
        x = ( double * )( ptr );
4330
        mWkt += QString::number( *x, 'f', 6 );
4331
        mWkt += ", ";
4332
        ptr += sizeof( double );
4333
        y = ( double * )( ptr );
4334
        mWkt += QString::number( *y, 'f', 6 );
4335
        ptr += sizeof( double );
4336
        if ( hasZValue )
4337
        {
4338
          ptr += sizeof( double );
4339
        }
4340
        mWkt += "]";
4341
      }
4342
      mWkt += " ] }";
4343
      return mWkt;
4344
    }
4345

  
4346
    case QGis::WKBMultiLineString25D:
4347
      hasZValue = true;
4348
    case QGis::WKBMultiLineString:
4349
    {
4350
      QgsDebugMsg( "MULTILINESTRING found" );
4351
      unsigned char *ptr;
4352
      int idx, jdx, numLineStrings;
4353
      int *nPoints;
4354

  
4355
      mWkt += "{ \"type\": \"MultiLineString\", \"coordinates\": [ ";
4356
      numLineStrings = ( int )( mGeometry[5] );
4357
      ptr = mGeometry + 9;
4358
      for ( jdx = 0; jdx < numLineStrings; jdx++ )
4359
      {
4360
        if ( jdx != 0 )
4361
        {
4362
          mWkt += ", ";
4363
        }
4364
        mWkt += "[ ";
4365
        ptr += 5; // skip type since we know its 2
4366
        nPoints = ( int * ) ptr;
4367
        ptr += sizeof( int );
4368
        for ( idx = 0; idx < *nPoints; idx++ )
4369
        {
4370
          if ( idx != 0 )
4371
          {
4372
            mWkt += ", ";
4373
          }
4374
          mWkt += "[";
4375
          x = ( double * ) ptr;
4376
          mWkt += QString::number( *x, 'f', 6 );
4377
          ptr += sizeof( double );
4378
          mWkt += ", ";
4379
          y = ( double * ) ptr;
4380
          mWkt += QString::number( *y, 'f', 6 );
4381
          ptr += sizeof( double );
4382
          if ( hasZValue )
4383
          {
4384
            ptr += sizeof( double );
4385
          }
4386
          mWkt += "]";
4387
        }
4388
        mWkt += " ]";
4389
      }
4390
      mWkt += " ] }";
4391
      return mWkt;
4392
    }
4393

  
4394
    case QGis::WKBMultiPolygon25D:
4395
      hasZValue = true;
4396
    case QGis::WKBMultiPolygon:
4397
    {
4398
      QgsDebugMsg( "MULTIPOLYGON found" );
4399
      unsigned char *ptr;
4400
      int idx, jdx, kdx;
4401
      int *numPolygons, *numRings, *nPoints;
4402

  
4403
      mWkt += "{ \"type\": \"MultiPolygon\", \"coordinates\": [ ";
4404
      ptr = mGeometry + 5;
4405
      numPolygons = ( int * ) ptr;
4406
      ptr = mGeometry + 9;
4407
      for ( kdx = 0; kdx < *numPolygons; kdx++ )
4408
      {
4409
        if ( kdx != 0 )
4410
        {
4411
          mWkt += ", ";
4412
        }
4413
        mWkt += "[ ";
4414
        ptr += 5;
4415
        numRings = ( int * ) ptr;
4416
        ptr += 4;
4417
        for ( idx = 0; idx < *numRings; idx++ )
4418
        {
4419
          if ( idx != 0 )
4420
          {
4421
            mWkt += ", ";
4422
          }
4423
          mWkt += "[ ";
4424
          nPoints = ( int * ) ptr;
4425
          ptr += 4;
4426
          for ( jdx = 0; jdx < *nPoints; jdx++ )
4427
          {
4428
            if ( jdx != 0 )
4429
            {
4430
              mWkt += ", ";
4431
            }
4432
            mWkt += "[";
4433
            x = ( double * ) ptr;
4434
            mWkt += QString::number( *x, 'f', 6 );
4435
            ptr += sizeof( double );
4436
            mWkt += ", ";
4437
            y = ( double * ) ptr;
4438
            mWkt += QString::number( *y, 'f', 6 );
4439
            ptr += sizeof( double );
4440
            if ( hasZValue )
4441
            {
4442
              ptr += sizeof( double );
4443
            }
4444
            mWkt += "]";
4445
          }
4446
          mWkt += " ]";
4447
        }
4448
        mWkt += " ]";
4449
      }
4450
      mWkt += " ] }";
4451
      return mWkt;
4452
    }
4453

  
4454
    default:
4455
      QgsDebugMsg( "error: mGeometry type not recognized" );
4456
      return QString::null;
4457
  }
4458
}
4459

  
4170 4460
bool QgsGeometry::exportWkbToGeos()
4171 4461
{
4172 4462
  QgsDebugMsgLevel( "entered.", 3 );
src/core/qgsgeometry.h
360 360
     */
361 361
    QString exportToWkt();
362 362

  
363
    /** Exports the geometry to mGeoJSON
364
        @return true in case of success and false else
365
     */
366
    QString exportToGeoJSON();
367

  
363 368
    /* Accessor functions for getting geometry data */
364 369

  
365 370
    /** return contents of the geometry as a point
src/mapserver/CMakeLists.txt
28 28
  qgssldparser.cpp
29 29
  qgssldrenderer.cpp
30 30
  qgswmsserver.cpp
31
  qgswfsserver.cpp
31 32
  qgsmapserviceexception.cpp
32 33
  qgsmslayercache.cpp
33 34
  qgsfilter.cpp
src/mapserver/qgis_map_serv.cpp
26 26
#include "qgsproviderregistry.h"
27 27
#include "qgslogger.h"
28 28
#include "qgswmsserver.h"
29
#include "qgswfsserver.h"
29 30
#include "qgsmaprenderer.h"
30 31
#include "qgsmapserviceexception.h"
31 32
#include "qgsprojectparser.h"
......
264 265

  
265 266
    //request to WMS?
266 267
    QString serviceString;
267
#ifndef QGISDEBUG
268
    serviceString = parameterMap.value( "SERVICE", "WMS" );
269
#else
270 268
    paramIt = parameterMap.find( "SERVICE" );
271 269
    if ( paramIt == parameterMap.constEnd() )
272 270
    {
271
#ifndef QGISDEBUG
272
      serviceString = parameterMap.value( "SERVICE", "WMS" );
273
#else
273 274
      QgsDebugMsg( "unable to find 'SERVICE' parameter, exiting..." );
274 275
      theRequestHandler->sendServiceException( QgsMapServiceException( "ServiceNotSpecified", "Service not specified. The SERVICE parameter is mandatory" ) );
275 276
      delete theRequestHandler;
276 277
      continue;
278
#endif
277 279
    }
278 280
    else
279 281
    {
280 282
      serviceString = paramIt.value();
281 283
    }
282
#endif
283 284

  
284 285
    QgsWMSServer* theServer = 0;
286
    if ( serviceString == "WFS" )
287
    {
288
      delete theServer;
289
      QgsWFSServer* theServer = 0;
290
      try
291
      {
292
        theServer = new QgsWFSServer( parameterMap );
293
      }
294
      catch ( QgsMapServiceException e ) //admin.sld may be invalid
295
      {
296
        theRequestHandler->sendServiceException( e );
297
        continue;
298
      }
299

  
300
      theServer->setAdminConfigParser( adminConfigParser );
301

  
302

  
303
      //request type
304
      QString request = parameterMap.value( "REQUEST" );
305
      if ( request.isEmpty() )
306
      {
307
        //do some error handling
308
        QgsDebugMsg( "unable to find 'REQUEST' parameter, exiting..." );
309
        theRequestHandler->sendServiceException( QgsMapServiceException( "OperationNotSupported", "Please check the value of the REQUEST parameter" ) );
310
        delete theRequestHandler;
311
        delete theServer;
312
        continue;
313
      }
314

  
315
      if ( request == "GetCapabilities" )
316
      {
317
        QDomDocument capabilitiesDocument;
318
        try
319
        {
320
          capabilitiesDocument = theServer->getCapabilities();
321
        }
322
        catch ( QgsMapServiceException& ex )
323
        {
324
          theRequestHandler->sendServiceException( ex );
325
          delete theRequestHandler;
326
          delete theServer;
327
          continue;
328
        }
329
        QgsDebugMsg( "sending GetCapabilities response" );
330
        theRequestHandler->sendGetCapabilitiesResponse( capabilitiesDocument );
331
        delete theRequestHandler;
332
        delete theServer;
333
        continue;
334
      }
335
      else if ( request == "DescribeFeatureType" )
336
      {
337
        QDomDocument describeDocument;
338
        try
339
        {
340
          describeDocument = theServer->describeFeatureType();
341
        }
342
        catch ( QgsMapServiceException& ex )
343
        {
344
          theRequestHandler->sendServiceException( ex );
345
          delete theRequestHandler;
346
          delete theServer;
347
          continue;
348
        }
349
        QgsDebugMsg( "sending GetCapabilities response" );
350
        theRequestHandler->sendGetCapabilitiesResponse( describeDocument );
351
        delete theRequestHandler;
352
        delete theServer;
353
        continue;
354
      }
355
      else if ( request == "GetFeature" )
356
      {
357
        //output format for GetFeature
358
        QString outputFormat = parameterMap.value( "OUTPUTFORMAT" );
359
        QByteArray fba;
360
        try
361
        {
362
          if ( theServer->getFeature( fba, outputFormat ) != 0 )
363
          {
364
            delete theRequestHandler;
365
            delete theServer;
366
            continue;
367
          } else {
368
            theRequestHandler->sendGetFeatureResponse( &fba, outputFormat );
369
            delete theRequestHandler;
370
            delete theServer;
371
            continue;
372
          }
373
        }
374
        catch ( QgsMapServiceException& ex )
375
        {
376
          theRequestHandler->sendServiceException( ex );
377
          delete theRequestHandler;
378
          delete theServer;
379
          continue;
380
        }
381
      }
382

  
383
      return 0;
384
    }
385

  
285 386
    try
286 387
    {
287 388
      theServer = new QgsWMSServer( parameterMap, theMapRenderer );
src/mapserver/qgsconfigparser.h
41 41
    /**Adds layer and style specific capabilities elements to the parent node. This includes the individual layers and styles, their description, native CRS, bounding boxes, etc.*/
42 42
    virtual void layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const = 0;
43 43

  
44
    virtual void featureTypeList( QDomElement& parentElement, QDomDocument& doc ) const = 0;
45

  
44 46
    /**Returns one or possibly several maplayers for a given layer name and style. If there are several layers, the layers should be drawn in inverse list order.
45 47
       If no layers/style are found, an empty list is returned
46 48
      @param allowCache true if layer can be read from / written to cache*/
src/mapserver/qgshttprequesthandler.cpp
277 277
  sendHttpResponse( ba, formatToMimeType( mFormat ) );
278 278
}
279 279

  
280
void QgsHttpRequestHandler::sendGetFeatureResponse( QByteArray* ba, const QString& infoFormat ) const
281
{
282
  if (infoFormat == "GeoJSON")
283
    sendHttpResponse( ba, "text/plain" );
284
  else
285
    sendHttpResponse( ba, "text/xml" );
286
}
287

  
280 288
void QgsHttpRequestHandler::requestStringToParameterMap( const QString& request, QMap<QString, QString>& parameters )
281 289
{
282 290
  parameters.clear();
src/mapserver/qgshttprequesthandler.h
34 34
    virtual void sendServiceException( const QgsMapServiceException& ex ) const;
35 35
    virtual void sendGetStyleResponse( const QDomDocument& doc ) const;
36 36
    virtual void sendGetPrintResponse( QByteArray* ba ) const;
37
    virtual void sendGetFeatureResponse( QByteArray* ba, const QString& infoFormat ) const;
37 38

  
38 39
  protected:
39 40
    void sendHttpResponse( QByteArray* ba, const QString& format ) const;
src/mapserver/qgsprojectparser.cpp
138 138
  combineExtentAndCrsOfGroupChildren( layerParentElem, doc );
139 139
}
140 140

  
141
void QgsProjectParser::featureTypeList( QDomElement& parentElement, QDomDocument& doc ) const
142
{
143
  QStringList nonIdentifiableLayers = identifyDisabledLayers();
144

  
145
  if ( mProjectLayerElements.size() < 1 )
146
  {
147
    return;
148
  }
149

  
150
  QMap<QString, QgsMapLayer *> layerMap;
151

  
152
  foreach( const QDomElement &elem, mProjectLayerElements )
153
  {
154
    QString type = elem.attribute( "type" );
155
    if ( type == "vector" )
156
    {
157
      //QgsMapLayer *layer = createLayerFromElement( *layerIt );
158
      QgsMapLayer *layer = createLayerFromElement( elem );
159
      if ( layer )
160
      {
161
        QgsDebugMsg( QString( "add layer %1 to map" ).arg( layer->id() ) );
162
        layerMap.insert( layer->id(), layer );
163

  
164
        QDomElement layerElem = doc.createElement( "FeatureType" );
165
        QDomElement nameElem = doc.createElement( "Name" );
166
        //We use the layer name even though it might not be unique.
167
        //Because the id sometimes contains user/pw information and the name is more descriptive
168
        QDomText nameText = doc.createTextNode( layer->name() );
169
        nameElem.appendChild( nameText );
170
        layerElem.appendChild( nameElem );
171

  
172
        QDomElement titleElem = doc.createElement( "Title" );
173
        QDomText titleText = doc.createTextNode( layer->name() );
174
        titleElem.appendChild( titleText );
175
        layerElem.appendChild( titleElem );
176

  
177
        //appendExGeographicBoundingBox( layerElem, doc, layer->extent(), layer->crs() );
178

  
179
        QDomElement srsElem = doc.createElement( "SRS" );
180
        QDomText srsText = doc.createTextNode( layer->crs().authid() );
181
        srsElem.appendChild( srsText );
182
        layerElem.appendChild( srsElem );
183

  
184
        QgsRectangle layerExtent = layer->extent();
185
        QDomElement bBoxElement = doc.createElement( "LatLongBoundingBox" );
186
        bBoxElement.setAttribute( "minx", QString::number( layerExtent.xMinimum() ) );
187
        bBoxElement.setAttribute( "miny", QString::number( layerExtent.yMinimum() ) );
188
        bBoxElement.setAttribute( "maxx", QString::number( layerExtent.xMaximum() ) );
189
        bBoxElement.setAttribute( "maxy", QString::number( layerExtent.yMaximum() ) );
190
        layerElem.appendChild( bBoxElement );
191

  
192
        parentElement.appendChild(layerElem);
193
      }
194
#if QGSMSDEBUG
195
      else
196
      {
197
        QString buf;
198
        QTextStream s( &buf );
199
        layerIt->save( s, 0 );
200
        QgsMSDebugMsg( QString( "layer %1 not found" ).arg( buf ) );
201
      }
202
#endif
203
    }
204
  }
205
  return;
206
}
207

  
141 208
void QgsProjectParser::addLayers( QDomDocument &doc,
142 209
                                  QDomElement &parentElem,
143 210
                                  const QDomElement &legendElem,
src/mapserver/qgsprojectparser.h
38 38
    /**Adds layer and style specific capabilities elements to the parent node. This includes the individual layers and styles, their description, native CRS, bounding boxes, etc.*/
39 39
    virtual void layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const;
40 40

  
41
    virtual void featureTypeList( QDomElement& parentElement, QDomDocument& doc ) const;
42

  
41 43
    int numberOfLayers() const;
42 44

  
43 45
    /**Returns one or possibly several maplayers for a given layer name and style. If no layers/style are found, an empty list is returned*/
src/mapserver/qgsrequesthandler.h
41 41
    virtual void sendServiceException( const QgsMapServiceException& ex ) const = 0;
42 42
    virtual void sendGetStyleResponse( const QDomDocument& doc ) const = 0;
43 43
    virtual void sendGetPrintResponse( QByteArray* ba ) const = 0;
44
    virtual void sendGetFeatureResponse( QByteArray* ba, const QString& infoFormat ) const = 0;
44 45
    QString format() const { return mFormat; }
45 46
  protected:
46 47
    /**This is set by the parseInput methods of the subclasses (parameter FORMAT, e.g. 'FORMAT=PNG')*/
src/mapserver/qgssldparser.h
56 56
    /**Adds layer and style specific capabilities elements to the parent node. This includes the individual layers and styles, their description, native CRS, bounding boxes, etc.*/
57 57
    void layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const;
58 58

  
59
    void featureTypeList( QDomElement& parentElement, QDomDocument& doc ) const {};
60

  
59 61
    /**Returns number of layers in configuration*/
60 62
    int numberOfLayers() const;
61 63

  
src/mapserver/qgswfsserver.cpp
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
  }
... This diff was truncated because it exceeds the maximum size that can be displayed.