QGIS API Documentation 3.99.0-Master (e69e8341d6a)
qgsmaplayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayer.cpp - description
3 -------------------
4 begin : Fri Jun 28 2002
5 copyright : (C) 2002 by Gary E.Sherman
6 email : sherman at mrcc.com
7***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19#include "qgssqliteutils.h"
22#include "qgsapplication.h"
25#include "qgsdatasourceuri.h"
26#include "qgsfileutils.h"
27#include "qgslogger.h"
28#include "qgsauthmanager.h"
29#include "qgsmaplayer.h"
30#include "moc_qgsmaplayer.cpp"
31#include "qgsmaplayerlegend.h"
33#include "qgspathresolver.h"
35#include "qgsproject.h"
36#include "qgsproviderregistry.h"
37#include "qgsprovidermetadata.h"
38#include "qgsrasterlayer.h"
39#include "qgsreadwritecontext.h"
40#include "qgsrectangle.h"
41#include "qgsscaleutils.h"
42#include "qgssldexportcontext.h"
43#include "qgsvectorlayer.h"
44#include "qgsxmlutils.h"
45#include "qgsstringutils.h"
46#include "qgsmessagelog.h"
49#include "qgslayernotesutils.h"
50#include "qgsdatums.h"
51#include "qgsprojoperation.h"
52#include "qgsthreadingutils.h"
53#include "qgsunittypes.h"
54
55#include <QDir>
56#include <QDomDocument>
57#include <QDomElement>
58#include <QDomImplementation>
59#include <QDomNode>
60#include <QFile>
61#include <QFileInfo>
62#include <QLocale>
63#include <QTextStream>
64#include <QUrl>
65#include <QTimer>
66#include <QStandardPaths>
67#include <QUuid>
68#include <QRegularExpression>
69#include <QXmlStreamReader>
70
71#include <sqlite3.h>
72
74{
75 switch ( type )
76 {
77 case Metadata:
78 return QStringLiteral( ".qmd" );
79
80 case Style:
81 return QStringLiteral( ".qml" );
82 }
83 return QString();
84}
85
87 const QString &lyrname,
88 const QString &source )
89 : mDataSource( source )
90 , mLayerName( lyrname )
91 , mLayerType( type )
92 , mServerProperties( std::make_unique<QgsMapLayerServerProperties>( this ) )
93 , mUndoStack( new QUndoStack( this ) )
94 , mUndoStackStyles( new QUndoStack( this ) )
95 , mStyleManager( std::make_unique<QgsMapLayerStyleManager>( this ) )
96 , mRefreshTimer( new QTimer( this ) )
97{
98 mID = generateId( lyrname );
101 connect( mRefreshTimer, &QTimer::timeout, this, [this]
102 {
103
104 switch ( mAutoRefreshMode )
105 {
107 break;
109 triggerRepaint( true );
110 break;
112 reload();
113 break;
114 }
115 } );
116}
117
119{
120 if ( project() && project()->pathResolver().writePath( mDataSource ).startsWith( "attachment:" ) )
121 {
123 }
124
125
126
127
128}
129
130void QgsMapLayer::clone( QgsMapLayer *layer ) const
131{
133
134 QgsDebugMsgLevel( QStringLiteral( "Cloning layer '%1'" ).arg( name() ), 3 );
135 layer->setBlendMode( blendMode() );
136
137 const auto constStyles = styleManager()->styles();
138 for ( const QString &s : constStyles )
139 {
140 layer->styleManager()->addStyle( s, styleManager()->style( s ) );
141 }
142
143 layer->setName( name() );
144
145 if ( layer->dataProvider() && layer->dataProvider()->elevationProperties() )
146 {
148 layer->mExtent3D = mExtent3D;
149 else
150 layer->mExtent2D = mExtent2D;
151 }
152
153 layer->setMaximumScale( maximumScale() );
154 layer->setMinimumScale( minimumScale() );
156 layer->setDependencies( dependencies() );
158 layer->setCrs( crs() );
159 layer->setCustomProperties( mCustomProperties );
160 layer->setOpacity( mLayerOpacity );
161 layer->setMetadata( mMetadata );
162 layer->serverProperties()->copyTo( mServerProperties.get() );
163}
164
166{
167 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
169
170 return mLayerType;
171}
172
179
181{
183
184 if ( flags == mFlags )
185 return;
186
187 mFlags = flags;
188 emit flagsChanged();
189}
190
197
198QString QgsMapLayer::id() const
199{
200 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
202
203 return mID;
204}
205
206bool QgsMapLayer::setId( const QString &id )
207{
209 if ( qobject_cast< QgsMapLayerStore * >( parent() ) )
210 {
211 // layer is already registered, cannot change id
212 return false;
213 }
214
215 if ( id == mID )
216 return false;
217
218 mID = id;
219 emit idChanged( id );
220 return true;
221}
222
223void QgsMapLayer::setName( const QString &name )
224{
226
227 if ( name == mLayerName )
228 return;
229
231
232 emit nameChanged();
233}
234
235QString QgsMapLayer::name() const
236{
237 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
239
240 QgsDebugMsgLevel( "returning name '" + mLayerName + '\'', 4 );
241 return mLayerName;
242}
243
250
252{
254
255 return nullptr;
256}
257
262
263void QgsMapLayer::setShortName( const QString &shortName )
264{
266
267 mServerProperties->setShortName( shortName );
268}
269
271{
273
274 return mServerProperties->shortName();
275}
276
277void QgsMapLayer::setTitle( const QString &title )
278{
280
281 mServerProperties->setTitle( title );
282}
283
284QString QgsMapLayer::title() const
285{
287
288 return mServerProperties->title();
289}
290
291void QgsMapLayer::setAbstract( const QString &abstract )
292{
294
295 mServerProperties->setAbstract( abstract );
296}
297
299{
301
302 return mServerProperties->abstract();
303}
304
305void QgsMapLayer::setKeywordList( const QString &keywords )
306{
308
309 mServerProperties->setKeywordList( keywords );
310}
311
313{
315
316 return mServerProperties->keywordList();
317}
318
319void QgsMapLayer::setDataUrl( const QString &dataUrl )
320{
322
323 mServerProperties->setDataUrl( dataUrl );
324}
325
326QString QgsMapLayer::dataUrl() const
327{
329
330 return mServerProperties->dataUrl();
331}
332
333void QgsMapLayer::setDataUrlFormat( const QString &dataUrlFormat )
334{
336
337 mServerProperties->setDataUrlFormat( dataUrlFormat );
338}
339
341{
343
344 return mServerProperties->dataUrlFormat();
345}
346
347void QgsMapLayer::setAttribution( const QString &attrib )
348{
350
351 mServerProperties->setAttribution( attrib );
352}
353
355{
357
358 return mServerProperties->attribution();
359}
360
361void QgsMapLayer::setAttributionUrl( const QString &attribUrl )
362{
364
365 mServerProperties->setAttributionUrl( attribUrl );
366}
367
369{
371
372 return mServerProperties->attributionUrl();
373}
374
375void QgsMapLayer::setLegendUrl( const QString &legendUrl )
376{
378
379 mServerProperties->setLegendUrl( legendUrl );
380}
381
383{
385
386 return mServerProperties->legendUrl();
387}
388
389void QgsMapLayer::setLegendUrlFormat( const QString &legendUrlFormat )
390{
392
393 mServerProperties->setLegendUrlFormat( legendUrlFormat );
394}
395
397{
399
400 return mServerProperties->legendUrlFormat();
401}
402
403void QgsMapLayer::setMetadataUrl( const QString &metaUrl )
404{
406
407 QList<QgsMapLayerServerProperties::MetadataUrl> urls = serverProperties()->metadataUrls();
408 if ( urls.isEmpty() )
409 {
410 const QgsMapLayerServerProperties::MetadataUrl newItem = QgsMapLayerServerProperties::MetadataUrl( metaUrl, QLatin1String(), QLatin1String() );
411 urls.prepend( newItem );
412 }
413 else
414 {
415 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
416 const QgsMapLayerServerProperties::MetadataUrl newItem( metaUrl, old.type, old.format );
417 urls.prepend( newItem );
418 }
420}
421
423{
425
426 if ( mServerProperties->metadataUrls().isEmpty() )
427 {
428 return QLatin1String();
429 }
430 else
431 {
432 return mServerProperties->metadataUrls().first().url;
433 }
434}
435
436void QgsMapLayer::setMetadataUrlType( const QString &metaUrlType )
437{
439
440 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
441 if ( urls.isEmpty() )
442 {
443 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), metaUrlType, QLatin1String() );
444 urls.prepend( newItem );
445 }
446 else
447 {
448 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
449 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, metaUrlType, old.format );
450 urls.prepend( newItem );
451 }
452 mServerProperties->setMetadataUrls( urls );
453}
454
456{
458
459 if ( mServerProperties->metadataUrls().isEmpty() )
460 {
461 return QLatin1String();
462 }
463 else
464 {
465 return mServerProperties->metadataUrls().first().type;
466 }
467}
468
469void QgsMapLayer::setMetadataUrlFormat( const QString &metaUrlFormat )
470{
472
473 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
474 if ( urls.isEmpty() )
475 {
476 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), QLatin1String(), metaUrlFormat );
477 urls.prepend( newItem );
478 }
479 else
480 {
481 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst( );
482 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, old.type, metaUrlFormat );
483 urls.prepend( newItem );
484 }
485 mServerProperties->setMetadataUrls( urls );
486}
487
489{
491
492 if ( mServerProperties->metadataUrls().isEmpty() )
493 {
494 return QString();
495 }
496 else
497 {
498 return mServerProperties->metadataUrls().first().format;
499 }
500}
501
502QString QgsMapLayer::publicSource( bool redactCredentials ) const
503{
505
506 // Redo this every time we're asked for it, as we don't know if
507 // dataSource has changed.
509 {
511 }
512 else
513 {
514 return QgsDataSourceUri::removePassword( mDataSource, redactCredentials );
515 }
516}
517
518QString QgsMapLayer::source() const
519{
521
522 return mDataSource;
523}
524
526{
528
529 return mExtent2D.isNull() ? mExtent3D.toRectangle() : mExtent2D;
530}
531
533{
535
536 return mExtent3D;
537}
538
539void QgsMapLayer::setBlendMode( const QPainter::CompositionMode blendMode )
540{
542
543 if ( mBlendMode == blendMode )
544 return;
545
546 mBlendMode = blendMode;
549}
550
551QPainter::CompositionMode QgsMapLayer::blendMode() const
552{
553 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
555
556 return mBlendMode;
557}
558
559void QgsMapLayer::setOpacity( double opacity )
560{
562
564 return;
566 emit opacityChanged( opacity );
568}
569
571{
572 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
574
575 return mLayerOpacity;
576}
577
578bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags, QgsDataProvider *preloadedProvider )
579{
581
582 mPreloadedProvider.reset( preloadedProvider );
583
584 bool layerError;
586
587 QDomNode mnl;
588 QDomElement mne;
589
590 // read provider
591 QString provider;
592 mnl = layerElement.namedItem( QStringLiteral( "provider" ) );
593 mne = mnl.toElement();
594 provider = mne.text();
595
596 // set data source
597 mnl = layerElement.namedItem( QStringLiteral( "datasource" ) );
598 mne = mnl.toElement();
599 const QString dataSourceRaw = mne.text();
600
601 // if the layer needs authentication, ensure the master password is set
602 const thread_local QRegularExpression rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
603 if ( rx.match( dataSourceRaw ).hasMatch()
605 {
606 return false;
607 }
608
609 mDataSource = decodedSource( dataSourceRaw, provider, context );
610
611 // Set the CRS from project file, asking the user if necessary.
612 // Make it the saved CRS to have WMS layer projected correctly.
613 // We will still overwrite whatever GDAL etc picks up anyway
614 // further down this function.
615 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
616 mne = mnl.toElement();
617
619 CUSTOM_CRS_VALIDATION savedValidation;
620
621 const QDomNode srsNode = layerElement.namedItem( QStringLiteral( "srs" ) );
622 mCRS.readXml( srsNode );
623 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) );
625 mCRS.validate();
626 savedCRS = mCRS;
627
628 // Do not validate any projections in children, they will be overwritten anyway.
629 // No need to ask the user for a projections when it is overwritten, is there?
632
633 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Layer" ), mne.text() );
634
635 // the internal name is just the data source basename
636 //QFileInfo dataSourceFileInfo( mDataSource );
637 //internalName = dataSourceFileInfo.baseName();
638
639 // set ID
640 mnl = layerElement.namedItem( QStringLiteral( "id" ) );
641 if ( ! mnl.isNull() )
642 {
643 mne = mnl.toElement();
644 if ( ! mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz)
645 {
646 const QString newId = mne.text();
647 if ( newId != mID )
648 {
649 mID = mne.text();
650 emit idChanged( mID );
651 }
652 }
653 }
654
655 // set name
656 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
657 mne = mnl.toElement();
658
659 //name can be translated
660 setName( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );
661
662 // now let the children grab what they need from the Dom node.
663 layerError = !readXml( layerElement, context );
664
665 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
666 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
667
668 // overwrite CRS with what we read from project file before the raster/vector
669 // file reading functions changed it. They will if projections is specified in the file.
670 // FIXME: is this necessary? Yes, it is (autumn 2019)
672 mCRS = savedCRS;
673
674 //vertical CRS
675 {
677 const QDomNode verticalCrsNode = layerElement.firstChildElement( QStringLiteral( "verticalCrs" ) );
678 if ( !verticalCrsNode.isNull() )
679 {
680 verticalCrs.readXml( verticalCrsNode );
681 }
682 mVerticalCrs = verticalCrs;
683 }
684 rebuildCrs3D();
685
686 serverProperties()->readXml( layerElement );
687
688 // mMetadata.readFromLayer( this );
689 const QDomElement metadataElem = layerElement.firstChildElement( QStringLiteral( "resourceMetadata" ) );
690 mMetadata.readMetadataXml( metadataElem );
691
692 setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ), QStringLiteral( "0" ) ).toInt() );
693 if ( layerElement.hasAttribute( QStringLiteral( "autoRefreshMode" ) ) )
694 {
695 setAutoRefreshMode( qgsEnumKeyToValue( layerElement.attribute( QStringLiteral( "autoRefreshMode" ) ), Qgis::AutoRefreshMode::Disabled ) );
696 }
697 else
698 {
699 setAutoRefreshMode( layerElement.attribute( QStringLiteral( "autoRefreshEnabled" ), QStringLiteral( "0" ) ).toInt() ? Qgis::AutoRefreshMode::RedrawOnly : Qgis::AutoRefreshMode::Disabled );
700 }
701 setRefreshOnNofifyMessage( layerElement.attribute( QStringLiteral( "refreshOnNotifyMessage" ), QString() ) );
702 setRefreshOnNotifyEnabled( layerElement.attribute( QStringLiteral( "refreshOnNotifyEnabled" ), QStringLiteral( "0" ) ).toInt() );
703
704 // geographic extent is read only if necessary
706 {
707 const QDomNode wgs84ExtentNode = layerElement.namedItem( QStringLiteral( "wgs84extent" ) );
708 if ( !wgs84ExtentNode.isNull() )
709 mWgs84Extent = QgsXmlUtils::readRectangle( wgs84ExtentNode.toElement() );
710 }
711
712 mLegendPlaceholderImage = layerElement.attribute( QStringLiteral( "legendPlaceholderImage" ) );
713
714 if ( verticalCrs() != oldVerticalCrs )
715 emit verticalCrsChanged();
716 if ( mCrs3D != oldCrs3D )
717 emit crs3DChanged();
718
719 return ! layerError;
720} // bool QgsMapLayer::readLayerXML
721
722
723bool QgsMapLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
724{
726
727 Q_UNUSED( layer_node )
728 Q_UNUSED( context )
729 // NOP by default; children will over-ride with behavior specific to them
730
731 // read Extent
733 {
734 const QDomNode extent3DNode = layer_node.namedItem( QStringLiteral( "extent3D" ) );
735 if ( extent3DNode.isNull() )
736 {
737 const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
738 if ( !extentNode.isNull() )
739 {
740 mExtent2D = QgsXmlUtils::readRectangle( extentNode.toElement() );
741 }
742 }
743 else
744 {
745 mExtent3D = QgsXmlUtils::readBox3D( extent3DNode.toElement() );
746 }
747 }
748
749 return true;
750} // void QgsMapLayer::readXml
751
752
753bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const
754{
756
757 if ( !mExtent3D.isNull() && dataProvider() && dataProvider()->elevationProperties() && dataProvider()->elevationProperties()->containsElevationData() )
758 layerElement.appendChild( QgsXmlUtils::writeBox3D( mExtent3D, document ) );
759 else
760 {
761 // Extent might be null because lazily set
762 const QgsRectangle extent2D { mExtent2D.isNull() ? extent() : mExtent2D };
763 if ( !extent2D.isNull() )
764 {
765 layerElement.appendChild( QgsXmlUtils::writeRectangle( extent2D, document ) );
766 }
767 }
768
769 if ( const QgsRectangle lWgs84Extent = wgs84Extent( true ); !lWgs84Extent.isNull() )
770 {
771 layerElement.appendChild( QgsXmlUtils::writeRectangle( lWgs84Extent, document, QStringLiteral( "wgs84extent" ) ) );
772 }
773
774 layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( mRefreshTimer->interval() ) );
775 layerElement.setAttribute( QStringLiteral( "autoRefreshMode" ), qgsEnumValueToKey( mAutoRefreshMode ) );
776 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyEnabled" ), mIsRefreshOnNofifyEnabled ? 1 : 0 );
777 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyMessage" ), mRefreshOnNofifyMessage );
778
779 // ID
780 QDomElement layerId = document.createElement( QStringLiteral( "id" ) );
781 const QDomText layerIdText = document.createTextNode( id() );
782 layerId.appendChild( layerIdText );
783
784 layerElement.appendChild( layerId );
785
786 if ( mVerticalCrs.isValid() )
787 {
788 QDomElement verticalSrsNode = document.createElement( QStringLiteral( "verticalCrs" ) );
789 mVerticalCrs.writeXml( verticalSrsNode, document );
790 layerElement.appendChild( verticalSrsNode );
791 }
792
793 // data source
794 QDomElement dataSource = document.createElement( QStringLiteral( "datasource" ) );
795 const QString src = encodedSource( source(), context );
796 const QDomText dataSourceText = document.createTextNode( src );
797 dataSource.appendChild( dataSourceText );
798 layerElement.appendChild( dataSource );
799
800 // layer name
801 QDomElement layerName = document.createElement( QStringLiteral( "layername" ) );
802 const QDomText layerNameText = document.createTextNode( name() );
803 layerName.appendChild( layerNameText );
804 layerElement.appendChild( layerName );
805
806 // timestamp if supported
807 if ( timestamp() > QDateTime() )
808 {
809 QDomElement stamp = document.createElement( QStringLiteral( "timestamp" ) );
810 const QDomText stampText = document.createTextNode( timestamp().toString( Qt::ISODate ) );
811 stamp.appendChild( stampText );
812 layerElement.appendChild( stamp );
813 }
814
815 layerElement.appendChild( layerName );
816
817 // zorder
818 // This is no longer stored in the project file. It is superfluous since the layers
819 // are written and read in the proper order.
820
821 // spatial reference system id
822 QDomElement mySrsElement = document.createElement( QStringLiteral( "srs" ) );
823 mCRS.writeXml( mySrsElement, document );
824 layerElement.appendChild( mySrsElement );
825
826 // layer metadata
827 QDomElement myMetadataElem = document.createElement( QStringLiteral( "resourceMetadata" ) );
828 mMetadata.writeMetadataXml( myMetadataElem, document );
829 layerElement.appendChild( myMetadataElem );
830
831 layerElement.setAttribute( QStringLiteral( "legendPlaceholderImage" ), mLegendPlaceholderImage );
832
833 serverProperties()->writeXml( layerElement, document );
834
835 // now append layer node to map layer node
836 return writeXml( layerElement, document, context );
837}
838
839void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &document,
840 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
841{
843
844 // save categories
845 const QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::StyleCategories>();
846 const QString categoriesKeys( metaEnum.valueToKeys( static_cast<int>( categories ) ) );
847 layerElement.setAttribute( QStringLiteral( "styleCategories" ), categoriesKeys );
848
849 if ( categories.testFlag( Rendering ) )
850 {
851 // use scale dependent visibility flag
852 layerElement.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
853 layerElement.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
854 layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );
855 layerElement.setAttribute( QStringLiteral( "autoRefreshMode" ), qgsEnumValueToKey( mAutoRefreshMode ) );
856 layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( autoRefreshInterval() ) );
857 }
858
859 if ( categories.testFlag( Symbology3D ) )
860 {
861 if ( m3DRenderer )
862 {
863 QDomElement renderer3DElem = document.createElement( QStringLiteral( "renderer-3d" ) );
864 renderer3DElem.setAttribute( QStringLiteral( "type" ), m3DRenderer->type() );
865 m3DRenderer->writeXml( renderer3DElem, context );
866 layerElement.appendChild( renderer3DElem );
867 }
868 }
869
870 if ( categories.testFlag( LayerConfiguration ) )
871 {
872 // flags
873 // this code is saving automatically all the flags entries
874 QDomElement layerFlagsElem = document.createElement( QStringLiteral( "flags" ) );
875 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
876 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
877 {
878 const bool flagValue = mFlags.testFlag( it.key() );
879 QDomElement flagElem = document.createElement( it.value() );
880 flagElem.appendChild( document.createTextNode( QString::number( flagValue ) ) );
881 layerFlagsElem.appendChild( flagElem );
882 }
883 layerElement.appendChild( layerFlagsElem );
884 }
885
886 if ( categories.testFlag( Temporal ) )
887 {
889 properties->writeXml( layerElement, document, context );
890 }
891
892 if ( categories.testFlag( Elevation ) )
893 {
895 properties->writeXml( layerElement, document, context );
896 }
897
898 if ( categories.testFlag( Notes ) && QgsLayerNotesUtils::layerHasNotes( this ) )
899 {
900 QDomElement notesElem = document.createElement( QStringLiteral( "userNotes" ) );
901 notesElem.setAttribute( QStringLiteral( "value" ), QgsLayerNotesUtils::layerNotes( this ) );
902 layerElement.appendChild( notesElem );
903 }
904
905 // custom properties
906 if ( categories.testFlag( CustomProperties ) )
907 {
908 writeCustomProperties( layerElement, document );
909 }
910}
911
912
913bool QgsMapLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const
914{
916
917 Q_UNUSED( layer_node )
918 Q_UNUSED( document )
919 Q_UNUSED( context )
920 // NOP by default; children will over-ride with behavior specific to them
921
922 return true;
923}
924
925QString QgsMapLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
926{
928
929 Q_UNUSED( context )
930 return source;
931}
932
933QString QgsMapLayer::decodedSource( const QString &source, const QString &dataProvider, const QgsReadWriteContext &context ) const
934{
936
937 Q_UNUSED( context )
938 Q_UNUSED( dataProvider )
939 return source;
940}
941
943{
945
947 if ( m3DRenderer )
948 m3DRenderer->resolveReferences( *project );
949}
950
951
952void QgsMapLayer::readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith )
953{
955
956 const QgsObjectCustomProperties oldKeys = mCustomProperties;
957
958 mCustomProperties.readXml( layerNode, keyStartsWith );
959
960 for ( const QString &key : mCustomProperties.keys() )
961 {
962 if ( !oldKeys.contains( key ) || mCustomProperties.value( key ) != oldKeys.value( key ) )
963 {
964 emit customPropertyChanged( key );
965 }
966 }
967}
968
969void QgsMapLayer::writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const
970{
972
973 mCustomProperties.writeXml( layerNode, doc );
974}
975
976void QgsMapLayer::readStyleManager( const QDomNode &layerNode )
977{
979
980 const QDomElement styleMgrElem = layerNode.firstChildElement( QStringLiteral( "map-layer-style-manager" ) );
981 if ( !styleMgrElem.isNull() )
982 mStyleManager->readXml( styleMgrElem );
983 else
984 mStyleManager->reset();
985}
986
987void QgsMapLayer::writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const
988{
990
991 if ( mStyleManager )
992 {
993 QDomElement styleMgrElem = doc.createElement( QStringLiteral( "map-layer-style-manager" ) );
994 mStyleManager->writeXml( styleMgrElem );
995 layerNode.appendChild( styleMgrElem );
996 }
997}
998
1000{
1002
1003 return mMapTipTemplate;
1004}
1005
1006void QgsMapLayer::setMapTipTemplate( const QString &mapTip )
1007{
1009
1010 if ( mMapTipTemplate == mapTip )
1011 return;
1012
1013 mMapTipTemplate = mapTip;
1014 emit mapTipTemplateChanged();
1015}
1016
1018{
1020
1021 if ( mMapTipsEnabled == enabled )
1022 return;
1023
1024 mMapTipsEnabled = enabled;
1025 emit mapTipsEnabledChanged();
1026}
1027
1029{
1031
1032 return mMapTipsEnabled;
1033}
1034
1036{
1038 if ( layerReadFlags & QgsMapLayer::FlagTrustLayerMetadata )
1039 {
1041 }
1042 if ( layerReadFlags & QgsMapLayer::FlagForceReadOnly )
1043 {
1045 }
1046
1047 if ( layerReadFlags & QgsMapLayer::FlagReadExtentFromXml )
1048 {
1049 const QDomNode extent3DNode = layerNode.namedItem( QStringLiteral( "extent3D" ) );
1050 if ( extent3DNode.isNull() )
1051 {
1052 const QDomNode extentNode = layerNode.namedItem( QStringLiteral( "extent" ) );
1053 if ( !extentNode.isNull() )
1054 {
1056 }
1057 }
1058 else
1059 {
1061 }
1062 }
1063
1064 return flags;
1065}
1066
1068{
1069 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
1071
1072 return mValid;
1073}
1074
1075#if 0
1076void QgsMapLayer::connectNotify( const char *signal )
1077{
1078 Q_UNUSED( signal )
1079 QgsDebugMsgLevel( "QgsMapLayer connected to " + QString( signal ), 3 );
1080} // QgsMapLayer::connectNotify
1081#endif
1082
1083bool QgsMapLayer::isInScaleRange( double scale ) const
1084{
1085 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1087
1088 // mMinScale (denominator!) is inclusive ( >= --> In range )
1089 // mMaxScale (denominator!) is exclusive ( < --> In range )
1090 return !mScaleBasedVisibility
1091 || ( ( mMinScale == 0 || !QgsScaleUtils::lessThanMaximumScale( scale, mMinScale ) )
1092 && ( mMaxScale == 0 || !QgsScaleUtils::equalToOrGreaterThanMinimumScale( scale, mMaxScale ) ) );
1093}
1094
1096{
1097 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1099
1100 return mScaleBasedVisibility;
1101}
1102
1104{
1106
1107 return mAutoRefreshMode != Qgis::AutoRefreshMode::Disabled;;
1108}
1109
1111{
1113
1114 return mAutoRefreshMode;
1115}
1116
1118{
1120
1121 return mRefreshTimer->interval();
1122}
1123
1125{
1127
1128 if ( interval <= 0 )
1129 {
1130 mRefreshTimer->stop();
1131 mRefreshTimer->setInterval( 0 );
1133 }
1134 else
1135 {
1136 mRefreshTimer->setInterval( interval );
1137 }
1138 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1139}
1140
1147
1149{
1151
1152 if ( mode == mAutoRefreshMode )
1153 return;
1154
1155 mAutoRefreshMode = mode;
1156 switch ( mAutoRefreshMode )
1157 {
1159 mRefreshTimer->stop();
1160 break;
1161
1164 if ( mRefreshTimer->interval() > 0 )
1165 mRefreshTimer->start();
1166 break;
1167 }
1168
1169 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1170}
1171
1173{
1175
1176 return mMetadata;
1177}
1178
1180{
1182
1183 mMinScale = scale;
1184}
1185
1187{
1189
1190 return mMinScale;
1191}
1192
1194{
1196
1197 mMaxScale = scale;
1198}
1199
1201{
1203
1204 mScaleBasedVisibility = enabled;
1205}
1206
1208{
1210
1211 return mMaxScale;
1212}
1213
1214QStringList QgsMapLayer::subLayers() const
1215{
1217
1218 return QStringList();
1219}
1220
1221void QgsMapLayer::setLayerOrder( const QStringList &layers )
1222{
1224
1225 Q_UNUSED( layers )
1226}
1227
1228void QgsMapLayer::setSubLayerVisibility( const QString &name, bool vis )
1229{
1231
1232 Q_UNUSED( name )
1233 Q_UNUSED( vis )
1234}
1235
1237{
1239
1240 return false;
1241}
1242
1244{
1245 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1247
1248 return mCRS;
1249}
1250
1252{
1253 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1255
1256 switch ( mCRS.type() )
1257 {
1258 case Qgis::CrsType::Vertical: // would hope this never happens!
1259 QgsDebugError( QStringLiteral( "Layer has a vertical CRS set as the horizontal CRS!" ) );
1260 return mCRS;
1261
1263 return mCRS.verticalCrs();
1264
1276 break;
1277 }
1278 return mVerticalCrs;
1279}
1280
1282{
1284
1285 return mCrs3D.isValid() ? mCrs3D : mCRS;
1286}
1287
1288void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal )
1289{
1291 const bool needToValidateCrs = mShouldValidateCrs && isSpatial() && !srs.isValid() && type() != Qgis::LayerType::Annotation;
1292
1293 if ( mCRS == srs && !needToValidateCrs )
1294 return;
1295
1296 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
1297 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
1298 const QgsCoordinateReferenceSystem oldCrs = mCRS;
1299
1300 mCRS = srs;
1301
1302 if ( needToValidateCrs )
1303 {
1304 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( name() ) );
1305 mCRS.validate();
1306 }
1307
1308 rebuildCrs3D();
1309
1310 if ( emitSignal && mCRS != oldCrs )
1311 emit crsChanged();
1312
1313 // Did vertical crs also change as a result of this? If so, emit signal
1314 if ( oldVerticalCrs != verticalCrs() )
1315 emit verticalCrsChanged();
1316 if ( oldCrs3D != mCrs3D )
1317 emit crs3DChanged();
1318}
1319
1321{
1323 bool res = true;
1324 if ( crs.isValid() )
1325 {
1326 // validate that passed crs is a vertical crs
1327 switch ( crs.type() )
1328 {
1330 break;
1331
1344 if ( errorMessage )
1345 *errorMessage = QObject::tr( "Specified CRS is a %1 CRS, not a Vertical CRS" ).arg( qgsEnumValueToKey( crs.type() ) );
1346 return false;
1347 }
1348 }
1349
1350 if ( crs != mVerticalCrs )
1351 {
1352 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
1353 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
1354
1355 switch ( mCRS.type() )
1356 {
1358 if ( crs != oldVerticalCrs )
1359 {
1360 if ( errorMessage )
1361 *errorMessage = QObject::tr( "Layer CRS is a Compound CRS, specified Vertical CRS will be ignored" );
1362 return false;
1363 }
1364 break;
1365
1367 if ( crs != oldVerticalCrs )
1368 {
1369 if ( errorMessage )
1370 *errorMessage = QObject::tr( "Layer CRS is a Geographic 3D CRS, specified Vertical CRS will be ignored" );
1371 return false;
1372 }
1373 break;
1374
1376 if ( crs != oldVerticalCrs )
1377 {
1378 if ( errorMessage )
1379 *errorMessage = QObject::tr( "Layer CRS is a Geocentric CRS, specified Vertical CRS will be ignored" );
1380 return false;
1381 }
1382 break;
1383
1385 if ( mCRS.hasVerticalAxis() && crs != oldVerticalCrs )
1386 {
1387 if ( errorMessage )
1388 *errorMessage = QObject::tr( "Layer CRS is a Projected 3D CRS, specified Vertical CRS will be ignored" );
1389 return false;
1390 }
1391 break;
1392
1402 break;
1403 }
1404
1405 mVerticalCrs = crs;
1406 res = rebuildCrs3D( errorMessage );
1407
1408 // only emit signal if vertical crs was actually changed, so eg if mCrs is compound
1409 // then we haven't actually changed the vertical crs by this call!
1410 if ( verticalCrs() != oldVerticalCrs )
1411 emit verticalCrsChanged();
1412 if ( mCrs3D != oldCrs3D )
1413 emit crs3DChanged();
1414 }
1415 return res;
1416}
1417
1419{
1421
1422 const QgsDataProvider *lDataProvider = dataProvider();
1423 return lDataProvider ? lDataProvider->transformContext() : QgsCoordinateTransformContext();
1424}
1425
1426QString QgsMapLayer::formatLayerName( const QString &name )
1427{
1428 QString layerName( name );
1429 layerName.replace( '_', ' ' );
1431 return layerName;
1432}
1433
1434QString QgsMapLayer::baseURI( PropertyType type ) const
1435{
1437
1438 QString myURI = publicSource();
1439
1440 // first get base path for delimited text, spatialite and OGR layers,
1441 // as in these cases URI may contain layer name and/or additional
1442 // information. This also strips prefix in case if VSIFILE mechanism
1443 // is used
1444 if ( providerType() == QLatin1String( "ogr" ) || providerType() == QLatin1String( "delimitedtext" )
1445 || providerType() == QLatin1String( "gdal" ) || providerType() == QLatin1String( "spatialite" ) )
1446 {
1447 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( providerType(), myURI );
1448 myURI = components["path"].toString();
1449 }
1450
1451 QFileInfo myFileInfo( myURI );
1452 QString key;
1453
1454 if ( myFileInfo.exists() )
1455 {
1456 // if file is using the /vsizip/ or /vsigzip/ mechanism, cleanup the name
1457 if ( myURI.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
1458 myURI.chop( 3 );
1459 else if ( myURI.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
1460 myURI.chop( 4 );
1461 else if ( myURI.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) )
1462 myURI.chop( 4 );
1463 else if ( myURI.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) )
1464 myURI.chop( 7 );
1465 else if ( myURI.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
1466 myURI.chop( 4 );
1467 myFileInfo.setFile( myURI );
1468 // get the file name for our .qml style file
1469 key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1470 }
1471 else
1472 {
1473 key = publicSource();
1474 }
1475
1476 return key;
1477}
1478
1480{
1482
1483 return baseURI( PropertyType::Metadata );
1484}
1485
1486QString QgsMapLayer::saveDefaultMetadata( bool &resultFlag )
1487{
1489
1491 {
1492 if ( metadata->providerCapabilities() & QgsProviderMetadata::SaveLayerMetadata )
1493 {
1494 try
1495 {
1496 QString errorMessage;
1497 resultFlag = QgsProviderRegistry::instance()->saveLayerMetadata( providerType(), mDataSource, mMetadata, errorMessage );
1498 if ( resultFlag )
1499 return tr( "Successfully saved default layer metadata" );
1500 else
1501 return errorMessage;
1502 }
1503 catch ( QgsNotSupportedException &e )
1504 {
1505 resultFlag = false;
1506 return e.what();
1507 }
1508 }
1509 }
1510
1511 // fallback default metadata saving method, for providers which don't support (or implement) saveLayerMetadata
1512 return saveNamedMetadata( metadataUri(), resultFlag );
1513}
1514
1515QString QgsMapLayer::loadDefaultMetadata( bool &resultFlag )
1516{
1518
1519 return loadNamedMetadata( metadataUri(), resultFlag );
1520}
1521
1523{
1525
1526 return baseURI( PropertyType::Style );
1527}
1528
1535
1536bool QgsMapLayer::loadNamedMetadataFromDatabase( const QString &db, const QString &uri, QString &qmd )
1537{
1539
1540 return loadNamedPropertyFromDatabase( db, uri, qmd, PropertyType::Metadata );
1541}
1542
1543bool QgsMapLayer::loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml )
1544{
1546
1547 return loadNamedPropertyFromDatabase( db, uri, qml, PropertyType::Style );
1548}
1549
1550bool QgsMapLayer::loadNamedPropertyFromDatabase( const QString &db, const QString &uri, QString &xml, QgsMapLayer::PropertyType type )
1551{
1553
1554 QgsDebugMsgLevel( QStringLiteral( "db = %1 uri = %2" ).arg( db, uri ), 4 );
1555
1556 bool resultFlag = false;
1557
1558 // read from database
1561
1562 int myResult;
1563
1564 QgsDebugMsgLevel( QStringLiteral( "Trying to load style or metadata for \"%1\" from \"%2\"" ).arg( uri, db ), 4 );
1565
1566 if ( db.isEmpty() || !QFile( db ).exists() )
1567 return false;
1568
1569 myResult = database.open_v2( db, SQLITE_OPEN_READONLY, nullptr );
1570 if ( myResult != SQLITE_OK )
1571 {
1572 return false;
1573 }
1574
1575 QString mySql;
1576 switch ( type )
1577 {
1578 case Metadata:
1579 mySql = QStringLiteral( "select qmd from tbl_metadata where metadata=?" );
1580 break;
1581
1582 case Style:
1583 mySql = QStringLiteral( "select qml from tbl_styles where style=?" );
1584 break;
1585 }
1586
1587 statement = database.prepare( mySql, myResult );
1588 if ( myResult == SQLITE_OK )
1589 {
1590 QByteArray param = uri.toUtf8();
1591
1592 if ( sqlite3_bind_text( statement.get(), 1, param.data(), param.length(), SQLITE_STATIC ) == SQLITE_OK &&
1593 sqlite3_step( statement.get() ) == SQLITE_ROW )
1594 {
1595 xml = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( statement.get(), 0 ) ) );
1596 resultFlag = true;
1597 }
1598 }
1599 return resultFlag;
1600}
1601
1602
1603QString QgsMapLayer::loadNamedStyle( const QString &uri, bool &resultFlag, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
1604{
1606
1607 return loadNamedStyle( uri, resultFlag, false, categories, flags );
1608}
1609
1610QString QgsMapLayer::loadNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &namedPropertyExists, bool &propertySuccessfullyLoaded, StyleCategories categories, Qgis::LoadStyleFlags flags )
1611{
1613
1614 QgsDebugMsgLevel( QStringLiteral( "uri = %1 myURI = %2" ).arg( uri, publicSource() ), 4 );
1615
1616 namedPropertyExists = false;
1617 propertySuccessfullyLoaded = false;
1618 if ( uri.isEmpty() )
1619 return QString();
1620
1621 QDomDocument myDocument( QStringLiteral( "qgis" ) );
1622
1623 // location of problem associated with errorMsg
1624 int line, column;
1625 QString myErrorMessage;
1626
1627 QFile myFile( uri );
1628 if ( myFile.open( QFile::ReadOnly ) )
1629 {
1630 QgsDebugMsgLevel( QStringLiteral( "file found %1" ).arg( uri ), 2 );
1631 namedPropertyExists = true;
1632
1633 // read file
1634 propertySuccessfullyLoaded = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
1635 if ( !propertySuccessfullyLoaded )
1636 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1637 myFile.close();
1638 }
1639 else
1640 {
1641 const QFileInfo project( QgsProject::instance()->fileName() ); // skip-keyword-check
1642 QgsDebugMsgLevel( QStringLiteral( "project fileName: %1" ).arg( project.absoluteFilePath() ), 4 );
1643
1644 QString xml;
1645 switch ( type )
1646 {
1647 case QgsMapLayer::Style:
1648 {
1649 if ( loadNamedStyleFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1650 ( project.exists() && loadNamedStyleFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1651 loadNamedStyleFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1652 {
1653 namedPropertyExists = true;
1654 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1655 if ( !propertySuccessfullyLoaded )
1656 {
1657 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1658 }
1659 }
1660 else
1661 {
1663 {
1664 myErrorMessage = tr( "Style not found in database" );
1665 }
1666 }
1667 break;
1668 }
1670 {
1671 if ( loadNamedMetadataFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1672 ( project.exists() && loadNamedMetadataFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1673 loadNamedMetadataFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1674 {
1675 namedPropertyExists = true;
1676 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1677 if ( !propertySuccessfullyLoaded )
1678 {
1679 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1680 }
1681 }
1682 else
1683 {
1684 myErrorMessage = tr( "Metadata not found in database" );
1685 }
1686 break;
1687 }
1688 }
1689 }
1690
1691 if ( !propertySuccessfullyLoaded )
1692 {
1693 return myErrorMessage;
1694 }
1695
1696 switch ( type )
1697 {
1698 case QgsMapLayer::Style:
1699 propertySuccessfullyLoaded = importNamedStyle( myDocument, myErrorMessage, categories );
1700 if ( !propertySuccessfullyLoaded )
1701 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1702 break;
1704 propertySuccessfullyLoaded = importNamedMetadata( myDocument, myErrorMessage );
1705 if ( !propertySuccessfullyLoaded )
1706 myErrorMessage = tr( "Loading metadata file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1707 break;
1708 }
1709 return myErrorMessage;
1710}
1711
1712bool QgsMapLayer::importNamedMetadata( QDomDocument &document, QString &errorMessage )
1713{
1715
1716 const QDomElement myRoot = document.firstChildElement( QStringLiteral( "qgis" ) );
1717 if ( myRoot.isNull() )
1718 {
1719 errorMessage = tr( "Root <qgis> element could not be found" );
1720 return false;
1721 }
1722
1723 return mMetadata.readMetadataXml( myRoot );
1724}
1725
1726bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMessage, QgsMapLayer::StyleCategories categories )
1727{
1729
1730 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "qgis" ) );
1731 if ( myRoot.isNull() )
1732 {
1733 myErrorMessage = tr( "Root <qgis> element could not be found" );
1734 return false;
1735 }
1736
1737 // get style file version string, if any
1738 const QgsProjectVersion fileVersion( myRoot.attribute( QStringLiteral( "version" ) ) );
1739 const QgsProjectVersion thisVersion( Qgis::version() );
1740
1741 if ( thisVersion > fileVersion )
1742 {
1743 QgsProjectFileTransform styleFile( myDocument, fileVersion );
1744 styleFile.updateRevision( thisVersion );
1745 }
1746
1747 // Get source categories
1748 const QgsMapLayer::StyleCategories sourceCategories = QgsXmlUtils::readFlagAttribute( myRoot, QStringLiteral( "styleCategories" ), QgsMapLayer::AllStyleCategories );
1749
1750 //Test for matching geometry type on vector layers when applying, if geometry type is given in the style
1751 if ( ( sourceCategories.testFlag( QgsMapLayer::Symbology ) || sourceCategories.testFlag( QgsMapLayer::Symbology3D ) ) &&
1752 ( categories.testFlag( QgsMapLayer::Symbology ) || categories.testFlag( QgsMapLayer::Symbology3D ) ) )
1753 {
1754 if ( type() == Qgis::LayerType::Vector && !myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).isNull() )
1755 {
1756 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( this );
1757 const Qgis::GeometryType importLayerGeometryType = static_cast<Qgis::GeometryType>( myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).text().toInt() );
1758 if ( importLayerGeometryType != Qgis::GeometryType::Unknown && vl->geometryType() != importLayerGeometryType )
1759 {
1760 myErrorMessage = tr( "Cannot apply style with symbology to layer with a different geometry type" );
1761 return false;
1762 }
1763 }
1764 }
1765
1767 return readSymbology( myRoot, myErrorMessage, context, categories ); // TODO: support relative paths in QML?
1768}
1769
1770void QgsMapLayer::exportNamedMetadata( QDomDocument &doc, QString &errorMsg ) const
1771{
1773
1774 QDomImplementation DomImplementation;
1775 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1776 QDomDocument myDocument( documentType );
1777
1778 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1779 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1780 myDocument.appendChild( myRootNode );
1781
1782 if ( !mMetadata.writeMetadataXml( myRootNode, myDocument ) )
1783 {
1784 errorMsg = QObject::tr( "Could not save metadata" );
1785 return;
1786 }
1787
1788 doc = myDocument;
1789}
1790
1791void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
1792{
1794
1795 QDomImplementation DomImplementation;
1796 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1797 QDomDocument myDocument( documentType );
1798
1799 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1800 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1801 myDocument.appendChild( myRootNode );
1802
1803 if ( !writeSymbology( myRootNode, myDocument, errorMsg, context, categories ) ) // TODO: support relative paths in QML?
1804 {
1805 errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1806 return;
1807 }
1808
1809 /*
1810 * Check to see if the layer is vector - in which case we should also export its geometryType
1811 * to avoid eventually pasting to a layer with a different geometry
1812 */
1813 if ( type() == Qgis::LayerType::Vector )
1814 {
1815 //Getting the selectionLayer geometry
1816 const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( this );
1817 const QString geoType = QString::number( static_cast<int>( vl->geometryType() ) );
1818
1819 //Adding geometryinformation
1820 QDomElement layerGeometryType = myDocument.createElement( QStringLiteral( "layerGeometryType" ) );
1821 const QDomText type = myDocument.createTextNode( geoType );
1822
1823 layerGeometryType.appendChild( type );
1824 myRootNode.appendChild( layerGeometryType );
1825 }
1826
1827 doc = myDocument;
1828}
1829
1830QString QgsMapLayer::saveDefaultStyle( bool &resultFlag )
1831{
1833
1834 return saveDefaultStyle( resultFlag, AllStyleCategories );
1835}
1836
1837QString QgsMapLayer::saveDefaultStyle( bool &resultFlag, StyleCategories categories )
1838{
1840
1841 return saveNamedStyle( styleURI(), resultFlag, categories );
1842}
1843
1844QString QgsMapLayer::saveNamedMetadata( const QString &uri, bool &resultFlag )
1845{
1847
1848 return saveNamedProperty( uri, QgsMapLayer::Metadata, resultFlag );
1849}
1850
1851QString QgsMapLayer::loadNamedMetadata( const QString &uri, bool &resultFlag )
1852{
1854
1855 bool metadataExists = false;
1856 bool metadataSuccessfullyLoaded = false;
1857 const QString message = loadNamedProperty( uri, QgsMapLayer::Metadata, metadataExists, metadataSuccessfullyLoaded );
1858
1859 // TODO QGIS 4.0 -- fix API for loadNamedMetadata so we can return metadataExists too
1860 ( void )metadataExists;
1861 resultFlag = metadataSuccessfullyLoaded;
1862 return message;
1863}
1864
1865QString QgsMapLayer::saveNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag, StyleCategories categories )
1866{
1868
1869 // check if the uri is a file or ends with .qml/.qmd,
1870 // which indicates that it should become one
1871 // everything else goes to the database
1872 QString filename;
1873
1874 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1875 if ( vlayer && vlayer->providerType() == QLatin1String( "ogr" ) )
1876 {
1877 QStringList theURIParts = uri.split( '|' );
1878 filename = theURIParts[0];
1879 }
1880 else if ( vlayer && vlayer->providerType() == QLatin1String( "gpx" ) )
1881 {
1882 QStringList theURIParts = uri.split( '?' );
1883 filename = theURIParts[0];
1884 }
1885 else if ( vlayer && vlayer->providerType() == QLatin1String( "delimitedtext" ) )
1886 {
1887 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
1888 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
1889 if ( filename.isEmpty() )
1890 filename = uri;
1891 }
1892 else
1893 {
1894 filename = uri;
1895 }
1896
1897 QString myErrorMessage;
1898 QDomDocument myDocument;
1899 switch ( type )
1900 {
1901 case Metadata:
1902 exportNamedMetadata( myDocument, myErrorMessage );
1903 break;
1904
1905 case Style:
1906 const QgsReadWriteContext context;
1907 exportNamedStyle( myDocument, myErrorMessage, context, categories );
1908 break;
1909 }
1910
1911 const QFileInfo myFileInfo( filename );
1912 if ( myFileInfo.exists() || filename.endsWith( QgsMapLayer::extensionPropertyType( type ), Qt::CaseInsensitive ) )
1913 {
1914 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
1915 if ( !myDirInfo.isWritable() )
1916 {
1917 resultFlag = false;
1918 return tr( "The directory containing your dataset needs to be writable!" );
1919 }
1920
1921 // now construct the file name for our .qml or .qmd file
1922 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1923
1924 QFile myFile( myFileName );
1925 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1926 {
1927 QTextStream myFileStream( &myFile );
1928 // save as utf-8 with 2 spaces for indents
1929 myDocument.save( myFileStream, 2 );
1930 myFile.close();
1931 resultFlag = true;
1932 switch ( type )
1933 {
1934 case Metadata:
1935 return tr( "Created default metadata file as %1" ).arg( myFileName );
1936
1937 case Style:
1938 return tr( "Created default style file as %1" ).arg( myFileName );
1939 }
1940
1941 }
1942 else
1943 {
1944 resultFlag = false;
1945 switch ( type )
1946 {
1947 case Metadata:
1948 return tr( "ERROR: Failed to created default metadata file as %1. Check file permissions and retry." ).arg( myFileName );
1949
1950 case Style:
1951 return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName );
1952 }
1953 }
1954 }
1955 else
1956 {
1957 const QString qml = myDocument.toString();
1958
1959 // read from database
1962
1963 int myResult = database.open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ) );
1964 if ( myResult != SQLITE_OK )
1965 {
1966 return tr( "User database could not be opened." );
1967 }
1968
1969 QByteArray param0 = uri.toUtf8();
1970 QByteArray param1 = qml.toUtf8();
1971
1972 QString mySql;
1973 switch ( type )
1974 {
1975 case Metadata:
1976 mySql = QStringLiteral( "create table if not exists tbl_metadata(metadata varchar primary key,qmd varchar)" );
1977 break;
1978
1979 case Style:
1980 mySql = QStringLiteral( "create table if not exists tbl_styles(style varchar primary key,qml varchar)" );
1981 break;
1982 }
1983
1984 statement = database.prepare( mySql, myResult );
1985 if ( myResult == SQLITE_OK )
1986 {
1987 if ( sqlite3_step( statement.get() ) != SQLITE_DONE )
1988 {
1989 resultFlag = false;
1990 switch ( type )
1991 {
1992 case Metadata:
1993 return tr( "The metadata table could not be created." );
1994
1995 case Style:
1996 return tr( "The style table could not be created." );
1997 }
1998 }
1999 }
2000
2001 switch ( type )
2002 {
2003 case Metadata:
2004 mySql = QStringLiteral( "insert into tbl_metadata(metadata,qmd) values (?,?)" );
2005 break;
2006
2007 case Style:
2008 mySql = QStringLiteral( "insert into tbl_styles(style,qml) values (?,?)" );
2009 break;
2010 }
2011 statement = database.prepare( mySql, myResult );
2012 if ( myResult == SQLITE_OK )
2013 {
2014 if ( sqlite3_bind_text( statement.get(), 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
2015 sqlite3_bind_text( statement.get(), 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
2016 sqlite3_step( statement.get() ) == SQLITE_DONE )
2017 {
2018 resultFlag = true;
2019 switch ( type )
2020 {
2021 case Metadata:
2022 myErrorMessage = tr( "The metadata %1 was saved to database" ).arg( uri );
2023 break;
2024
2025 case Style:
2026 myErrorMessage = tr( "The style %1 was saved to database" ).arg( uri );
2027 break;
2028 }
2029 }
2030 }
2031
2032 if ( !resultFlag )
2033 {
2034 QString mySql;
2035 switch ( type )
2036 {
2037 case Metadata:
2038 mySql = QStringLiteral( "update tbl_metadata set qmd=? where metadata=?" );
2039 break;
2040
2041 case Style:
2042 mySql = QStringLiteral( "update tbl_styles set qml=? where style=?" );
2043 break;
2044 }
2045 statement = database.prepare( mySql, myResult );
2046 if ( myResult == SQLITE_OK )
2047 {
2048 if ( sqlite3_bind_text( statement.get(), 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
2049 sqlite3_bind_text( statement.get(), 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
2050 sqlite3_step( statement.get() ) == SQLITE_DONE )
2051 {
2052 resultFlag = true;
2053 switch ( type )
2054 {
2055 case Metadata:
2056 myErrorMessage = tr( "The metadata %1 was updated in the database." ).arg( uri );
2057 break;
2058
2059 case Style:
2060 myErrorMessage = tr( "The style %1 was updated in the database." ).arg( uri );
2061 break;
2062 }
2063 }
2064 else
2065 {
2066 resultFlag = false;
2067 switch ( type )
2068 {
2069 case Metadata:
2070 myErrorMessage = tr( "The metadata %1 could not be updated in the database." ).arg( uri );
2071 break;
2072
2073 case Style:
2074 myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( uri );
2075 break;
2076 }
2077 }
2078 }
2079 else
2080 {
2081 resultFlag = false;
2082 switch ( type )
2083 {
2084 case Metadata:
2085 myErrorMessage = tr( "The metadata %1 could not be inserted into database." ).arg( uri );
2086 break;
2087
2088 case Style:
2089 myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( uri );
2090 break;
2091 }
2092 }
2093 }
2094 }
2095
2096 return myErrorMessage;
2097}
2098
2099QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag, StyleCategories categories )
2100{
2102
2103 return saveNamedProperty( uri, QgsMapLayer::Style, resultFlag, categories );
2104}
2105
2106void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
2107{
2108 QgsSldExportContext exportContext;
2109 doc = exportSldStyleV3( exportContext );
2110 if ( !exportContext.errors().empty() )
2111 errorMsg = exportContext.errors().join( "\n" );
2112}
2113
2114void QgsMapLayer::exportSldStyleV2( QDomDocument &doc, QString &errorMsg, QgsSldExportContext &exportContext ) const
2115{
2117 doc = exportSldStyleV3( exportContext );
2118 if ( !exportContext.errors().empty() )
2119 errorMsg = exportContext.errors().join( "\n" );
2120}
2121
2122QDomDocument QgsMapLayer::exportSldStyleV3( QgsSldExportContext &exportContext ) const
2123{
2125
2126 QDomDocument myDocument = QDomDocument();
2127
2128 const QDomNode header = myDocument.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
2129 myDocument.appendChild( header );
2130
2131 const QgsVectorLayer *vlayer = qobject_cast<const QgsVectorLayer *>( this );
2132 const QgsRasterLayer *rlayer = qobject_cast<const QgsRasterLayer *>( this );
2133 if ( !vlayer && !rlayer )
2134 {
2135 exportContext.pushError( tr( "Could not save symbology because:\n%1" )
2136 .arg( tr( "Only vector and raster layers are supported" ) ) );
2137 return myDocument;
2138 }
2139
2140 // Create the root element
2141 QDomElement root = myDocument.createElementNS( QStringLiteral( "http://www.opengis.net/sld" ), QStringLiteral( "StyledLayerDescriptor" ) );
2142 QDomElement layerNode;
2143 if ( vlayer )
2144 {
2145 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.1.0" ) );
2146 root.setAttribute( QStringLiteral( "xsi:schemaLocation" ), QStringLiteral( "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" ) );
2147 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
2148 root.setAttribute( QStringLiteral( "xmlns:se" ), QStringLiteral( "http://www.opengis.net/se" ) );
2149 root.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
2150 root.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
2151 myDocument.appendChild( root );
2152
2153 // Create the NamedLayer element
2154 layerNode = myDocument.createElement( QStringLiteral( "NamedLayer" ) );
2155 root.appendChild( layerNode );
2156 }
2157
2158 // note: Only SLD 1.0 version is generated because seems none is using SE1.1.0 at least for rasters
2159 if ( rlayer )
2160 {
2161 // Create the root element
2162 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0.0" ) );
2163 root.setAttribute( QStringLiteral( "xmlns:gml" ), QStringLiteral( "http://www.opengis.net/gml" ) );
2164 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
2165 root.setAttribute( QStringLiteral( "xmlns:sld" ), QStringLiteral( "http://www.opengis.net/sld" ) );
2166 myDocument.appendChild( root );
2167
2168 // Create the NamedLayer element
2169 layerNode = myDocument.createElement( QStringLiteral( "UserLayer" ) );
2170 root.appendChild( layerNode );
2171 }
2172
2173 QVariantMap props = exportContext.extraProperties();
2174
2175 QVariant context;
2176 context.setValue( exportContext );
2177
2178 // TODO -- move this to proper members of QgsSldExportContext
2179 props[ QStringLiteral( "SldExportContext" ) ] = context;
2180
2182 {
2183 props[ QStringLiteral( "scaleMinDenom" ) ] = QString::number( mMinScale );
2184 props[ QStringLiteral( "scaleMaxDenom" ) ] = QString::number( mMaxScale );
2185 }
2186 exportContext.setExtraProperties( props );
2187
2188 if ( vlayer )
2189 {
2190 if ( !vlayer->writeSld( layerNode, myDocument, exportContext ) )
2191 {
2192 return myDocument;
2193 }
2194 }
2195 else if ( rlayer )
2196 {
2197 if ( !rlayer->writeSld( layerNode, myDocument, exportContext ) )
2198 {
2199 return myDocument;
2200 }
2201 }
2202
2203 return myDocument;
2204}
2205
2206QString QgsMapLayer::saveSldStyle( const QString &uri, bool &resultFlag ) const
2207{
2208 QgsSldExportContext context;
2209 context.setExportFilePath( uri );
2210 return saveSldStyleV2( resultFlag, context );
2211}
2212
2213QString QgsMapLayer::saveSldStyleV2( bool &resultFlag, QgsSldExportContext &exportContext ) const
2214{
2216
2217 const QgsMapLayer *mlayer = qobject_cast<const QgsMapLayer *>( this );
2218
2219 const QString uri { exportContext.exportFilePath() };
2220
2221 // check if the uri is a file or ends with .sld,
2222 // which indicates that it should become one
2223 QString filename;
2224 if ( mlayer->providerType() == QLatin1String( "ogr" ) )
2225 {
2226 QStringList theURIParts = uri.split( '|' );
2227 filename = theURIParts[0];
2228 }
2229 else if ( mlayer->providerType() == QLatin1String( "gpx" ) )
2230 {
2231 QStringList theURIParts = uri.split( '?' );
2232 filename = theURIParts[0];
2233 }
2234 else if ( mlayer->providerType() == QLatin1String( "delimitedtext" ) )
2235 {
2236 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
2237 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
2238 if ( filename.isEmpty() )
2239 filename = uri;
2240 }
2241 else
2242 {
2243 filename = uri;
2244 }
2245
2246 const QFileInfo myFileInfo( filename );
2247 if ( myFileInfo.exists() || filename.endsWith( QLatin1String( ".sld" ), Qt::CaseInsensitive ) )
2248 {
2249 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
2250 if ( !myDirInfo.isWritable() )
2251 {
2252 resultFlag = false;
2253 return tr( "The directory containing your dataset needs to be writable!" );
2254 }
2255
2256 // now construct the file name for our .sld style file
2257 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld";
2258
2259 QgsSldExportContext context { exportContext };
2260 context.setExportFilePath( myFileName );
2261
2262 QDomDocument myDocument = mlayer->exportSldStyleV3( context );
2263
2264 if ( !context.errors().empty() )
2265 {
2266 resultFlag = false;
2267 return context.errors().join( '\n' );
2268 }
2269
2270 QFile myFile( myFileName );
2271 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
2272 {
2273 QTextStream myFileStream( &myFile );
2274 // save as utf-8 with 2 spaces for indents
2275 myDocument.save( myFileStream, 2 );
2276 myFile.close();
2277 resultFlag = true;
2278 return tr( "Created default style file as %1" ).arg( myFileName );
2279 }
2280 }
2281
2282 resultFlag = false;
2283 return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename );
2284
2285}
2286
2287QString QgsMapLayer::loadSldStyle( const QString &uri, bool &resultFlag )
2288{
2290
2291 resultFlag = false;
2292
2293 QDomDocument myDocument;
2294
2295 // location of problem associated with errorMsg
2296 int line = 0, column = 0;
2297 QString myErrorMessage;
2298
2299 QFile myFile( uri );
2300 if ( myFile.open( QFile::ReadOnly ) )
2301 {
2302 // read file
2303#if QT_VERSION >= QT_VERSION_CHECK( 6, 5, 0 )
2304 QXmlStreamReader xmlReader( &myFile );
2305 xmlReader.addExtraNamespaceDeclaration( QXmlStreamNamespaceDeclaration( QStringLiteral( "sld" ), QStringLiteral( "http://www.opengis.net/sld" ) ) );
2306 xmlReader.addExtraNamespaceDeclaration( QXmlStreamNamespaceDeclaration( QStringLiteral( "fes" ), QStringLiteral( "http://www.opengis.net/fes/2.0" ) ) );
2307 xmlReader.addExtraNamespaceDeclaration( QXmlStreamNamespaceDeclaration( QStringLiteral( "ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) ) );
2308 const QDomDocument::ParseResult result = myDocument.setContent( &xmlReader, QDomDocument::ParseOption::UseNamespaceProcessing );
2309 if ( result )
2310 {
2311 resultFlag = true;
2312 }
2313 else
2314 {
2315 myErrorMessage = result.errorMessage;
2316 line = result.errorLine;
2317 column = result.errorColumn;
2318 }
2319#else
2320 resultFlag = myDocument.setContent( &myFile, true, &myErrorMessage, &line, &column );
2321#endif
2322 if ( !resultFlag )
2323 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
2324 myFile.close();
2325 }
2326 else
2327 {
2328 myErrorMessage = tr( "Unable to open file %1" ).arg( uri );
2329 }
2330
2331 if ( !resultFlag )
2332 {
2333 return myErrorMessage;
2334 }
2335
2336 // check for root SLD element
2337 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "StyledLayerDescriptor" ) );
2338 if ( myRoot.isNull() )
2339 {
2340 myErrorMessage = QStringLiteral( "Error: StyledLayerDescriptor element not found in %1" ).arg( uri );
2341 resultFlag = false;
2342 return myErrorMessage;
2343 }
2344
2345 // now get the style node out and pass it over to the layer
2346 // to deserialise...
2347 const QDomElement namedLayerElem = myRoot.firstChildElement( QStringLiteral( "NamedLayer" ) );
2348 if ( namedLayerElem.isNull() )
2349 {
2350 myErrorMessage = QStringLiteral( "Info: NamedLayer element not found." );
2351 resultFlag = false;
2352 return myErrorMessage;
2353 }
2354
2355 QString errorMsg;
2356 resultFlag = readSld( namedLayerElem, errorMsg );
2357 if ( !resultFlag )
2358 {
2359 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, errorMsg );
2360 return myErrorMessage;
2361 }
2362
2363 return QString();
2364}
2365
2366bool QgsMapLayer::readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
2367{
2369
2370 Q_UNUSED( node )
2371 Q_UNUSED( errorMessage )
2372 Q_UNUSED( context )
2373 Q_UNUSED( categories )
2374 return false;
2375}
2376
2377bool QgsMapLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
2378 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
2379{
2381
2382 Q_UNUSED( node )
2383 Q_UNUSED( doc )
2384 Q_UNUSED( errorMessage )
2385 Q_UNUSED( context )
2386 Q_UNUSED( categories )
2387 return false;
2388}
2389
2390
2391void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2392 bool loadDefaultStyleFlag )
2393{
2395
2397
2399 if ( loadDefaultStyleFlag )
2400 {
2402 }
2403
2405 {
2407 }
2408 setDataSource( dataSource,
2409 baseName.isEmpty() ? mLayerName : baseName,
2410 provider.isEmpty() ? mProviderKey : provider,
2411 options, flags );
2412}
2413
2414void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2415 const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag )
2416{
2418
2420 if ( loadDefaultStyleFlag )
2421 {
2423 }
2424
2426 {
2428 }
2429 setDataSource( dataSource, baseName, provider, options, flags );
2430}
2431
2432void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2434{
2436
2439 {
2441 }
2442 setDataSourcePrivate( dataSource, baseName, provider, options, flags );
2443 emit dataSourceChanged();
2444 emit dataChanged();
2446}
2447
2448
2449void QgsMapLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider,
2451{
2453
2454 Q_UNUSED( dataSource )
2455 Q_UNUSED( baseName )
2456 Q_UNUSED( provider )
2457 Q_UNUSED( options )
2458 Q_UNUSED( flags )
2459}
2460
2461
2463{
2465
2466 return mProviderKey;
2467}
2468
2469void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
2470 QgsMapLayer::StyleCategories categories )
2471{
2473
2474 if ( categories.testFlag( Symbology3D ) )
2475 {
2476 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "3D Symbology" ) );
2477
2478 QgsAbstract3DRenderer *r3D = nullptr;
2479 QDomElement renderer3DElem = layerElement.firstChildElement( QStringLiteral( "renderer-3d" ) );
2480 if ( !renderer3DElem.isNull() )
2481 {
2482 const QString type3D = renderer3DElem.attribute( QStringLiteral( "type" ) );
2484 if ( meta3D )
2485 {
2486 r3D = meta3D->createRenderer( renderer3DElem, context );
2487 }
2488 }
2489 setRenderer3D( r3D );
2490 }
2491
2492 if ( categories.testFlag( CustomProperties ) )
2493 {
2494 // read custom properties before passing reading further to a subclass, so that
2495 // the subclass can also read custom properties
2496 readCustomProperties( layerElement );
2497 }
2498
2499 // use scale dependent visibility flag
2500 if ( categories.testFlag( Rendering ) )
2501 {
2502 setScaleBasedVisibility( layerElement.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
2503 if ( layerElement.hasAttribute( QStringLiteral( "minimumScale" ) ) )
2504 {
2505 // older element, when scales were reversed
2506 setMaximumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
2507 setMinimumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
2508 }
2509 else
2510 {
2511 setMaximumScale( layerElement.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
2512 setMinimumScale( layerElement.attribute( QStringLiteral( "minScale" ) ).toDouble() );
2513 }
2514 if ( layerElement.hasAttribute( QStringLiteral( "autoRefreshMode" ) ) )
2515 {
2516 setAutoRefreshMode( qgsEnumKeyToValue( layerElement.attribute( QStringLiteral( "autoRefreshMode" ) ), Qgis::AutoRefreshMode::Disabled ) );
2517 setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ) ).toInt() );
2518 }
2519 }
2520
2521 if ( categories.testFlag( LayerConfiguration ) )
2522 {
2523 // flags
2524 const QDomElement flagsElem = layerElement.firstChildElement( QStringLiteral( "flags" ) );
2525 LayerFlags flags = mFlags;
2526 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
2527 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
2528 {
2529 const QDomNode flagNode = flagsElem.namedItem( it.value() );
2530 if ( flagNode.isNull() )
2531 continue;
2532 const bool flagValue = flagNode.toElement().text() == "1" ? true : false;
2533 if ( flags.testFlag( it.key() ) && !flagValue )
2534 flags &= ~it.key();
2535 else if ( !flags.testFlag( it.key() ) && flagValue )
2536 flags |= it.key();
2537 }
2538 setFlags( flags );
2539 }
2540
2541 if ( categories.testFlag( Temporal ) )
2542 {
2543 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Temporal" ) );
2544
2546 properties->readXml( layerElement.toElement(), context );
2547 }
2548
2549 if ( categories.testFlag( Elevation ) )
2550 {
2551 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Elevation" ) );
2552
2554 properties->readXml( layerElement.toElement(), context );
2555 }
2556
2557 if ( categories.testFlag( Notes ) )
2558 {
2559 const QDomElement notesElem = layerElement.firstChildElement( QStringLiteral( "userNotes" ) );
2560 if ( !notesElem.isNull() )
2561 {
2562 const QString notes = notesElem.attribute( QStringLiteral( "value" ) );
2563 QgsLayerNotesUtils::setLayerNotes( this, notes );
2564 }
2565 }
2566}
2567
2569{
2571
2572 return mUndoStack;
2573}
2574
2576{
2578
2579 return mUndoStackStyles;
2580}
2581
2583{
2585
2586 return mCustomProperties.keys();
2587}
2588
2589void QgsMapLayer::setCustomProperty( const QString &key, const QVariant &value )
2590{
2592
2593 if ( !mCustomProperties.contains( key ) || mCustomProperties.value( key ) != value )
2594 {
2595 mCustomProperties.setValue( key, value );
2596 emit customPropertyChanged( key );
2597 }
2598}
2599
2601{
2603
2604 mCustomProperties = properties;
2605 for ( const QString &key : mCustomProperties.keys() )
2606 {
2607 emit customPropertyChanged( key );
2608 }
2609}
2610
2612{
2614
2615 return mCustomProperties;
2616}
2617
2618QVariant QgsMapLayer::customProperty( const QString &value, const QVariant &defaultValue ) const
2619{
2620 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
2622
2623 return mCustomProperties.value( value, defaultValue );
2624}
2625
2626void QgsMapLayer::removeCustomProperty( const QString &key )
2627{
2629
2630 if ( mCustomProperties.contains( key ) )
2631 {
2632 mCustomProperties.remove( key );
2633 emit customPropertyChanged( key );
2634 }
2635}
2636
2637int QgsMapLayer::listStylesInDatabase( QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError )
2638{
2640
2641 return QgsProviderRegistry::instance()->listStyles( mProviderKey, mDataSource, ids, names, descriptions, msgError );
2642}
2643
2644QString QgsMapLayer::getStyleFromDatabase( const QString &styleId, QString &msgError )
2645{
2647
2648 return QgsProviderRegistry::instance()->getStyleById( mProviderKey, mDataSource, styleId, msgError );
2649}
2650
2651bool QgsMapLayer::deleteStyleFromDatabase( const QString &styleId, QString &msgError )
2652{
2654
2656}
2657
2658void QgsMapLayer::saveStyleToDatabase( const QString &name, const QString &description,
2659 bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories )
2660{
2662
2663 QString sldStyle, qmlStyle;
2664 QDomDocument qmlDocument;
2665 QgsReadWriteContext context;
2666 exportNamedStyle( qmlDocument, msgError, context, categories );
2667 if ( !msgError.isNull() )
2668 {
2669 return;
2670 }
2671 qmlStyle = qmlDocument.toString();
2672
2673 QgsSldExportContext sldContext;
2674 QDomDocument sldDocument = this->exportSldStyleV3( sldContext );
2675 if ( !sldContext.errors().empty() )
2676 {
2677 return;
2678 }
2679 sldStyle = sldDocument.toString();
2680
2682 mDataSource, qmlStyle, sldStyle, name,
2683 description, uiFileContent, useAsDefault, msgError );
2684}
2685
2686QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &resultFlag, bool loadFromLocalDB, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
2687{
2689
2690 QString returnMessage;
2691 QString qml, errorMsg;
2692 QString styleName;
2693 if ( !loadFromLocalDB && dataProvider() && dataProvider()->styleStorageCapabilities().testFlag( Qgis::ProviderStyleStorageCapability::LoadFromDatabase ) )
2694 {
2696 }
2697
2698 // Style was successfully loaded from provider storage
2699 if ( !qml.isEmpty() )
2700 {
2701 QDomDocument myDocument( QStringLiteral( "qgis" ) );
2702 myDocument.setContent( qml );
2703 resultFlag = importNamedStyle( myDocument, errorMsg );
2704 returnMessage = QObject::tr( "Loaded from Provider" );
2705 }
2706 else
2707 {
2709
2710 bool styleExists = false;
2711 bool styleSuccessfullyLoaded = false;
2712
2713 returnMessage = loadNamedProperty( theURI, PropertyType::Style, styleExists, styleSuccessfullyLoaded, categories, flags );
2714
2715 // TODO QGIS 4.0 -- fix API for loadNamedStyle so we can return styleExists too
2716 ( void )styleExists;
2717 resultFlag = styleSuccessfullyLoaded;
2718 }
2719
2720 if ( ! styleName.isEmpty() )
2721 {
2722 styleManager()->renameStyle( styleManager()->currentStyle(), styleName );
2723 }
2724
2725 if ( resultFlag )
2726 emit styleLoaded( categories );
2727
2728 return returnMessage;
2729}
2730
2737
2739{
2741
2742 return false;
2743}
2744
2746{
2748
2749 return false;
2750}
2751
2753{
2755
2756 return true;
2757}
2758
2760{
2762
2763 // invalid layers are temporary? -- who knows?!
2764 if ( !isValid() )
2765 return false;
2766
2767 if ( mProviderKey == QLatin1String( "memory" ) )
2768 return true;
2769
2770 const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( mProviderKey, mDataSource );
2771 const QString path = sourceParts.value( QStringLiteral( "path" ) ).toString();
2772 if ( path.isEmpty() )
2773 return false;
2774
2775 // check if layer path is inside one of the standard temporary file locations for this platform
2776 const QStringList tempPaths = QStandardPaths::standardLocations( QStandardPaths::TempLocation );
2777 for ( const QString &tempPath : tempPaths )
2778 {
2779 if ( path.startsWith( tempPath ) )
2780 return true;
2781 }
2782
2783 return false;
2784}
2785
2786void QgsMapLayer::setValid( bool valid )
2787{
2789
2790 if ( mValid == valid )
2791 return;
2792
2793 mValid = valid;
2794 emit isValidChanged();
2795}
2796
2798{
2800
2801 if ( legend == mLegend.get() )
2802 return;
2803
2804 mLegend.reset( legend );
2805
2806
2807 if ( mLegend )
2808 {
2809 mLegend->setParent( this );
2810 connect( mLegend.get(), &QgsMapLayerLegend::itemsChanged, this, &QgsMapLayer::legendChanged, Qt::UniqueConnection );
2811 }
2812
2813 emit legendChanged();
2814}
2815
2817{
2819
2820 return mLegend.get();
2821}
2822
2824{
2826
2827 return mStyleManager.get();
2828}
2829
2831{
2833
2834 if ( renderer == m3DRenderer.get() )
2835 return;
2836
2837 m3DRenderer.reset( renderer );
2838
2839 emit renderer3DChanged();
2840 emit repaintRequested();
2842}
2843
2845{
2847
2848 return m3DRenderer.get();
2849}
2850
2851void QgsMapLayer::triggerRepaint( bool deferredUpdate )
2852{
2854
2855 if ( mRepaintRequestedFired )
2856 return;
2857 mRepaintRequestedFired = true;
2858 emit repaintRequested( deferredUpdate );
2859 mRepaintRequestedFired = false;
2860}
2861
2868
2870{
2872
2873 mMetadata = metadata;
2874// mMetadata.saveToLayer( this );
2875 emit metadataChanged();
2876}
2877
2879{
2881
2882 return QString();
2883}
2884
2885QDateTime QgsMapLayer::timestamp() const
2886{
2888
2889 return QDateTime();
2890}
2891
2899
2901{
2902 updateExtent( extent );
2903}
2904
2906{
2908
2909 updateExtent( extent );
2910}
2911
2912bool QgsMapLayer::isReadOnly() const
2913{
2915
2916 return true;
2917}
2918
2920{
2922
2923 return mOriginalXmlProperties;
2924}
2925
2926void QgsMapLayer::setOriginalXmlProperties( const QString &originalXmlProperties )
2927{
2929
2930 mOriginalXmlProperties = originalXmlProperties;
2931}
2932
2933QString QgsMapLayer::generateId( const QString &layerName )
2934{
2935 // Generate the unique ID of this layer
2936 const QString uuid = QUuid::createUuid().toString();
2937 // trim { } from uuid
2938 QString id = layerName + '_' + uuid.mid( 1, uuid.length() - 2 );
2939 // Tidy the ID up to avoid characters that may cause problems
2940 // elsewhere (e.g in some parts of XML). Replaces every non-word
2941 // character (word characters are the alphabet, numbers and
2942 // underscore) with an underscore.
2943 // Note that the first backslash in the regular expression is
2944 // there for the compiler, so the pattern is actually \W
2945 const thread_local QRegularExpression idRx( QStringLiteral( "[\\W]" ) );
2946 id.replace( idRx, QStringLiteral( "_" ) );
2947 return id;
2948}
2949
2951{
2953
2954 return true;
2955}
2956
2958{
2960
2961 return mapTipsEnabled() && !mMapTipTemplate.isEmpty();
2962}
2963
2964void QgsMapLayer::setProviderType( const QString &providerType )
2965{
2967
2969}
2970
2971QSet<QgsMapLayerDependency> QgsMapLayer::dependencies() const
2972{
2974
2975 return mDependencies;
2976}
2977
2978bool QgsMapLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
2979{
2981
2982 QSet<QgsMapLayerDependency> deps;
2983 const auto constODeps = oDeps;
2984 for ( const QgsMapLayerDependency &dep : constODeps )
2985 {
2986 if ( dep.origin() == QgsMapLayerDependency::FromUser )
2987 deps << dep;
2988 }
2989
2990 mDependencies = deps;
2991 emit dependenciesChanged();
2992 return true;
2993}
2994
2996{
2998
2999 QgsDataProvider *lDataProvider = dataProvider();
3000
3001 if ( !lDataProvider )
3002 return;
3003
3004 if ( enabled && !isRefreshOnNotifyEnabled() )
3005 {
3006 lDataProvider->setListening( enabled );
3007 connect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
3008 }
3009 else if ( !enabled && isRefreshOnNotifyEnabled() )
3010 {
3011 // we don't want to disable provider listening because someone else could need it (e.g. actions)
3012 disconnect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
3013 }
3014 mIsRefreshOnNofifyEnabled = enabled;
3015}
3016
3018{
3020
3021 if ( QgsMapLayerStore *store = qobject_cast<QgsMapLayerStore *>( parent() ) )
3022 {
3023 return qobject_cast<QgsProject *>( store->parent() );
3024 }
3025 return nullptr;
3026}
3027
3028void QgsMapLayer::onNotified( const QString &message )
3029{
3031
3032 if ( refreshOnNotifyMessage().isEmpty() || refreshOnNotifyMessage() == message )
3033 {
3035 emit dataChanged();
3036 }
3037}
3038
3039QgsRectangle QgsMapLayer::wgs84Extent( bool forceRecalculate ) const
3040{
3042
3044
3045 if ( ! forceRecalculate && ! mWgs84Extent.isNull() )
3046 {
3047 wgs84Extent = mWgs84Extent;
3048 }
3049 else if ( ! mExtent2D.isNull() || ! mExtent3D.isNull() )
3050 {
3051 QgsCoordinateTransform transformer { crs(), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), transformContext() };
3052 transformer.setBallparkTransformsAreAppropriate( true );
3053 try
3054 {
3055 if ( mExtent2D.isNull() )
3056 wgs84Extent = transformer.transformBoundingBox( mExtent3D.toRectangle() );
3057 else
3058 wgs84Extent = transformer.transformBoundingBox( mExtent2D );
3059 }
3060 catch ( const QgsCsException &cse )
3061 {
3062 QgsMessageLog::logMessage( tr( "Error transforming extent: %1" ).arg( cse.what() ) );
3064 }
3065 }
3066 return wgs84Extent;
3067}
3068
3069void QgsMapLayer::updateExtent( const QgsRectangle &extent ) const
3070{
3072
3073 if ( extent == mExtent2D )
3074 return;
3075
3076 mExtent2D = extent;
3077
3078 // do not update the wgs84 extent if we trust layer metadata
3080 return;
3081
3082 mWgs84Extent = wgs84Extent( true );
3083}
3084
3085void QgsMapLayer::updateExtent( const QgsBox3D &extent ) const
3086{
3088
3089 if ( extent == mExtent3D )
3090 return;
3091
3092 if ( extent.isNull() )
3093 {
3094 if ( !extent.toRectangle().isNull() )
3095 {
3096 // bad 3D extent param but valid in 2d --> update 2D extent
3097 updateExtent( extent.toRectangle() );
3098 }
3099 else
3100 {
3101 QgsDebugMsgLevel( QStringLiteral( "Unable to update extent with empty parameter" ), 1 );
3102 }
3103 }
3104 else
3105 {
3106 mExtent3D = extent;
3107
3108 // do not update the wgs84 extent if we trust layer metadata
3110 return;
3111
3112 mWgs84Extent = wgs84Extent( true );
3113 }
3114}
3115
3116bool QgsMapLayer::rebuildCrs3D( QString *error )
3117{
3118 bool res = true;
3119 if ( !mCRS.isValid() )
3120 {
3122 }
3123 else if ( !mVerticalCrs.isValid() )
3124 {
3125 mCrs3D = mCRS;
3126 }
3127 else
3128 {
3129 switch ( mCRS.type() )
3130 {
3134 mCrs3D = mCRS;
3135 break;
3136
3138 {
3139 QString tempError;
3140 mCrs3D = mCRS.hasVerticalAxis() ? mCRS : QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3141 res = mCrs3D.isValid();
3142 break;
3143 }
3144
3146 // nonsense situation
3148 res = false;
3149 break;
3150
3159 {
3160 QString tempError;
3161 mCrs3D = QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3162 res = mCrs3D.isValid();
3163 break;
3164 }
3165 }
3166 }
3167 return res;
3168}
3169
3171{
3173
3174 // do not update the wgs84 extent if we trust layer metadata
3176 return;
3177
3178 mWgs84Extent = QgsRectangle();
3179}
3180
3182{
3184
3185 QString metadata = QStringLiteral( "<h1>" ) + tr( "General" ) + QStringLiteral( "</h1>\n<hr>\n" ) + QStringLiteral( "<table class=\"list-view\">\n" );
3186
3187 // name
3188 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + name() + QStringLiteral( "</td></tr>\n" );
3189
3190 const QString lPublicSource = publicSource();
3191
3192 QString path;
3193 bool isLocalPath = false;
3194 if ( dataProvider() )
3195 {
3196 // local path
3197 QVariantMap uriComponents = QgsProviderRegistry::instance()->decodeUri( dataProvider()->name(), lPublicSource );
3198 if ( uriComponents.contains( QStringLiteral( "path" ) ) )
3199 {
3200 path = uriComponents[QStringLiteral( "path" )].toString();
3201 QFileInfo fi( path );
3202 if ( fi.exists() )
3203 {
3204 isLocalPath = true;
3205 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Path" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( path ).toString(), QDir::toNativeSeparators( path ) ) ) + QStringLiteral( "</td></tr>\n" );
3206
3207 QDateTime lastModified = fi.lastModified();
3208 QString lastModifiedFileName;
3209 QSet<QString> sidecarFiles = QgsFileUtils::sidecarFilesForPath( path );
3210 if ( fi.isFile() )
3211 {
3212 qint64 fileSize = fi.size();
3213 if ( !sidecarFiles.isEmpty() )
3214 {
3215 lastModifiedFileName = fi.fileName();
3216 QStringList sidecarFileNames;
3217 for ( const QString &sidecarFile : sidecarFiles )
3218 {
3219 QFileInfo sidecarFi( sidecarFile );
3220 fileSize += sidecarFi.size();
3221 if ( sidecarFi.lastModified() > lastModified )
3222 {
3223 lastModified = sidecarFi.lastModified();
3224 lastModifiedFileName = sidecarFi.fileName();
3225 }
3226 sidecarFileNames << sidecarFi.fileName();
3227 }
3228 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + ( sidecarFiles.size() > 1 ? tr( "Sidecar files" ) : tr( "Sidecar file" ) ) + QStringLiteral( "</td><td>%1" ).arg( sidecarFileNames.join( QLatin1String( ", " ) ) ) + QStringLiteral( "</td></tr>\n" );
3229 }
3230 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + ( !sidecarFiles.isEmpty() ? tr( "Total size" ) : tr( "Size" ) ) + QStringLiteral( "</td><td>%1" ).arg( QgsFileUtils::representFileSize( fileSize ) ) + QStringLiteral( "</td></tr>\n" );
3231 }
3232 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Last modified" ) + QStringLiteral( "</td><td>%1" ).arg( QLocale().toString( fi.lastModified() ) ) + ( !lastModifiedFileName.isEmpty() ? QStringLiteral( " (%1)" ).arg( lastModifiedFileName ) : QString() ) + QStringLiteral( "</td></tr>\n" );
3233 }
3234 }
3235 if ( uriComponents.contains( QStringLiteral( "url" ) ) )
3236 {
3237 const QString url = uriComponents[QStringLiteral( "url" )].toString();
3238 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "URL" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl( url ).toString(), url ) ) + QStringLiteral( "</td></tr>\n" );
3239 }
3240 }
3241
3242 // data source
3243 if ( lPublicSource != path || !isLocalPath )
3244 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Source" ) + QStringLiteral( "</td><td>%1" ).arg( lPublicSource != path ? lPublicSource : path ) + QStringLiteral( "</td></tr>\n" );
3245
3246 // provider
3247 if ( dataProvider() )
3248 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Provider" ) + QStringLiteral( "</td><td>%1" ).arg( dataProvider()->name() ) + QStringLiteral( "</td></tr>\n" );
3249
3250 // Layer ID
3251 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Layer ID" ) + QStringLiteral( "</td><td>%1" ).arg( id() ) + QStringLiteral( "</td></tr>\n" );
3252
3253 metadata += QLatin1String( "</table>\n<br><br>" );
3254
3255 return metadata;
3256}
3257
3259{
3260 QString metadata;
3261 // custom properties
3262 if ( const auto keys = customPropertyKeys(); !keys.isEmpty() )
3263 {
3264 metadata += QStringLiteral( "<h1>" ) + tr( "Custom properties" ) + QStringLiteral( "</h1>\n<hr>\n" );
3265 metadata += QLatin1String( "<table class=\"list-view\">\n<tbody>" );
3266 for ( const QString &key : keys )
3267 {
3268 // keys prefaced with _ are considered private/internal details
3269 if ( key.startsWith( '_' ) )
3270 continue;
3271
3272 const QVariant propValue = customProperty( key );
3273 QString stringValue;
3274 if ( propValue.type() == QVariant::List || propValue.type() == QVariant::StringList )
3275 {
3276 for ( const QString &s : propValue.toStringList() )
3277 {
3278 stringValue += "<p style=\"margin: 0;\">" + s.toHtmlEscaped() + "</p>";
3279 }
3280 }
3281 else
3282 {
3283 stringValue = propValue.toString().toHtmlEscaped();
3284
3285 //if the result string is empty but propValue is not, the conversion has failed
3286 if ( stringValue.isEmpty() && !QgsVariantUtils::isNull( propValue ) )
3287 stringValue = tr( "<i>value cannot be displayed</i>" );
3288 }
3289
3290 metadata += QStringLiteral( "<tr><td class=\"highlight\">%1</td><td>%2</td></tr>" ).arg( key.toHtmlEscaped(), stringValue );
3291 }
3292 metadata += QLatin1String( "</tbody></table>\n" );
3293 metadata += QLatin1String( "<br><br>\n" );
3294 }
3295 return metadata;
3296}
3297
3299{
3301 QString metadata;
3302
3303 auto addCrsInfo = [&metadata]( const QgsCoordinateReferenceSystem & c, bool includeType, bool includeOperation, bool includeCelestialBody )
3304 {
3305 if ( !c.isValid() )
3306 metadata += QStringLiteral( "<tr><td colspan=\"2\" class=\"highlight\">" ) + tr( "Unknown" ) + QStringLiteral( "</td></tr>\n" );
3307 else
3308 {
3309 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + c.userFriendlyIdentifier( Qgis::CrsIdentifierType::FullString ) + QStringLiteral( "</td></tr>\n" );
3310
3311 // map units
3312 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Units" ) + QStringLiteral( "</td><td>" )
3313 + ( c.isGeographic() ? tr( "Geographic (uses latitude and longitude for coordinates)" ) : QgsUnitTypes::toString( c.mapUnits() ) )
3314 + QStringLiteral( "</td></tr>\n" );
3315
3316 if ( includeType )
3317 {
3318 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Type" ) + QStringLiteral( "</td><td>" ) + QgsCoordinateReferenceSystemUtils::crsTypeToString( c.type() ) + QStringLiteral( "</td></tr>\n" );
3319 }
3320
3321 if ( includeOperation )
3322 {
3323 // operation
3324 const QgsProjOperation operation = c.operation();
3325 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Method" ) + QStringLiteral( "</td><td>" ) + operation.description() + QStringLiteral( "</td></tr>\n" );
3326 }
3327
3328 if ( includeCelestialBody )
3329 {
3330 // celestial body
3331 try
3332 {
3333 const QString celestialBody = c.celestialBodyName();
3334 if ( !celestialBody.isEmpty() )
3335 {
3336 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Celestial Body" ) + QStringLiteral( "</td><td>" ) + celestialBody + QStringLiteral( "</td></tr>\n" );
3337 }
3338 }
3339 catch ( QgsNotSupportedException & )
3340 {
3341
3342 }
3343 }
3344
3345 QString accuracyString;
3346 // dynamic crs with no epoch?
3347 if ( c.isDynamic() && std::isnan( c.coordinateEpoch() ) )
3348 {
3349 accuracyString = tr( "Based on a dynamic CRS, but no coordinate epoch is set. Coordinates are ambiguous and of limited accuracy." );
3350 }
3351
3352 // based on datum ensemble?
3353 try
3354 {
3355 const QgsDatumEnsemble ensemble = c.datumEnsemble();
3356 if ( ensemble.isValid() )
3357 {
3358 QString id;
3359 if ( !ensemble.code().isEmpty() )
3360 id = QStringLiteral( "<i>%1</i> (%2:%3)" ).arg( ensemble.name(), ensemble.authority(), ensemble.code() );
3361 else
3362 id = QStringLiteral( "<i>%1</i>”" ).arg( ensemble.name() );
3363
3364 if ( ensemble.accuracy() > 0 )
3365 {
3366 accuracyString = tr( "Based on %1, which has a limited accuracy of <b>at best %2 meters</b>." ).arg( id ).arg( ensemble.accuracy() );
3367 }
3368 else
3369 {
3370 accuracyString = tr( "Based on %1, which has a limited accuracy." ).arg( id );
3371 }
3372 }
3373 }
3374 catch ( QgsNotSupportedException & )
3375 {
3376
3377 }
3378
3379 if ( !accuracyString.isEmpty() )
3380 {
3381 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Accuracy" ) + QStringLiteral( "</td><td>" ) + accuracyString + QStringLiteral( "</td></tr>\n" );
3382 }
3383
3384 // static/dynamic
3385 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Reference" ) + QStringLiteral( "</td><td>%1</td></tr>\n" ).arg( c.isDynamic() ? tr( "Dynamic (relies on a datum which is not plate-fixed)" ) : tr( "Static (relies on a datum which is plate-fixed)" ) );
3386
3387 // coordinate epoch
3388 if ( !std::isnan( c.coordinateEpoch() ) )
3389 {
3390 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Coordinate Epoch" ) + QStringLiteral( "</td><td>%1</td></tr>\n" ).arg( qgsDoubleToString( c.coordinateEpoch(), 3 ) );
3391 }
3392 }
3393 };
3394
3395 metadata += QStringLiteral( "<h1>" ) + tr( "Coordinate Reference System (CRS)" ) + QStringLiteral( "</h1>\n<hr>\n" );
3396 metadata += QLatin1String( "<table class=\"list-view\">\n" );
3397 addCrsInfo( crs().horizontalCrs(), true, true, true );
3398 metadata += QLatin1String( "</table>\n<br><br>\n" );
3399
3400 if ( verticalCrs().isValid() )
3401 {
3402 metadata += QStringLiteral( "<h1>" ) + tr( "Vertical Coordinate Reference System (CRS)" ) + QStringLiteral( "</h1>\n<hr>\n" );
3403 metadata += QLatin1String( "<table class=\"list-view\">\n" );
3404 addCrsInfo( verticalCrs(), false, false, false );
3405 metadata += QLatin1String( "</table>\n<br><br>\n" );
3406 }
3407
3408 return metadata;
3409}
static QString version()
Version string.
Definition qgis.cpp:362
@ FullString
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions.
@ Vertical
Vertical CRS.
@ Temporal
Temporal CRS.
@ Compound
Compound (horizontal + vertical) CRS.
@ Projected
Projected CRS.
@ Other
Other type.
@ Bound
Bound CRS.
@ DerivedProjected
Derived projected CRS.
@ Unknown
Unknown type.
@ Engineering
Engineering CRS.
@ Geographic3d
3D geopraphic CRS
@ Geodetic
Geodetic CRS.
@ Geographic2d
2D geographic CRS
@ Geocentric
Geocentric CRS.
@ RemoveCredentials
Completely remove credentials (eg passwords) from the URI. This flag is not compatible with the Redac...
@ RedactCredentials
Replace the value of credentials (eg passwords) with 'xxxxxxxx'. This flag is not compatible with the...
@ ForceFirstLetterToCapital
Convert just the first letter of each word to uppercase, leave the rest untouched.
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
@ Unknown
Unknown types.
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
Definition qgis.h:450
LayerType
Types of layers that can be added to a map.
Definition qgis.h:169
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
QFlags< MapLayerProperty > MapLayerProperties
Map layer properties.
Definition qgis.h:2235
QFlags< LoadStyleFlag > LoadStyleFlags
Flags for loading layer styles.
Definition qgis.h:225
@ LoadDefaultStyle
Reset the layer's style to the default for the datasource.
@ ForceReadOnly
Open layer in a read-only mode.
@ SkipGetExtent
Skip the extent from provider.
@ TrustDataSource
Trust datasource config (primary key unicity, geometry type and srid, etc). Improves provider load ti...
@ IgnoreMissingStyleErrors
If the style is missing, then don't flag it as an error. This flag can be used when the caller is not...
AutoRefreshMode
Map layer automatic refresh modes.
Definition qgis.h:2245
@ RedrawOnly
Redraw current data only.
@ ReloadData
Reload data (and draw the new data)
@ Disabled
Automatic refreshing is disabled.
Base metadata class for 3D renderers.
virtual QgsAbstract3DRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context)=0
Returns new instance of the renderer given the DOM element.
Qgs3DRendererAbstractMetadata * rendererMetadata(const QString &type) const
Returns metadata for a 3D renderer type (may be used to create a new instance of the type)
Base class for all renderers that participate in 3D views.
static QString pkgDataPath()
Returns the common root path of all application data directories.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static Qgs3DRendererRegistry * renderer3DRegistry()
Returns registry of available 3D renderers.
bool setMasterPassword(bool verify=false)
Main call to initially set or continually check master password is set.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
QgsRectangle toRectangle() const
Converts the box to a 2D rectangle.
Definition qgsbox3d.h:379
bool isNull() const
Test if the box is null (holding no spatial information).
Definition qgsbox3d.cpp:310
static QString crsTypeToString(Qgis::CrsType type)
Returns a translated string representing a CRS type.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool hasVerticalAxis() const
Returns true if the CRS has a vertical axis.
void validate()
Perform some validation on this CRS.
static CUSTOM_CRS_VALIDATION customCrsValidation()
Gets custom function.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
static QgsCoordinateReferenceSystem createCompoundCrs(const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error)
Given a horizontal and vertical CRS, attempts to create a compound CRS from them.
static void setCustomCrsValidation(CUSTOM_CRS_VALIDATION f)
Sets custom function to force valid CRS.
void setValidationHint(const QString &html)
Set user hint for validation.
QgsCoordinateReferenceSystem verticalCrs() const
Returns the vertical CRS associated with this CRS object.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
Qgis::CrsType type() const
Returns the type of the CRS.
Contains information about the context in which a coordinate transform is executed.
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
virtual bool containsElevationData() const
Returns true if the data provider definitely contains elevation related data.
Abstract base class for spatial data provider implementations.
void notify(const QString &msg)
Emitted when the datasource issues a notification.
virtual QgsDataProviderElevationProperties * elevationProperties()
Returns the provider's elevation properties.
static QString removePassword(const QString &aUri, bool hide=false)
Removes the password element from a URI.
Contains information about a datum ensemble.
Definition qgsdatums.h:95
QString code() const
Identification code, e.g.
Definition qgsdatums.h:122
QString authority() const
Authority name, e.g.
Definition qgsdatums.h:117
bool isValid() const
Returns true if the datum ensemble is a valid object, or false if it is a null/invalid object.
Definition qgsdatums.h:102
QString name() const
Display name of datum ensemble.
Definition qgsdatums.h:107
double accuracy() const
Positional accuracy (in meters).
Definition qgsdatums.h:112
A container for error messages.
Definition qgserror.h:81
QString what() const
static QSet< QString > sidecarFilesForPath(const QString &path)
Returns a list of the sidecar files which exist for the dataset a the specified path.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
A structured metadata store for a map layer.
bool readMetadataXml(const QDomElement &metadataElement) override
Sets state from DOM document.
bool writeMetadataXml(QDomElement &metadataElement, QDomDocument &document) const override
Stores state in a DOM node.
static void setLayerNotes(QgsMapLayer *layer, const QString &notes)
Sets the notes for the specified layer, where notes is a HTML formatted string.
static bool layerHasNotes(const QgsMapLayer *layer)
Returns true if the specified layer has notes available.
static QString layerNotes(const QgsMapLayer *layer)
Returns the notes for the specified layer.
Models dependencies with or between map layers.
Base class for storage of map layer elevation properties.
An abstract interface for implementations of legends for one map layer.
void itemsChanged()
Emitted when existing items/nodes got invalid and should be replaced by new ones.
Manages QGIS Server properties for a map layer.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
void copyTo(QgsMapLayerServerProperties *properties) const
Copy properties to another instance.
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Management of styles for use with one map layer.
bool addStyle(const QString &name, const QgsMapLayerStyle &style)
Add a style with given name and data.
QStringList styles() const
Returns list of all defined style names.
bool renameStyle(const QString &name, const QString &newName)
Rename a stored style to a different name.
QgsMapLayerStyle style(const QString &name) const
Returns data of a stored style - accessed by its unique name.
Base class for storage of map layer temporal properties.
Base class for all map layer types.
Definition qgsmaplayer.h:77
void crs3DChanged()
Emitted when the crs3D() of the layer has changed.
Q_DECL_DEPRECATED void setShortName(const QString &shortName)
Sets the short name of the layer used by QGIS Server to identify the layer.
virtual bool deleteStyleFromDatabase(const QString &styleId, QString &msgError)
Deletes a style from the database.
bool importNamedMetadata(QDomDocument &document, QString &errorMessage)
Import the metadata of this layer from a QDomDocument.
QString name
Definition qgsmaplayer.h:81
void readStyleManager(const QDomNode &layerNode)
Read style manager's configuration (if any). To be called by subclasses.
virtual bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const =0
Write the style for the layer into the document provided.
Q_DECL_DEPRECATED QString legendUrlFormat() const
Returns the format for a URL based layer legend.
QgsRectangle wgs84Extent(bool forceRecalculate=false) const
Returns the WGS84 extent (EPSG:4326) of the layer according to ReadFlag::FlagTrustLayerMetadata.
void setRefreshOnNotifyEnabled(bool enabled)
Set whether provider notification is connected to triggerRepaint.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QgsAbstract3DRenderer * renderer3D() const
Returns 3D renderer associated with the layer.
virtual bool isTemporary() const
Returns true if the layer is considered a temporary layer.
virtual Q_DECL_DEPRECATED void exportSldStyleV2(QDomDocument &doc, QString &errorMsg, QgsSldExportContext &exportContext) const
Export the properties of this layer as SLD style in a QDomDocument.
virtual void exportNamedStyle(QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context=QgsReadWriteContext(), QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) const
Export the properties of this layer as named style in a QDomDocument.
bool setId(const QString &id)
Sets the layer's id.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the layer.
void dependenciesChanged()
Emitted when dependencies are changed.
virtual bool hasMapTips() const
Returns true if the layer contains map tips.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
void legendChanged()
Signal emitted when legend of the layer has changed.
void writeStyleManager(QDomNode &layerNode, QDomDocument &doc) const
Write style manager's configuration (if exists). To be called by subclasses.
QgsMapLayerLegend * legend() const
Can be nullptr.
QFlags< ReadFlag > ReadFlags
QFlags< LayerFlag > LayerFlags
virtual bool importNamedStyle(QDomDocument &doc, QString &errorMsg, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Import the properties of this layer from a QDomDocument.
Q_DECL_DEPRECATED void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
void metadataChanged()
Emitted when the layer's metadata is changed.
virtual QgsRectangle extent() const
Returns the extent of the layer.
virtual QString saveSldStyle(const QString &uri, bool &resultFlag) const
Saves the properties of this layer to an SLD format file.
QString source() const
Returns the source for the layer.
Q_DECL_DEPRECATED void setLegendUrl(const QString &legendUrl)
Sets the URL for the layer's legend.
virtual bool setDependencies(const QSet< QgsMapLayerDependency > &layers)
Sets the list of dependencies.
void request3DUpdate()
Signal emitted when a layer requires an update in any 3D maps.
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QgsError mError
Error.
int mBlockStyleChangedSignal
If non-zero, the styleChanged signal should not be emitted.
QString providerType() const
Returns the provider type (provider key) for this layer.
virtual void setExtent3D(const QgsBox3D &box)
Sets the extent.
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
Qgis::AutoRefreshMode autoRefreshMode() const
Returns the layer's automatic refresh mode.
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
void configChanged()
Emitted whenever the configuration is changed.
void trigger3DUpdate()
Will advise any 3D maps that this layer requires to be updated in the scene.
void autoRefreshIntervalChanged(int interval)
Emitted when the auto refresh interval changes.
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
virtual QSet< QgsMapLayerDependency > dependencies() const
Gets the list of dependencies.
void setCustomProperties(const QgsObjectCustomProperties &properties)
Set custom properties for layer.
static Qgis::DataProviderReadFlags providerReadFlags(const QDomNode &layerNode, QgsMapLayer::ReadFlags layerReadFlags)
Returns provider read flag deduced from layer read flags layerReadFlags and a dom node layerNode that...
virtual QString loadNamedStyle(const QString &theURI, bool &resultFlag, bool loadFromLocalDb, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories, Qgis::LoadStyleFlags flags=Qgis::LoadStyleFlags())
Loads a named style from file/local db/datasource db.
virtual QString encodedSource(const QString &source, const QgsReadWriteContext &context) const
Called by writeLayerXML(), used by derived classes to encode provider's specific data source to proje...
QgsMapLayer::LayerFlags flags() const
Returns the flags for this layer.
QgsCoordinateReferenceSystem crs3D
Definition qgsmaplayer.h:86
virtual void setSubLayerVisibility(const QString &name, bool visible)
Set the visibility of the given sublayer name.
void isValidChanged()
Emitted when the validity of this layer changed.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:84
bool loadNamedMetadataFromDatabase(const QString &db, const QString &uri, QString &qmd)
Retrieve a named metadata for this layer from a sqlite database.
virtual bool readXml(const QDomNode &layer_node, QgsReadWriteContext &context)
Called by readLayerXML(), used by children to read state specific to them from project files.
QgsMapLayerServerProperties * serverProperties()
Returns QGIS Server Properties for the map layer.
Q_DECL_DEPRECATED QString attribution() const
Returns the attribution of the layer used by QGIS Server in GetCapabilities request.
void setOriginalXmlProperties(const QString &originalXmlProperties)
Sets the original XML properties for the layer to originalXmlProperties.
void writeCustomProperties(QDomNode &layerNode, QDomDocument &doc) const
Write custom properties to project file.
QString mRefreshOnNofifyMessage
virtual int listStylesInDatabase(QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError)
Lists all the style in db split into related to the layer and not related to.
virtual QString loadDefaultStyle(bool &resultFlag)
Retrieve the default style for this layer if one exists (either as a .qml file on disk or as a record...
void setDataSource(const QString &dataSource, const QString &baseName=QString(), const QString &provider=QString(), bool loadDefaultStyleFlag=false)
Updates the data source of the layer.
QString mLayerName
Name of the layer - used for display.
virtual QString loadNamedMetadata(const QString &uri, bool &resultFlag)
Retrieve a named metadata for this layer if one exists (either as a .qmd file on disk or as a record ...
virtual bool writeXml(QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context) const
Called by writeLayerXML(), used by children to write state specific to them to project files.
Q_DECL_DEPRECATED bool hasAutoRefreshEnabled() const
Returns true if auto refresh is enabled for the layer.
QString id
Definition qgsmaplayer.h:80
void mapTipTemplateChanged()
Emitted when the map tip template changes.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
QString crsHtmlMetadata() const
Returns a HTML fragment containing the layer's CRS metadata, for use in the htmlMetadata() method.
Q_DECL_DEPRECATED void setAttributionUrl(const QString &attribUrl)
Sets the attribution URL of the layer used by QGIS Server in GetCapabilities request.
Q_DECL_DEPRECATED void setAutoRefreshEnabled(bool enabled)
Sets whether auto refresh is enabled for the layer.
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
QgsLayerMetadata metadata
Definition qgsmaplayer.h:83
static QString formatLayerName(const QString &name)
A convenience function to capitalize and format a layer name.
void renderer3DChanged()
Signal emitted when 3D renderer associated with the layer has changed.
Q_DECL_DEPRECATED QString abstract() const
Returns the abstract of the layer used by QGIS Server in GetCapabilities request.
QgsMapLayer(Qgis::LayerType type=Qgis::LayerType::Vector, const QString &name=QString(), const QString &source=QString())
Constructor for QgsMapLayer.
QString originalXmlProperties() const
Returns the XML properties of the original layer as they were when the layer was first read from the ...
Qgis::LayerType type
Definition qgsmaplayer.h:87
Q_DECL_DEPRECATED QString dataUrlFormat() const
Returns the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
Q_DECL_DEPRECATED void setDataUrl(const QString &dataUrl)
Sets the DataUrl of the layer used by QGIS Server in GetCapabilities request.
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
Q_DECL_DEPRECATED void setKeywordList(const QString &keywords)
Sets the keyword list of the layerused by QGIS Server in GetCapabilities request.
Q_DECL_DEPRECATED void setAttribution(const QString &attrib)
Sets the attribution of the layerused by QGIS Server in GetCapabilities request.
void setFlags(QgsMapLayer::LayerFlags flags)
Returns the flags for this layer.
bool isRefreshOnNotifyEnabled() const
Returns true if the refresh on provider nofification is enabled.
QString publicSource(bool hidePassword=false) const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
Q_DECL_DEPRECATED QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
QSet< QgsMapLayerDependency > mDependencies
List of layers that may modify this layer on modification.
void readCustomProperties(const QDomNode &layerNode, const QString &keyStartsWith=QString())
Read custom properties from project file.
virtual Qgis::MapLayerProperties properties() const
Returns the map layer properties of this layer.
virtual QString loadSldStyle(const QString &uri, bool &resultFlag)
Attempts to style the layer using the formatting from an SLD type file.
virtual void setMetadata(const QgsLayerMetadata &metadata)
Sets the layer's metadata store.
virtual bool readStyle(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read the style for the current layer from the DOM node supplied.
virtual QString saveDefaultMetadata(bool &resultFlag)
Save the current metadata of this layer as the default metadata (either as a .qmd file on disk or as ...
virtual bool supportsEditing() const
Returns whether the layer supports editing or not.
Q_DECL_DEPRECATED void setDataUrlFormat(const QString &dataUrlFormat)
Sets the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
QFlags< StyleCategory > StyleCategories
virtual void saveStyleToDatabase(const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Saves named and sld style of the layer to the style table in the db.
Q_INVOKABLE void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
QString mProviderKey
Data provider key (name of the data provider)
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
void styleChanged()
Signal emitted whenever a change affects the layer's style.
virtual bool isEditable() const
Returns true if the layer can be edited.
QUndoStack * undoStack()
Returns pointer to layer's undo stack.
std::unique_ptr< QgsDataProvider > mPreloadedProvider
Optionally used when loading a project, it is released when the layer is effectively created.
Q_DECL_DEPRECATED QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
void crsChanged()
Emitted when the crs() of the layer has changed.
virtual QgsError error() const
Gets current status error.
bool writeLayerXml(QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores state in DOM node.
virtual QString styleURI() const
Retrieve the style URI for this layer (either as a .qml file on disk or as a record in the users styl...
void setScaleBasedVisibility(bool enabled)
Sets whether scale based visibility is enabled for the layer.
void dataSourceChanged()
Emitted whenever the layer's data source has been changed.
void idChanged(const QString &id)
Emitted when the layer's ID has been changed.
Q_DECL_DEPRECATED QString dataUrl() const
Returns the DataUrl of the layer used by QGIS Server in GetCapabilities request.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
Q_DECL_DEPRECATED QString metadataUrlFormat() const
Returns the metadata format of the layer used by QGIS Server in GetCapabilities request.
void setRefreshOnNofifyMessage(const QString &message)
Set the notification message that triggers repaint If refresh on notification is enabled,...
static QString generateId(const QString &layerName)
Generates an unique identifier for this layer, the generate ID is prefixed by layerName.
QgsProviderMetadata * providerMetadata() const
Returns the layer data provider's metadata, it may be nullptr.
void opacityChanged(double opacity)
Emitted when the layer's opacity is changed, where opacity is a value between 0 (transparent) and 1 (...
virtual bool isModified() const
Returns true if the layer has been modified since last commit/save.
void styleLoaded(QgsMapLayer::StyleCategories categories)
Emitted when a style has been loaded.
virtual QString getStyleFromDatabase(const QString &styleId, QString &msgError)
Returns the named style corresponding to style id provided.
void emitStyleChanged()
Triggers an emission of the styleChanged() signal.
virtual QgsMapLayerTemporalProperties * temporalProperties()
Returns the layer's temporal properties.
QUndoStack * undoStackStyles()
Returns pointer to layer's style undo stack.
void dataChanged()
Data of layer changed.
virtual QStringList subLayers() const
Returns the sublayers of this layer.
virtual QString htmlMetadata() const
Obtain a formatted HTML string containing assorted metadata for this layer.
Q_DECL_DEPRECATED void setMetadataUrlFormat(const QString &metaUrlFormat)
Sets the metadata format of the layer used by QGIS Server in GetCapabilities request.
virtual bool loadNamedStyleFromDatabase(const QString &db, const QString &uri, QString &qml)
Retrieve a named style for this layer from a sqlite database.
void verticalCrsChanged()
Emitted when the verticalCrs() of the layer has changed.
virtual QgsBox3D extent3D() const
Returns the 3D extent of the layer.
static QString extensionPropertyType(PropertyType type)
Returns the extension of a Property.
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
void blendModeChanged(QPainter::CompositionMode blendMode)
Signal emitted when the blend mode is changed, through QgsMapLayer::setBlendMode()
void setName(const QString &name)
Set the display name of the layer.
void setAutoRefreshInterval(int interval)
Sets the auto refresh interval (in milliseconds) for the layer.
virtual bool readSymbology(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)=0
Read the symbology for the current layer from the DOM node supplied.
Q_DECL_DEPRECATED QString metadataUrl() const
Returns the metadata URL of the layer used by QGIS Server in GetCapabilities request.
virtual void setExtent(const QgsRectangle &rect)
Sets the extent.
virtual void resolveReferences(QgsProject *project)
Resolve references to other layers (kept as layer IDs after reading XML) into layer objects.
QString saveNamedMetadata(const QString &uri, bool &resultFlag)
Save the current metadata of this layer as a named metadata (either as a .qmd file on disk or as a re...
QString mDataSource
Data source description string, varies by layer type.
void setAutoRefreshMode(Qgis::AutoRefreshMode mode)
Sets the automatic refresh mode for the layer.
QString refreshOnNotifyMessage() const
Returns the message that should be notified by the provider to triggerRepaint.
virtual bool readSld(const QDomNode &node, QString &errorMessage)
void setMapTipsEnabled(bool enabled)
Enable or disable map tips for this layer.
virtual QString loadDefaultMetadata(bool &resultFlag)
Retrieve the default metadata for this layer if one exists (either as a .qmd file on disk or as a rec...
virtual QString saveSldStyleV2(bool &resultFlag, QgsSldExportContext &exportContext) const
Saves the properties of this layer to an SLD format file.
@ FlagReadExtentFromXml
Read extent from xml and skip get extent from provider.
@ FlagTrustLayerMetadata
Trust layer metadata. Improves layer load time by skipping expensive checks like primary key unicity,...
@ FlagForceReadOnly
Force open as read only.
void setValid(bool valid)
Sets whether layer is valid or not.
Q_DECL_DEPRECATED QString attributionUrl() const
Returns the attribution URL of the layer used by QGIS Server in GetCapabilities request.
void readCommonStyle(const QDomElement &layerElement, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read style data common to all layer types.
void customPropertyChanged(const QString &key)
Emitted when a custom property of the layer has been changed or removed.
virtual QDomDocument exportSldStyleV3(QgsSldExportContext &exportContext) const
Export the properties of this layer as SLD style in a QDomDocument.
QgsMapLayer::ReadFlags mReadFlags
Read flags. It's up to the subclass to respect these when restoring state from XML.
double minimumScale() const
Returns the minimum map scale (i.e.
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
Q_DECL_DEPRECATED QString legendUrl() const
Returns the URL for the layer's legend.
void flagsChanged()
Emitted when layer's flags have been modified.
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
Q_DECL_DEPRECATED void setLegendUrlFormat(const QString &legendUrlFormat)
Sets the format for a URL based layer legend.
void exportNamedMetadata(QDomDocument &doc, QString &errorMsg) const
Export the current metadata of this layer as named metadata in a QDomDocument.
virtual QString saveNamedStyle(const QString &uri, bool &resultFlag, StyleCategories categories=AllStyleCategories)
Save the properties of this layer as a named style (either as a .qml file on disk or as a record in t...
virtual Q_DECL_DEPRECATED void exportSldStyle(QDomDocument &doc, QString &errorMsg) const
Export the properties of this layer as SLD style in a QDomDocument.
void beforeResolveReferences(QgsProject *project)
Emitted when all layers are loaded and references can be resolved, just before the references of this...
void setMapTipTemplate(const QString &mapTipTemplate)
The mapTip is a pretty, html representation for feature information.
Q_DECL_DEPRECATED void setMetadataUrl(const QString &metaUrl)
Sets the metadata URL of the layer used by QGIS Server in GetCapabilities request.
virtual QgsMapLayerElevationProperties * elevationProperties()
Returns the layer's elevation properties.
bool setVerticalCrs(const QgsCoordinateReferenceSystem &crs, QString *errorMessage=nullptr)
Sets the layer's vertical coordinate reference system.
Q_INVOKABLE QStringList customPropertyKeys() const
Returns list of all keys within custom properties.
QgsProject * project() const
Returns the parent project if this map layer is added to a project.
Q_DECL_DEPRECATED void setMetadataUrlType(const QString &metaUrlType)
Set the metadata type of the layer used by QGIS Server in GetCapabilities request MetadataUrlType ind...
bool mapTipsEnabled
Definition qgsmaplayer.h:91
bool readLayerXml(const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags=QgsMapLayer::ReadFlags(), QgsDataProvider *preloadedProvider=nullptr)
Sets state from DOM document.
void setLegend(QgsMapLayerLegend *legend)
Assign a legend controller to the map layer.
double opacity
Definition qgsmaplayer.h:89
virtual QString decodedSource(const QString &source, const QString &dataProvider, const QgsReadWriteContext &context) const
Called by readLayerXML(), used by derived classes to decode provider's specific data source from proj...
void nameChanged()
Emitted when the name has been changed.
virtual QString metadataUri() const
Retrieve the metadata URI for this layer (either as a .qmd file on disk or as a record in the users s...
int autoRefreshInterval
Definition qgsmaplayer.h:82
QgsCoordinateReferenceSystem verticalCrs
Definition qgsmaplayer.h:85
virtual bool writeStyle(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write just the symbology information for the layer into the document.
bool mIsRefreshOnNofifyEnabled
virtual Q_INVOKABLE QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
double mLayerOpacity
Layer opacity.
bool mValid
Indicates if the layer is valid and can be drawn.
@ LayerConfiguration
General configuration: identifiable, removable, searchable, display expression, read-only.
@ Symbology
Symbology.
@ Notes
Layer user notes.
@ Temporal
Temporal properties.
@ Rendering
Rendering: scale visibility, simplify method, opacity.
@ Elevation
Elevation settings.
@ Symbology3D
3D symbology
@ CustomProperties
Custom properties (by plugins for instance)
virtual Q_INVOKABLE void reload()
Synchronises with changes in the datasource.
virtual QDateTime timestamp() const
Time stamp of data source in the moment when data/metadata were loaded by provider.
void setProviderType(const QString &providerType)
Sets the providerType (provider key)
void mapTipsEnabledChanged()
Emitted when map tips are enabled or disabled for the layer.
virtual QString saveDefaultStyle(bool &resultFlag, StyleCategories categories)
Save the properties of this layer as the default style (either as a .qml file on disk or as a record ...
void setRenderer3D(QgsAbstract3DRenderer *renderer)
Sets 3D renderer for the layer.
~QgsMapLayer() override
QString customPropertyHtmlMetadata() const
Returns an HTML fragment containing custom property information, for use in the htmlMetadata() method...
const QgsObjectCustomProperties & customProperties() const
Read all custom properties from layer.
QString generalHtmlMetadata() const
Returns an HTML fragment containing general metadata information, for use in the htmlMetadata() metho...
Q_DECL_DEPRECATED QString metadataUrlType() const
Returns the metadata type of the layer used by QGIS Server in GetCapabilities request.
void writeCommonStyle(QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write style data common to all layer types.
double maximumScale() const
Returns the maximum map scale (i.e.
Q_DECL_DEPRECATED QString keywordList() const
Returns the keyword list of the layer used by QGIS Server in GetCapabilities request.
virtual void setLayerOrder(const QStringList &layers)
Reorders the previously selected sublayers of this layer from bottom to top.
void invalidateWgs84Extent()
Invalidates the WGS84 extent.
QString mapTipTemplate
Definition qgsmaplayer.h:90
Q_DECL_DEPRECATED void setTitle(const QString &title)
Sets the title of the layer used by QGIS Server in GetCapabilities request.
PropertyType
Maplayer has a style and a metadata property.
bool mShouldValidateCrs
true if the layer's CRS should be validated and invalid CRSes are not permitted.
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Custom exception class which is raised when an operation is not supported.
Simple key-value store (keys = strings, values = variants) that supports loading/saving to/from XML i...
void setValue(const QString &key, const QVariant &value)
Add an entry to the store with the specified key.
QStringList keys() const
Returns a list of all stored keys.
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Writes the store contents to an XML node.
void remove(const QString &key)
Removes a key (entry) from the store.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value for the given key.
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from an XML node.
bool contains(const QString &key) const
Returns true if the properties contains a key with the specified name.
Contains information about a PROJ operation.
QString description() const
Description.
Convert from older project file versions to newer.
bool updateRevision(const QgsProjectVersion &version)
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
Translates a string using the Qt QTranslator mechanism.
Describes the version of a project.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
bool removeAttachedFile(const QString &path)
Removes the attached file.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QString baseName() const
Returns the base name of the project file without the path and without extension - derived from fileN...
QString absoluteFilePath() const
Returns full absolute path to the project file if the project is stored in a file system - derived fr...
Holds data provider key, description, and associated shared library file or function pointer informat...
@ SaveLayerMetadata
Indicates that the provider supports saving native layer metadata.
QString getStyleById(const QString &providerKey, const QString &uri, const QString &styleId, QString &errCause)
Gets a layer style defined by styleId.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
bool saveLayerMetadata(const QString &providerKey, const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage)
Saves metadata to the layer corresponding to the specified uri.
bool deleteStyleById(const QString &providerKey, const QString &uri, const QString &styleId, QString &errCause)
Deletes a layer style defined by styleId.
QString loadStoredStyle(const QString &providerKey, const QString &uri, QString &styleName, QString &errCause)
Loads a layer style from the provider storage, reporting its name.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
int listStyles(const QString &providerKey, const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause)
Lists stored layer styles in the provider defined by providerKey and uri.
bool saveStyle(const QString &providerKey, const QString &uri, const QString &qmlStyle, const QString &sldStyle, const QString &styleName, const QString &styleDescription, const QString &uiFileContent, bool useAsDefault, QString &errCause)
Saves a layer style to provider.
Represents a raster layer.
Q_DECL_DEPRECATED bool writeSld(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props=QVariantMap()) const
Writes the symbology of the layer into the document provided in SLD 1.0.0 format.
Allows entering a context category and takes care of leaving this category on deletion of the class.
A container for the context for various read/write operations on objects.
MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory(const QString &category, const QString &details=QString()) const
Push a category to the stack.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
A rectangle specified with double values.
static bool equalToOrGreaterThanMinimumScale(const double scale, const double minScale)
Returns whether the scale is equal to or greater than the minScale, taking non-round numbers into acc...
static bool lessThanMaximumScale(const double scale, const double maxScale)
Returns whether the scale is less than the maxScale, taking non-round numbers into account.
void setMetadataUrls(const QList< QgsServerMetadataUrlProperties::MetadataUrl > &metaUrls)
Sets a the list of metadata URL for the layer.
QList< QgsServerMetadataUrlProperties::MetadataUrl > metadataUrls() const
Returns a list of metadataUrl resources associated for the layer.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
QString exportFilePath() const
Returns the export file path for the SLD.
QStringList errors() const
Returns a list of errors which occurred during the conversion.
void setExtraProperties(const QVariantMap &properties)
Sets the open ended set of properties that can drive/inform the SLD encoding.
void setExportFilePath(const QString &exportFilePath)
Sets the export file path for the SLD to exportFilePath.
QVariantMap extraProperties() const
Returns the open ended set of properties that can drive/inform the SLD encoding.
void pushError(const QString &error)
Pushes a error message generated during the conversion.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
An interface for classes which can visit style entity (e.g.
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based dataset.
Q_DECL_DEPRECATED bool writeSld(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props=QVariantMap()) const
Writes the symbology of the layer into the document provided in SLD 1.1 format.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
static T readFlagAttribute(const QDomElement &element, const QString &attributeName, T defaultValue)
Read a flag value from an attribute of the element.
static QDomElement writeBox3D(const QgsBox3D &box, QDomDocument &doc, const QString &elementName=QStringLiteral("extent3D"))
Encodes a 3D box to a DOM element.
static QgsBox3D readBox3D(const QDomElement &element)
Decodes a DOM element to a 3D box.
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc, const QString &elementName=QStringLiteral("extent"))
Encodes a rectangle to a DOM element.
static QgsRectangle readRectangle(const QDomElement &element)
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
int open(const QString &path)
Opens the database at the specified file path.
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:6597
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6304
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6578
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6387
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:41
#define QgsDebugError(str)
Definition qgslogger.h:40
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
const QgsCoordinateReferenceSystem & crs
Setting options for creating vector data providers.
QString format
Format specification of online resource.