@@ -88,7 +88,7 @@ QgsWmsProvider::QgsWmsProvider( QString const &uri )
88
88
, mHttpCapabilitiesResponse( 0 )
89
89
, mHttpGetLegendGraphicResponse( 0 )
90
90
, mGetLegendGraphicImage()
91
- , mGetLegendGraphicScale( 0 )
91
+ , mGetLegendGraphicScale( 0.0 )
92
92
, mImageCrs( DEFAULT_LATLON_CRS )
93
93
, mCachedImage( 0 )
94
94
, mCacheReply( 0 )
@@ -370,6 +370,44 @@ QString QgsWmsProvider::getTileUrl() const
370
370
}
371
371
}
372
372
373
+ QString QgsWmsProvider::getLegendGraphicUrl () const
374
+ {
375
+ QString url;
376
+
377
+ for ( int i = 0 ; i < mLayersSupported .size () && url.isEmpty (); i++ )
378
+ {
379
+ const QgsWmsLayerProperty &l = mLayersSupported [i];
380
+
381
+ if ( l.name != mActiveSubLayers [0 ] )
382
+ continue ;
383
+
384
+ for ( int j = 0 ; j < l.style .size () && url.isEmpty (); j++ )
385
+ {
386
+ const QgsWmsStyleProperty &s = l.style [j];
387
+
388
+ if ( s.name != mActiveSubStyles [0 ] )
389
+ continue ;
390
+
391
+ for ( int k = 0 ; k < s.legendUrl .size () && url.isEmpty (); k++ )
392
+ {
393
+ const QgsWmsLegendUrlProperty &l = s.legendUrl [k];
394
+
395
+ if ( l.format != mImageMimeType )
396
+ continue ;
397
+
398
+ url = l.onlineResource .xlinkHref ;
399
+ }
400
+ }
401
+ }
402
+
403
+ if ( url.isEmpty () && mCapabilities .capability .request .getLegendGraphic .dcpType .size () > 0 )
404
+ {
405
+ url = mCapabilities .capability .request .getLegendGraphic .dcpType .front ().http .get .onlineResource .xlinkHref ;
406
+ }
407
+
408
+ return url.isEmpty () ? url : prepareUri ( url );
409
+ }
410
+
373
411
void QgsWmsProvider::addLayers ( QStringList const &layers,
374
412
QStringList const &styles )
375
413
{
@@ -411,14 +449,46 @@ void QgsWmsProvider::setLayerOrder( QStringList const &layers )
411
449
{
412
450
QgsDebugMsg ( " Entering." );
413
451
452
+ if ( layers.size () != mActiveSubLayers .size () )
453
+ {
454
+ QgsDebugMsg ( " Invalid layer list length" );
455
+ return ;
456
+ }
457
+
458
+ QMap<QString, QString> styleMap;
459
+ for ( int i = 0 ; i < mActiveSubLayers .size (); i++ )
460
+ {
461
+ styleMap.insert ( mActiveSubLayers [i], mActiveSubStyles [i] );
462
+ }
463
+
464
+ for ( int i = 0 ; i < layers.size (); i++ )
465
+ {
466
+ if ( !styleMap.contains ( layers[i] ) )
467
+ {
468
+ QgsDebugMsg ( QString ( " Layer %1 not found" ).arg ( layers[i] ) );
469
+ return ;
470
+ }
471
+ }
472
+
414
473
mActiveSubLayers = layers;
474
+ mActiveSubStyles .clear ();
475
+ for ( int i = 0 ; i < layers.size (); i++ )
476
+ {
477
+ mActiveSubStyles .append ( styleMap[ layers[i] ] );
478
+ }
415
479
416
480
QgsDebugMsg ( " Exiting." );
417
481
}
418
482
419
483
420
484
void QgsWmsProvider::setSubLayerVisibility ( QString const & name, bool vis )
421
485
{
486
+ if ( !mActiveSubLayerVisibility .contains ( name ) )
487
+ {
488
+ QgsDebugMsg ( QString ( " Layer %1 not found." ).arg ( name ) );
489
+ return ;
490
+ }
491
+
422
492
mActiveSubLayerVisibility [name] = vis;
423
493
}
424
494
@@ -1822,6 +1892,10 @@ void QgsWmsProvider::parseCapability( QDomElement const & e, QgsWmsCapabilityPro
1822
1892
{
1823
1893
ot = &capabilityProperty.request .getFeatureInfo ;
1824
1894
}
1895
+ else if ( name == " GetLegendGraphic" || name == " sld:GetLegendGraphic" )
1896
+ {
1897
+ ot = &capabilityProperty.request .getLegendGraphic ;
1898
+ }
1825
1899
else
1826
1900
{
1827
1901
QgsDebugMsg ( QString ( " ows:Operation %1 ignored" ).arg ( name ) );
@@ -2197,6 +2271,11 @@ void QgsWmsProvider::parseRequest( QDomElement const & e, QgsWmsRequestProperty&
2197
2271
QgsDebugMsg ( " GetFeatureInfo." );
2198
2272
parseOperationType ( e1 , requestProperty.getFeatureInfo );
2199
2273
}
2274
+ else if ( operation == " GetLegendGraphic" || operation == " sld:GetLegendGraphic" )
2275
+ {
2276
+ QgsDebugMsg ( " GetLegendGraphic." );
2277
+ parseOperationType ( e1 , requestProperty.getLegendGraphic );
2278
+ }
2200
2279
}
2201
2280
n1 = n1.nextSibling ();
2202
2281
}
@@ -2266,7 +2345,8 @@ void QgsWmsProvider::parseStyle( QDomElement const & e, QgsWmsStyleProperty& sty
2266
2345
}
2267
2346
else if ( tagName == " LegendURL" )
2268
2347
{
2269
- // TODO
2348
+ styleProperty.legendUrl << QgsWmsLegendUrlProperty ();
2349
+ parseLegendUrl ( e1 , styleProperty.legendUrl .last () );
2270
2350
}
2271
2351
else if ( tagName == " StyleSheetURL" )
2272
2352
{
@@ -3556,6 +3636,8 @@ QString QgsWmsProvider::layerMetadata( QgsWmsLayerProperty &layer )
3556
3636
// Layer Styles
3557
3637
for ( int j = 0 ; j < layer.style .size (); j++ )
3558
3638
{
3639
+ const QgsWmsStyleProperty &style = layer.style [j];
3640
+
3559
3641
metadata += " <tr><td>" ;
3560
3642
metadata += tr ( " Available in style" );
3561
3643
metadata += " </td>" ;
@@ -3569,25 +3651,41 @@ QString QgsWmsProvider::layerMetadata( QgsWmsLayerProperty &layer )
3569
3651
metadata += tr ( " Name" );
3570
3652
metadata += " </th>" ;
3571
3653
metadata += " <td>" ;
3572
- metadata += layer. style [j] .name ;
3654
+ metadata += style.name ;
3573
3655
metadata += " </td></tr>" ;
3574
3656
3575
3657
// Layer Style Title
3576
3658
metadata += " <tr><th class=\" glossy\" >" ;
3577
3659
metadata += tr ( " Title" );
3578
3660
metadata += " </th>" ;
3579
3661
metadata += " <td>" ;
3580
- metadata += layer. style [j] .title ;
3662
+ metadata += style.title ;
3581
3663
metadata += " </td></tr>" ;
3582
3664
3583
3665
// Layer Style Abstract
3584
3666
metadata += " <tr><th class=\" glossy\" >" ;
3585
3667
metadata += tr ( " Abstract" );
3586
3668
metadata += " </th>" ;
3587
3669
metadata += " <td>" ;
3588
- metadata += layer. style [j] .abstract ;
3670
+ metadata += style.abstract ;
3589
3671
metadata += " </td></tr>" ;
3590
3672
3673
+ // LegendURLs
3674
+ if ( !style.legendUrl .isEmpty () )
3675
+ {
3676
+ metadata += " <tr><th class=\" glossy\" >" ;
3677
+ metadata += tr ( " LegendURLs" );
3678
+ metadata += " </th>" ;
3679
+ metadata += " <td><table>" ;
3680
+ metadata += " <tr><th>Format</th><th>URL</th></tr>" ;
3681
+ for ( int k = 0 ; k < style.legendUrl .size (); k++ )
3682
+ {
3683
+ const QgsWmsLegendUrlProperty &l = style.legendUrl [k];
3684
+ metadata += " <tr><td>" + l.format + " </td><td>" + l.onlineResource .xlinkHref + " </td></tr>" ;
3685
+ }
3686
+ metadata += " </table></td></tr>" ;
3687
+ }
3688
+
3591
3689
// Close the nested table
3592
3690
metadata += " </table>" ;
3593
3691
metadata += " </td></tr>" ;
@@ -3773,6 +3871,13 @@ QString QgsWmsProvider::metadata()
3773
3871
metadata += getFeatureInfoUrl () + ( mIgnoreGetFeatureInfoUrl ? tr ( " <font color=\" red\" >(advertised but ignored)</font>" ) : " " );
3774
3872
metadata += " </td></tr>" ;
3775
3873
3874
+ metadata += " <tr><td>" ;
3875
+ metadata += tr ( " GetLegendGraphic" );
3876
+ metadata += " </td>" ;
3877
+ metadata += " <td>" ;
3878
+ metadata += getLegendGraphicUrl () + ( mIgnoreGetMapUrl ? tr ( " <font color=\" red\" >(advertised but ignored)</font>" ) : " " );
3879
+ metadata += " </td></tr>" ;
3880
+
3776
3881
if ( mTiled )
3777
3882
{
3778
3883
metadata += " <tr><td>" ;
@@ -3821,7 +3926,7 @@ QString QgsWmsProvider::metadata()
3821
3926
3822
3927
for ( int i = 0 ; i < mLayersSupported .size (); i++ )
3823
3928
{
3824
- if ( !mTiled && mActiveSubLayers .indexOf ( mLayersSupported [i].name ) >= 0 )
3929
+ if ( !mTiled && mActiveSubLayers .contains ( mLayersSupported [i].name ) )
3825
3930
{
3826
3931
metadata += layerMetadata ( mLayersSupported [i] );
3827
3932
}
@@ -3834,7 +3939,7 @@ QString QgsWmsProvider::metadata()
3834
3939
3835
3940
for ( int i = 0 ; i < mLayersSupported .size (); i++ )
3836
3941
{
3837
- if ( mActiveSubLayers .indexOf ( mLayersSupported [i].name ) < 0 )
3942
+ if ( ! mActiveSubLayers .contains ( mLayersSupported [i].name ) )
3838
3943
{
3839
3944
metadata += layerMetadata ( mLayersSupported [i] );
3840
3945
}
@@ -4102,7 +4207,7 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPoint & thePoint, Qgs
4102
4207
int count = -1 ;
4103
4208
// Test for which layers are suitable for querying with
4104
4209
for ( QStringList::const_iterator
4105
- layers = mActiveSubLayers .begin (),
4210
+ layers = mActiveSubLayers .begin (),
4106
4211
styles = mActiveSubStyles .begin ();
4107
4212
layers != mActiveSubLayers .end ();
4108
4213
++layers, ++styles )
@@ -4603,116 +4708,70 @@ QImage QgsWmsProvider::getLegendGraphic( double scale, bool forceRefresh )
4603
4708
// the layer tags inside capabilities
4604
4709
QgsDebugMsg ( " entering." );
4605
4710
4606
- if ( !scale && !mGetLegendGraphicScale )
4607
- {
4608
- QgsDebugMsg ( QString ( " No scale factor set" ) );
4609
- return QImage ();
4610
- }
4711
+ QString lurl = getLegendGraphicUrl ();
4611
4712
4612
- if ( scale && scale != mGetLegendGraphicScale )
4713
+ if ( lurl. isEmpty () )
4613
4714
{
4614
- forceRefresh = true ;
4615
- QgsDebugMsg ( QString ( " Download again due to scale change from: %1 to: %2 " ). arg ( mGetLegendGraphicScale ). arg ( scale ) );
4715
+ QgsDebugMsg ( " getLegendGraphic url is empty " ) ;
4716
+ return QImage ( );
4616
4717
}
4617
4718
4618
- if ( forceRefresh )
4619
- {
4620
- if ( scale )
4621
- {
4622
- mGetLegendGraphicScale = scale;
4623
- }
4624
-
4625
- // if style is not defined, set as "default"
4626
- QString currentStyle ( " default" );
4627
- if ( mActiveSubStyles [0 ] != " " )
4628
- {
4629
- currentStyle = mActiveSubStyles [0 ];
4630
- }
4719
+ forceRefresh |= mGetLegendGraphicImage .isNull () || mGetLegendGraphicScale != scale;
4720
+ if ( !forceRefresh )
4721
+ return mGetLegendGraphicImage ;
4631
4722
4632
- #if 0
4633
- // add WMS GetGraphicLegend request
4634
- // TODO set sld version using instance var something like mSldVersion
4635
- // TODO at this moment LSD version can be get from LegendURL in getCapability,but parsing of
4636
- // this tag is not complete. Below the code that should work if parsing would correct
4723
+ QUrl url ( lurl );
4637
4724
4638
- if ( mActiveSubLayers[0] == mCapabilities.capability.layer.name )
4639
- {
4640
- foreach ( QgsWmsStyleProperty style, mCapabilities.capability.layer.style )
4641
- {
4642
- if ( currentStyle == style.name )
4643
- {
4644
- url.setUrl( style.legendUrl[0].onlineResource.xlinkHref, QUrl::StrictMode );
4645
- }
4646
- }
4647
- } // is a sublayer
4648
- else if ( mActiveSubLayers[0].contains( mCapabilities.capability.layer.name ) )
4649
- {
4650
- foreach ( QgsWmsLayerProperty layerProperty, mCapabilities.capability.layer.layer )
4651
- {
4652
- if ( mActiveSubLayers[0] == layerProperty.name )
4653
- {
4654
- foreach ( QgsWmsStyleProperty style, layerProperty.style )
4655
- {
4656
- if ( currentStyle == style.name )
4657
- {
4658
- url.setUrl( style.legendUrl[0].onlineResource.xlinkHref, QUrl::StrictMode );
4659
- }
4660
- }
4661
- }
4662
- }
4663
- }
4664
- #endif
4665
- QUrl url ( mIgnoreGetMapUrl ? mBaseUrl : getMapUrl (), QUrl::StrictMode );
4725
+ if ( !url.hasQueryItem ( " SERVICE" ) )
4666
4726
setQueryItem ( url, " SERVICE" , " WMS" );
4727
+ if ( !url.hasQueryItem ( " VERSION" ) )
4667
4728
setQueryItem ( url, " VERSION" , mCapabilities .version );
4729
+ if ( !url.hasQueryItem ( " SLD_VERSION" ) )
4668
4730
setQueryItem ( url, " SLD_VERSION" , " 1.1.0" ); // can not determine SLD_VERSION
4731
+ if ( !url.hasQueryItem ( " REQUEST" ) )
4669
4732
setQueryItem ( url, " REQUEST" , " GetLegendGraphic" );
4670
- setQueryItem ( url, " LAYER" , mActiveSubLayers [0 ] );
4671
- setQueryItem ( url, " STYLE" , currentStyle );
4672
- setQueryItem ( url, " SCALE" , QString::number ( scale, ' f' ) );
4733
+ if ( !url.hasQueryItem ( " FORMAT" ) )
4673
4734
setQueryItem ( url, " FORMAT" , mImageMimeType );
4735
+ if ( !url.hasQueryItem ( " LAYER" ) )
4736
+ setQueryItem ( url, " LAYER" , mActiveSubLayers [0 ] );
4737
+ if ( !url.hasQueryItem ( " STYLE" ) )
4738
+ setQueryItem ( url, " STYLE" , mActiveSubStyles [0 ] );
4674
4739
4675
- // add config parameter related to resolution
4676
- QSettings s;
4677
- int defaultLegendGraphicResolution = s.value ( " /qgis/defaultLegendGraphicResolution" , 0 ).toInt ();
4678
- QgsDebugMsg ( QString ( " defaultLegendGraphicResolution: %1" ).arg ( defaultLegendGraphicResolution ) );
4679
- if ( defaultLegendGraphicResolution )
4740
+ // add config parameter related to resolution
4741
+ QSettings s;
4742
+ int defaultLegendGraphicResolution = s.value ( " /qgis/defaultLegendGraphicResolution" , 0 ).toInt ();
4743
+ QgsDebugMsg ( QString ( " defaultLegendGraphicResolution: %1" ).arg ( defaultLegendGraphicResolution ) );
4744
+ if ( defaultLegendGraphicResolution )
4745
+ {
4746
+ if ( mDpiMode & dpiQGIS )
4747
+ setQueryItem ( url, " DPI" , QString::number ( defaultLegendGraphicResolution ) );
4748
+ if ( mDpiMode & dpiUMN )
4749
+ setQueryItem ( url, " MAP_RESOLUTION" , QString::number ( defaultLegendGraphicResolution ) );
4750
+ if ( mDpiMode & dpiGeoServer )
4680
4751
{
4681
- if ( url.queryItemValue ( " map_resolution" ) != " " )
4682
- {
4683
- setQueryItem ( url, " map_resolution" , QString::number ( defaultLegendGraphicResolution ) );
4684
- }
4685
- else if ( url.queryItemValue ( " dpi" ) != " " )
4686
- {
4687
- setQueryItem ( url, " dpi" , QString::number ( defaultLegendGraphicResolution ) );
4688
- }
4689
- else
4690
- {
4691
- QgsLogger::warning ( tr ( " getLegendGraphic: Can not determine resolution uri parameter [map_resolution | dpi]. No resolution parameter will be used" ) );
4692
- }
4752
+ setQueryItem ( url, " FORMAT_OPTIONS" , QString ( " dpi:%1" ).arg ( defaultLegendGraphicResolution ) );
4753
+ setQueryItem ( url, " SCALE" , QString::number ( scale, ' f' ) );
4693
4754
}
4755
+ }
4694
4756
4695
- mError = " " ;
4757
+ mGetLegendGraphicScale = scale ;
4696
4758
4697
- QNetworkRequest request ( url );
4698
- setAuthorization ( request );
4699
- request.setAttribute ( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
4700
- request.setAttribute ( QNetworkRequest::CacheSaveControlAttribute, true );
4759
+ mError = " " ;
4760
+
4761
+ QNetworkRequest request ( url );
4762
+ setAuthorization ( request );
4763
+ request.setAttribute ( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
4764
+ request.setAttribute ( QNetworkRequest::CacheSaveControlAttribute, true );
4701
4765
4702
- QgsDebugMsg ( QString ( " getlegendgraphics: %1" ).arg ( url.toString () ) );
4703
- mGetLegendGraphicReply = QgsNetworkAccessManager::instance ()->get ( request );
4766
+ QgsDebugMsg ( QString ( " getlegendgraphics: %1" ).arg ( url.toString () ) );
4767
+ mGetLegendGraphicReply = QgsNetworkAccessManager::instance ()->get ( request );
4704
4768
4705
- connect ( mGetLegendGraphicReply , SIGNAL ( finished () ), this , SLOT ( getLegendGraphicReplyFinished () ) );
4706
- connect ( mGetLegendGraphicReply , SIGNAL ( downloadProgress ( qint64, qint64 ) ), this , SLOT ( getLegendGraphicReplyProgress ( qint64, qint64 ) ) );
4769
+ connect ( mGetLegendGraphicReply , SIGNAL ( finished () ), this , SLOT ( getLegendGraphicReplyFinished () ) );
4770
+ connect ( mGetLegendGraphicReply , SIGNAL ( downloadProgress ( qint64, qint64 ) ), this , SLOT ( getLegendGraphicReplyProgress ( qint64, qint64 ) ) );
4707
4771
4708
- while ( mGetLegendGraphicReply )
4709
- {
4710
- QCoreApplication::processEvents ( QEventLoop::ExcludeUserInputEvents, WMS_THRESHOLD );
4711
- }
4712
- }
4713
- else
4772
+ while ( mGetLegendGraphicReply )
4714
4773
{
4715
- QgsDebugMsg ( " get cached pixmap. " );
4774
+ QCoreApplication::processEvents ( QEventLoop::ExcludeUserInputEvents, WMS_THRESHOLD );
4716
4775
}
4717
4776
4718
4777
QgsDebugMsg ( " exiting." );
@@ -4723,6 +4782,7 @@ QImage QgsWmsProvider::getLegendGraphic( double scale, bool forceRefresh )
4723
4782
void QgsWmsProvider::getLegendGraphicReplyFinished ()
4724
4783
{
4725
4784
QgsDebugMsg ( " entering." );
4785
+
4726
4786
if ( mGetLegendGraphicReply ->error () == QNetworkReply::NoError )
4727
4787
{
4728
4788
QgsDebugMsg ( " reply ok" );
0 commit comments