QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
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 "qgsmaplayer.h"
20
21#include <sqlite3.h>
22
25#include "qgsapplication.h"
26#include "qgsauthmanager.h"
29#include "qgsdatasourceuri.h"
30#include "qgsdatums.h"
31#include "qgsfileutils.h"
32#include "qgslayernotesutils.h"
33#include "qgslogger.h"
35#include "qgsmaplayerlegend.h"
38#include "qgsmessagelog.h"
39#include "qgsobjectvisitor.h"
40#include "qgspathresolver.h"
41#include "qgsproject.h"
43#include "qgsprojoperation.h"
44#include "qgsprovidermetadata.h"
45#include "qgsproviderregistry.h"
46#include "qgsrasterlayer.h"
47#include "qgsreadwritecontext.h"
48#include "qgsrectangle.h"
49#include "qgsscaleutils.h"
50#include "qgssldexportcontext.h"
51#include "qgssqliteutils.h"
52#include "qgsstringutils.h"
53#include "qgsthreadingutils.h"
54#include "qgsunittypes.h"
55#include "qgsvectorlayer.h"
56#include "qgsxmlutils.h"
57
58#include <QDir>
59#include <QDomDocument>
60#include <QDomElement>
61#include <QDomImplementation>
62#include <QDomNode>
63#include <QFile>
64#include <QFileInfo>
65#include <QLocale>
66#include <QRegularExpression>
67#include <QStandardPaths>
68#include <QString>
69#include <QTextStream>
70#include <QTimer>
71#include <QUrl>
72#include <QUuid>
73#include <QXmlStreamReader>
74
75#include "moc_qgsmaplayer.cpp"
76
77using namespace Qt::StringLiterals;
78
80{
81 switch ( type )
82 {
83 case Metadata:
84 return u".qmd"_s;
85
86 case Style:
87 return u".qml"_s;
88 }
89 return QString();
90}
91
92QgsMapLayer::QgsMapLayer( Qgis::LayerType type, const QString &lyrname, const QString &source )
94 , mLayerName( lyrname )
95 , mLayerType( type )
96 , mServerProperties( std::make_unique<QgsMapLayerServerProperties>( this ) )
97 , mUndoStack( new QUndoStack( this ) )
98 , mUndoStackStyles( new QUndoStack( this ) )
99 , mStyleManager( std::make_unique<QgsMapLayerStyleManager>( this ) )
100 , mRefreshTimer( new QTimer( this ) )
101{
102 mID = generateId( lyrname );
103 connect( this, &QgsMapLayer::crsChanged, this, &QgsMapLayer::configChanged );
105 connect( mRefreshTimer, &QTimer::timeout, this, [this] {
106 switch ( mAutoRefreshMode )
107 {
109 break;
111 triggerRepaint( true );
112 break;
114 reload();
115 break;
116 }
117 } );
118}
119
121{
122 if ( project() && project()->pathResolver().writePath( mDataSource ).startsWith( "attachment:" ) )
123 {
125 }
126}
127
128void QgsMapLayer::clone( QgsMapLayer *layer ) const
129{
131
132 QgsDebugMsgLevel( u"Cloning layer '%1'"_s.arg( name() ), 3 );
133 layer->setBlendMode( blendMode() );
134
135 const auto constStyles = styleManager()->styles();
136 for ( const QString &s : constStyles )
137 {
138 layer->styleManager()->addStyle( s, styleManager()->style( s ) );
139 }
140
141 layer->setName( name() );
142
143 if ( layer->dataProvider() && layer->dataProvider()->elevationProperties() )
144 {
146 layer->mExtent3D = mExtent3D;
147 else
148 layer->mExtent2D = mExtent2D;
149 }
150
151 layer->setMaximumScale( maximumScale() );
152 layer->setMinimumScale( minimumScale() );
154 layer->setDependencies( dependencies() );
156 layer->setCrs( crs() );
157 layer->setCustomProperties( mCustomProperties );
158 layer->setOpacity( mLayerOpacity );
159 layer->setMetadata( mMetadata );
160 layer->serverProperties()->copyTo( mServerProperties.get() );
161}
162
164{
165 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
167
168 return mLayerType;
169}
170
177
179{
181
182 if ( flags == mFlags )
183 return;
184
185 mFlags = flags;
186 emit flagsChanged();
187}
188
195
196QString QgsMapLayer::id() const
197{
198 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
200
201 return mID;
202}
203
204bool QgsMapLayer::setId( const QString &id )
205{
207 if ( qobject_cast< QgsMapLayerStore * >( parent() ) )
208 {
209 // layer is already registered, cannot change id
210 return false;
211 }
212
213 if ( id == mID )
214 return false;
215
216 mID = id;
217 emit idChanged( id );
218 return true;
219}
220
221void QgsMapLayer::setName( const QString &name )
222{
224
225 if ( name == mLayerName )
226 return;
227
229
230 emit nameChanged();
231}
232
233QString QgsMapLayer::name() const
234{
235 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
237
238 QgsDebugMsgLevel( "returning name '" + mLayerName + '\'', 4 );
239 return mLayerName;
240}
241
248
250{
252
253 return nullptr;
254}
255
260
262{
264
265 mServerProperties->setShortName( shortName );
266}
267
269{
271
272 return mServerProperties->shortName();
273}
274
275void QgsMapLayer::setTitle( const QString &title )
276{
278
279 mServerProperties->setTitle( title );
280}
281
282QString QgsMapLayer::title() const
283{
285
286 return mServerProperties->title();
287}
288
289void QgsMapLayer::setAbstract( const QString &abstract )
290{
292
293 mServerProperties->setAbstract( abstract );
294}
295
297{
299
300 return mServerProperties->abstract();
301}
302
303void QgsMapLayer::setKeywordList( const QString &keywords )
304{
306
307 mServerProperties->setKeywordList( keywords );
308}
309
311{
313
314 return mServerProperties->keywordList();
315}
316
317void QgsMapLayer::setDataUrl( const QString &dataUrl )
318{
320
321 mServerProperties->setDataUrl( dataUrl );
322}
323
324QString QgsMapLayer::dataUrl() const
325{
327
328 return mServerProperties->dataUrl();
329}
330
332{
334
335 mServerProperties->setDataUrlFormat( dataUrlFormat );
336}
337
339{
341
342 return mServerProperties->dataUrlFormat();
343}
344
345void QgsMapLayer::setAttribution( const QString &attrib )
346{
348
349 mServerProperties->setAttribution( attrib );
350}
351
353{
355
356 return mServerProperties->attribution();
357}
358
359void QgsMapLayer::setAttributionUrl( const QString &attribUrl )
360{
362
363 mServerProperties->setAttributionUrl( attribUrl );
364}
365
367{
369
370 return mServerProperties->attributionUrl();
371}
372
374{
376
377 mServerProperties->setLegendUrl( legendUrl );
378}
379
381{
383
384 return mServerProperties->legendUrl();
385}
386
388{
390
391 mServerProperties->setLegendUrlFormat( legendUrlFormat );
392}
393
395{
397
398 return mServerProperties->legendUrlFormat();
399}
400
401void QgsMapLayer::setMetadataUrl( const QString &metaUrl )
402{
404
405 QList<QgsMapLayerServerProperties::MetadataUrl> urls = serverProperties()->metadataUrls();
406 if ( urls.isEmpty() )
407 {
408 const QgsMapLayerServerProperties::MetadataUrl newItem = QgsMapLayerServerProperties::MetadataUrl( metaUrl, QLatin1String(), QLatin1String() );
409 urls.prepend( newItem );
410 }
411 else
412 {
413 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
414 const QgsMapLayerServerProperties::MetadataUrl newItem( metaUrl, old.type, old.format );
415 urls.prepend( newItem );
416 }
418}
419
421{
423
424 if ( mServerProperties->metadataUrls().isEmpty() )
425 {
426 return QLatin1String();
427 }
428 else
429 {
430 return mServerProperties->metadataUrls().first().url;
431 }
432}
433
434void QgsMapLayer::setMetadataUrlType( const QString &metaUrlType )
435{
437
438 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
439 if ( urls.isEmpty() )
440 {
441 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), metaUrlType, QLatin1String() );
442 urls.prepend( newItem );
443 }
444 else
445 {
446 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
447 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, metaUrlType, old.format );
448 urls.prepend( newItem );
449 }
450 mServerProperties->setMetadataUrls( urls );
451}
452
454{
456
457 if ( mServerProperties->metadataUrls().isEmpty() )
458 {
459 return QLatin1String();
460 }
461 else
462 {
463 return mServerProperties->metadataUrls().first().type;
464 }
465}
466
467void QgsMapLayer::setMetadataUrlFormat( const QString &metaUrlFormat )
468{
470
471 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
472 if ( urls.isEmpty() )
473 {
474 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), QLatin1String(), metaUrlFormat );
475 urls.prepend( newItem );
476 }
477 else
478 {
479 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
480 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, old.type, metaUrlFormat );
481 urls.prepend( newItem );
482 }
483 mServerProperties->setMetadataUrls( urls );
484}
485
487{
489
490 if ( mServerProperties->metadataUrls().isEmpty() )
491 {
492 return QString();
493 }
494 else
495 {
496 return mServerProperties->metadataUrls().first().format;
497 }
498}
499
500QString QgsMapLayer::publicSource( bool redactCredentials ) const
501{
503
504 // Redo this every time we're asked for it, as we don't know if
505 // dataSource has changed.
507 {
509 }
510 else
511 {
512 return QgsDataSourceUri::removePassword( mDataSource, redactCredentials );
513 }
514}
515
516QString QgsMapLayer::source() const
517{
519
520 return mDataSource;
521}
522
524{
526
527 return mExtent2D.isNull() ? mExtent3D.toRectangle() : mExtent2D;
528}
529
531{
533
534 return mExtent3D;
535}
536
537void QgsMapLayer::setBlendMode( const QPainter::CompositionMode blendMode )
538{
540
541 if ( mBlendMode == blendMode )
542 return;
543
544 mBlendMode = blendMode;
547}
548
549QPainter::CompositionMode QgsMapLayer::blendMode() const
550{
551 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
553
554 return mBlendMode;
555}
556
567
569{
570 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
572
573 return mLayerOpacity;
574}
575
576bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags, QgsDataProvider *preloadedProvider )
577{
579
580 mPreloadedProvider.reset( preloadedProvider );
581
582 bool layerError;
584
585 QDomNode mnl;
586 QDomElement mne;
587
588 // read provider
589 QString provider;
590 mnl = layerElement.namedItem( u"provider"_s );
591 mne = mnl.toElement();
592 provider = mne.text();
593
594 // set data source
595 mnl = layerElement.namedItem( u"datasource"_s );
596 mne = mnl.toElement();
597 const QString dataSourceRaw = mne.text();
598
599 // if the layer needs authentication, ensure the master password is set
600 const thread_local QRegularExpression rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
601 if ( rx.match( dataSourceRaw ).hasMatch() && !QgsApplication::authManager()->setMasterPassword( true ) )
602 {
603 return false;
604 }
605
606 mDataSource = decodedSource( dataSourceRaw, provider, context );
607
608 // Set the CRS from project file, asking the user if necessary.
609 // Make it the saved CRS to have WMS layer projected correctly.
610 // We will still overwrite whatever GDAL etc picks up anyway
611 // further down this function.
612 mnl = layerElement.namedItem( u"layername"_s );
613 mne = mnl.toElement();
614
616 CUSTOM_CRS_VALIDATION savedValidation;
617
618 const QDomNode srsNode = layerElement.namedItem( u"srs"_s );
619 mCRS.readXml( srsNode );
620 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) );
622 mCRS.validate();
623 savedCRS = mCRS;
624
625 // Do not validate any projections in children, they will be overwritten anyway.
626 // No need to ask the user for a projections when it is overwritten, is there?
629
630 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Layer" ), mne.text() );
631
632 // the internal name is just the data source basename
633 //QFileInfo dataSourceFileInfo( mDataSource );
634 //internalName = dataSourceFileInfo.baseName();
635
636 // set ID
637 mnl = layerElement.namedItem( u"id"_s );
638 if ( !mnl.isNull() )
639 {
640 mne = mnl.toElement();
641 if ( !mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz)
642 {
643 const QString newId = mne.text();
644 if ( newId != mID )
645 {
646 mID = mne.text();
647 emit idChanged( mID );
648 }
649 }
650 }
651
652 // set name
653 mnl = layerElement.namedItem( u"layername"_s );
654 mne = mnl.toElement();
655
656 //name can be translated
657 setName( context.projectTranslator()->translate( u"project:layers:%1"_s.arg( layerElement.namedItem( u"id"_s ).toElement().text() ), mne.text() ) );
658
659 // now let the children grab what they need from the Dom node.
660 layerError = !readXml( layerElement, context );
661
662 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
663 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
664
665 // overwrite CRS with what we read from project file before the raster/vector
666 // file reading functions changed it. They will if projections is specified in the file.
667 // FIXME: is this necessary? Yes, it is (autumn 2019)
669 mCRS = savedCRS;
670
671 //vertical CRS
672 {
674 const QDomNode verticalCrsNode = layerElement.firstChildElement( u"verticalCrs"_s );
675 if ( !verticalCrsNode.isNull() )
676 {
677 verticalCrs.readXml( verticalCrsNode );
678 }
679 mVerticalCrs = verticalCrs;
680 }
681 rebuildCrs3D();
682
683 serverProperties()->readXml( layerElement );
684
685 // mMetadata.readFromLayer( this );
686 const QDomElement metadataElem = layerElement.firstChildElement( u"resourceMetadata"_s );
687 mMetadata.readMetadataXml( metadataElem, context );
688
689 setAutoRefreshInterval( layerElement.attribute( u"autoRefreshTime"_s, u"0"_s ).toInt() );
690 if ( layerElement.hasAttribute( u"autoRefreshMode"_s ) )
691 {
692 setAutoRefreshMode( qgsEnumKeyToValue( layerElement.attribute( u"autoRefreshMode"_s ), Qgis::AutoRefreshMode::Disabled ) );
693 }
694 else
695 {
696 setAutoRefreshMode( layerElement.attribute( u"autoRefreshEnabled"_s, u"0"_s ).toInt() ? Qgis::AutoRefreshMode::RedrawOnly : Qgis::AutoRefreshMode::Disabled );
697 }
698 setRefreshOnNofifyMessage( layerElement.attribute( u"refreshOnNotifyMessage"_s, QString() ) );
699 setRefreshOnNotifyEnabled( layerElement.attribute( u"refreshOnNotifyEnabled"_s, u"0"_s ).toInt() );
700
701 // geographic extent is read only if necessary
703 {
704 const QDomNode wgs84ExtentNode = layerElement.namedItem( u"wgs84extent"_s );
705 if ( !wgs84ExtentNode.isNull() )
706 mWgs84Extent = QgsXmlUtils::readRectangle( wgs84ExtentNode.toElement() );
707 }
708
709 mLegendPlaceholderImage = layerElement.attribute( u"legendPlaceholderImage"_s );
710
711 if ( verticalCrs() != oldVerticalCrs )
712 emit verticalCrsChanged();
713 if ( mCrs3D != oldCrs3D )
714 emit crs3DChanged();
715
716 return !layerError;
717} // bool QgsMapLayer::readLayerXML
718
719
720bool QgsMapLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
721{
723
724 Q_UNUSED( layer_node )
725 Q_UNUSED( context )
726 // NOP by default; children will over-ride with behavior specific to them
727
728 // read Extent
730 {
731 const QDomNode extent3DNode = layer_node.namedItem( u"extent3D"_s );
732 if ( extent3DNode.isNull() )
733 {
734 const QDomNode extentNode = layer_node.namedItem( u"extent"_s );
735 if ( !extentNode.isNull() )
736 {
737 mExtent2D = QgsXmlUtils::readRectangle( extentNode.toElement() );
738 }
739 }
740 else
741 {
742 mExtent3D = QgsXmlUtils::readBox3D( extent3DNode.toElement() );
743 }
744 }
745
746 return true;
747} // void QgsMapLayer::readXml
748
749
750bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const
751{
753
754 if ( !mExtent3D.isNull() && dataProvider() && dataProvider()->elevationProperties() && dataProvider()->elevationProperties()->containsElevationData() )
755 layerElement.appendChild( QgsXmlUtils::writeBox3D( mExtent3D, document ) );
756 else
757 {
758 // Extent might be null because lazily set
759 const QgsRectangle extent2D { mExtent2D.isNull() ? extent() : mExtent2D };
760 if ( !extent2D.isNull() )
761 {
762 layerElement.appendChild( QgsXmlUtils::writeRectangle( extent2D, document ) );
763 }
764 }
765
766 if ( const QgsRectangle lWgs84Extent = wgs84Extent( true ); !lWgs84Extent.isNull() )
767 {
768 layerElement.appendChild( QgsXmlUtils::writeRectangle( lWgs84Extent, document, u"wgs84extent"_s ) );
769 }
770
771 layerElement.setAttribute( u"autoRefreshTime"_s, QString::number( mRefreshTimer->interval() ) );
772 layerElement.setAttribute( u"autoRefreshMode"_s, qgsEnumValueToKey( mAutoRefreshMode ) );
773 layerElement.setAttribute( u"refreshOnNotifyEnabled"_s, mIsRefreshOnNofifyEnabled ? 1 : 0 );
774 layerElement.setAttribute( u"refreshOnNotifyMessage"_s, mRefreshOnNofifyMessage );
775
776 // ID
777 QDomElement layerId = document.createElement( u"id"_s );
778 const QDomText layerIdText = document.createTextNode( id() );
779 layerId.appendChild( layerIdText );
780
781 layerElement.appendChild( layerId );
782
783 if ( mVerticalCrs.isValid() )
784 {
785 QDomElement verticalSrsNode = document.createElement( u"verticalCrs"_s );
786 mVerticalCrs.writeXml( verticalSrsNode, document );
787 layerElement.appendChild( verticalSrsNode );
788 }
789
790 // data source
791 QDomElement dataSource = document.createElement( u"datasource"_s );
792 const QString src = encodedSource( source(), context );
793 const QDomText dataSourceText = document.createTextNode( src );
794 dataSource.appendChild( dataSourceText );
795 layerElement.appendChild( dataSource );
796
797 // layer name
798 QDomElement layerName = document.createElement( u"layername"_s );
799 const QDomText layerNameText = document.createTextNode( name() );
800 layerName.appendChild( layerNameText );
801 layerElement.appendChild( layerName );
802
803 // timestamp if supported
804 if ( timestamp() > QDateTime() )
805 {
806 QDomElement stamp = document.createElement( u"timestamp"_s );
807 const QDomText stampText = document.createTextNode( timestamp().toString( Qt::ISODate ) );
808 stamp.appendChild( stampText );
809 layerElement.appendChild( stamp );
810 }
811
812 layerElement.appendChild( layerName );
813
814 // zorder
815 // This is no longer stored in the project file. It is superfluous since the layers
816 // are written and read in the proper order.
817
818 // spatial reference system id
819 QDomElement mySrsElement = document.createElement( u"srs"_s );
820 mCRS.writeXml( mySrsElement, document );
821 layerElement.appendChild( mySrsElement );
822
823 // layer metadata
824 QDomElement myMetadataElem = document.createElement( u"resourceMetadata"_s );
825 mMetadata.writeMetadataXml( myMetadataElem, document );
826 layerElement.appendChild( myMetadataElem );
827
828 layerElement.setAttribute( u"legendPlaceholderImage"_s, mLegendPlaceholderImage );
829
830 serverProperties()->writeXml( layerElement, document );
831
832 // now append layer node to map layer node
833 return writeXml( layerElement, document, context );
834}
835
836void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
837{
839
840 // save categories
841 const QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::StyleCategories>();
842 const QString categoriesKeys( metaEnum.valueToKeys( static_cast<int>( categories ) ) );
843 layerElement.setAttribute( u"styleCategories"_s, categoriesKeys );
845 // Store layer type
846 layerElement.setAttribute( u"layerType"_s, qgsEnumValueToKey( type() ) );
847
848 if ( categories.testFlag( Rendering ) )
849 {
850 // use scale dependent visibility flag
851 layerElement.setAttribute( u"hasScaleBasedVisibilityFlag"_s, hasScaleBasedVisibility() ? 1 : 0 );
852 layerElement.setAttribute( u"maxScale"_s, QString::number( maximumScale() ) );
853 layerElement.setAttribute( u"minScale"_s, QString::number( minimumScale() ) );
854 layerElement.setAttribute( u"autoRefreshMode"_s, qgsEnumValueToKey( mAutoRefreshMode ) );
855 layerElement.setAttribute( u"autoRefreshTime"_s, QString::number( autoRefreshInterval() ) );
856 }
857
858 if ( categories.testFlag( Symbology3D ) )
859 {
860 if ( m3DRenderer )
861 {
862 QDomElement renderer3DElem = document.createElement( u"renderer-3d"_s );
863 renderer3DElem.setAttribute( u"type"_s, m3DRenderer->type() );
864 m3DRenderer->writeXml( renderer3DElem, context );
865 layerElement.appendChild( renderer3DElem );
866 }
867 }
868
869 if ( categories.testFlag( LayerConfiguration ) )
870 {
871 // flags
872 // this code is saving automatically all the flags entries
873 QDomElement layerFlagsElem = document.createElement( u"flags"_s );
874 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
875 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
876 {
877 const bool flagValue = mFlags.testFlag( it.key() );
878 QDomElement flagElem = document.createElement( it.value() );
879 flagElem.appendChild( document.createTextNode( QString::number( flagValue ) ) );
880 layerFlagsElem.appendChild( flagElem );
881 }
882 layerElement.appendChild( layerFlagsElem );
883 }
884
885 if ( categories.testFlag( Temporal ) )
886 {
887 if ( QgsMapLayerTemporalProperties *properties = const_cast< QgsMapLayer * >( this )->temporalProperties() )
888 properties->writeXml( layerElement, document, context );
889 }
890
891 if ( categories.testFlag( Elevation ) )
892 {
893 if ( QgsMapLayerElevationProperties *properties = const_cast< QgsMapLayer * >( this )->elevationProperties() )
894 properties->writeXml( layerElement, document, context );
895 }
896
897 if ( categories.testFlag( Notes ) && QgsLayerNotesUtils::layerHasNotes( this ) )
898 {
899 QDomElement notesElem = document.createElement( u"userNotes"_s );
900 notesElem.setAttribute( u"value"_s, QgsLayerNotesUtils::layerNotes( this ) );
901 layerElement.appendChild( notesElem );
902 }
903
904 // custom properties
905 if ( categories.testFlag( CustomProperties ) )
906 {
907 writeCustomProperties( layerElement, document );
908 }
909}
910
911
912bool QgsMapLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const
913{
915
916 Q_UNUSED( layer_node )
917 Q_UNUSED( document )
918 Q_UNUSED( context )
919 // NOP by default; children will over-ride with behavior specific to them
920
921 return true;
922}
923
924QString QgsMapLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
925{
927
928 Q_UNUSED( context )
929 return source;
930}
931
932QString QgsMapLayer::decodedSource( const QString &source, const QString &dataProvider, const QgsReadWriteContext &context ) const
933{
935
936 Q_UNUSED( context )
937 Q_UNUSED( dataProvider )
938 return source;
939}
940
942{
944
946 if ( m3DRenderer )
947 m3DRenderer->resolveReferences( *project );
948}
949
950
951void QgsMapLayer::readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith )
952{
954
955 const QgsObjectCustomProperties oldKeys = mCustomProperties;
956
957 mCustomProperties.readXml( layerNode, keyStartsWith );
958
959 for ( const QString &key : mCustomProperties.keys() )
960 {
961 if ( !oldKeys.contains( key ) || mCustomProperties.value( key ) != oldKeys.value( key ) )
962 {
963 emit customPropertyChanged( key );
964 }
965 }
966}
967
968void QgsMapLayer::writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const
969{
971
972 mCustomProperties.writeXml( layerNode, doc );
973}
974
975void QgsMapLayer::readStyleManager( const QDomNode &layerNode )
976{
978
979 const QDomElement styleMgrElem = layerNode.firstChildElement( u"map-layer-style-manager"_s );
980 if ( !styleMgrElem.isNull() )
981 mStyleManager->readXml( styleMgrElem );
982 else
983 mStyleManager->reset();
984}
985
986void QgsMapLayer::writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const
987{
989
990 if ( mStyleManager )
991 {
992 QDomElement styleMgrElem = doc.createElement( u"map-layer-style-manager"_s );
993 mStyleManager->writeXml( styleMgrElem );
994 layerNode.appendChild( styleMgrElem );
995 }
996}
997
999{
1001
1002 return mMapTipTemplate;
1003}
1004
1005void QgsMapLayer::setMapTipTemplate( const QString &mapTip )
1006{
1008
1009 if ( mMapTipTemplate == mapTip )
1010 return;
1011
1012 mMapTipTemplate = mapTip;
1013 emit mapTipTemplateChanged();
1014}
1015
1017{
1019
1020 if ( mMapTipsEnabled == enabled )
1021 return;
1022
1023 mMapTipsEnabled = enabled;
1024 emit mapTipsEnabledChanged();
1025}
1026
1028{
1030
1031 return mMapTipsEnabled;
1032}
1033
1035{
1037 if ( layerReadFlags & QgsMapLayer::FlagTrustLayerMetadata )
1038 {
1040 }
1041 if ( layerReadFlags & QgsMapLayer::FlagForceReadOnly )
1042 {
1044 }
1045
1046 if ( layerReadFlags & QgsMapLayer::FlagReadExtentFromXml )
1047 {
1048 const QDomNode extent3DNode = layerNode.namedItem( u"extent3D"_s );
1049 if ( extent3DNode.isNull() )
1050 {
1051 const QDomNode extentNode = layerNode.namedItem( u"extent"_s );
1052 if ( !extentNode.isNull() )
1053 {
1055 }
1056 }
1057 else
1058 {
1060 }
1061 }
1062
1063 return flags;
1064}
1065
1067{
1068 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
1070
1071 return mValid;
1072}
1073
1074#if 0
1075void QgsMapLayer::connectNotify( const char *signal )
1076{
1077 Q_UNUSED( signal )
1078 QgsDebugMsgLevel( "QgsMapLayer connected to " + QString( signal ), 3 );
1079} // QgsMapLayer::connectNotify
1080#endif
1081
1082bool QgsMapLayer::isInScaleRange( double scale ) const
1083{
1084 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1086
1087 // mMinScale (denominator!) is inclusive ( >= --> In range )
1088 // mMaxScale (denominator!) is exclusive ( < --> In range )
1089 return !mScaleBasedVisibility
1090 || ( ( mMinScale == 0 || !QgsScaleUtils::lessThanMaximumScale( scale, mMinScale ) ) && ( mMaxScale == 0 || !QgsScaleUtils::equalToOrGreaterThanMinimumScale( scale, mMaxScale ) ) );
1091}
1092
1094{
1095 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1097
1098 return mScaleBasedVisibility;
1099}
1100
1102{
1104
1105 return mAutoRefreshMode != Qgis::AutoRefreshMode::Disabled;
1106 ;
1107}
1108
1110{
1112
1113 return mAutoRefreshMode;
1114}
1115
1117{
1119
1120 return mRefreshTimer->interval();
1121}
1122
1124{
1126
1127 if ( interval <= 0 )
1128 {
1129 mRefreshTimer->stop();
1130 mRefreshTimer->setInterval( 0 );
1132 }
1133 else
1134 {
1135 mRefreshTimer->setInterval( interval );
1136 }
1137 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1138}
1139
1146
1148{
1150
1151 if ( mode == mAutoRefreshMode )
1152 return;
1153
1154 mAutoRefreshMode = mode;
1155 switch ( mAutoRefreshMode )
1156 {
1158 mRefreshTimer->stop();
1159 break;
1160
1163 if ( mRefreshTimer->interval() > 0 )
1164 mRefreshTimer->start();
1165 break;
1166 }
1167
1168 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1169}
1170
1172{
1174
1175 return mMetadata;
1176}
1177
1179{
1181
1182 mMinScale = scale;
1183}
1184
1186{
1188
1189 return mMinScale;
1190}
1191
1193{
1195
1196 mMaxScale = scale;
1197}
1198
1200{
1202
1203 mScaleBasedVisibility = enabled;
1204}
1205
1207{
1209
1210 return mMaxScale;
1211}
1212
1213QStringList QgsMapLayer::subLayers() const
1214{
1216
1217 return QStringList();
1218}
1219
1220void QgsMapLayer::setLayerOrder( const QStringList &layers )
1221{
1223
1224 Q_UNUSED( layers )
1225}
1226
1227void QgsMapLayer::setSubLayerVisibility( const QString &name, bool vis )
1228{
1230
1231 Q_UNUSED( name )
1232 Q_UNUSED( vis )
1233}
1234
1236{
1238
1239 return false;
1240}
1241
1243{
1244 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1246
1247 return mCRS;
1248}
1249
1251{
1252 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1254
1255 switch ( mCRS.type() )
1256 {
1257 case Qgis::CrsType::Vertical: // would hope this never happens!
1258 QgsDebugError( u"Layer has a vertical CRS set as the horizontal CRS!"_s );
1259 return mCRS;
1260
1262 return mCRS.verticalCrs();
1263
1275 break;
1276 }
1277 return mVerticalCrs;
1278}
1279
1281{
1283
1284 return mCrs3D.isValid() ? mCrs3D : mCRS;
1285}
1286
1287void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal )
1288{
1290 const bool needToValidateCrs = mShouldValidateCrs && isSpatial() && !srs.isValid() && type() != Qgis::LayerType::Annotation;
1291
1292 if ( mCRS == srs && !needToValidateCrs )
1293 return;
1294
1295 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
1296 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
1297 const QgsCoordinateReferenceSystem oldCrs = mCRS;
1298
1299 mCRS = srs;
1300
1301 if ( needToValidateCrs )
1302 {
1303 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( name() ) );
1304 mCRS.validate();
1305 }
1306
1307 rebuildCrs3D();
1308
1309 if ( emitSignal && mCRS != oldCrs )
1310 emit crsChanged();
1311
1312 // Did vertical crs also change as a result of this? If so, emit signal
1313 if ( oldVerticalCrs != verticalCrs() )
1314 emit verticalCrsChanged();
1315 if ( oldCrs3D != mCrs3D )
1316 emit crs3DChanged();
1317}
1318
1320{
1322 bool res = true;
1323 if ( crs.isValid() )
1324 {
1325 // validate that passed crs is a vertical crs
1326 switch ( crs.type() )
1327 {
1329 break;
1330
1343 if ( errorMessage )
1344 *errorMessage = QObject::tr( "Specified CRS is a %1 CRS, not a Vertical CRS" ).arg( qgsEnumValueToKey( crs.type() ) );
1345 return false;
1346 }
1347 }
1348
1349 if ( crs != mVerticalCrs )
1350 {
1351 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
1352 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
1353
1354 switch ( mCRS.type() )
1355 {
1357 if ( crs != oldVerticalCrs )
1358 {
1359 if ( errorMessage )
1360 *errorMessage = QObject::tr( "Layer CRS is a Compound CRS, specified Vertical CRS will be ignored" );
1361 return false;
1362 }
1363 break;
1364
1366 if ( crs != oldVerticalCrs )
1367 {
1368 if ( errorMessage )
1369 *errorMessage = QObject::tr( "Layer CRS is a Geographic 3D CRS, specified Vertical CRS will be ignored" );
1370 return false;
1371 }
1372 break;
1373
1375 if ( crs != oldVerticalCrs )
1376 {
1377 if ( errorMessage )
1378 *errorMessage = QObject::tr( "Layer CRS is a Geocentric CRS, specified Vertical CRS will be ignored" );
1379 return false;
1380 }
1381 break;
1382
1384 if ( mCRS.hasVerticalAxis() && crs != oldVerticalCrs )
1385 {
1386 if ( errorMessage )
1387 *errorMessage = QObject::tr( "Layer CRS is a Projected 3D CRS, specified Vertical CRS will be ignored" );
1388 return false;
1389 }
1390 break;
1391
1401 break;
1402 }
1403
1404 mVerticalCrs = crs;
1405 res = rebuildCrs3D( errorMessage );
1406
1407 // only emit signal if vertical crs was actually changed, so eg if mCrs is compound
1408 // then we haven't actually changed the vertical crs by this call!
1409 if ( verticalCrs() != oldVerticalCrs )
1410 emit verticalCrsChanged();
1411 if ( mCrs3D != oldCrs3D )
1412 emit crs3DChanged();
1413 }
1414 return res;
1415}
1416
1418{
1420
1421 const QgsDataProvider *lDataProvider = dataProvider();
1422 return lDataProvider ? lDataProvider->transformContext() : QgsCoordinateTransformContext();
1423}
1424
1425QString QgsMapLayer::formatLayerName( const QString &name )
1426{
1427 QString layerName( name );
1428 layerName.replace( '_', ' ' );
1430 return layerName;
1431}
1432
1433QString QgsMapLayer::baseURI( PropertyType type ) const
1434{
1436
1437 QString myURI = publicSource();
1438
1439 // first get base path for delimited text, spatialite and OGR layers,
1440 // as in these cases URI may contain layer name and/or additional
1441 // information. This also strips prefix in case if VSIFILE mechanism
1442 // is used
1443 if ( providerType() == "ogr"_L1 || providerType() == "delimitedtext"_L1 || providerType() == "gdal"_L1 || providerType() == "spatialite"_L1 )
1444 {
1445 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( providerType(), myURI );
1446 myURI = components["path"].toString();
1447 }
1448
1449 QFileInfo myFileInfo( myURI );
1450 QString key;
1451
1452 if ( myFileInfo.exists() )
1453 {
1454 // if file is using the /vsizip/ or /vsigzip/ mechanism, cleanup the name
1455 if ( myURI.endsWith( ".gz"_L1, Qt::CaseInsensitive ) )
1456 myURI.chop( 3 );
1457 else if ( myURI.endsWith( ".zip"_L1, Qt::CaseInsensitive ) )
1458 myURI.chop( 4 );
1459 else if ( myURI.endsWith( ".tar"_L1, Qt::CaseInsensitive ) )
1460 myURI.chop( 4 );
1461 else if ( myURI.endsWith( ".tar.gz"_L1, Qt::CaseInsensitive ) )
1462 myURI.chop( 7 );
1463 else if ( myURI.endsWith( ".tgz"_L1, Qt::CaseInsensitive ) )
1464 myURI.chop( 4 );
1465 myFileInfo.setFile( myURI );
1466 // get the file name for our .qml style file
1467 key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1468 }
1469 else
1470 {
1471 key = publicSource();
1472 }
1473
1474 return key;
1475}
1476
1478{
1480
1481 return baseURI( PropertyType::Metadata );
1482}
1483
1484QString QgsMapLayer::saveDefaultMetadata( bool &resultFlag )
1485{
1487
1489 {
1490 if ( metadata->providerCapabilities() & QgsProviderMetadata::SaveLayerMetadata )
1491 {
1492 try
1493 {
1494 QString errorMessage;
1495 resultFlag = QgsProviderRegistry::instance()->saveLayerMetadata( providerType(), mDataSource, mMetadata, errorMessage );
1496 if ( resultFlag )
1497 return tr( "Successfully saved default layer metadata" );
1498 else
1499 return errorMessage;
1500 }
1501 catch ( QgsNotSupportedException &e )
1502 {
1503 resultFlag = false;
1504 return e.what();
1505 }
1506 }
1507 }
1508
1509 // fallback default metadata saving method, for providers which don't support (or implement) saveLayerMetadata
1510 return saveNamedMetadata( metadataUri(), resultFlag );
1511}
1512
1513QString QgsMapLayer::loadDefaultMetadata( bool &resultFlag )
1514{
1516
1517 return loadNamedMetadata( metadataUri(), resultFlag );
1518}
1519
1521{
1523
1524 return baseURI( PropertyType::Style );
1525}
1526
1533
1534bool QgsMapLayer::loadNamedMetadataFromDatabase( const QString &db, const QString &uri, QString &qmd )
1535{
1537
1538 return loadNamedPropertyFromDatabase( db, uri, qmd, PropertyType::Metadata );
1539}
1540
1541bool QgsMapLayer::loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml )
1542{
1544
1545 return loadNamedPropertyFromDatabase( db, uri, qml, PropertyType::Style );
1546}
1547
1548bool QgsMapLayer::loadNamedPropertyFromDatabase( const QString &db, const QString &uri, QString &xml, QgsMapLayer::PropertyType type )
1549{
1551
1552 QgsDebugMsgLevel( u"db = %1 uri = %2"_s.arg( db, uri ), 4 );
1553
1554 bool resultFlag = false;
1555
1556 // read from database
1559
1560 int myResult;
1561
1562 QgsDebugMsgLevel( u"Trying to load style or metadata for \"%1\" from \"%2\""_s.arg( uri, db ), 4 );
1563
1564 if ( db.isEmpty() || !QFile( db ).exists() )
1565 return false;
1566
1567 myResult = database.open_v2( db, SQLITE_OPEN_READONLY, nullptr );
1568 if ( myResult != SQLITE_OK )
1569 {
1570 return false;
1571 }
1572
1573 QString mySql;
1574 switch ( type )
1575 {
1576 case Metadata:
1577 mySql = u"select qmd from tbl_metadata where metadata=?"_s;
1578 break;
1579
1580 case Style:
1581 mySql = u"select qml from tbl_styles where style=?"_s;
1582 break;
1583 }
1584
1585 statement = database.prepare( mySql, myResult );
1586 if ( myResult == SQLITE_OK )
1587 {
1588 QByteArray param = uri.toUtf8();
1589
1590 if ( sqlite3_bind_text( statement.get(), 1, param.data(), param.length(), SQLITE_STATIC ) == SQLITE_OK && sqlite3_step( statement.get() ) == SQLITE_ROW )
1591 {
1592 xml = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( statement.get(), 0 ) ) );
1593 resultFlag = true;
1594 }
1595 }
1596 return resultFlag;
1597}
1598
1599
1600QString QgsMapLayer::loadNamedStyle( const QString &uri, bool &resultFlag, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
1601{
1603
1604 return loadNamedStyle( uri, resultFlag, false, categories, flags );
1605}
1606
1607QString QgsMapLayer::loadNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &namedPropertyExists, bool &propertySuccessfullyLoaded, StyleCategories categories, Qgis::LoadStyleFlags flags )
1608{
1610
1611 QgsDebugMsgLevel( u"uri = %1 myURI = %2"_s.arg( uri, publicSource() ), 4 );
1612
1613 namedPropertyExists = false;
1614 propertySuccessfullyLoaded = false;
1615 if ( uri.isEmpty() )
1616 return QString();
1617
1618 QDomDocument myDocument( u"qgis"_s );
1619
1620 // location of problem associated with errorMsg
1621 int line, column;
1622 QString myErrorMessage;
1623
1624 QFile myFile( uri );
1625 if ( myFile.open( QFile::ReadOnly ) )
1626 {
1627 QgsDebugMsgLevel( u"file found %1"_s.arg( uri ), 2 );
1628 namedPropertyExists = true;
1629
1630 // read file
1631 propertySuccessfullyLoaded = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
1632 if ( !propertySuccessfullyLoaded )
1633 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1634 myFile.close();
1635 }
1636 else
1637 {
1638 const QFileInfo project( QgsProject::instance()->fileName() ); // skip-keyword-check
1639 QgsDebugMsgLevel( u"project fileName: %1"_s.arg( project.absoluteFilePath() ), 4 );
1640
1641 QString xml;
1642 switch ( type )
1643 {
1644 case QgsMapLayer::Style:
1645 {
1646 if ( loadNamedStyleFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( u"qgis.qmldb"_s ), uri, xml )
1647 || ( project.exists() && loadNamedStyleFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) )
1648 || loadNamedStyleFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( u"resources/qgis.qmldb"_s ), uri, xml ) )
1649 {
1650 namedPropertyExists = true;
1651 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1652 if ( !propertySuccessfullyLoaded )
1653 {
1654 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1655 }
1656 }
1657 else
1658 {
1660 {
1661 myErrorMessage = tr( "Style not found in database" );
1662 }
1663 }
1664 break;
1665 }
1667 {
1668 if ( loadNamedMetadataFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( u"qgis.qmldb"_s ), uri, xml )
1669 || ( project.exists() && loadNamedMetadataFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) )
1670 || loadNamedMetadataFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( u"resources/qgis.qmldb"_s ), uri, xml ) )
1671 {
1672 namedPropertyExists = true;
1673 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1674 if ( !propertySuccessfullyLoaded )
1675 {
1676 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1677 }
1678 }
1679 else
1680 {
1681 myErrorMessage = tr( "Metadata not found in database" );
1682 }
1683 break;
1684 }
1685 }
1686 }
1687
1688 if ( !propertySuccessfullyLoaded )
1689 {
1690 return myErrorMessage;
1691 }
1692
1693 switch ( type )
1694 {
1695 case QgsMapLayer::Style:
1696 propertySuccessfullyLoaded = importNamedStyle( myDocument, myErrorMessage, categories );
1697 if ( !propertySuccessfullyLoaded )
1698 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1699 break;
1701 propertySuccessfullyLoaded = importNamedMetadata( myDocument, myErrorMessage );
1702 if ( !propertySuccessfullyLoaded )
1703 myErrorMessage = tr( "Loading metadata file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1704 break;
1705 }
1706 return myErrorMessage;
1707}
1708
1709bool QgsMapLayer::importNamedMetadata( QDomDocument &document, QString &errorMessage )
1710{
1712
1713 const QDomElement myRoot = document.firstChildElement( u"qgis"_s );
1714 if ( myRoot.isNull() )
1715 {
1716 errorMessage = tr( "Root <qgis> element could not be found" );
1717 return false;
1718 }
1719
1720 return mMetadata.readMetadataXml( myRoot );
1721}
1722
1723bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMessage, QgsMapLayer::StyleCategories categories )
1724{
1726
1727 const QDomElement myRoot = myDocument.firstChildElement( u"qgis"_s );
1728 if ( myRoot.isNull() )
1729 {
1730 myErrorMessage = tr( "Root <qgis> element could not be found" );
1731 return false;
1732 }
1733
1734 // get style file version string, if any
1735 const QgsProjectVersion fileVersion( myRoot.attribute( u"version"_s ) );
1736 const QgsProjectVersion thisVersion( Qgis::version() );
1737
1738 if ( thisVersion > fileVersion )
1739 {
1740 QgsProjectFileTransform styleFile( myDocument, fileVersion );
1741 styleFile.updateRevision( thisVersion );
1742 }
1743
1744 // Get source categories
1745 const QgsMapLayer::StyleCategories sourceCategories = QgsXmlUtils::readFlagAttribute( myRoot, u"styleCategories"_s, QgsMapLayer::AllStyleCategories );
1746
1747 //Test for matching geometry type on vector layers when applying, if geometry type is given in the style
1748 if ( ( sourceCategories.testFlag( QgsMapLayer::Symbology ) || sourceCategories.testFlag( QgsMapLayer::Symbology3D ) )
1749 && ( categories.testFlag( QgsMapLayer::Symbology ) || categories.testFlag( QgsMapLayer::Symbology3D ) ) )
1750 {
1751 if ( type() == Qgis::LayerType::Vector && !myRoot.firstChildElement( u"layerGeometryType"_s ).isNull() )
1752 {
1753 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( this );
1754 const Qgis::GeometryType importLayerGeometryType = static_cast<Qgis::GeometryType>( myRoot.firstChildElement( u"layerGeometryType"_s ).text().toInt() );
1755 if ( importLayerGeometryType != Qgis::GeometryType::Unknown && vl->geometryType() != importLayerGeometryType )
1756 {
1757 myErrorMessage = tr( "Cannot apply style with symbology to layer with a different geometry type" );
1758 return false;
1759 }
1760 }
1761 }
1762
1763 // Pass the intersection between the desired categories and those that are really in the document
1765 return readSymbology( myRoot, myErrorMessage, context, categories & sourceCategories ); // TODO: support relative paths in QML?
1766}
1767
1768void QgsMapLayer::exportNamedMetadata( QDomDocument &doc, QString &errorMsg ) const
1769{
1771
1772 QDomImplementation DomImplementation;
1773 const QDomDocumentType documentType = DomImplementation.createDocumentType( u"qgis"_s, u"http://mrcc.com/qgis.dtd"_s, u"SYSTEM"_s );
1774 QDomDocument myDocument( documentType );
1775
1776 QDomElement myRootNode = myDocument.createElement( u"qgis"_s );
1777 myRootNode.setAttribute( u"version"_s, Qgis::version() );
1778 myDocument.appendChild( myRootNode );
1779
1780 if ( !mMetadata.writeMetadataXml( myRootNode, myDocument ) )
1781 {
1782 errorMsg = QObject::tr( "Could not save metadata" );
1783 return;
1784 }
1785
1786 doc = myDocument;
1787}
1788
1789void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
1790{
1792
1793 QDomImplementation DomImplementation;
1794 const QDomDocumentType documentType = DomImplementation.createDocumentType( u"qgis"_s, u"http://mrcc.com/qgis.dtd"_s, u"SYSTEM"_s );
1795 QDomDocument myDocument( documentType );
1796
1797 QDomElement myRootNode = myDocument.createElement( u"qgis"_s );
1798 myRootNode.setAttribute( u"version"_s, Qgis::version() );
1799 myDocument.appendChild( myRootNode );
1800
1801 if ( !writeSymbology( myRootNode, myDocument, errorMsg, context, categories ) ) // TODO: support relative paths in QML?
1802 {
1803 errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1804 return;
1805 }
1806
1807 /*
1808 * Check to see if the layer is vector - in which case we should also export its geometryType
1809 * to avoid eventually pasting to a layer with a different geometry
1810 */
1811 if ( type() == Qgis::LayerType::Vector )
1812 {
1813 //Getting the selectionLayer geometry
1814 const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( this );
1815 const QString geoType = QString::number( static_cast<int>( vl->geometryType() ) );
1816
1817 //Adding geometryinformation
1818 QDomElement layerGeometryType = myDocument.createElement( u"layerGeometryType"_s );
1819 const QDomText type = myDocument.createTextNode( geoType );
1820
1821 layerGeometryType.appendChild( type );
1822 myRootNode.appendChild( layerGeometryType );
1823 }
1824
1825 doc = myDocument;
1826}
1827
1828QString QgsMapLayer::saveDefaultStyle( bool &resultFlag )
1829{
1831
1832 return saveDefaultStyle( resultFlag, AllStyleCategories );
1833}
1834
1835QString QgsMapLayer::saveDefaultStyle( bool &resultFlag, StyleCategories categories )
1836{
1838
1839 return saveNamedStyle( styleURI(), resultFlag, categories );
1840}
1841
1842QString QgsMapLayer::saveNamedMetadata( const QString &uri, bool &resultFlag )
1843{
1845
1846 return saveNamedProperty( uri, QgsMapLayer::Metadata, resultFlag );
1847}
1848
1849QString QgsMapLayer::loadNamedMetadata( const QString &uri, bool &resultFlag )
1850{
1852
1853 bool metadataExists = false;
1854 bool metadataSuccessfullyLoaded = false;
1855 const QString message = loadNamedProperty( uri, QgsMapLayer::Metadata, metadataExists, metadataSuccessfullyLoaded );
1856
1857 // TODO QGIS 5.0 -- fix API for loadNamedMetadata so we can return metadataExists too
1858 ( void ) metadataExists;
1859 resultFlag = metadataSuccessfullyLoaded;
1860 return message;
1861}
1862
1863QString QgsMapLayer::saveNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag, StyleCategories categories )
1864{
1866
1867 // check if the uri is a file or ends with .qml/.qmd,
1868 // which indicates that it should become one
1869 // everything else goes to the database
1870 QString filename;
1871
1872 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1873 if ( vlayer && vlayer->providerType() == "ogr"_L1 )
1874 {
1875 QStringList theURIParts = uri.split( '|' );
1876 filename = theURIParts[0];
1877 }
1878 else if ( vlayer && vlayer->providerType() == "gpx"_L1 )
1879 {
1880 QStringList theURIParts = uri.split( '?' );
1881 filename = theURIParts[0];
1882 }
1883 else if ( vlayer && vlayer->providerType() == "delimitedtext"_L1 )
1884 {
1885 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
1886 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
1887 if ( filename.isEmpty() )
1888 filename = uri;
1889 }
1890 else
1891 {
1892 filename = uri;
1893 }
1894
1895 QString myErrorMessage;
1896 QDomDocument myDocument;
1897 switch ( type )
1898 {
1899 case Metadata:
1900 exportNamedMetadata( myDocument, myErrorMessage );
1901 break;
1902
1903 case Style:
1904 const QgsReadWriteContext context;
1905 exportNamedStyle( myDocument, myErrorMessage, context, categories );
1906 break;
1907 }
1908
1909 const QFileInfo myFileInfo( filename );
1910 if ( myFileInfo.exists() || filename.endsWith( QgsMapLayer::extensionPropertyType( type ), Qt::CaseInsensitive ) )
1911 {
1912 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
1913 if ( !myDirInfo.isWritable() )
1914 {
1915 resultFlag = false;
1916 return tr( "The directory containing your dataset needs to be writable!" );
1917 }
1918
1919 // now construct the file name for our .qml or .qmd file
1920 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1921
1922 QFile myFile( myFileName );
1923 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1924 {
1925 QTextStream myFileStream( &myFile );
1926 // save as utf-8 with 2 spaces for indents
1927 myDocument.save( myFileStream, 2 );
1928 myFile.close();
1929 resultFlag = true;
1930 switch ( type )
1931 {
1932 case Metadata:
1933 return tr( "Created default metadata file as %1" ).arg( myFileName );
1934
1935 case Style:
1936 return tr( "Created default style file as %1" ).arg( myFileName );
1937 }
1938 }
1939 else
1940 {
1941 resultFlag = false;
1942 switch ( type )
1943 {
1944 case Metadata:
1945 return tr( "ERROR: Failed to created default metadata file as %1. Check file permissions and retry." ).arg( myFileName );
1946
1947 case Style:
1948 return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName );
1949 }
1950 }
1951 }
1952 else
1953 {
1954 const QString qml = myDocument.toString();
1955
1956 // read from database
1957 sqlite3_database_unique_ptr database;
1958 sqlite3_statement_unique_ptr statement;
1959
1960 int myResult = database.open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( u"qgis.qmldb"_s ) );
1961 if ( myResult != SQLITE_OK )
1962 {
1963 return tr( "User database could not be opened." );
1964 }
1965
1966 QByteArray param0 = uri.toUtf8();
1967 QByteArray param1 = qml.toUtf8();
1968
1969 QString mySql;
1970 switch ( type )
1971 {
1972 case Metadata:
1973 mySql = u"create table if not exists tbl_metadata(metadata varchar primary key,qmd varchar)"_s;
1974 break;
1975
1976 case Style:
1977 mySql = u"create table if not exists tbl_styles(style varchar primary key,qml varchar)"_s;
1978 break;
1979 }
1980
1981 statement = database.prepare( mySql, myResult );
1982 if ( myResult == SQLITE_OK )
1983 {
1984 if ( sqlite3_step( statement.get() ) != SQLITE_DONE )
1985 {
1986 resultFlag = false;
1987 switch ( type )
1988 {
1989 case Metadata:
1990 return tr( "The metadata table could not be created." );
1991
1992 case Style:
1993 return tr( "The style table could not be created." );
1994 }
1995 }
1996 }
1997
1998 switch ( type )
1999 {
2000 case Metadata:
2001 mySql = u"insert into tbl_metadata(metadata,qmd) values (?,?)"_s;
2002 break;
2003
2004 case Style:
2005 mySql = u"insert into tbl_styles(style,qml) values (?,?)"_s;
2006 break;
2007 }
2008 statement = database.prepare( mySql, myResult );
2009 if ( myResult == SQLITE_OK )
2010 {
2011 if ( sqlite3_bind_text( statement.get(), 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK
2012 && sqlite3_bind_text( statement.get(), 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK
2013 && sqlite3_step( statement.get() ) == SQLITE_DONE )
2014 {
2015 resultFlag = true;
2016 switch ( type )
2017 {
2018 case Metadata:
2019 myErrorMessage = tr( "The metadata %1 was saved to database" ).arg( uri );
2020 break;
2021
2022 case Style:
2023 myErrorMessage = tr( "The style %1 was saved to database" ).arg( uri );
2024 break;
2025 }
2026 }
2027 }
2028
2029 if ( !resultFlag )
2030 {
2031 QString mySql;
2032 switch ( type )
2033 {
2034 case Metadata:
2035 mySql = u"update tbl_metadata set qmd=? where metadata=?"_s;
2036 break;
2037
2038 case Style:
2039 mySql = u"update tbl_styles set qml=? where style=?"_s;
2040 break;
2041 }
2042 statement = database.prepare( mySql, myResult );
2043 if ( myResult == SQLITE_OK )
2044 {
2045 if ( sqlite3_bind_text( statement.get(), 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK
2046 && sqlite3_bind_text( statement.get(), 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK
2047 && sqlite3_step( statement.get() ) == SQLITE_DONE )
2048 {
2049 resultFlag = true;
2050 switch ( type )
2051 {
2052 case Metadata:
2053 myErrorMessage = tr( "The metadata %1 was updated in the database." ).arg( uri );
2054 break;
2055
2056 case Style:
2057 myErrorMessage = tr( "The style %1 was updated in the database." ).arg( uri );
2058 break;
2059 }
2060 }
2061 else
2062 {
2063 resultFlag = false;
2064 switch ( type )
2065 {
2066 case Metadata:
2067 myErrorMessage = tr( "The metadata %1 could not be updated in the database." ).arg( uri );
2068 break;
2069
2070 case Style:
2071 myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( uri );
2072 break;
2073 }
2074 }
2075 }
2076 else
2077 {
2078 resultFlag = false;
2079 switch ( type )
2080 {
2081 case Metadata:
2082 myErrorMessage = tr( "The metadata %1 could not be inserted into database." ).arg( uri );
2083 break;
2084
2085 case Style:
2086 myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( uri );
2087 break;
2088 }
2089 }
2090 }
2091 }
2092
2093 return myErrorMessage;
2094}
2095
2096QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag, StyleCategories categories )
2097{
2099
2100 return saveNamedProperty( uri, QgsMapLayer::Style, resultFlag, categories );
2101}
2102
2103void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
2104{
2105 QgsSldExportContext exportContext;
2106 doc = exportSldStyleV3( exportContext );
2107 if ( !exportContext.errors().empty() )
2108 errorMsg = exportContext.errors().join( "\n" );
2109}
2110
2111void QgsMapLayer::exportSldStyleV2( QDomDocument &doc, QString &errorMsg, QgsSldExportContext &exportContext ) const
2112{
2114 doc = exportSldStyleV3( exportContext );
2115 if ( !exportContext.errors().empty() )
2116 errorMsg = exportContext.errors().join( "\n" );
2117}
2118
2119QDomDocument QgsMapLayer::exportSldStyleV3( QgsSldExportContext &exportContext ) const
2120{
2122
2123 QDomDocument myDocument = QDomDocument();
2124
2125 const QDomNode header = myDocument.createProcessingInstruction( u"xml"_s, u"version=\"1.0\" encoding=\"UTF-8\""_s );
2126 myDocument.appendChild( header );
2127
2128 const QgsVectorLayer *vlayer = qobject_cast<const QgsVectorLayer *>( this );
2129 const QgsRasterLayer *rlayer = qobject_cast<const QgsRasterLayer *>( this );
2130 if ( !vlayer && !rlayer )
2131 {
2132 exportContext.pushError( tr( "Could not save symbology because:\n%1" ).arg( tr( "Only vector and raster layers are supported" ) ) );
2133 return myDocument;
2134 }
2135
2136 // Create the root element
2137 QDomElement root = myDocument.createElementNS( u"http://www.opengis.net/sld"_s, u"StyledLayerDescriptor"_s );
2138 QDomElement layerNode;
2139 if ( vlayer )
2140 {
2141 root.setAttribute( u"version"_s, u"1.1.0"_s );
2142 root.setAttribute( u"xsi:schemaLocation"_s, u"http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd"_s );
2143 root.setAttribute( u"xmlns:ogc"_s, u"http://www.opengis.net/ogc"_s );
2144 root.setAttribute( u"xmlns:se"_s, u"http://www.opengis.net/se"_s );
2145 root.setAttribute( u"xmlns:xlink"_s, u"http://www.w3.org/1999/xlink"_s );
2146 root.setAttribute( u"xmlns:xsi"_s, u"http://www.w3.org/2001/XMLSchema-instance"_s );
2147 myDocument.appendChild( root );
2148
2149 // Create the NamedLayer element
2150 layerNode = myDocument.createElement( u"NamedLayer"_s );
2151 root.appendChild( layerNode );
2152 }
2153
2154 // note: Only SLD 1.0 version is generated because seems none is using SE1.1.0 at least for rasters
2155 if ( rlayer )
2156 {
2157 // Create the root element
2158 root.setAttribute( u"version"_s, u"1.0.0"_s );
2159 root.setAttribute( u"xmlns:gml"_s, u"http://www.opengis.net/gml"_s );
2160 root.setAttribute( u"xmlns:ogc"_s, u"http://www.opengis.net/ogc"_s );
2161 root.setAttribute( u"xmlns:sld"_s, u"http://www.opengis.net/sld"_s );
2162 myDocument.appendChild( root );
2163
2164 // Create the NamedLayer element
2165 layerNode = myDocument.createElement( u"UserLayer"_s );
2166 root.appendChild( layerNode );
2167 }
2168
2169 QVariantMap props = exportContext.extraProperties();
2170
2171 QVariant context;
2172 context.setValue( exportContext );
2173
2174 // TODO -- move this to proper members of QgsSldExportContext
2175 props[u"SldExportContext"_s] = context;
2176
2178 {
2179 props[u"scaleMinDenom"_s] = QString::number( mMinScale );
2180 props[u"scaleMaxDenom"_s] = QString::number( mMaxScale );
2181 }
2182 exportContext.setExtraProperties( props );
2183
2184 if ( vlayer )
2185 {
2186 if ( !vlayer->writeSld( layerNode, myDocument, exportContext ) )
2187 {
2188 return myDocument;
2189 }
2190 }
2191 else if ( rlayer )
2192 {
2193 if ( !rlayer->writeSld( layerNode, myDocument, exportContext ) )
2194 {
2195 return myDocument;
2196 }
2197 }
2198
2199 return myDocument;
2200}
2201
2202QString QgsMapLayer::saveSldStyle( const QString &uri, bool &resultFlag ) const
2203{
2204 QgsSldExportContext context;
2205 context.setExportFilePath( uri );
2206 return saveSldStyleV2( resultFlag, context );
2207}
2208
2209QString QgsMapLayer::saveSldStyleV2( bool &resultFlag, QgsSldExportContext &exportContext ) const
2210{
2212
2213 const QgsMapLayer *mlayer = qobject_cast<const QgsMapLayer *>( this );
2214
2215 const QString uri { exportContext.exportFilePath() };
2216
2217 // check if the uri is a file or ends with .sld,
2218 // which indicates that it should become one
2219 QString filename;
2220 if ( mlayer->providerType() == "ogr"_L1 )
2221 {
2222 QStringList theURIParts = uri.split( '|' );
2223 filename = theURIParts[0];
2224 }
2225 else if ( mlayer->providerType() == "gpx"_L1 )
2226 {
2227 QStringList theURIParts = uri.split( '?' );
2228 filename = theURIParts[0];
2229 }
2230 else if ( mlayer->providerType() == "delimitedtext"_L1 )
2231 {
2232 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
2233 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
2234 if ( filename.isEmpty() )
2235 filename = uri;
2236 }
2237 else
2238 {
2239 filename = uri;
2240 }
2241
2242 const QFileInfo myFileInfo( filename );
2243 if ( myFileInfo.exists() || filename.endsWith( ".sld"_L1, Qt::CaseInsensitive ) )
2244 {
2245 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
2246 if ( !myDirInfo.isWritable() )
2247 {
2248 resultFlag = false;
2249 return tr( "The directory containing your dataset needs to be writable!" );
2250 }
2251
2252 // now construct the file name for our .sld style file
2253 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld";
2254
2255 QgsSldExportContext context { exportContext };
2256 context.setExportFilePath( myFileName );
2257
2258 QDomDocument myDocument = mlayer->exportSldStyleV3( context );
2259
2260 if ( !context.errors().empty() )
2261 {
2262 resultFlag = false;
2263 return context.errors().join( '\n' );
2264 }
2265
2266 QFile myFile( myFileName );
2267 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
2268 {
2269 QTextStream myFileStream( &myFile );
2270 // save as utf-8 with 2 spaces for indents
2271 myDocument.save( myFileStream, 2 );
2272 myFile.close();
2273 resultFlag = true;
2274 return tr( "Created default style file as %1" ).arg( myFileName );
2275 }
2276 }
2277
2278 resultFlag = false;
2279 return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename );
2280}
2281
2282QString QgsMapLayer::loadSldStyle( const QString &uri, bool &resultFlag )
2283{
2285
2286 resultFlag = false;
2287
2288 QDomDocument myDocument;
2289
2290 // location of problem associated with errorMsg
2291 int line = 0, column = 0;
2292 QString myErrorMessage;
2293
2294 QFile myFile( uri );
2295 if ( myFile.open( QFile::ReadOnly ) )
2296 {
2297 // read file
2298 QXmlStreamReader xmlReader( &myFile );
2299 xmlReader.addExtraNamespaceDeclaration( QXmlStreamNamespaceDeclaration( u"sld"_s, u"http://www.opengis.net/sld"_s ) );
2300 xmlReader.addExtraNamespaceDeclaration( QXmlStreamNamespaceDeclaration( u"fes"_s, u"http://www.opengis.net/fes/2.0"_s ) );
2301 xmlReader.addExtraNamespaceDeclaration( QXmlStreamNamespaceDeclaration( u"ogc"_s, u"http://www.opengis.net/ogc"_s ) );
2302 const QDomDocument::ParseResult result = myDocument.setContent( &xmlReader, QDomDocument::ParseOption::UseNamespaceProcessing );
2303 if ( result )
2304 {
2305 resultFlag = true;
2306 }
2307 else
2308 {
2309 myErrorMessage = result.errorMessage;
2310 line = result.errorLine;
2311 column = result.errorColumn;
2312 }
2313 if ( !resultFlag )
2314 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
2315 myFile.close();
2316 }
2317 else
2318 {
2319 myErrorMessage = tr( "Unable to open file %1" ).arg( uri );
2320 }
2321
2322 if ( !resultFlag )
2323 {
2324 return myErrorMessage;
2325 }
2326
2327 // check for root SLD element
2328 const QDomElement myRoot = myDocument.firstChildElement( u"StyledLayerDescriptor"_s );
2329 if ( myRoot.isNull() )
2330 {
2331 myErrorMessage = u"Error: StyledLayerDescriptor element not found in %1"_s.arg( uri );
2332 resultFlag = false;
2333 return myErrorMessage;
2334 }
2335
2336 // now get the style node out and pass it over to the layer
2337 // to deserialise...
2338 const QDomElement namedLayerElem = myRoot.firstChildElement( u"NamedLayer"_s );
2339 if ( namedLayerElem.isNull() )
2340 {
2341 myErrorMessage = u"Info: NamedLayer element not found."_s;
2342 resultFlag = false;
2343 return myErrorMessage;
2344 }
2345
2346 QString errorMsg;
2347 resultFlag = readSld( namedLayerElem, errorMsg );
2348 if ( !resultFlag )
2349 {
2350 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, errorMsg );
2351 return myErrorMessage;
2352 }
2353
2354 return QString();
2355}
2356
2357bool QgsMapLayer::readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
2358{
2360
2361 Q_UNUSED( node )
2362 Q_UNUSED( errorMessage )
2363 Q_UNUSED( context )
2364 Q_UNUSED( categories )
2365 return false;
2366}
2367
2368bool QgsMapLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
2369{
2371
2372 Q_UNUSED( node )
2373 Q_UNUSED( doc )
2374 Q_UNUSED( errorMessage )
2375 Q_UNUSED( context )
2376 Q_UNUSED( categories )
2377 return false;
2378}
2379
2380
2381void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, bool loadDefaultStyleFlag )
2382{
2384
2386
2388 if ( loadDefaultStyleFlag )
2389 {
2391 }
2392
2394 {
2396 }
2397 setDataSource( dataSource, baseName.isEmpty() ? mLayerName : baseName, provider.isEmpty() ? mProviderKey : provider, options, flags );
2398}
2399
2400void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag )
2401{
2403
2405 if ( loadDefaultStyleFlag )
2406 {
2408 }
2409
2411 {
2413 }
2414 setDataSource( dataSource, baseName, provider, options, flags );
2415}
2416
2417void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, Qgis::DataProviderReadFlags flags )
2418{
2420
2422 {
2424 }
2425 setDataSourcePrivate( dataSource, baseName, provider, options, flags );
2426 emit dataSourceChanged();
2427 emit dataChanged();
2429}
2430
2431
2432void QgsMapLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, Qgis::DataProviderReadFlags flags )
2433{
2435
2436 Q_UNUSED( dataSource )
2437 Q_UNUSED( baseName )
2438 Q_UNUSED( provider )
2439 Q_UNUSED( options )
2440 Q_UNUSED( flags )
2441}
2442
2443
2445{
2447
2448 return mProviderKey;
2449}
2450
2451void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
2452{
2454
2455 if ( categories.testFlag( Symbology3D ) )
2456 {
2457 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "3D Symbology" ) );
2458
2459 QgsAbstract3DRenderer *r3D = nullptr;
2460 QDomElement renderer3DElem = layerElement.firstChildElement( u"renderer-3d"_s );
2461 if ( !renderer3DElem.isNull() )
2462 {
2463 const QString type3D = renderer3DElem.attribute( u"type"_s );
2465 if ( meta3D )
2466 {
2467 r3D = meta3D->createRenderer( renderer3DElem, context );
2468 }
2469 }
2470 setRenderer3D( r3D );
2471 }
2472
2473 if ( categories.testFlag( CustomProperties ) )
2474 {
2475 // read custom properties before passing reading further to a subclass, so that
2476 // the subclass can also read custom properties
2477 readCustomProperties( layerElement );
2478 }
2479
2480 // use scale dependent visibility flag
2481 if ( categories.testFlag( Rendering ) )
2482 {
2483 setScaleBasedVisibility( layerElement.attribute( u"hasScaleBasedVisibilityFlag"_s ).toInt() == 1 );
2484 if ( layerElement.hasAttribute( u"minimumScale"_s ) )
2485 {
2486 // older element, when scales were reversed
2487 setMaximumScale( layerElement.attribute( u"minimumScale"_s ).toDouble() );
2488 setMinimumScale( layerElement.attribute( u"maximumScale"_s ).toDouble() );
2489 }
2490 else
2491 {
2492 setMaximumScale( layerElement.attribute( u"maxScale"_s ).toDouble() );
2493 setMinimumScale( layerElement.attribute( u"minScale"_s ).toDouble() );
2494 }
2495 if ( layerElement.hasAttribute( u"autoRefreshMode"_s ) )
2496 {
2497 setAutoRefreshInterval( layerElement.attribute( u"autoRefreshTime"_s ).toInt() );
2498 setAutoRefreshMode( qgsEnumKeyToValue( layerElement.attribute( u"autoRefreshMode"_s ), Qgis::AutoRefreshMode::Disabled ) );
2499 }
2500 }
2501
2502 if ( categories.testFlag( LayerConfiguration ) )
2503 {
2504 // flags
2505 const QDomElement flagsElem = layerElement.firstChildElement( u"flags"_s );
2506 LayerFlags flags = mFlags;
2507 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
2508 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
2509 {
2510 const QDomNode flagNode = flagsElem.namedItem( it.value() );
2511 if ( flagNode.isNull() )
2512 continue;
2513 const bool flagValue = flagNode.toElement().text() == "1" ? true : false;
2514 if ( flags.testFlag( it.key() ) && !flagValue )
2515 flags &= ~it.key();
2516 else if ( !flags.testFlag( it.key() ) && flagValue )
2517 flags |= it.key();
2518 }
2519 setFlags( flags );
2520 }
2521
2522 if ( categories.testFlag( Temporal ) )
2523 {
2524 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Temporal" ) );
2525
2527 properties->readXml( layerElement.toElement(), context );
2528 }
2529
2530 if ( categories.testFlag( Elevation ) )
2531 {
2532 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Elevation" ) );
2533
2535 properties->readXml( layerElement.toElement(), context );
2536 }
2537
2538 if ( categories.testFlag( Notes ) )
2539 {
2540 const QDomElement notesElem = layerElement.firstChildElement( u"userNotes"_s );
2541 if ( !notesElem.isNull() )
2542 {
2543 const QString notes = notesElem.attribute( u"value"_s );
2544 QgsLayerNotesUtils::setLayerNotes( this, notes );
2545 }
2546 }
2547}
2548
2550{
2552
2553 return mUndoStack;
2554}
2555
2557{
2559
2560 return mUndoStackStyles;
2561}
2562
2564{
2566
2567 return mCustomProperties.keys();
2568}
2569
2570void QgsMapLayer::setCustomProperty( const QString &key, const QVariant &value )
2571{
2573
2574 if ( !mCustomProperties.contains( key ) || mCustomProperties.value( key ) != value )
2575 {
2576 mCustomProperties.setValue( key, value );
2577 emit customPropertyChanged( key );
2578 }
2579}
2580
2582{
2584
2585 mCustomProperties = properties;
2586 for ( const QString &key : mCustomProperties.keys() )
2587 {
2588 emit customPropertyChanged( key );
2589 }
2590}
2591
2593{
2595
2596 return mCustomProperties;
2597}
2598
2599QVariant QgsMapLayer::customProperty( const QString &value, const QVariant &defaultValue ) const
2600{
2601 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
2603
2604 return mCustomProperties.value( value, defaultValue );
2605}
2606
2607void QgsMapLayer::removeCustomProperty( const QString &key )
2608{
2610
2611 if ( mCustomProperties.contains( key ) )
2612 {
2613 mCustomProperties.remove( key );
2614 emit customPropertyChanged( key );
2615 }
2616}
2617
2618int QgsMapLayer::listStylesInDatabase( QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError )
2619{
2621
2622 return QgsProviderRegistry::instance()->listStyles( mProviderKey, mDataSource, ids, names, descriptions, msgError );
2623}
2624
2625QString QgsMapLayer::getStyleFromDatabase( const QString &styleId, QString &msgError )
2626{
2628
2629 return QgsProviderRegistry::instance()->getStyleById( mProviderKey, mDataSource, styleId, msgError );
2630}
2631
2632bool QgsMapLayer::deleteStyleFromDatabase( const QString &styleId, QString &msgError )
2633{
2635
2637}
2638
2639void QgsMapLayer::saveStyleToDatabase( const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories )
2640{
2641 saveStyleToDatabaseV2( name, description, useAsDefault, uiFileContent, msgError, categories );
2642}
2643
2645 const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories
2646)
2647{
2649
2651
2652 QString sldStyle, qmlStyle;
2653 QDomDocument qmlDocument;
2654 QgsReadWriteContext context;
2655 exportNamedStyle( qmlDocument, msgError, context, categories );
2656 if ( !msgError.isEmpty() )
2657 {
2659 }
2660 else
2661 {
2662 qmlStyle = qmlDocument.toString();
2663 }
2664
2665 QgsSldExportContext sldContext;
2666 QDomDocument sldDocument = this->exportSldStyleV3( sldContext );
2667 if ( !sldContext.errors().empty() )
2668 {
2670 }
2671 else
2672 {
2673 sldStyle = sldDocument.toString();
2674 }
2675
2676 if ( !QgsProviderRegistry::instance()->saveStyle( mProviderKey, mDataSource, qmlStyle, sldStyle, name, description, uiFileContent, useAsDefault, msgError ) )
2677 {
2679 }
2680 return results;
2681}
2682
2683QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &resultFlag, bool loadFromLocalDB, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
2684{
2686
2687 QString returnMessage;
2688 QString qml, errorMsg;
2689 QString styleName;
2690 if ( !loadFromLocalDB && dataProvider() && dataProvider()->styleStorageCapabilities().testFlag( Qgis::ProviderStyleStorageCapability::LoadFromDatabase ) )
2691 {
2693 }
2694
2695 // Style was successfully loaded from provider storage
2696 if ( !qml.isEmpty() )
2697 {
2698 QDomDocument myDocument( u"qgis"_s );
2699 myDocument.setContent( qml );
2700 resultFlag = importNamedStyle( myDocument, errorMsg );
2701 returnMessage = QObject::tr( "Loaded from Provider" );
2702 }
2703 else
2704 {
2706
2707 bool styleExists = false;
2708 bool styleSuccessfullyLoaded = false;
2709
2710 returnMessage = loadNamedProperty( theURI, PropertyType::Style, styleExists, styleSuccessfullyLoaded, categories, flags );
2711
2712 // TODO QGIS 5.0 -- fix API for loadNamedStyle so we can return styleExists too
2713 ( void ) styleExists;
2714 resultFlag = styleSuccessfullyLoaded;
2715 }
2716
2717 if ( !styleName.isEmpty() )
2718 {
2719 styleManager()->renameStyle( styleManager()->currentStyle(), styleName );
2720 }
2721
2722 if ( resultFlag )
2723 emit styleLoaded( categories );
2724
2725 return returnMessage;
2726}
2727
2734
2736{
2738
2739 return false;
2740}
2741
2743{
2745
2746 return false;
2747}
2748
2750{
2752
2753 return true;
2754}
2755
2757{
2759
2760 // invalid layers are temporary? -- who knows?!
2761 if ( !isValid() )
2762 return false;
2763
2764 if ( mProviderKey == "memory"_L1 )
2765 return true;
2766
2767 const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( mProviderKey, mDataSource );
2768 const QString path = sourceParts.value( u"path"_s ).toString();
2769 if ( path.isEmpty() )
2770 return false;
2771
2772 // check if layer path is inside one of the standard temporary file locations for this platform
2773 const QStringList tempPaths = QStandardPaths::standardLocations( QStandardPaths::TempLocation );
2774 for ( const QString &tempPath : tempPaths )
2775 {
2776 if ( path.startsWith( tempPath ) )
2777 return true;
2778 }
2779
2780 return false;
2781}
2782
2783void QgsMapLayer::setValid( bool valid )
2784{
2786
2787 if ( mValid == valid )
2788 return;
2789
2790 mValid = valid;
2791 emit isValidChanged();
2792}
2793
2795{
2797
2798 if ( legend == mLegend.get() )
2799 return;
2800
2801 mLegend.reset( legend );
2802
2803
2804 if ( mLegend )
2805 {
2806 mLegend->setParent( this );
2807 connect( mLegend.get(), &QgsMapLayerLegend::itemsChanged, this, &QgsMapLayer::legendChanged, Qt::UniqueConnection );
2808 }
2809
2810 emit legendChanged();
2811}
2812
2814{
2816
2817 return mLegend.get();
2818}
2819
2821{
2823
2824 return mStyleManager.get();
2825}
2826
2828{
2830
2831 if ( renderer == m3DRenderer.get() )
2832 return;
2833
2834 m3DRenderer.reset( renderer );
2835
2836 emit renderer3DChanged();
2837 emit repaintRequested();
2839}
2840
2842{
2844
2845 return m3DRenderer.get();
2846}
2847
2848void QgsMapLayer::triggerRepaint( bool deferredUpdate )
2849{
2851
2852 if ( mRepaintRequestedFired )
2853 return;
2854 mRepaintRequestedFired = true;
2855 emit repaintRequested( deferredUpdate );
2856 mRepaintRequestedFired = false;
2857}
2858
2865
2867{
2869
2870 mMetadata = metadata;
2871 // mMetadata.saveToLayer( this );
2872 emit metadataChanged();
2873}
2874
2876{
2878
2879 return QString();
2880}
2881
2882QDateTime QgsMapLayer::timestamp() const
2883{
2885
2886 return QDateTime();
2887}
2888
2896
2898{
2899 updateExtent( extent );
2900}
2901
2903{
2905
2906 updateExtent( extent );
2907}
2908
2909bool QgsMapLayer::isReadOnly() const
2910{
2912
2913 return true;
2914}
2915
2917{
2919
2920 return mOriginalXmlProperties;
2921}
2922
2924{
2926
2927 mOriginalXmlProperties = originalXmlProperties;
2928}
2929
2930QString QgsMapLayer::generateId( const QString &layerName )
2931{
2932 // Generate the unique ID of this layer
2933 const QString uuid = QUuid::createUuid().toString();
2934 // trim { } from uuid
2935 QString id = layerName + '_' + uuid.mid( 1, uuid.length() - 2 );
2936 // Tidy the ID up to avoid characters that may cause problems
2937 // elsewhere (e.g in some parts of XML). Replaces every non-word
2938 // character (word characters are the alphabet, numbers and
2939 // underscore) with an underscore.
2940 // Note that the first backslash in the regular expression is
2941 // there for the compiler, so the pattern is actually \W
2942 const thread_local QRegularExpression idRx( u"[\\W]"_s );
2943 id.replace( idRx, u"_"_s );
2944 return id;
2945}
2946
2948{
2950
2951 return true;
2952}
2953
2960
2962{
2964
2965 return mapTipsEnabled() && !mMapTipTemplate.isEmpty();
2966}
2967
2974
2975QSet<QgsMapLayerDependency> QgsMapLayer::dependencies() const
2976{
2978
2979 return mDependencies;
2980}
2981
2982bool QgsMapLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
2983{
2985
2986 QSet<QgsMapLayerDependency> deps;
2987 const auto constODeps = oDeps;
2988 for ( const QgsMapLayerDependency &dep : constODeps )
2989 {
2990 if ( dep.origin() == QgsMapLayerDependency::FromUser )
2991 deps << dep;
2992 }
2993
2994 mDependencies = deps;
2995 emit dependenciesChanged();
2996 return true;
2997}
2998
3000{
3002
3003 QgsDataProvider *lDataProvider = dataProvider();
3004
3005 if ( !lDataProvider )
3006 return;
3007
3008 if ( enabled && !isRefreshOnNotifyEnabled() )
3009 {
3010 lDataProvider->setListening( enabled );
3011 connect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
3012 }
3013 else if ( !enabled && isRefreshOnNotifyEnabled() )
3014 {
3015 // we don't want to disable provider listening because someone else could need it (e.g. actions)
3016 disconnect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
3017 }
3018 mIsRefreshOnNofifyEnabled = enabled;
3019}
3020
3022{
3023 // aggregate based tests aren't thread safe
3025
3026 if ( QgsMapLayerStore *store = qobject_cast<QgsMapLayerStore *>( parent() ) )
3027 {
3028 return qobject_cast<QgsProject *>( store->parent() );
3029 }
3030 return nullptr;
3031}
3032
3033void QgsMapLayer::onNotified( const QString &message )
3034{
3036
3037 if ( refreshOnNotifyMessage().isEmpty() || refreshOnNotifyMessage() == message )
3038 {
3040 emit dataChanged();
3041 }
3042}
3043
3044QgsRectangle QgsMapLayer::wgs84Extent( bool forceRecalculate ) const
3045{
3047
3049
3050 if ( !forceRecalculate && !mWgs84Extent.isNull() )
3051 {
3052 wgs84Extent = mWgs84Extent;
3053 }
3054 else if ( !mExtent2D.isNull() || !mExtent3D.isNull() )
3055 {
3056 QgsCoordinateTransform transformer { crs(), QgsCoordinateReferenceSystem( u"EPSG:4326"_s ), transformContext() };
3057 transformer.setBallparkTransformsAreAppropriate( true );
3058 try
3059 {
3060 if ( mExtent2D.isNull() )
3061 wgs84Extent = transformer.transformBoundingBox( mExtent3D.toRectangle() );
3062 else
3063 wgs84Extent = transformer.transformBoundingBox( mExtent2D );
3064 }
3065 catch ( const QgsCsException &cse )
3066 {
3067 QgsMessageLog::logMessage( tr( "Error transforming extent: %1" ).arg( cse.what() ) );
3069 }
3070 }
3071 return wgs84Extent;
3072}
3073
3074void QgsMapLayer::updateExtent( const QgsRectangle &extent ) const
3075{
3077
3078 if ( extent == mExtent2D )
3079 return;
3080
3081 mExtent2D = extent;
3082
3083 // do not update the wgs84 extent if we trust layer metadata
3085 return;
3086
3087 mWgs84Extent = wgs84Extent( true );
3088}
3089
3090void QgsMapLayer::updateExtent( const QgsBox3D &extent ) const
3091{
3093
3094 if ( extent == mExtent3D )
3095 return;
3096
3097 if ( extent.isNull() )
3098 {
3099 if ( !extent.toRectangle().isNull() )
3100 {
3101 // bad 3D extent param but valid in 2d --> update 2D extent
3102 updateExtent( extent.toRectangle() );
3103 }
3104 else
3105 {
3106 QgsDebugMsgLevel( u"Unable to update extent with empty parameter"_s, 1 );
3107 }
3108 }
3109 else
3110 {
3111 mExtent3D = extent;
3112
3113 // do not update the wgs84 extent if we trust layer metadata
3115 return;
3116
3117 mWgs84Extent = wgs84Extent( true );
3118 }
3119}
3120
3121bool QgsMapLayer::rebuildCrs3D( QString *error )
3122{
3123 bool res = true;
3124 if ( !mCRS.isValid() )
3125 {
3126 mCrs3D = QgsCoordinateReferenceSystem();
3127 }
3128 else if ( !mVerticalCrs.isValid() )
3129 {
3130 mCrs3D = mCRS;
3131 }
3132 else
3133 {
3134 switch ( mCRS.type() )
3135 {
3139 mCrs3D = mCRS;
3140 break;
3141
3143 {
3144 QString tempError;
3145 mCrs3D = mCRS.hasVerticalAxis() ? mCRS : QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3146 res = mCrs3D.isValid();
3147 break;
3148 }
3149
3151 // nonsense situation
3152 mCrs3D = QgsCoordinateReferenceSystem();
3153 res = false;
3154 break;
3155
3164 {
3165 QString tempError;
3166 mCrs3D = QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3167 res = mCrs3D.isValid();
3168 break;
3169 }
3170 }
3171 }
3172 return res;
3173}
3174
3176{
3178
3179 // do not update the wgs84 extent if we trust layer metadata
3181 return;
3182
3183 mWgs84Extent = QgsRectangle();
3184}
3185
3187{
3189
3190 QString metadata = u"<h1>"_s + tr( "General" ) + u"</h1>\n<hr>\n"_s + u"<table class=\"list-view\">\n"_s;
3191
3192 // name
3193 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Name" ) + u"</td><td>"_s + name() + u"</td></tr>\n"_s;
3194
3195 const QString lPublicSource = publicSource();
3196
3197 QString path;
3198 bool isLocalPath = false;
3199 if ( dataProvider() )
3200 {
3201 // local path
3202 QVariantMap uriComponents = QgsProviderRegistry::instance()->decodeUri( dataProvider()->name(), lPublicSource );
3203 if ( uriComponents.contains( u"path"_s ) )
3204 {
3205 path = uriComponents[u"path"_s].toString();
3206 QFileInfo fi( path );
3207 if ( fi.exists() )
3208 {
3209 isLocalPath = true;
3210 metadata += u"<tr><td class=\"highlight\">"_s
3211 + tr( "Path" )
3212 + u"</td><td>%1"_s.arg( u"<a href=\"%1\">%2</a>"_s.arg( QUrl::fromLocalFile( path ).toString(), QDir::toNativeSeparators( path ) ) )
3213 + u"</td></tr>\n"_s;
3214
3215 QDateTime lastModified = fi.lastModified();
3216 QString lastModifiedFileName;
3217 QSet<QString> sidecarFiles = QgsFileUtils::sidecarFilesForPath( path );
3218 if ( fi.isFile() )
3219 {
3220 qint64 fileSize = fi.size();
3221 if ( !sidecarFiles.isEmpty() )
3222 {
3223 lastModifiedFileName = fi.fileName();
3224 QStringList sidecarFileNames;
3225 for ( const QString &sidecarFile : sidecarFiles )
3226 {
3227 QFileInfo sidecarFi( sidecarFile );
3228 fileSize += sidecarFi.size();
3229 if ( sidecarFi.lastModified() > lastModified )
3230 {
3231 lastModified = sidecarFi.lastModified();
3232 lastModifiedFileName = sidecarFi.fileName();
3233 }
3234 sidecarFileNames << sidecarFi.fileName();
3235 }
3236 metadata += u"<tr><td class=\"highlight\">"_s
3237 + ( sidecarFiles.size() > 1 ? tr( "Sidecar files" ) : tr( "Sidecar file" ) )
3238 + u"</td><td>%1"_s.arg( sidecarFileNames.join( ", "_L1 ) )
3239 + u"</td></tr>\n"_s;
3240 }
3241 metadata += u"<tr><td class=\"highlight\">"_s
3242 + ( !sidecarFiles.isEmpty() ? tr( "Total size" ) : tr( "Size" ) )
3243 + u"</td><td>%1"_s.arg( QgsFileUtils::representFileSize( fileSize ) )
3244 + u"</td></tr>\n"_s;
3245 }
3246 metadata += u"<tr><td class=\"highlight\">"_s
3247 + tr( "Last modified" )
3248 + u"</td><td>%1"_s.arg( QLocale().toString( fi.lastModified() ) )
3249 + ( !lastModifiedFileName.isEmpty() ? u" (%1)"_s.arg( lastModifiedFileName ) : QString() )
3250 + u"</td></tr>\n"_s;
3251 }
3252 }
3253 if ( uriComponents.contains( u"url"_s ) )
3254 {
3255 QUrl decodedUri = QUrl::fromPercentEncoding( uriComponents[u"url"_s].toString().toLocal8Bit() );
3256 const QString url = decodedUri.toString();
3257 metadata += u"<tr><td class=\"highlight\">"_s + tr( "URL" ) + u"</td><td>%1"_s.arg( u"<a href=\"%1\">%2</a>"_s.arg( url, url ) ) + u"</td></tr>\n"_s;
3258 }
3259 }
3260
3261 // data source
3262 if ( lPublicSource != path || !isLocalPath )
3263 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Source" ) + u"</td><td>%1"_s.arg( lPublicSource != path ? lPublicSource : path ) + u"</td></tr>\n"_s;
3264
3265 // provider
3266 if ( dataProvider() )
3267 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Provider" ) + u"</td><td>%1"_s.arg( dataProvider()->name() ) + u"</td></tr>\n"_s;
3268
3269 // Layer ID
3270 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Layer ID" ) + u"</td><td>%1"_s.arg( id() ) + u"</td></tr>\n"_s;
3271
3272 metadata += "</table>\n<br><br>"_L1;
3273
3274 return metadata;
3275}
3276
3278{
3279 QString metadata;
3280 // custom properties
3281 if ( const auto keys = customPropertyKeys(); !keys.isEmpty() )
3282 {
3283 metadata += u"<h1>"_s + tr( "Custom properties" ) + u"</h1>\n<hr>\n"_s;
3284 metadata += "<table class=\"list-view\">\n<tbody>"_L1;
3285 for ( const QString &key : keys )
3286 {
3287 // keys prefaced with _ are considered private/internal details
3288 if ( key.startsWith( '_' ) )
3289 continue;
3290
3291 const QVariant propValue = customProperty( key );
3292 QString stringValue;
3293 if ( propValue.type() == QVariant::List || propValue.type() == QVariant::StringList )
3294 {
3295 for ( const QString &s : propValue.toStringList() )
3296 {
3297 stringValue += "<p style=\"margin: 0;\">" + s.toHtmlEscaped() + "</p>";
3298 }
3299 }
3300 else
3301 {
3302 stringValue = propValue.toString().toHtmlEscaped();
3303
3304 //if the result string is empty but propValue is not, the conversion has failed
3305 if ( stringValue.isEmpty() && !QgsVariantUtils::isNull( propValue ) )
3306 stringValue = tr( "<i>value cannot be displayed</i>" );
3307 }
3308
3309 metadata += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>"_s.arg( key.toHtmlEscaped(), stringValue );
3310 }
3311 metadata += "</tbody></table>\n"_L1;
3312 metadata += "<br><br>\n"_L1;
3313 }
3314 return metadata;
3315}
3316
3318{
3320 QString metadata;
3321
3322 auto addCrsInfo = [&metadata]( const QgsCoordinateReferenceSystem &c, bool includeType, bool includeOperation, bool includeCelestialBody ) {
3323 if ( !c.isValid() )
3324 metadata += u"<tr><td colspan=\"2\" class=\"highlight\">"_s + tr( "Unknown" ) + u"</td></tr>\n"_s;
3325 else
3326 {
3327 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Name" ) + u"</td><td>"_s + c.userFriendlyIdentifier( Qgis::CrsIdentifierType::FullString ) + u"</td></tr>\n"_s;
3328
3329 // map units
3330 metadata += u"<tr><td class=\"highlight\">"_s
3331 + tr( "Units" )
3332 + u"</td><td>"_s
3333 + ( c.isGeographic() ? tr( "Geographic (uses latitude and longitude for coordinates)" ) : QgsUnitTypes::toString( c.mapUnits() ) )
3334 + u"</td></tr>\n"_s;
3335
3336 if ( includeType )
3337 {
3338 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Type" ) + u"</td><td>"_s + QgsCoordinateReferenceSystemUtils::crsTypeToString( c.type() ) + u"</td></tr>\n"_s;
3339 }
3340
3341 if ( includeOperation )
3342 {
3343 // operation
3344 const QgsProjOperation operation = c.operation();
3345 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Method" ) + u"</td><td>"_s + operation.description() + u"</td></tr>\n"_s;
3346 }
3347
3348 if ( includeCelestialBody )
3349 {
3350 // celestial body
3351 try
3352 {
3353 const QString celestialBody = c.celestialBodyName();
3354 if ( !celestialBody.isEmpty() )
3355 {
3356 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Celestial Body" ) + u"</td><td>"_s + celestialBody + u"</td></tr>\n"_s;
3357 }
3358 }
3359 catch ( QgsNotSupportedException & )
3360 {}
3361 }
3362
3363 QString accuracyString;
3364 // dynamic crs with no epoch?
3365 if ( c.isDynamic() && std::isnan( c.coordinateEpoch() ) )
3366 {
3367 accuracyString = tr( "Based on a dynamic CRS, but no coordinate epoch is set. Coordinates are ambiguous and of limited accuracy." );
3368 }
3369
3370 // based on datum ensemble?
3371 try
3372 {
3373 const QgsDatumEnsemble ensemble = c.datumEnsemble();
3374 if ( ensemble.isValid() )
3375 {
3376 QString id;
3377 if ( !ensemble.code().isEmpty() )
3378 id = u"<i>%1</i> (%2:%3)"_s.arg( ensemble.name(), ensemble.authority(), ensemble.code() );
3379 else
3380 id = u"<i>%1</i>”"_s.arg( ensemble.name() );
3381
3382 if ( ensemble.accuracy() > 0 )
3383 {
3384 accuracyString = tr( "Based on %1, which has a limited accuracy of <b>at best %2 meters</b>." ).arg( id ).arg( ensemble.accuracy() );
3385 }
3386 else
3387 {
3388 accuracyString = tr( "Based on %1, which has a limited accuracy." ).arg( id );
3389 }
3390 }
3391 }
3392 catch ( QgsNotSupportedException & )
3393 {}
3394
3395 if ( !accuracyString.isEmpty() )
3396 {
3397 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Accuracy" ) + u"</td><td>"_s + accuracyString + u"</td></tr>\n"_s;
3398 }
3399
3400 // static/dynamic
3401 metadata += u"<tr><td class=\"highlight\">"_s
3402 + tr( "Reference" )
3403 + u"</td><td>%1</td></tr>\n"_s.arg( c.isDynamic() ? tr( "Dynamic (relies on a datum which is not plate-fixed)" ) : tr( "Static (relies on a datum which is plate-fixed)" ) );
3404
3405 // coordinate epoch
3406 if ( !std::isnan( c.coordinateEpoch() ) )
3407 {
3408 metadata += u"<tr><td class=\"highlight\">"_s + tr( "Coordinate Epoch" ) + u"</td><td>%1</td></tr>\n"_s.arg( qgsDoubleToString( c.coordinateEpoch(), 3 ) );
3409 }
3410 }
3411 };
3412
3413 metadata += u"<h1>"_s + tr( "Coordinate Reference System (CRS)" ) + u"</h1>\n<hr>\n"_s;
3414 metadata += "<table class=\"list-view\">\n"_L1;
3415 addCrsInfo( crs().horizontalCrs(), true, true, true );
3416 metadata += "</table>\n<br><br>\n"_L1;
3417
3418 if ( verticalCrs().isValid() )
3419 {
3420 metadata += u"<h1>"_s + tr( "Vertical Coordinate Reference System (CRS)" ) + u"</h1>\n<hr>\n"_s;
3421 metadata += "<table class=\"list-view\">\n"_L1;
3422 addCrsInfo( verticalCrs(), false, false, false );
3423 metadata += "</table>\n<br><br>\n"_L1;
3424 }
3425
3426 return metadata;
3427}
static QString version()
Version string.
Definition qgis.cpp:682
@ FullString
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions.
Definition qgis.h:2502
@ Vertical
Vertical CRS.
Definition qgis.h:2415
@ Temporal
Temporal CRS.
Definition qgis.h:2418
@ Compound
Compound (horizontal + vertical) CRS.
Definition qgis.h:2417
@ Projected
Projected CRS.
Definition qgis.h:2416
@ Other
Other type.
Definition qgis.h:2421
@ Bound
Bound CRS.
Definition qgis.h:2420
@ DerivedProjected
Derived projected CRS.
Definition qgis.h:2422
@ Unknown
Unknown type.
Definition qgis.h:2410
@ Engineering
Engineering CRS.
Definition qgis.h:2419
@ Geographic3d
3D geopraphic CRS
Definition qgis.h:2414
@ Geodetic
Geodetic CRS.
Definition qgis.h:2411
@ Geographic2d
2D geographic CRS
Definition qgis.h:2413
@ Geocentric
Geocentric CRS.
Definition qgis.h:2412
@ RemoveCredentials
Completely remove credentials (eg passwords) from the URI. This flag is not compatible with the Redac...
Definition qgis.h:1437
@ RedactCredentials
Replace the value of credentials (eg passwords) with 'xxxxxxxx'. This flag is not compatible with the...
Definition qgis.h:1438
@ ForceFirstLetterToCapital
Convert just the first letter of each word to uppercase, leave the rest untouched.
Definition qgis.h:3507
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:379
@ Unknown
Unknown types.
Definition qgis.h:383
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
Definition qgis.h:512
LayerType
Types of layers that can be added to a map.
Definition qgis.h:206
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:212
@ Vector
Vector layer.
Definition qgis.h:207
QFlags< MapLayerProperty > MapLayerProperties
Map layer properties.
Definition qgis.h:2367
QFlags< LoadStyleFlag > LoadStyleFlags
Flags for loading layer styles.
Definition qgis.h:263
@ LoadDefaultStyle
Reset the layer's style to the default for the datasource.
Definition qgis.h:495
@ ForceReadOnly
Open layer in a read-only mode.
Definition qgis.h:498
@ SkipGetExtent
Skip the extent from provider.
Definition qgis.h:496
@ TrustDataSource
Trust datasource config (primary key unicity, geometry type and srid, etc). Improves provider load ti...
Definition qgis.h:492
@ IgnoreMissingStyleErrors
If the style is missing, then don't flag it as an error. This flag can be used when the caller is not...
Definition qgis.h:253
AutoRefreshMode
Map layer automatic refresh modes.
Definition qgis.h:2377
@ RedrawOnly
Redraw current data only.
Definition qgis.h:2380
@ ReloadData
Reload data (and draw the new data).
Definition qgis.h:2379
@ Disabled
Automatic refreshing is disabled.
Definition qgis.h:2378
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.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:45
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.
static CUSTOM_CRS_VALIDATION customCrsValidation()
Gets custom function.
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.
Contains information about the context in which a coordinate transform is executed.
Handles coordinate transforms between two coordinate systems.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
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:100
QString code() const
Identification code, e.g.
Definition qgsdatums.h:126
QString authority() const
Authority name, e.g.
Definition qgsdatums.h:121
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:106
QString name() const
Display name of datum ensemble.
Definition qgsdatums.h:111
double accuracy() const
Positional accuracy (in meters).
Definition qgsdatums.h:116
A container for error messages.
Definition qgserror.h:83
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.
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.
Base class for storage of map layer temporal properties.
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:87
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.
@ SldGenerationFailed
Generation of the SLD failed, and was not written to the database.
@ DatabaseWriteFailed
An error occurred when attempting to write to the database.
@ QmlGenerationFailed
Generation of the QML failed, and was not written to the database.
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...
QgsCoordinateReferenceSystem crs3D
Definition qgsmaplayer.h:92
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:90
bool loadNamedMetadataFromDatabase(const QString &db, const QString &uri, QString &qmd)
Retrieve a named metadata for this layer from a sqlite database.
friend class QgsVectorLayer
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:86
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.
QgsMapLayer::SaveStyleResults saveStyleToDatabaseV2(const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Saves QML and SLD representations of the layer's style to a table in the database.
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:89
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:93
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 Q_DECL_DEPRECATED void saveStyleToDatabase(const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Saves QML and SLD representations of the layer's style to a table in the database.
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.
QgsMapLayer::LayerFlags flags
Definition qgsmaplayer.h:99
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:97
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:95
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:88
QgsCoordinateReferenceSystem verticalCrs
Definition qgsmaplayer.h:91
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 ...
QFlags< SaveStyleResult > SaveStyleResults
Results of saving styles to database.
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:96
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(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
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 readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from an XML node.
An interface for classes which can visit various object entity (e.g.
A QgsObjectEntityVisitorInterface context object.
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:113
bool removeAttachedFile(const QString &path)
Removes the attached file.
static QgsProject * instance()
Returns the QgsProject singleton instance.
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.
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.
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 QgsBox3D readBox3D(const QDomElement &element)
Decodes a DOM element to a 3D box.
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc, const QString &elementName=u"extent"_s)
Encodes a rectangle to a DOM element.
static QgsRectangle readRectangle(const QDomElement &element)
static QDomElement writeBox3D(const QgsBox3D &box, QDomDocument &doc, const QString &elementName=u"extent3D"_s)
Encodes a 3D box to a DOM 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:7176
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6893
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7157
const QMap< T, QString > qgsEnumMap()
Returns a map of all enum entries.
Definition qgis.h:7140
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Setting options for creating vector data providers.
QString format
Format specification of online resource.