QGIS API Documentation 3.43.0-Master (56aa1fd18d7)
qgsmaplayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayer.cpp - description
3 -------------------
4 begin : Fri Jun 28 2002
5 copyright : (C) 2002 by Gary E.Sherman
6 email : sherman at mrcc.com
7***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19#include "qgssqliteutils.h"
22#include "qgsapplication.h"
25#include "qgsdatasourceuri.h"
26#include "qgsfileutils.h"
27#include "qgslogger.h"
28#include "qgsauthmanager.h"
29#include "qgsmaplayer.h"
30#include "moc_qgsmaplayer.cpp"
31#include "qgsmaplayerlegend.h"
33#include "qgspathresolver.h"
35#include "qgsproject.h"
36#include "qgsproviderregistry.h"
37#include "qgsprovidermetadata.h"
38#include "qgsrasterlayer.h"
39#include "qgsreadwritecontext.h"
40#include "qgsrectangle.h"
41#include "qgsscaleutils.h"
42#include "qgssldexportcontext.h"
43#include "qgsvectorlayer.h"
44#include "qgsxmlutils.h"
45#include "qgsstringutils.h"
46#include "qgsmessagelog.h"
49#include "qgslayernotesutils.h"
50#include "qgsdatums.h"
51#include "qgsprojoperation.h"
52#include "qgsthreadingutils.h"
53#include "qgsunittypes.h"
54
55#include <QDir>
56#include <QDomDocument>
57#include <QDomElement>
58#include <QDomImplementation>
59#include <QDomNode>
60#include <QFile>
61#include <QFileInfo>
62#include <QLocale>
63#include <QTextStream>
64#include <QUrl>
65#include <QTimer>
66#include <QStandardPaths>
67#include <QUuid>
68#include <QRegularExpression>
69
70#include <sqlite3.h>
71
73{
74 switch ( type )
75 {
76 case Metadata:
77 return QStringLiteral( ".qmd" );
78
79 case Style:
80 return QStringLiteral( ".qml" );
81 }
82 return QString();
83}
84
86 const QString &lyrname,
87 const QString &source )
88 : mDataSource( source )
89 , mLayerName( lyrname )
90 , mLayerType( type )
91 , mServerProperties( std::make_unique<QgsMapLayerServerProperties>( this ) )
92 , mUndoStack( new QUndoStack( this ) )
93 , mUndoStackStyles( new QUndoStack( this ) )
94 , mStyleManager( std::make_unique<QgsMapLayerStyleManager>( this ) )
95 , mRefreshTimer( new QTimer( this ) )
96{
97 mID = generateId( lyrname );
100 connect( mRefreshTimer, &QTimer::timeout, this, [this]
101 {
102
103 switch ( mAutoRefreshMode )
104 {
106 break;
108 triggerRepaint( true );
109 break;
111 reload();
112 break;
113 }
114 } );
115}
116
118{
119 if ( project() && project()->pathResolver().writePath( mDataSource ).startsWith( "attachment:" ) )
120 {
122 }
123
124 delete m3DRenderer;
125 delete mLegend;
126
127}
128
129void QgsMapLayer::clone( QgsMapLayer *layer ) const
130{
132
133 QgsDebugMsgLevel( QStringLiteral( "Cloning layer '%1'" ).arg( name() ), 3 );
134 layer->setBlendMode( blendMode() );
135
136 const auto constStyles = styleManager()->styles();
137 for ( const QString &s : constStyles )
138 {
139 layer->styleManager()->addStyle( s, styleManager()->style( s ) );
140 }
141
142 layer->setName( name() );
143
144 if ( layer->dataProvider() && layer->dataProvider()->elevationProperties() )
145 {
147 layer->mExtent3D = mExtent3D;
148 else
149 layer->mExtent2D = mExtent2D;
150 }
151
152 layer->setMaximumScale( maximumScale() );
153 layer->setMinimumScale( minimumScale() );
155 layer->setDependencies( dependencies() );
157 layer->setCrs( crs() );
158 layer->setCustomProperties( mCustomProperties );
159 layer->setOpacity( mLayerOpacity );
160 layer->setMetadata( mMetadata );
161 layer->serverProperties()->copyTo( mServerProperties.get() );
162}
163
165{
166 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
168
169 return mLayerType;
170}
171
178
180{
182
183 if ( flags == mFlags )
184 return;
185
186 mFlags = flags;
187 emit flagsChanged();
188}
189
196
197QString QgsMapLayer::id() const
198{
199 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
201
202 return mID;
203}
204
205bool QgsMapLayer::setId( const QString &id )
206{
208 if ( qobject_cast< QgsMapLayerStore * >( parent() ) )
209 {
210 // layer is already registered, cannot change id
211 return false;
212 }
213
214 if ( id == mID )
215 return false;
216
217 mID = id;
218 emit idChanged( id );
219 return true;
220}
221
222void QgsMapLayer::setName( const QString &name )
223{
225
226 if ( name == mLayerName )
227 return;
228
230
231 emit nameChanged();
232}
233
234QString QgsMapLayer::name() const
235{
236 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
238
239 QgsDebugMsgLevel( "returning name '" + mLayerName + '\'', 4 );
240 return mLayerName;
241}
242
249
251{
253
254 return nullptr;
255}
256
261
262void QgsMapLayer::setShortName( const QString &shortName )
263{
265
266 mServerProperties->setShortName( shortName );
267}
268
270{
272
273 return mServerProperties->shortName();
274}
275
276void QgsMapLayer::setTitle( const QString &title )
277{
279
280 mServerProperties->setTitle( title );
281}
282
283QString QgsMapLayer::title() const
284{
286
287 return mServerProperties->title();
288}
289
290void QgsMapLayer::setAbstract( const QString &abstract )
291{
293
294 mServerProperties->setAbstract( abstract );
295}
296
298{
300
301 return mServerProperties->abstract();
302}
303
304void QgsMapLayer::setKeywordList( const QString &keywords )
305{
307
308 mServerProperties->setKeywordList( keywords );
309}
310
312{
314
315 return mServerProperties->keywordList();
316}
317
318void QgsMapLayer::setDataUrl( const QString &dataUrl )
319{
321
322 mServerProperties->setDataUrl( dataUrl );
323}
324
325QString QgsMapLayer::dataUrl() const
326{
328
329 return mServerProperties->dataUrl();
330}
331
332void QgsMapLayer::setDataUrlFormat( const QString &dataUrlFormat )
333{
335
336 mServerProperties->setDataUrlFormat( dataUrlFormat );
337}
338
340{
342
343 return mServerProperties->dataUrlFormat();
344}
345
346void QgsMapLayer::setAttribution( const QString &attrib )
347{
349
350 mServerProperties->setAttribution( attrib );
351}
352
354{
356
357 return mServerProperties->attribution();
358}
359
360void QgsMapLayer::setAttributionUrl( const QString &attribUrl )
361{
363
364 mServerProperties->setAttributionUrl( attribUrl );
365}
366
368{
370
371 return mServerProperties->attributionUrl();
372}
373
374void QgsMapLayer::setLegendUrl( const QString &legendUrl )
375{
377
378 mServerProperties->setLegendUrl( legendUrl );
379}
380
382{
384
385 return mServerProperties->legendUrl();
386}
387
388void QgsMapLayer::setLegendUrlFormat( const QString &legendUrlFormat )
389{
391
392 mServerProperties->setLegendUrlFormat( legendUrlFormat );
393}
394
396{
398
399 return mServerProperties->legendUrlFormat();
400}
401
402void QgsMapLayer::setMetadataUrl( const QString &metaUrl )
403{
405
406 QList<QgsMapLayerServerProperties::MetadataUrl> urls = serverProperties()->metadataUrls();
407 if ( urls.isEmpty() )
408 {
409 const QgsMapLayerServerProperties::MetadataUrl newItem = QgsMapLayerServerProperties::MetadataUrl( metaUrl, QLatin1String(), QLatin1String() );
410 urls.prepend( newItem );
411 }
412 else
413 {
414 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
415 const QgsMapLayerServerProperties::MetadataUrl newItem( metaUrl, old.type, old.format );
416 urls.prepend( newItem );
417 }
419}
420
422{
424
425 if ( mServerProperties->metadataUrls().isEmpty() )
426 {
427 return QLatin1String();
428 }
429 else
430 {
431 return mServerProperties->metadataUrls().first().url;
432 }
433}
434
435void QgsMapLayer::setMetadataUrlType( const QString &metaUrlType )
436{
438
439 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
440 if ( urls.isEmpty() )
441 {
442 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), metaUrlType, QLatin1String() );
443 urls.prepend( newItem );
444 }
445 else
446 {
447 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
448 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, metaUrlType, old.format );
449 urls.prepend( newItem );
450 }
451 mServerProperties->setMetadataUrls( urls );
452}
453
455{
457
458 if ( mServerProperties->metadataUrls().isEmpty() )
459 {
460 return QLatin1String();
461 }
462 else
463 {
464 return mServerProperties->metadataUrls().first().type;
465 }
466}
467
468void QgsMapLayer::setMetadataUrlFormat( const QString &metaUrlFormat )
469{
471
472 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
473 if ( urls.isEmpty() )
474 {
475 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), QLatin1String(), metaUrlFormat );
476 urls.prepend( newItem );
477 }
478 else
479 {
480 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst( );
481 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, old.type, metaUrlFormat );
482 urls.prepend( newItem );
483 }
484 mServerProperties->setMetadataUrls( urls );
485}
486
488{
490
491 if ( mServerProperties->metadataUrls().isEmpty() )
492 {
493 return QString();
494 }
495 else
496 {
497 return mServerProperties->metadataUrls().first().format;
498 }
499}
500
501QString QgsMapLayer::publicSource( bool redactCredentials ) const
502{
504
505 // Redo this every time we're asked for it, as we don't know if
506 // dataSource has changed.
508 {
510 }
511 else
512 {
513 return QgsDataSourceUri::removePassword( mDataSource, redactCredentials );
514 }
515}
516
517QString QgsMapLayer::source() const
518{
520
521 return mDataSource;
522}
523
525{
527
528 return mExtent2D.isNull() ? mExtent3D.toRectangle() : mExtent2D;
529}
530
532{
534
535 return mExtent3D;
536}
537
538void QgsMapLayer::setBlendMode( const QPainter::CompositionMode blendMode )
539{
541
542 if ( mBlendMode == blendMode )
543 return;
544
545 mBlendMode = blendMode;
548}
549
550QPainter::CompositionMode QgsMapLayer::blendMode() const
551{
552 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
554
555 return mBlendMode;
556}
557
558void QgsMapLayer::setOpacity( double opacity )
559{
561
563 return;
565 emit opacityChanged( opacity );
567}
568
570{
571 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
573
574 return mLayerOpacity;
575}
576
577bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags, QgsDataProvider *preloadedProvider )
578{
580
581 mPreloadedProvider.reset( preloadedProvider );
582
583 bool layerError;
585
586 QDomNode mnl;
587 QDomElement mne;
588
589 // read provider
590 QString provider;
591 mnl = layerElement.namedItem( QStringLiteral( "provider" ) );
592 mne = mnl.toElement();
593 provider = mne.text();
594
595 // set data source
596 mnl = layerElement.namedItem( QStringLiteral( "datasource" ) );
597 mne = mnl.toElement();
598 const QString dataSourceRaw = mne.text();
599
600 // if the layer needs authentication, ensure the master password is set
601 const thread_local QRegularExpression rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
602 if ( rx.match( dataSourceRaw ).hasMatch()
604 {
605 return false;
606 }
607
608 mDataSource = decodedSource( dataSourceRaw, provider, context );
609
610 // Set the CRS from project file, asking the user if necessary.
611 // Make it the saved CRS to have WMS layer projected correctly.
612 // We will still overwrite whatever GDAL etc picks up anyway
613 // further down this function.
614 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
615 mne = mnl.toElement();
616
618 CUSTOM_CRS_VALIDATION savedValidation;
619
620 const QDomNode srsNode = layerElement.namedItem( QStringLiteral( "srs" ) );
621 mCRS.readXml( srsNode );
622 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) );
624 mCRS.validate();
625 savedCRS = mCRS;
626
627 // Do not validate any projections in children, they will be overwritten anyway.
628 // No need to ask the user for a projections when it is overwritten, is there?
631
632 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Layer" ), mne.text() );
633
634 // the internal name is just the data source basename
635 //QFileInfo dataSourceFileInfo( mDataSource );
636 //internalName = dataSourceFileInfo.baseName();
637
638 // set ID
639 mnl = layerElement.namedItem( QStringLiteral( "id" ) );
640 if ( ! mnl.isNull() )
641 {
642 mne = mnl.toElement();
643 if ( ! mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz)
644 {
645 const QString newId = mne.text();
646 if ( newId != mID )
647 {
648 mID = mne.text();
649 emit idChanged( mID );
650 }
651 }
652 }
653
654 // set name
655 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
656 mne = mnl.toElement();
657
658 //name can be translated
659 setName( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );
660
661 // now let the children grab what they need from the Dom node.
662 layerError = !readXml( layerElement, context );
663
664 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
665 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
666
667 // overwrite CRS with what we read from project file before the raster/vector
668 // file reading functions changed it. They will if projections is specified in the file.
669 // FIXME: is this necessary? Yes, it is (autumn 2019)
671 mCRS = savedCRS;
672
673 //vertical CRS
674 {
676 const QDomNode verticalCrsNode = layerElement.firstChildElement( QStringLiteral( "verticalCrs" ) );
677 if ( !verticalCrsNode.isNull() )
678 {
679 verticalCrs.readXml( verticalCrsNode );
680 }
681 mVerticalCrs = verticalCrs;
682 }
683 rebuildCrs3D();
684
685 serverProperties()->readXml( layerElement );
686
687 // mMetadata.readFromLayer( this );
688 const QDomElement metadataElem = layerElement.firstChildElement( QStringLiteral( "resourceMetadata" ) );
689 mMetadata.readMetadataXml( metadataElem );
690
691 setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ), QStringLiteral( "0" ) ).toInt() );
692 if ( layerElement.hasAttribute( QStringLiteral( "autoRefreshMode" ) ) )
693 {
694 setAutoRefreshMode( qgsEnumKeyToValue( layerElement.attribute( QStringLiteral( "autoRefreshMode" ) ), Qgis::AutoRefreshMode::Disabled ) );
695 }
696 else
697 {
698 setAutoRefreshMode( layerElement.attribute( QStringLiteral( "autoRefreshEnabled" ), QStringLiteral( "0" ) ).toInt() ? Qgis::AutoRefreshMode::RedrawOnly : Qgis::AutoRefreshMode::Disabled );
699 }
700 setRefreshOnNofifyMessage( layerElement.attribute( QStringLiteral( "refreshOnNotifyMessage" ), QString() ) );
701 setRefreshOnNotifyEnabled( layerElement.attribute( QStringLiteral( "refreshOnNotifyEnabled" ), QStringLiteral( "0" ) ).toInt() );
702
703 // geographic extent is read only if necessary
705 {
706 const QDomNode wgs84ExtentNode = layerElement.namedItem( QStringLiteral( "wgs84extent" ) );
707 if ( !wgs84ExtentNode.isNull() )
708 mWgs84Extent = QgsXmlUtils::readRectangle( wgs84ExtentNode.toElement() );
709 }
710
711 mLegendPlaceholderImage = layerElement.attribute( QStringLiteral( "legendPlaceholderImage" ) );
712
713 if ( verticalCrs() != oldVerticalCrs )
714 emit verticalCrsChanged();
715 if ( mCrs3D != oldCrs3D )
716 emit crs3DChanged();
717
718 return ! layerError;
719} // bool QgsMapLayer::readLayerXML
720
721
722bool QgsMapLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
723{
725
726 Q_UNUSED( layer_node )
727 Q_UNUSED( context )
728 // NOP by default; children will over-ride with behavior specific to them
729
730 // read Extent
732 {
733 const QDomNode extent3DNode = layer_node.namedItem( QStringLiteral( "extent3D" ) );
734 if ( extent3DNode.isNull() )
735 {
736 const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
737 if ( !extentNode.isNull() )
738 {
739 mExtent2D = QgsXmlUtils::readRectangle( extentNode.toElement() );
740 }
741 }
742 else
743 {
744 mExtent3D = QgsXmlUtils::readBox3D( extent3DNode.toElement() );
745 }
746 }
747
748 return true;
749} // void QgsMapLayer::readXml
750
751
752bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const
753{
755
756 if ( !mExtent3D.isNull() && dataProvider() && dataProvider()->elevationProperties() && dataProvider()->elevationProperties()->containsElevationData() )
757 layerElement.appendChild( QgsXmlUtils::writeBox3D( mExtent3D, document ) );
758 else
759 {
760 // Extent might be null because lazily set
761 const QgsRectangle extent2D { mExtent2D.isNull() ? extent() : mExtent2D };
762 if ( !extent2D.isNull() )
763 {
764 layerElement.appendChild( QgsXmlUtils::writeRectangle( extent2D, document ) );
765 }
766 }
767
768 if ( const QgsRectangle lWgs84Extent = wgs84Extent( true ); !lWgs84Extent.isNull() )
769 {
770 layerElement.appendChild( QgsXmlUtils::writeRectangle( lWgs84Extent, document, QStringLiteral( "wgs84extent" ) ) );
771 }
772
773 layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( mRefreshTimer->interval() ) );
774 layerElement.setAttribute( QStringLiteral( "autoRefreshMode" ), qgsEnumValueToKey( mAutoRefreshMode ) );
775 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyEnabled" ), mIsRefreshOnNofifyEnabled ? 1 : 0 );
776 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyMessage" ), mRefreshOnNofifyMessage );
777
778 // ID
779 QDomElement layerId = document.createElement( QStringLiteral( "id" ) );
780 const QDomText layerIdText = document.createTextNode( id() );
781 layerId.appendChild( layerIdText );
782
783 layerElement.appendChild( layerId );
784
785 if ( mVerticalCrs.isValid() )
786 {
787 QDomElement verticalSrsNode = document.createElement( QStringLiteral( "verticalCrs" ) );
788 mVerticalCrs.writeXml( verticalSrsNode, document );
789 layerElement.appendChild( verticalSrsNode );
790 }
791
792 // data source
793 QDomElement dataSource = document.createElement( QStringLiteral( "datasource" ) );
794 const QString src = encodedSource( source(), context );
795 const QDomText dataSourceText = document.createTextNode( src );
796 dataSource.appendChild( dataSourceText );
797 layerElement.appendChild( dataSource );
798
799 // layer name
800 QDomElement layerName = document.createElement( QStringLiteral( "layername" ) );
801 const QDomText layerNameText = document.createTextNode( name() );
802 layerName.appendChild( layerNameText );
803 layerElement.appendChild( layerName );
804
805 // timestamp if supported
806 if ( timestamp() > QDateTime() )
807 {
808 QDomElement stamp = document.createElement( QStringLiteral( "timestamp" ) );
809 const QDomText stampText = document.createTextNode( timestamp().toString( Qt::ISODate ) );
810 stamp.appendChild( stampText );
811 layerElement.appendChild( stamp );
812 }
813
814 layerElement.appendChild( layerName );
815
816 // zorder
817 // This is no longer stored in the project file. It is superfluous since the layers
818 // are written and read in the proper order.
819
820 // spatial reference system id
821 QDomElement mySrsElement = document.createElement( QStringLiteral( "srs" ) );
822 mCRS.writeXml( mySrsElement, document );
823 layerElement.appendChild( mySrsElement );
824
825 // layer metadata
826 QDomElement myMetadataElem = document.createElement( QStringLiteral( "resourceMetadata" ) );
827 mMetadata.writeMetadataXml( myMetadataElem, document );
828 layerElement.appendChild( myMetadataElem );
829
830 layerElement.setAttribute( QStringLiteral( "legendPlaceholderImage" ), mLegendPlaceholderImage );
831
832 serverProperties()->writeXml( layerElement, document );
833
834 // now append layer node to map layer node
835 return writeXml( layerElement, document, context );
836}
837
838void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &document,
839 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
840{
842
843 // save categories
844 const QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::StyleCategories>();
845 const QString categoriesKeys( metaEnum.valueToKeys( static_cast<int>( categories ) ) );
846 layerElement.setAttribute( QStringLiteral( "styleCategories" ), categoriesKeys );
847
848 if ( categories.testFlag( Rendering ) )
849 {
850 // use scale dependent visibility flag
851 layerElement.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
852 layerElement.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
853 layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );
854 layerElement.setAttribute( QStringLiteral( "autoRefreshMode" ), qgsEnumValueToKey( mAutoRefreshMode ) );
855 layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( autoRefreshInterval() ) );
856 }
857
858 if ( categories.testFlag( Symbology3D ) )
859 {
860 if ( m3DRenderer )
861 {
862 QDomElement renderer3DElem = document.createElement( QStringLiteral( "renderer-3d" ) );
863 renderer3DElem.setAttribute( QStringLiteral( "type" ), 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( QStringLiteral( "flags" ) );
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 {
888 properties->writeXml( layerElement, document, context );
889 }
890
891 if ( categories.testFlag( Elevation ) )
892 {
894 properties->writeXml( layerElement, document, context );
895 }
896
897 if ( categories.testFlag( Notes ) && QgsLayerNotesUtils::layerHasNotes( this ) )
898 {
899 QDomElement notesElem = document.createElement( QStringLiteral( "userNotes" ) );
900 notesElem.setAttribute( QStringLiteral( "value" ), 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( QStringLiteral( "map-layer-style-manager" ) );
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( QStringLiteral( "map-layer-style-manager" ) );
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( QStringLiteral( "extent3D" ) );
1049 if ( extent3DNode.isNull() )
1050 {
1051 const QDomNode extentNode = layerNode.namedItem( QStringLiteral( "extent" ) );
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 ) )
1091 && ( mMaxScale == 0 || !QgsScaleUtils::equalToOrGreaterThanMinimumScale( scale, mMaxScale ) ) );
1092}
1093
1095{
1096 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1098
1099 return mScaleBasedVisibility;
1100}
1101
1103{
1105
1106 return mAutoRefreshMode != Qgis::AutoRefreshMode::Disabled;;
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( QStringLiteral( "Layer has a vertical CRS set as the horizontal CRS!" ) );
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() == QLatin1String( "ogr" ) || providerType() == QLatin1String( "delimitedtext" )
1444 || providerType() == QLatin1String( "gdal" ) || providerType() == QLatin1String( "spatialite" ) )
1445 {
1446 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( providerType(), myURI );
1447 myURI = components["path"].toString();
1448 }
1449
1450 QFileInfo myFileInfo( myURI );
1451 QString key;
1452
1453 if ( myFileInfo.exists() )
1454 {
1455 // if file is using the /vsizip/ or /vsigzip/ mechanism, cleanup the name
1456 if ( myURI.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
1457 myURI.chop( 3 );
1458 else if ( myURI.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
1459 myURI.chop( 4 );
1460 else if ( myURI.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) )
1461 myURI.chop( 4 );
1462 else if ( myURI.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) )
1463 myURI.chop( 7 );
1464 else if ( myURI.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
1465 myURI.chop( 4 );
1466 myFileInfo.setFile( myURI );
1467 // get the file name for our .qml style file
1468 key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1469 }
1470 else
1471 {
1472 key = publicSource();
1473 }
1474
1475 return key;
1476}
1477
1479{
1481
1482 return baseURI( PropertyType::Metadata );
1483}
1484
1485QString QgsMapLayer::saveDefaultMetadata( bool &resultFlag )
1486{
1488
1490 {
1491 if ( metadata->providerCapabilities() & QgsProviderMetadata::SaveLayerMetadata )
1492 {
1493 try
1494 {
1495 QString errorMessage;
1496 resultFlag = QgsProviderRegistry::instance()->saveLayerMetadata( providerType(), mDataSource, mMetadata, errorMessage );
1497 if ( resultFlag )
1498 return tr( "Successfully saved default layer metadata" );
1499 else
1500 return errorMessage;
1501 }
1502 catch ( QgsNotSupportedException &e )
1503 {
1504 resultFlag = false;
1505 return e.what();
1506 }
1507 }
1508 }
1509
1510 // fallback default metadata saving method, for providers which don't support (or implement) saveLayerMetadata
1511 return saveNamedMetadata( metadataUri(), resultFlag );
1512}
1513
1514QString QgsMapLayer::loadDefaultMetadata( bool &resultFlag )
1515{
1517
1518 return loadNamedMetadata( metadataUri(), resultFlag );
1519}
1520
1522{
1524
1525 return baseURI( PropertyType::Style );
1526}
1527
1534
1535bool QgsMapLayer::loadNamedMetadataFromDatabase( const QString &db, const QString &uri, QString &qmd )
1536{
1538
1539 return loadNamedPropertyFromDatabase( db, uri, qmd, PropertyType::Metadata );
1540}
1541
1542bool QgsMapLayer::loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml )
1543{
1545
1546 return loadNamedPropertyFromDatabase( db, uri, qml, PropertyType::Style );
1547}
1548
1549bool QgsMapLayer::loadNamedPropertyFromDatabase( const QString &db, const QString &uri, QString &xml, QgsMapLayer::PropertyType type )
1550{
1552
1553 QgsDebugMsgLevel( QStringLiteral( "db = %1 uri = %2" ).arg( db, uri ), 4 );
1554
1555 bool resultFlag = false;
1556
1557 // read from database
1560
1561 int myResult;
1562
1563 QgsDebugMsgLevel( QStringLiteral( "Trying to load style or metadata for \"%1\" from \"%2\"" ).arg( uri, db ), 4 );
1564
1565 if ( db.isEmpty() || !QFile( db ).exists() )
1566 return false;
1567
1568 myResult = database.open_v2( db, SQLITE_OPEN_READONLY, nullptr );
1569 if ( myResult != SQLITE_OK )
1570 {
1571 return false;
1572 }
1573
1574 QString mySql;
1575 switch ( type )
1576 {
1577 case Metadata:
1578 mySql = QStringLiteral( "select qmd from tbl_metadata where metadata=?" );
1579 break;
1580
1581 case Style:
1582 mySql = QStringLiteral( "select qml from tbl_styles where style=?" );
1583 break;
1584 }
1585
1586 statement = database.prepare( mySql, myResult );
1587 if ( myResult == SQLITE_OK )
1588 {
1589 QByteArray param = uri.toUtf8();
1590
1591 if ( sqlite3_bind_text( statement.get(), 1, param.data(), param.length(), SQLITE_STATIC ) == SQLITE_OK &&
1592 sqlite3_step( statement.get() ) == SQLITE_ROW )
1593 {
1594 xml = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( statement.get(), 0 ) ) );
1595 resultFlag = true;
1596 }
1597 }
1598 return resultFlag;
1599}
1600
1601
1602QString QgsMapLayer::loadNamedStyle( const QString &uri, bool &resultFlag, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
1603{
1605
1606 return loadNamedStyle( uri, resultFlag, false, categories, flags );
1607}
1608
1609QString QgsMapLayer::loadNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &namedPropertyExists, bool &propertySuccessfullyLoaded, StyleCategories categories, Qgis::LoadStyleFlags flags )
1610{
1612
1613 QgsDebugMsgLevel( QStringLiteral( "uri = %1 myURI = %2" ).arg( uri, publicSource() ), 4 );
1614
1615 namedPropertyExists = false;
1616 propertySuccessfullyLoaded = false;
1617 if ( uri.isEmpty() )
1618 return QString();
1619
1620 QDomDocument myDocument( QStringLiteral( "qgis" ) );
1621
1622 // location of problem associated with errorMsg
1623 int line, column;
1624 QString myErrorMessage;
1625
1626 QFile myFile( uri );
1627 if ( myFile.open( QFile::ReadOnly ) )
1628 {
1629 QgsDebugMsgLevel( QStringLiteral( "file found %1" ).arg( uri ), 2 );
1630 namedPropertyExists = true;
1631
1632 // read file
1633 propertySuccessfullyLoaded = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
1634 if ( !propertySuccessfullyLoaded )
1635 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1636 myFile.close();
1637 }
1638 else
1639 {
1640 const QFileInfo project( QgsProject::instance()->fileName() ); // skip-keyword-check
1641 QgsDebugMsgLevel( QStringLiteral( "project fileName: %1" ).arg( project.absoluteFilePath() ), 4 );
1642
1643 QString xml;
1644 switch ( type )
1645 {
1646 case QgsMapLayer::Style:
1647 {
1648 if ( loadNamedStyleFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1649 ( project.exists() && loadNamedStyleFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1650 loadNamedStyleFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1651 {
1652 namedPropertyExists = true;
1653 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1654 if ( !propertySuccessfullyLoaded )
1655 {
1656 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1657 }
1658 }
1659 else
1660 {
1662 {
1663 myErrorMessage = tr( "Style not found in database" );
1664 }
1665 }
1666 break;
1667 }
1669 {
1670 if ( loadNamedMetadataFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1671 ( project.exists() && loadNamedMetadataFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1672 loadNamedMetadataFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1673 {
1674 namedPropertyExists = true;
1675 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1676 if ( !propertySuccessfullyLoaded )
1677 {
1678 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1679 }
1680 }
1681 else
1682 {
1683 myErrorMessage = tr( "Metadata not found in database" );
1684 }
1685 break;
1686 }
1687 }
1688 }
1689
1690 if ( !propertySuccessfullyLoaded )
1691 {
1692 return myErrorMessage;
1693 }
1694
1695 switch ( type )
1696 {
1697 case QgsMapLayer::Style:
1698 propertySuccessfullyLoaded = importNamedStyle( myDocument, myErrorMessage, categories );
1699 if ( !propertySuccessfullyLoaded )
1700 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1701 break;
1703 propertySuccessfullyLoaded = importNamedMetadata( myDocument, myErrorMessage );
1704 if ( !propertySuccessfullyLoaded )
1705 myErrorMessage = tr( "Loading metadata file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1706 break;
1707 }
1708 return myErrorMessage;
1709}
1710
1711bool QgsMapLayer::importNamedMetadata( QDomDocument &document, QString &errorMessage )
1712{
1714
1715 const QDomElement myRoot = document.firstChildElement( QStringLiteral( "qgis" ) );
1716 if ( myRoot.isNull() )
1717 {
1718 errorMessage = tr( "Root <qgis> element could not be found" );
1719 return false;
1720 }
1721
1722 return mMetadata.readMetadataXml( myRoot );
1723}
1724
1725bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMessage, QgsMapLayer::StyleCategories categories )
1726{
1728
1729 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "qgis" ) );
1730 if ( myRoot.isNull() )
1731 {
1732 myErrorMessage = tr( "Root <qgis> element could not be found" );
1733 return false;
1734 }
1735
1736 // get style file version string, if any
1737 const QgsProjectVersion fileVersion( myRoot.attribute( QStringLiteral( "version" ) ) );
1738 const QgsProjectVersion thisVersion( Qgis::version() );
1739
1740 if ( thisVersion > fileVersion )
1741 {
1742 QgsProjectFileTransform styleFile( myDocument, fileVersion );
1743 styleFile.updateRevision( thisVersion );
1744 }
1745
1746 // Get source categories
1747 const QgsMapLayer::StyleCategories sourceCategories = QgsXmlUtils::readFlagAttribute( myRoot, QStringLiteral( "styleCategories" ), QgsMapLayer::AllStyleCategories );
1748
1749 //Test for matching geometry type on vector layers when applying, if geometry type is given in the style
1750 if ( ( sourceCategories.testFlag( QgsMapLayer::Symbology ) || sourceCategories.testFlag( QgsMapLayer::Symbology3D ) ) &&
1751 ( categories.testFlag( QgsMapLayer::Symbology ) || categories.testFlag( QgsMapLayer::Symbology3D ) ) )
1752 {
1753 if ( type() == Qgis::LayerType::Vector && !myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).isNull() )
1754 {
1755 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( this );
1756 const Qgis::GeometryType importLayerGeometryType = static_cast<Qgis::GeometryType>( myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).text().toInt() );
1757 if ( importLayerGeometryType != Qgis::GeometryType::Unknown && vl->geometryType() != importLayerGeometryType )
1758 {
1759 myErrorMessage = tr( "Cannot apply style with symbology to layer with a different geometry type" );
1760 return false;
1761 }
1762 }
1763 }
1764
1766 return readSymbology( myRoot, myErrorMessage, context, categories ); // TODO: support relative paths in QML?
1767}
1768
1769void QgsMapLayer::exportNamedMetadata( QDomDocument &doc, QString &errorMsg ) const
1770{
1772
1773 QDomImplementation DomImplementation;
1774 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1775 QDomDocument myDocument( documentType );
1776
1777 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1778 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1779 myDocument.appendChild( myRootNode );
1780
1781 if ( !mMetadata.writeMetadataXml( myRootNode, myDocument ) )
1782 {
1783 errorMsg = QObject::tr( "Could not save metadata" );
1784 return;
1785 }
1786
1787 doc = myDocument;
1788}
1789
1790void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
1791{
1793
1794 QDomImplementation DomImplementation;
1795 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1796 QDomDocument myDocument( documentType );
1797
1798 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1799 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1800 myDocument.appendChild( myRootNode );
1801
1802 if ( !writeSymbology( myRootNode, myDocument, errorMsg, context, categories ) ) // TODO: support relative paths in QML?
1803 {
1804 errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1805 return;
1806 }
1807
1808 /*
1809 * Check to see if the layer is vector - in which case we should also export its geometryType
1810 * to avoid eventually pasting to a layer with a different geometry
1811 */
1812 if ( type() == Qgis::LayerType::Vector )
1813 {
1814 //Getting the selectionLayer geometry
1815 const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( this );
1816 const QString geoType = QString::number( static_cast<int>( vl->geometryType() ) );
1817
1818 //Adding geometryinformation
1819 QDomElement layerGeometryType = myDocument.createElement( QStringLiteral( "layerGeometryType" ) );
1820 const QDomText type = myDocument.createTextNode( geoType );
1821
1822 layerGeometryType.appendChild( type );
1823 myRootNode.appendChild( layerGeometryType );
1824 }
1825
1826 doc = myDocument;
1827}
1828
1829QString QgsMapLayer::saveDefaultStyle( bool &resultFlag )
1830{
1832
1833 return saveDefaultStyle( resultFlag, AllStyleCategories );
1834}
1835
1836QString QgsMapLayer::saveDefaultStyle( bool &resultFlag, StyleCategories categories )
1837{
1839
1840 return saveNamedStyle( styleURI(), resultFlag, categories );
1841}
1842
1843QString QgsMapLayer::saveNamedMetadata( const QString &uri, bool &resultFlag )
1844{
1846
1847 return saveNamedProperty( uri, QgsMapLayer::Metadata, resultFlag );
1848}
1849
1850QString QgsMapLayer::loadNamedMetadata( const QString &uri, bool &resultFlag )
1851{
1853
1854 bool metadataExists = false;
1855 bool metadataSuccessfullyLoaded = false;
1856 const QString message = loadNamedProperty( uri, QgsMapLayer::Metadata, metadataExists, metadataSuccessfullyLoaded );
1857
1858 // TODO QGIS 4.0 -- fix API for loadNamedMetadata so we can return metadataExists too
1859 ( void )metadataExists;
1860 resultFlag = metadataSuccessfullyLoaded;
1861 return message;
1862}
1863
1864QString QgsMapLayer::saveNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag, StyleCategories categories )
1865{
1867
1868 // check if the uri is a file or ends with .qml/.qmd,
1869 // which indicates that it should become one
1870 // everything else goes to the database
1871 QString filename;
1872
1873 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1874 if ( vlayer && vlayer->providerType() == QLatin1String( "ogr" ) )
1875 {
1876 QStringList theURIParts = uri.split( '|' );
1877 filename = theURIParts[0];
1878 }
1879 else if ( vlayer && vlayer->providerType() == QLatin1String( "gpx" ) )
1880 {
1881 QStringList theURIParts = uri.split( '?' );
1882 filename = theURIParts[0];
1883 }
1884 else if ( vlayer && vlayer->providerType() == QLatin1String( "delimitedtext" ) )
1885 {
1886 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
1887 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
1888 if ( filename.isEmpty() )
1889 filename = uri;
1890 }
1891 else
1892 {
1893 filename = uri;
1894 }
1895
1896 QString myErrorMessage;
1897 QDomDocument myDocument;
1898 switch ( type )
1899 {
1900 case Metadata:
1901 exportNamedMetadata( myDocument, myErrorMessage );
1902 break;
1903
1904 case Style:
1905 const QgsReadWriteContext context;
1906 exportNamedStyle( myDocument, myErrorMessage, context, categories );
1907 break;
1908 }
1909
1910 const QFileInfo myFileInfo( filename );
1911 if ( myFileInfo.exists() || filename.endsWith( QgsMapLayer::extensionPropertyType( type ), Qt::CaseInsensitive ) )
1912 {
1913 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
1914 if ( !myDirInfo.isWritable() )
1915 {
1916 resultFlag = false;
1917 return tr( "The directory containing your dataset needs to be writable!" );
1918 }
1919
1920 // now construct the file name for our .qml or .qmd file
1921 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1922
1923 QFile myFile( myFileName );
1924 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1925 {
1926 QTextStream myFileStream( &myFile );
1927 // save as utf-8 with 2 spaces for indents
1928 myDocument.save( myFileStream, 2 );
1929 myFile.close();
1930 resultFlag = true;
1931 switch ( type )
1932 {
1933 case Metadata:
1934 return tr( "Created default metadata file as %1" ).arg( myFileName );
1935
1936 case Style:
1937 return tr( "Created default style file as %1" ).arg( myFileName );
1938 }
1939
1940 }
1941 else
1942 {
1943 resultFlag = false;
1944 switch ( type )
1945 {
1946 case Metadata:
1947 return tr( "ERROR: Failed to created default metadata file as %1. Check file permissions and retry." ).arg( myFileName );
1948
1949 case Style:
1950 return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName );
1951 }
1952 }
1953 }
1954 else
1955 {
1956 const QString qml = myDocument.toString();
1957
1958 // read from database
1961
1962 int myResult = database.open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ) );
1963 if ( myResult != SQLITE_OK )
1964 {
1965 return tr( "User database could not be opened." );
1966 }
1967
1968 QByteArray param0 = uri.toUtf8();
1969 QByteArray param1 = qml.toUtf8();
1970
1971 QString mySql;
1972 switch ( type )
1973 {
1974 case Metadata:
1975 mySql = QStringLiteral( "create table if not exists tbl_metadata(metadata varchar primary key,qmd varchar)" );
1976 break;
1977
1978 case Style:
1979 mySql = QStringLiteral( "create table if not exists tbl_styles(style varchar primary key,qml varchar)" );
1980 break;
1981 }
1982
1983 statement = database.prepare( mySql, myResult );
1984 if ( myResult == SQLITE_OK )
1985 {
1986 if ( sqlite3_step( statement.get() ) != SQLITE_DONE )
1987 {
1988 resultFlag = false;
1989 switch ( type )
1990 {
1991 case Metadata:
1992 return tr( "The metadata table could not be created." );
1993
1994 case Style:
1995 return tr( "The style table could not be created." );
1996 }
1997 }
1998 }
1999
2000 switch ( type )
2001 {
2002 case Metadata:
2003 mySql = QStringLiteral( "insert into tbl_metadata(metadata,qmd) values (?,?)" );
2004 break;
2005
2006 case Style:
2007 mySql = QStringLiteral( "insert into tbl_styles(style,qml) values (?,?)" );
2008 break;
2009 }
2010 statement = database.prepare( mySql, myResult );
2011 if ( myResult == SQLITE_OK )
2012 {
2013 if ( sqlite3_bind_text( statement.get(), 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
2014 sqlite3_bind_text( statement.get(), 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
2015 sqlite3_step( statement.get() ) == SQLITE_DONE )
2016 {
2017 resultFlag = true;
2018 switch ( type )
2019 {
2020 case Metadata:
2021 myErrorMessage = tr( "The metadata %1 was saved to database" ).arg( uri );
2022 break;
2023
2024 case Style:
2025 myErrorMessage = tr( "The style %1 was saved to database" ).arg( uri );
2026 break;
2027 }
2028 }
2029 }
2030
2031 if ( !resultFlag )
2032 {
2033 QString mySql;
2034 switch ( type )
2035 {
2036 case Metadata:
2037 mySql = QStringLiteral( "update tbl_metadata set qmd=? where metadata=?" );
2038 break;
2039
2040 case Style:
2041 mySql = QStringLiteral( "update tbl_styles set qml=? where style=?" );
2042 break;
2043 }
2044 statement = database.prepare( mySql, myResult );
2045 if ( myResult == SQLITE_OK )
2046 {
2047 if ( sqlite3_bind_text( statement.get(), 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
2048 sqlite3_bind_text( statement.get(), 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
2049 sqlite3_step( statement.get() ) == SQLITE_DONE )
2050 {
2051 resultFlag = true;
2052 switch ( type )
2053 {
2054 case Metadata:
2055 myErrorMessage = tr( "The metadata %1 was updated in the database." ).arg( uri );
2056 break;
2057
2058 case Style:
2059 myErrorMessage = tr( "The style %1 was updated in the database." ).arg( uri );
2060 break;
2061 }
2062 }
2063 else
2064 {
2065 resultFlag = false;
2066 switch ( type )
2067 {
2068 case Metadata:
2069 myErrorMessage = tr( "The metadata %1 could not be updated in the database." ).arg( uri );
2070 break;
2071
2072 case Style:
2073 myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( uri );
2074 break;
2075 }
2076 }
2077 }
2078 else
2079 {
2080 resultFlag = false;
2081 switch ( type )
2082 {
2083 case Metadata:
2084 myErrorMessage = tr( "The metadata %1 could not be inserted into database." ).arg( uri );
2085 break;
2086
2087 case Style:
2088 myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( uri );
2089 break;
2090 }
2091 }
2092 }
2093 }
2094
2095 return myErrorMessage;
2096}
2097
2098QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag, StyleCategories categories )
2099{
2101
2102 return saveNamedProperty( uri, QgsMapLayer::Style, resultFlag, categories );
2103}
2104
2105void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
2106{
2107 QgsSldExportContext exportContext;
2108 doc = exportSldStyleV3( exportContext );
2109 if ( !exportContext.errors().empty() )
2110 errorMsg = exportContext.errors().join( "\n" );
2111}
2112
2113void QgsMapLayer::exportSldStyleV2( QDomDocument &doc, QString &errorMsg, QgsSldExportContext &exportContext ) const
2114{
2116 doc = exportSldStyleV3( exportContext );
2117 if ( !exportContext.errors().empty() )
2118 errorMsg = exportContext.errors().join( "\n" );
2119}
2120
2121QDomDocument QgsMapLayer::exportSldStyleV3( QgsSldExportContext &exportContext ) const
2122{
2124
2125 QDomDocument myDocument = QDomDocument();
2126
2127 const QDomNode header = myDocument.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
2128 myDocument.appendChild( header );
2129
2130 const QgsVectorLayer *vlayer = qobject_cast<const QgsVectorLayer *>( this );
2131 const QgsRasterLayer *rlayer = qobject_cast<const QgsRasterLayer *>( this );
2132 if ( !vlayer && !rlayer )
2133 {
2134 exportContext.pushError( tr( "Could not save symbology because:\n%1" )
2135 .arg( tr( "Only vector and raster layers are supported" ) ) );
2136 return myDocument;
2137 }
2138
2139 // Create the root element
2140 QDomElement root = myDocument.createElementNS( QStringLiteral( "http://www.opengis.net/sld" ), QStringLiteral( "StyledLayerDescriptor" ) );
2141 QDomElement layerNode;
2142 if ( vlayer )
2143 {
2144 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.1.0" ) );
2145 root.setAttribute( QStringLiteral( "xsi:schemaLocation" ), QStringLiteral( "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" ) );
2146 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
2147 root.setAttribute( QStringLiteral( "xmlns:se" ), QStringLiteral( "http://www.opengis.net/se" ) );
2148 root.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
2149 root.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
2150 myDocument.appendChild( root );
2151
2152 // Create the NamedLayer element
2153 layerNode = myDocument.createElement( QStringLiteral( "NamedLayer" ) );
2154 root.appendChild( layerNode );
2155 }
2156
2157 // note: Only SLD 1.0 version is generated because seems none is using SE1.1.0 at least for rasters
2158 if ( rlayer )
2159 {
2160 // Create the root element
2161 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0.0" ) );
2162 root.setAttribute( QStringLiteral( "xmlns:gml" ), QStringLiteral( "http://www.opengis.net/gml" ) );
2163 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
2164 root.setAttribute( QStringLiteral( "xmlns:sld" ), QStringLiteral( "http://www.opengis.net/sld" ) );
2165 myDocument.appendChild( root );
2166
2167 // Create the NamedLayer element
2168 layerNode = myDocument.createElement( QStringLiteral( "UserLayer" ) );
2169 root.appendChild( layerNode );
2170 }
2171
2172 QVariantMap props = exportContext.extraProperties();
2173
2174 QVariant context;
2175 context.setValue( exportContext );
2176
2177 // TODO -- move this to proper members of QgsSldExportContext
2178 props[ QStringLiteral( "SldExportContext" ) ] = context;
2179
2181 {
2182 props[ QStringLiteral( "scaleMinDenom" ) ] = QString::number( mMinScale );
2183 props[ QStringLiteral( "scaleMaxDenom" ) ] = QString::number( mMaxScale );
2184 }
2185 exportContext.setExtraProperties( props );
2186
2187 if ( vlayer )
2188 {
2189 if ( !vlayer->writeSld( layerNode, myDocument, exportContext ) )
2190 {
2191 return myDocument;
2192 }
2193 }
2194 else if ( rlayer )
2195 {
2196 if ( !rlayer->writeSld( layerNode, myDocument, exportContext ) )
2197 {
2198 return myDocument;
2199 }
2200 }
2201
2202 return myDocument;
2203}
2204
2205QString QgsMapLayer::saveSldStyle( const QString &uri, bool &resultFlag ) const
2206{
2207 QgsSldExportContext context;
2208 context.setExportFilePath( uri );
2209 return saveSldStyleV2( resultFlag, context );
2210}
2211
2212QString QgsMapLayer::saveSldStyleV2( bool &resultFlag, QgsSldExportContext &exportContext ) const
2213{
2215
2216 const QgsMapLayer *mlayer = qobject_cast<const QgsMapLayer *>( this );
2217
2218 const QString uri { exportContext.exportFilePath() };
2219
2220 // check if the uri is a file or ends with .sld,
2221 // which indicates that it should become one
2222 QString filename;
2223 if ( mlayer->providerType() == QLatin1String( "ogr" ) )
2224 {
2225 QStringList theURIParts = uri.split( '|' );
2226 filename = theURIParts[0];
2227 }
2228 else if ( mlayer->providerType() == QLatin1String( "gpx" ) )
2229 {
2230 QStringList theURIParts = uri.split( '?' );
2231 filename = theURIParts[0];
2232 }
2233 else if ( mlayer->providerType() == QLatin1String( "delimitedtext" ) )
2234 {
2235 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
2236 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
2237 if ( filename.isEmpty() )
2238 filename = uri;
2239 }
2240 else
2241 {
2242 filename = uri;
2243 }
2244
2245 const QFileInfo myFileInfo( filename );
2246 if ( myFileInfo.exists() || filename.endsWith( QLatin1String( ".sld" ), Qt::CaseInsensitive ) )
2247 {
2248 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
2249 if ( !myDirInfo.isWritable() )
2250 {
2251 resultFlag = false;
2252 return tr( "The directory containing your dataset needs to be writable!" );
2253 }
2254
2255 // now construct the file name for our .sld style file
2256 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld";
2257
2258 QgsSldExportContext context { exportContext };
2259 context.setExportFilePath( myFileName );
2260
2261 QDomDocument myDocument = mlayer->exportSldStyleV3( context );
2262
2263 if ( !context.errors().empty() )
2264 {
2265 resultFlag = false;
2266 return context.errors().join( '\n' );
2267 }
2268
2269 QFile myFile( myFileName );
2270 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
2271 {
2272 QTextStream myFileStream( &myFile );
2273 // save as utf-8 with 2 spaces for indents
2274 myDocument.save( myFileStream, 2 );
2275 myFile.close();
2276 resultFlag = true;
2277 return tr( "Created default style file as %1" ).arg( myFileName );
2278 }
2279 }
2280
2281 resultFlag = false;
2282 return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename );
2283
2284}
2285
2286QString QgsMapLayer::loadSldStyle( const QString &uri, bool &resultFlag )
2287{
2289
2290 resultFlag = false;
2291
2292 QDomDocument myDocument;
2293
2294 // location of problem associated with errorMsg
2295 int line, column;
2296 QString myErrorMessage;
2297
2298 QFile myFile( uri );
2299 if ( myFile.open( QFile::ReadOnly ) )
2300 {
2301 // read file
2302 resultFlag = myDocument.setContent( &myFile, true, &myErrorMessage, &line, &column );
2303 if ( !resultFlag )
2304 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
2305 myFile.close();
2306 }
2307 else
2308 {
2309 myErrorMessage = tr( "Unable to open file %1" ).arg( uri );
2310 }
2311
2312 if ( !resultFlag )
2313 {
2314 return myErrorMessage;
2315 }
2316
2317 // check for root SLD element
2318 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "StyledLayerDescriptor" ) );
2319 if ( myRoot.isNull() )
2320 {
2321 myErrorMessage = QStringLiteral( "Error: StyledLayerDescriptor element not found in %1" ).arg( uri );
2322 resultFlag = false;
2323 return myErrorMessage;
2324 }
2325
2326 // now get the style node out and pass it over to the layer
2327 // to deserialise...
2328 const QDomElement namedLayerElem = myRoot.firstChildElement( QStringLiteral( "NamedLayer" ) );
2329 if ( namedLayerElem.isNull() )
2330 {
2331 myErrorMessage = QStringLiteral( "Info: NamedLayer element not found." );
2332 resultFlag = false;
2333 return myErrorMessage;
2334 }
2335
2336 QString errorMsg;
2337 resultFlag = readSld( namedLayerElem, errorMsg );
2338 if ( !resultFlag )
2339 {
2340 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, errorMsg );
2341 return myErrorMessage;
2342 }
2343
2344 return QString();
2345}
2346
2347bool QgsMapLayer::readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
2348{
2350
2351 Q_UNUSED( node )
2352 Q_UNUSED( errorMessage )
2353 Q_UNUSED( context )
2354 Q_UNUSED( categories )
2355 return false;
2356}
2357
2358bool QgsMapLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
2359 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
2360{
2362
2363 Q_UNUSED( node )
2364 Q_UNUSED( doc )
2365 Q_UNUSED( errorMessage )
2366 Q_UNUSED( context )
2367 Q_UNUSED( categories )
2368 return false;
2369}
2370
2371
2372void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2373 bool loadDefaultStyleFlag )
2374{
2376
2378
2380 if ( loadDefaultStyleFlag )
2381 {
2383 }
2384
2386 {
2388 }
2389 setDataSource( dataSource,
2390 baseName.isEmpty() ? mLayerName : baseName,
2391 provider.isEmpty() ? mProviderKey : provider,
2392 options, flags );
2393}
2394
2395void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2396 const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag )
2397{
2399
2401 if ( loadDefaultStyleFlag )
2402 {
2404 }
2405
2407 {
2409 }
2410 setDataSource( dataSource, baseName, provider, options, flags );
2411}
2412
2413void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2415{
2417
2420 {
2422 }
2423 setDataSourcePrivate( dataSource, baseName, provider, options, flags );
2424 emit dataSourceChanged();
2425 emit dataChanged();
2427}
2428
2429
2430void QgsMapLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider,
2432{
2434
2435 Q_UNUSED( dataSource )
2436 Q_UNUSED( baseName )
2437 Q_UNUSED( provider )
2438 Q_UNUSED( options )
2439 Q_UNUSED( flags )
2440}
2441
2442
2444{
2446
2447 return mProviderKey;
2448}
2449
2450void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
2451 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( QStringLiteral( "renderer-3d" ) );
2461 if ( !renderer3DElem.isNull() )
2462 {
2463 const QString type3D = renderer3DElem.attribute( QStringLiteral( "type" ) );
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( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
2484 if ( layerElement.hasAttribute( QStringLiteral( "minimumScale" ) ) )
2485 {
2486 // older element, when scales were reversed
2487 setMaximumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
2488 setMinimumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
2489 }
2490 else
2491 {
2492 setMaximumScale( layerElement.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
2493 setMinimumScale( layerElement.attribute( QStringLiteral( "minScale" ) ).toDouble() );
2494 }
2495 if ( layerElement.hasAttribute( QStringLiteral( "autoRefreshMode" ) ) )
2496 {
2497 setAutoRefreshMode( qgsEnumKeyToValue( layerElement.attribute( QStringLiteral( "autoRefreshMode" ) ), Qgis::AutoRefreshMode::Disabled ) );
2498 setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ) ).toInt() );
2499 }
2500 }
2501
2502 if ( categories.testFlag( LayerConfiguration ) )
2503 {
2504 // flags
2505 const QDomElement flagsElem = layerElement.firstChildElement( QStringLiteral( "flags" ) );
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( QStringLiteral( "userNotes" ) );
2541 if ( !notesElem.isNull() )
2542 {
2543 const QString notes = notesElem.attribute( QStringLiteral( "value" ) );
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,
2640 bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories )
2641{
2643
2644 QString sldStyle, qmlStyle;
2645 QDomDocument qmlDocument;
2646 QgsReadWriteContext context;
2647 exportNamedStyle( qmlDocument, msgError, context, categories );
2648 if ( !msgError.isNull() )
2649 {
2650 return;
2651 }
2652 qmlStyle = qmlDocument.toString();
2653
2654 QgsSldExportContext sldContext;
2655 QDomDocument sldDocument = this->exportSldStyleV3( sldContext );
2656 if ( !sldContext.errors().empty() )
2657 {
2658 return;
2659 }
2660 sldStyle = sldDocument.toString();
2661
2663 mDataSource, qmlStyle, sldStyle, name,
2664 description, uiFileContent, useAsDefault, msgError );
2665}
2666
2667QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &resultFlag, bool loadFromLocalDB, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
2668{
2670
2671 QString returnMessage;
2672 QString qml, errorMsg;
2673 QString styleName;
2674 if ( !loadFromLocalDB && dataProvider() && dataProvider()->styleStorageCapabilities().testFlag( Qgis::ProviderStyleStorageCapability::LoadFromDatabase ) )
2675 {
2677 }
2678
2679 // Style was successfully loaded from provider storage
2680 if ( !qml.isEmpty() )
2681 {
2682 QDomDocument myDocument( QStringLiteral( "qgis" ) );
2683 myDocument.setContent( qml );
2684 resultFlag = importNamedStyle( myDocument, errorMsg );
2685 returnMessage = QObject::tr( "Loaded from Provider" );
2686 }
2687 else
2688 {
2690
2691 bool styleExists = false;
2692 bool styleSuccessfullyLoaded = false;
2693
2694 returnMessage = loadNamedProperty( theURI, PropertyType::Style, styleExists, styleSuccessfullyLoaded, categories, flags );
2695
2696 // TODO QGIS 4.0 -- fix API for loadNamedStyle so we can return styleExists too
2697 ( void )styleExists;
2698 resultFlag = styleSuccessfullyLoaded;
2699 }
2700
2701 if ( ! styleName.isEmpty() )
2702 {
2703 styleManager()->renameStyle( styleManager()->currentStyle(), styleName );
2704 }
2705
2706 if ( resultFlag )
2707 emit styleLoaded( categories );
2708
2709 return returnMessage;
2710}
2711
2718
2720{
2722
2723 return false;
2724}
2725
2727{
2729
2730 return false;
2731}
2732
2734{
2736
2737 return true;
2738}
2739
2741{
2743
2744 // invalid layers are temporary? -- who knows?!
2745 if ( !isValid() )
2746 return false;
2747
2748 if ( mProviderKey == QLatin1String( "memory" ) )
2749 return true;
2750
2751 const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( mProviderKey, mDataSource );
2752 const QString path = sourceParts.value( QStringLiteral( "path" ) ).toString();
2753 if ( path.isEmpty() )
2754 return false;
2755
2756 // check if layer path is inside one of the standard temporary file locations for this platform
2757 const QStringList tempPaths = QStandardPaths::standardLocations( QStandardPaths::TempLocation );
2758 for ( const QString &tempPath : tempPaths )
2759 {
2760 if ( path.startsWith( tempPath ) )
2761 return true;
2762 }
2763
2764 return false;
2765}
2766
2767void QgsMapLayer::setValid( bool valid )
2768{
2770
2771 if ( mValid == valid )
2772 return;
2773
2774 mValid = valid;
2775 emit isValidChanged();
2776}
2777
2779{
2781
2782 if ( legend == mLegend )
2783 return;
2784
2785 delete mLegend;
2786 mLegend = legend;
2787
2788 if ( mLegend )
2789 {
2790 mLegend->setParent( this );
2791 connect( mLegend, &QgsMapLayerLegend::itemsChanged, this, &QgsMapLayer::legendChanged, Qt::UniqueConnection );
2792 }
2793
2794 emit legendChanged();
2795}
2796
2798{
2800
2801 return mLegend;
2802}
2803
2805{
2807
2808 return mStyleManager.get();
2809}
2810
2812{
2814
2815 if ( renderer == m3DRenderer )
2816 return;
2817
2818 delete m3DRenderer;
2819 m3DRenderer = renderer;
2820 emit renderer3DChanged();
2821 emit repaintRequested();
2823}
2824
2826{
2828
2829 return m3DRenderer;
2830}
2831
2832void QgsMapLayer::triggerRepaint( bool deferredUpdate )
2833{
2835
2836 if ( mRepaintRequestedFired )
2837 return;
2838 mRepaintRequestedFired = true;
2839 emit repaintRequested( deferredUpdate );
2840 mRepaintRequestedFired = false;
2841}
2842
2849
2851{
2853
2854 mMetadata = metadata;
2855// mMetadata.saveToLayer( this );
2856 emit metadataChanged();
2857}
2858
2860{
2862
2863 return QString();
2864}
2865
2866QDateTime QgsMapLayer::timestamp() const
2867{
2869
2870 return QDateTime();
2871}
2872
2880
2882{
2883 updateExtent( extent );
2884}
2885
2887{
2889
2890 updateExtent( extent );
2891}
2892
2893bool QgsMapLayer::isReadOnly() const
2894{
2896
2897 return true;
2898}
2899
2901{
2903
2904 return mOriginalXmlProperties;
2905}
2906
2907void QgsMapLayer::setOriginalXmlProperties( const QString &originalXmlProperties )
2908{
2910
2911 mOriginalXmlProperties = originalXmlProperties;
2912}
2913
2914QString QgsMapLayer::generateId( const QString &layerName )
2915{
2916 // Generate the unique ID of this layer
2917 const QString uuid = QUuid::createUuid().toString();
2918 // trim { } from uuid
2919 QString id = layerName + '_' + uuid.mid( 1, uuid.length() - 2 );
2920 // Tidy the ID up to avoid characters that may cause problems
2921 // elsewhere (e.g in some parts of XML). Replaces every non-word
2922 // character (word characters are the alphabet, numbers and
2923 // underscore) with an underscore.
2924 // Note that the first backslash in the regular expression is
2925 // there for the compiler, so the pattern is actually \W
2926 const thread_local QRegularExpression idRx( QStringLiteral( "[\\W]" ) );
2927 id.replace( idRx, QStringLiteral( "_" ) );
2928 return id;
2929}
2930
2932{
2934
2935 return true;
2936}
2937
2939{
2941
2942 return mapTipsEnabled() && !mMapTipTemplate.isEmpty();
2943}
2944
2945void QgsMapLayer::setProviderType( const QString &providerType )
2946{
2948
2950}
2951
2952QSet<QgsMapLayerDependency> QgsMapLayer::dependencies() const
2953{
2955
2956 return mDependencies;
2957}
2958
2959bool QgsMapLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
2960{
2962
2963 QSet<QgsMapLayerDependency> deps;
2964 const auto constODeps = oDeps;
2965 for ( const QgsMapLayerDependency &dep : constODeps )
2966 {
2967 if ( dep.origin() == QgsMapLayerDependency::FromUser )
2968 deps << dep;
2969 }
2970
2971 mDependencies = deps;
2972 emit dependenciesChanged();
2973 return true;
2974}
2975
2977{
2979
2980 QgsDataProvider *lDataProvider = dataProvider();
2981
2982 if ( !lDataProvider )
2983 return;
2984
2985 if ( enabled && !isRefreshOnNotifyEnabled() )
2986 {
2987 lDataProvider->setListening( enabled );
2988 connect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
2989 }
2990 else if ( !enabled && isRefreshOnNotifyEnabled() )
2991 {
2992 // we don't want to disable provider listening because someone else could need it (e.g. actions)
2993 disconnect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
2994 }
2995 mIsRefreshOnNofifyEnabled = enabled;
2996}
2997
2999{
3001
3002 if ( QgsMapLayerStore *store = qobject_cast<QgsMapLayerStore *>( parent() ) )
3003 {
3004 return qobject_cast<QgsProject *>( store->parent() );
3005 }
3006 return nullptr;
3007}
3008
3009void QgsMapLayer::onNotified( const QString &message )
3010{
3012
3013 if ( refreshOnNotifyMessage().isEmpty() || refreshOnNotifyMessage() == message )
3014 {
3016 emit dataChanged();
3017 }
3018}
3019
3020QgsRectangle QgsMapLayer::wgs84Extent( bool forceRecalculate ) const
3021{
3023
3025
3026 if ( ! forceRecalculate && ! mWgs84Extent.isNull() )
3027 {
3028 wgs84Extent = mWgs84Extent;
3029 }
3030 else if ( ! mExtent2D.isNull() || ! mExtent3D.isNull() )
3031 {
3032 QgsCoordinateTransform transformer { crs(), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), transformContext() };
3033 transformer.setBallparkTransformsAreAppropriate( true );
3034 try
3035 {
3036 if ( mExtent2D.isNull() )
3037 wgs84Extent = transformer.transformBoundingBox( mExtent3D.toRectangle() );
3038 else
3039 wgs84Extent = transformer.transformBoundingBox( mExtent2D );
3040 }
3041 catch ( const QgsCsException &cse )
3042 {
3043 QgsMessageLog::logMessage( tr( "Error transforming extent: %1" ).arg( cse.what() ) );
3045 }
3046 }
3047 return wgs84Extent;
3048}
3049
3050void QgsMapLayer::updateExtent( const QgsRectangle &extent ) const
3051{
3053
3054 if ( extent == mExtent2D )
3055 return;
3056
3057 mExtent2D = extent;
3058
3059 // do not update the wgs84 extent if we trust layer metadata
3061 return;
3062
3063 mWgs84Extent = wgs84Extent( true );
3064}
3065
3066void QgsMapLayer::updateExtent( const QgsBox3D &extent ) const
3067{
3069
3070 if ( extent == mExtent3D )
3071 return;
3072
3073 if ( extent.isNull() )
3074 {
3075 if ( !extent.toRectangle().isNull() )
3076 {
3077 // bad 3D extent param but valid in 2d --> update 2D extent
3078 updateExtent( extent.toRectangle() );
3079 }
3080 else
3081 {
3082 QgsDebugMsgLevel( QStringLiteral( "Unable to update extent with empty parameter" ), 1 );
3083 }
3084 }
3085 else
3086 {
3087 mExtent3D = extent;
3088
3089 // do not update the wgs84 extent if we trust layer metadata
3091 return;
3092
3093 mWgs84Extent = wgs84Extent( true );
3094 }
3095}
3096
3097bool QgsMapLayer::rebuildCrs3D( QString *error )
3098{
3099 bool res = true;
3100 if ( !mCRS.isValid() )
3101 {
3103 }
3104 else if ( !mVerticalCrs.isValid() )
3105 {
3106 mCrs3D = mCRS;
3107 }
3108 else
3109 {
3110 switch ( mCRS.type() )
3111 {
3115 mCrs3D = mCRS;
3116 break;
3117
3119 {
3120 QString tempError;
3121 mCrs3D = mCRS.hasVerticalAxis() ? mCRS : QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3122 res = mCrs3D.isValid();
3123 break;
3124 }
3125
3127 // nonsense situation
3129 res = false;
3130 break;
3131
3140 {
3141 QString tempError;
3142 mCrs3D = QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3143 res = mCrs3D.isValid();
3144 break;
3145 }
3146 }
3147 }
3148 return res;
3149}
3150
3152{
3154
3155 // do not update the wgs84 extent if we trust layer metadata
3157 return;
3158
3159 mWgs84Extent = QgsRectangle();
3160}
3161
3163{
3165
3166 QString metadata = QStringLiteral( "<h1>" ) + tr( "General" ) + QStringLiteral( "</h1>\n<hr>\n" ) + QStringLiteral( "<table class=\"list-view\">\n" );
3167
3168 // name
3169 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + name() + QStringLiteral( "</td></tr>\n" );
3170
3171 const QString lPublicSource = publicSource();
3172
3173 QString path;
3174 bool isLocalPath = false;
3175 if ( dataProvider() )
3176 {
3177 // local path
3178 QVariantMap uriComponents = QgsProviderRegistry::instance()->decodeUri( dataProvider()->name(), lPublicSource );
3179 if ( uriComponents.contains( QStringLiteral( "path" ) ) )
3180 {
3181 path = uriComponents[QStringLiteral( "path" )].toString();
3182 QFileInfo fi( path );
3183 if ( fi.exists() )
3184 {
3185 isLocalPath = true;
3186 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Path" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( path ).toString(), QDir::toNativeSeparators( path ) ) ) + QStringLiteral( "</td></tr>\n" );
3187
3188 QDateTime lastModified = fi.lastModified();
3189 QString lastModifiedFileName;
3190 QSet<QString> sidecarFiles = QgsFileUtils::sidecarFilesForPath( path );
3191 if ( fi.isFile() )
3192 {
3193 qint64 fileSize = fi.size();
3194 if ( !sidecarFiles.isEmpty() )
3195 {
3196 lastModifiedFileName = fi.fileName();
3197 QStringList sidecarFileNames;
3198 for ( const QString &sidecarFile : sidecarFiles )
3199 {
3200 QFileInfo sidecarFi( sidecarFile );
3201 fileSize += sidecarFi.size();
3202 if ( sidecarFi.lastModified() > lastModified )
3203 {
3204 lastModified = sidecarFi.lastModified();
3205 lastModifiedFileName = sidecarFi.fileName();
3206 }
3207 sidecarFileNames << sidecarFi.fileName();
3208 }
3209 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + ( sidecarFiles.size() > 1 ? tr( "Sidecar files" ) : tr( "Sidecar file" ) ) + QStringLiteral( "</td><td>%1" ).arg( sidecarFileNames.join( QLatin1String( ", " ) ) ) + QStringLiteral( "</td></tr>\n" );
3210 }
3211 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + ( !sidecarFiles.isEmpty() ? tr( "Total size" ) : tr( "Size" ) ) + QStringLiteral( "</td><td>%1" ).arg( QgsFileUtils::representFileSize( fileSize ) ) + QStringLiteral( "</td></tr>\n" );
3212 }
3213 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Last modified" ) + QStringLiteral( "</td><td>%1" ).arg( QLocale().toString( fi.lastModified() ) ) + ( !lastModifiedFileName.isEmpty() ? QStringLiteral( " (%1)" ).arg( lastModifiedFileName ) : QString() ) + QStringLiteral( "</td></tr>\n" );
3214 }
3215 }
3216 if ( uriComponents.contains( QStringLiteral( "url" ) ) )
3217 {
3218 const QString url = uriComponents[QStringLiteral( "url" )].toString();
3219 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "URL" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl( url ).toString(), url ) ) + QStringLiteral( "</td></tr>\n" );
3220 }
3221 }
3222
3223 // data source
3224 if ( lPublicSource != path || !isLocalPath )
3225 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Source" ) + QStringLiteral( "</td><td>%1" ).arg( lPublicSource != path ? lPublicSource : path ) + QStringLiteral( "</td></tr>\n" );
3226
3227 // provider
3228 if ( dataProvider() )
3229 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Provider" ) + QStringLiteral( "</td><td>%1" ).arg( dataProvider()->name() ) + QStringLiteral( "</td></tr>\n" );
3230
3231 // Layer ID
3232 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Layer ID" ) + QStringLiteral( "</td><td>%1" ).arg( id() ) + QStringLiteral( "</td></tr>\n" );
3233
3234 metadata += QLatin1String( "</table>\n<br><br>" );
3235
3236 return metadata;
3237}
3238
3240{
3241 QString metadata;
3242 // custom properties
3243 if ( const auto keys = customPropertyKeys(); !keys.isEmpty() )
3244 {
3245 metadata += QStringLiteral( "<h1>" ) + tr( "Custom properties" ) + QStringLiteral( "</h1>\n<hr>\n" );
3246 metadata += QLatin1String( "<table class=\"list-view\">\n<tbody>" );
3247 for ( const QString &key : keys )
3248 {
3249 // keys prefaced with _ are considered private/internal details
3250 if ( key.startsWith( '_' ) )
3251 continue;
3252
3253 const QVariant propValue = customProperty( key );
3254 QString stringValue;
3255 if ( propValue.type() == QVariant::List || propValue.type() == QVariant::StringList )
3256 {
3257 for ( const QString &s : propValue.toStringList() )
3258 {
3259 stringValue += "<p style=\"margin: 0;\">" + s.toHtmlEscaped() + "</p>";
3260 }
3261 }
3262 else
3263 {
3264 stringValue = propValue.toString().toHtmlEscaped();
3265
3266 //if the result string is empty but propValue is not, the conversion has failed
3267 if ( stringValue.isEmpty() && !QgsVariantUtils::isNull( propValue ) )
3268 stringValue = tr( "<i>value cannot be displayed</i>" );
3269 }
3270
3271 metadata += QStringLiteral( "<tr><td class=\"highlight\">%1</td><td>%2</td></tr>" ).arg( key.toHtmlEscaped(), stringValue );
3272 }
3273 metadata += QLatin1String( "</tbody></table>\n" );
3274 metadata += QLatin1String( "<br><br>\n" );
3275 }
3276 return metadata;
3277}
3278
3280{
3282 QString metadata;
3283
3284 auto addCrsInfo = [&metadata]( const QgsCoordinateReferenceSystem & c, bool includeType, bool includeOperation, bool includeCelestialBody )
3285 {
3286 if ( !c.isValid() )
3287 metadata += QStringLiteral( "<tr><td colspan=\"2\" class=\"highlight\">" ) + tr( "Unknown" ) + QStringLiteral( "</td></tr>\n" );
3288 else
3289 {
3290 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + c.userFriendlyIdentifier( Qgis::CrsIdentifierType::FullString ) + QStringLiteral( "</td></tr>\n" );
3291
3292 // map units
3293 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Units" ) + QStringLiteral( "</td><td>" )
3294 + ( c.isGeographic() ? tr( "Geographic (uses latitude and longitude for coordinates)" ) : QgsUnitTypes::toString( c.mapUnits() ) )
3295 + QStringLiteral( "</td></tr>\n" );
3296
3297 if ( includeType )
3298 {
3299 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Type" ) + QStringLiteral( "</td><td>" ) + QgsCoordinateReferenceSystemUtils::crsTypeToString( c.type() ) + QStringLiteral( "</td></tr>\n" );
3300 }
3301
3302 if ( includeOperation )
3303 {
3304 // operation
3305 const QgsProjOperation operation = c.operation();
3306 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Method" ) + QStringLiteral( "</td><td>" ) + operation.description() + QStringLiteral( "</td></tr>\n" );
3307 }
3308
3309 if ( includeCelestialBody )
3310 {
3311 // celestial body
3312 try
3313 {
3314 const QString celestialBody = c.celestialBodyName();
3315 if ( !celestialBody.isEmpty() )
3316 {
3317 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Celestial Body" ) + QStringLiteral( "</td><td>" ) + celestialBody + QStringLiteral( "</td></tr>\n" );
3318 }
3319 }
3320 catch ( QgsNotSupportedException & )
3321 {
3322
3323 }
3324 }
3325
3326 QString accuracyString;
3327 // dynamic crs with no epoch?
3328 if ( c.isDynamic() && std::isnan( c.coordinateEpoch() ) )
3329 {
3330 accuracyString = tr( "Based on a dynamic CRS, but no coordinate epoch is set. Coordinates are ambiguous and of limited accuracy." );
3331 }
3332
3333 // based on datum ensemble?
3334 try
3335 {
3336 const QgsDatumEnsemble ensemble = c.datumEnsemble();
3337 if ( ensemble.isValid() )
3338 {
3339 QString id;
3340 if ( !ensemble.code().isEmpty() )
3341 id = QStringLiteral( "<i>%1</i> (%2:%3)" ).arg( ensemble.name(), ensemble.authority(), ensemble.code() );
3342 else
3343 id = QStringLiteral( "<i>%1</i>”" ).arg( ensemble.name() );
3344
3345 if ( ensemble.accuracy() > 0 )
3346 {
3347 accuracyString = tr( "Based on %1, which has a limited accuracy of <b>at best %2 meters</b>." ).arg( id ).arg( ensemble.accuracy() );
3348 }
3349 else
3350 {
3351 accuracyString = tr( "Based on %1, which has a limited accuracy." ).arg( id );
3352 }
3353 }
3354 }
3355 catch ( QgsNotSupportedException & )
3356 {
3357
3358 }
3359
3360 if ( !accuracyString.isEmpty() )
3361 {
3362 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Accuracy" ) + QStringLiteral( "</td><td>" ) + accuracyString + QStringLiteral( "</td></tr>\n" );
3363 }
3364
3365 // static/dynamic
3366 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Reference" ) + QStringLiteral( "</td><td>%1</td></tr>\n" ).arg( c.isDynamic() ? tr( "Dynamic (relies on a datum which is not plate-fixed)" ) : tr( "Static (relies on a datum which is plate-fixed)" ) );
3367
3368 // coordinate epoch
3369 if ( !std::isnan( c.coordinateEpoch() ) )
3370 {
3371 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Coordinate Epoch" ) + QStringLiteral( "</td><td>%1</td></tr>\n" ).arg( qgsDoubleToString( c.coordinateEpoch(), 3 ) );
3372 }
3373 }
3374 };
3375
3376 metadata += QStringLiteral( "<h1>" ) + tr( "Coordinate Reference System (CRS)" ) + QStringLiteral( "</h1>\n<hr>\n" );
3377 metadata += QLatin1String( "<table class=\"list-view\">\n" );
3378 addCrsInfo( crs().horizontalCrs(), true, true, true );
3379 metadata += QLatin1String( "</table>\n<br><br>\n" );
3380
3381 if ( verticalCrs().isValid() )
3382 {
3383 metadata += QStringLiteral( "<h1>" ) + tr( "Vertical Coordinate Reference System (CRS)" ) + QStringLiteral( "</h1>\n<hr>\n" );
3384 metadata += QLatin1String( "<table class=\"list-view\">\n" );
3385 addCrsInfo( verticalCrs(), false, false, false );
3386 metadata += QLatin1String( "</table>\n<br><br>\n" );
3387 }
3388
3389 return metadata;
3390}
static QString version()
Version string.
Definition qgis.cpp:362
@ FullString
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions.
@ Vertical
Vertical CRS.
@ Temporal
Temporal CRS.
@ Compound
Compound (horizontal + vertical) CRS.
@ Projected
Projected CRS.
@ Other
Other type.
@ Bound
Bound CRS.
@ DerivedProjected
Derived projected CRS.
@ Unknown
Unknown type.
@ Engineering
Engineering CRS.
@ Geographic3d
3D geopraphic CRS
@ Geodetic
Geodetic CRS.
@ Geographic2d
2D geographic CRS
@ Geocentric
Geocentric CRS.
@ RemoveCredentials
Completely remove credentials (eg passwords) from the URI. This flag is not compatible with the Redac...
@ RedactCredentials
Replace the value of credentials (eg passwords) with 'xxxxxxxx'. This flag is not compatible with the...
@ ForceFirstLetterToCapital
Convert just the first letter of each word to uppercase, leave the rest untouched.
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
@ Unknown
Unknown types.
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
Definition qgis.h:450
LayerType
Types of layers that can be added to a map.
Definition qgis.h:169
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
QFlags< MapLayerProperty > MapLayerProperties
Map layer properties.
Definition qgis.h:2234
QFlags< LoadStyleFlag > LoadStyleFlags
Flags for loading layer styles.
Definition qgis.h:225
@ LoadDefaultStyle
Reset the layer's style to the default for the datasource.
@ ForceReadOnly
Open layer in a read-only mode.
@ SkipGetExtent
Skip the extent from provider.
@ TrustDataSource
Trust datasource config (primary key unicity, geometry type and srid, etc). Improves provider load ti...
@ IgnoreMissingStyleErrors
If the style is missing, then don't flag it as an error. This flag can be used when the caller is not...
AutoRefreshMode
Map layer automatic refresh modes.
Definition qgis.h:2244
@ RedrawOnly
Redraw current data only.
@ ReloadData
Reload data (and draw the new data)
@ Disabled
Automatic refreshing is disabled.
Base metadata class for 3D renderers.
virtual QgsAbstract3DRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context)=0
Returns new instance of the renderer given the DOM element.
Qgs3DRendererAbstractMetadata * rendererMetadata(const QString &type) const
Returns metadata for a 3D renderer type (may be used to create a new instance of the type)
Base class for all renderers that participate in 3D views.
virtual QString type() const =0
Returns unique identifier of the renderer class (used to identify subclass)
virtual void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const =0
Writes renderer's properties to given XML element.
virtual void resolveReferences(const QgsProject &project)
Resolves references to other objects - second phase of loading - after readXml()
static QString pkgDataPath()
Returns the common root path of all application data directories.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static Qgs3DRendererRegistry * renderer3DRegistry()
Returns registry of available 3D renderers.
bool setMasterPassword(bool verify=false)
Main call to initially set or continually check master password is set.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
QgsRectangle toRectangle() const
Converts the box to a 2D rectangle.
Definition qgsbox3d.h:379
bool isNull() const
Test if the box is null (holding no spatial information).
Definition qgsbox3d.cpp:310
static QString crsTypeToString(Qgis::CrsType type)
Returns a translated string representing a CRS type.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool hasVerticalAxis() const
Returns true if the CRS has a vertical axis.
void validate()
Perform some validation on this CRS.
static CUSTOM_CRS_VALIDATION customCrsValidation()
Gets custom function.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
static QgsCoordinateReferenceSystem createCompoundCrs(const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error)
Given a horizontal and vertical CRS, attempts to create a compound CRS from them.
static void setCustomCrsValidation(CUSTOM_CRS_VALIDATION f)
Sets custom function to force valid CRS.
void setValidationHint(const QString &html)
Set user hint for validation.
QgsCoordinateReferenceSystem verticalCrs() const
Returns the vertical CRS associated with this CRS object.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
Qgis::CrsType type() const
Returns the type of the CRS.
Contains information about the context in which a coordinate transform is executed.
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
virtual bool containsElevationData() const
Returns true if the data provider definitely contains elevation related data.
Abstract base class for spatial data provider implementations.
void notify(const QString &msg)
Emitted when the datasource issues a notification.
virtual QgsDataProviderElevationProperties * elevationProperties()
Returns the provider's elevation properties.
static QString removePassword(const QString &aUri, bool hide=false)
Removes the password element from a URI.
Contains information about a datum ensemble.
Definition qgsdatums.h:95
QString code() const
Identification code, e.g.
Definition qgsdatums.h:122
QString authority() const
Authority name, e.g.
Definition qgsdatums.h:117
bool isValid() const
Returns true if the datum ensemble is a valid object, or false if it is a null/invalid object.
Definition qgsdatums.h:102
QString name() const
Display name of datum ensemble.
Definition qgsdatums.h:107
double accuracy() const
Positional accuracy (in meters).
Definition qgsdatums.h:112
A container for error messages.
Definition qgserror.h:81
QString what() const
static QSet< QString > sidecarFilesForPath(const QString &path)
Returns a list of the sidecar files which exist for the dataset a the specified path.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
A structured metadata store for a map layer.
bool readMetadataXml(const QDomElement &metadataElement) override
Sets state from DOM document.
bool writeMetadataXml(QDomElement &metadataElement, QDomDocument &document) const override
Stores state in a DOM node.
static void setLayerNotes(QgsMapLayer *layer, const QString &notes)
Sets the notes for the specified layer, where notes is a HTML formatted string.
static bool layerHasNotes(const QgsMapLayer *layer)
Returns true if the specified layer has notes available.
static QString layerNotes(const QgsMapLayer *layer)
Returns the notes for the specified layer.
Models dependencies with or between map layers.
Base class for storage of map layer elevation properties.
An abstract interface for implementations of legends for one map layer.
void itemsChanged()
Emitted when existing items/nodes got invalid and should be replaced by new ones.
Manages QGIS Server properties for a map layer.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
void copyTo(QgsMapLayerServerProperties *properties) const
Copy properties to another instance.
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Management of styles for use with one map layer.
bool addStyle(const QString &name, const QgsMapLayerStyle &style)
Add a style with given name and data.
QStringList styles() const
Returns list of all defined style names.
bool renameStyle(const QString &name, const QString &newName)
Rename a stored style to a different name.
QgsMapLayerStyle style(const QString &name) const
Returns data of a stored style - accessed by its unique name.
Base class for storage of map layer temporal properties.
Base class for all map layer types.
Definition qgsmaplayer.h:77
void crs3DChanged()
Emitted when the crs3D() of the layer has changed.
Q_DECL_DEPRECATED void setShortName(const QString &shortName)
Sets the short name of the layer used by QGIS Server to identify the layer.
virtual bool deleteStyleFromDatabase(const QString &styleId, QString &msgError)
Deletes a style from the database.
bool importNamedMetadata(QDomDocument &document, QString &errorMessage)
Import the metadata of this layer from a QDomDocument.
QString name
Definition qgsmaplayer.h:81
void readStyleManager(const QDomNode &layerNode)
Read style manager's configuration (if any). To be called by subclasses.
virtual bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const =0
Write the style for the layer into the document provided.
Q_DECL_DEPRECATED QString legendUrlFormat() const
Returns the format for a URL based layer legend.
QgsRectangle wgs84Extent(bool forceRecalculate=false) const
Returns the WGS84 extent (EPSG:4326) of the layer according to ReadFlag::FlagTrustLayerMetadata.
void setRefreshOnNotifyEnabled(bool enabled)
Set whether provider notification is connected to triggerRepaint.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QgsAbstract3DRenderer * renderer3D() const
Returns 3D renderer associated with the layer.
virtual bool isTemporary() const
Returns true if the layer is considered a temporary layer.
virtual Q_DECL_DEPRECATED void exportSldStyleV2(QDomDocument &doc, QString &errorMsg, QgsSldExportContext &exportContext) const
Export the properties of this layer as SLD style in a QDomDocument.
virtual void exportNamedStyle(QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context=QgsReadWriteContext(), QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) const
Export the properties of this layer as named style in a QDomDocument.
bool setId(const QString &id)
Sets the layer's id.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the layer.
void dependenciesChanged()
Emitted when dependencies are changed.
virtual bool hasMapTips() const
Returns true if the layer contains map tips.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
void legendChanged()
Signal emitted when legend of the layer has changed.
void writeStyleManager(QDomNode &layerNode, QDomDocument &doc) const
Write style manager's configuration (if exists). To be called by subclasses.
QgsMapLayerLegend * legend() const
Can be nullptr.
QFlags< ReadFlag > ReadFlags
QFlags< LayerFlag > LayerFlags
virtual bool importNamedStyle(QDomDocument &doc, QString &errorMsg, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Import the properties of this layer from a QDomDocument.
Q_DECL_DEPRECATED void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
void metadataChanged()
Emitted when the layer's metadata is changed.
virtual QgsRectangle extent() const
Returns the extent of the layer.
virtual QString saveSldStyle(const QString &uri, bool &resultFlag) const
Saves the properties of this layer to an SLD format file.
QString source() const
Returns the source for the layer.
Q_DECL_DEPRECATED void setLegendUrl(const QString &legendUrl)
Sets the URL for the layer's legend.
virtual bool setDependencies(const QSet< QgsMapLayerDependency > &layers)
Sets the list of dependencies.
void request3DUpdate()
Signal emitted when a layer requires an update in any 3D maps.
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QgsError mError
Error.
int mBlockStyleChangedSignal
If non-zero, the styleChanged signal should not be emitted.
QString providerType() const
Returns the provider type (provider key) for this layer.
virtual void setExtent3D(const QgsBox3D &box)
Sets the extent.
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
Qgis::AutoRefreshMode autoRefreshMode() const
Returns the layer's automatic refresh mode.
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
void configChanged()
Emitted whenever the configuration is changed.
void trigger3DUpdate()
Will advise any 3D maps that this layer requires to be updated in the scene.
void autoRefreshIntervalChanged(int interval)
Emitted when the auto refresh interval changes.
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
virtual QSet< QgsMapLayerDependency > dependencies() const
Gets the list of dependencies.
void setCustomProperties(const QgsObjectCustomProperties &properties)
Set custom properties for layer.
static Qgis::DataProviderReadFlags providerReadFlags(const QDomNode &layerNode, QgsMapLayer::ReadFlags layerReadFlags)
Returns provider read flag deduced from layer read flags layerReadFlags and a dom node layerNode that...
virtual QString loadNamedStyle(const QString &theURI, bool &resultFlag, bool loadFromLocalDb, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories, Qgis::LoadStyleFlags flags=Qgis::LoadStyleFlags())
Loads a named style from file/local db/datasource db.
virtual QString encodedSource(const QString &source, const QgsReadWriteContext &context) const
Called by writeLayerXML(), used by derived classes to encode provider's specific data source to proje...
QgsMapLayer::LayerFlags flags() const
Returns the flags for this layer.
QgsCoordinateReferenceSystem crs3D
Definition qgsmaplayer.h:86
virtual void setSubLayerVisibility(const QString &name, bool visible)
Set the visibility of the given sublayer name.
void isValidChanged()
Emitted when the validity of this layer changed.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:84
bool loadNamedMetadataFromDatabase(const QString &db, const QString &uri, QString &qmd)
Retrieve a named metadata for this layer from a sqlite database.
virtual bool readXml(const QDomNode &layer_node, QgsReadWriteContext &context)
Called by readLayerXML(), used by children to read state specific to them from project files.
QgsMapLayerServerProperties * serverProperties()
Returns QGIS Server Properties for the map layer.
Q_DECL_DEPRECATED QString attribution() const
Returns the attribution of the layer used by QGIS Server in GetCapabilities request.
void setOriginalXmlProperties(const QString &originalXmlProperties)
Sets the original XML properties for the layer to originalXmlProperties.
void writeCustomProperties(QDomNode &layerNode, QDomDocument &doc) const
Write custom properties to project file.
QString mRefreshOnNofifyMessage
virtual int listStylesInDatabase(QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError)
Lists all the style in db split into related to the layer and not related to.
virtual QString loadDefaultStyle(bool &resultFlag)
Retrieve the default style for this layer if one exists (either as a .qml file on disk or as a record...
void setDataSource(const QString &dataSource, const QString &baseName=QString(), const QString &provider=QString(), bool loadDefaultStyleFlag=false)
Updates the data source of the layer.
QString mLayerName
Name of the layer - used for display.
virtual QString loadNamedMetadata(const QString &uri, bool &resultFlag)
Retrieve a named metadata for this layer if one exists (either as a .qmd file on disk or as a record ...
virtual bool writeXml(QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context) const
Called by writeLayerXML(), used by children to write state specific to them to project files.
Q_DECL_DEPRECATED bool hasAutoRefreshEnabled() const
Returns true if auto refresh is enabled for the layer.
QString id
Definition qgsmaplayer.h:80
void mapTipTemplateChanged()
Emitted when the map tip template changes.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
QString crsHtmlMetadata() const
Returns a HTML fragment containing the layer's CRS metadata, for use in the htmlMetadata() method.
Q_DECL_DEPRECATED void setAttributionUrl(const QString &attribUrl)
Sets the attribution URL of the layer used by QGIS Server in GetCapabilities request.
Q_DECL_DEPRECATED void setAutoRefreshEnabled(bool enabled)
Sets whether auto refresh is enabled for the layer.
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
QgsLayerMetadata metadata
Definition qgsmaplayer.h:83
static QString formatLayerName(const QString &name)
A convenience function to capitalize and format a layer name.
void renderer3DChanged()
Signal emitted when 3D renderer associated with the layer has changed.
Q_DECL_DEPRECATED QString abstract() const
Returns the abstract of the layer used by QGIS Server in GetCapabilities request.
QgsMapLayer(Qgis::LayerType type=Qgis::LayerType::Vector, const QString &name=QString(), const QString &source=QString())
Constructor for QgsMapLayer.
QString originalXmlProperties() const
Returns the XML properties of the original layer as they were when the layer was first read from the ...
Qgis::LayerType type
Definition qgsmaplayer.h:87
Q_DECL_DEPRECATED QString dataUrlFormat() const
Returns the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
Q_DECL_DEPRECATED void setDataUrl(const QString &dataUrl)
Sets the DataUrl of the layer used by QGIS Server in GetCapabilities request.
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
Q_DECL_DEPRECATED void setKeywordList(const QString &keywords)
Sets the keyword list of the layerused by QGIS Server in GetCapabilities request.
Q_DECL_DEPRECATED void setAttribution(const QString &attrib)
Sets the attribution of the layerused by QGIS Server in GetCapabilities request.
void setFlags(QgsMapLayer::LayerFlags flags)
Returns the flags for this layer.
bool isRefreshOnNotifyEnabled() const
Returns true if the refresh on provider nofification is enabled.
QString publicSource(bool hidePassword=false) const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
Q_DECL_DEPRECATED QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
QSet< QgsMapLayerDependency > mDependencies
List of layers that may modify this layer on modification.
void readCustomProperties(const QDomNode &layerNode, const QString &keyStartsWith=QString())
Read custom properties from project file.
virtual Qgis::MapLayerProperties properties() const
Returns the map layer properties of this layer.
virtual QString loadSldStyle(const QString &uri, bool &resultFlag)
Attempts to style the layer using the formatting from an SLD type file.
virtual void setMetadata(const QgsLayerMetadata &metadata)
Sets the layer's metadata store.
virtual bool readStyle(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read the style for the current layer from the DOM node supplied.
virtual QString saveDefaultMetadata(bool &resultFlag)
Save the current metadata of this layer as the default metadata (either as a .qmd file on disk or as ...
virtual bool supportsEditing() const
Returns whether the layer supports editing or not.
Q_DECL_DEPRECATED void setDataUrlFormat(const QString &dataUrlFormat)
Sets the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
QFlags< StyleCategory > StyleCategories
virtual void saveStyleToDatabase(const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Saves named and sld style of the layer to the style table in the db.
Q_INVOKABLE void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
QString mProviderKey
Data provider key (name of the data provider)
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
void styleChanged()
Signal emitted whenever a change affects the layer's style.
virtual bool isEditable() const
Returns true if the layer can be edited.
QUndoStack * undoStack()
Returns pointer to layer's undo stack.
std::unique_ptr< QgsDataProvider > mPreloadedProvider
Optionally used when loading a project, it is released when the layer is effectively created.
Q_DECL_DEPRECATED QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
void crsChanged()
Emitted when the crs() of the layer has changed.
virtual QgsError error() const
Gets current status error.
bool writeLayerXml(QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores state in DOM node.
virtual QString styleURI() const
Retrieve the style URI for this layer (either as a .qml file on disk or as a record in the users styl...
void setScaleBasedVisibility(bool enabled)
Sets whether scale based visibility is enabled for the layer.
void dataSourceChanged()
Emitted whenever the layer's data source has been changed.
void idChanged(const QString &id)
Emitted when the layer's ID has been changed.
Q_DECL_DEPRECATED QString dataUrl() const
Returns the DataUrl of the layer used by QGIS Server in GetCapabilities request.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
Q_DECL_DEPRECATED QString metadataUrlFormat() const
Returns the metadata format of the layer used by QGIS Server in GetCapabilities request.
void setRefreshOnNofifyMessage(const QString &message)
Set the notification message that triggers repaint If refresh on notification is enabled,...
static QString generateId(const QString &layerName)
Generates an unique identifier for this layer, the generate ID is prefixed by layerName.
QgsProviderMetadata * providerMetadata() const
Returns the layer data provider's metadata, it may be nullptr.
void opacityChanged(double opacity)
Emitted when the layer's opacity is changed, where opacity is a value between 0 (transparent) and 1 (...
virtual bool isModified() const
Returns true if the layer has been modified since last commit/save.
void styleLoaded(QgsMapLayer::StyleCategories categories)
Emitted when a style has been loaded.
virtual QString getStyleFromDatabase(const QString &styleId, QString &msgError)
Returns the named style corresponding to style id provided.
void emitStyleChanged()
Triggers an emission of the styleChanged() signal.
virtual QgsMapLayerTemporalProperties * temporalProperties()
Returns the layer's temporal properties.
QUndoStack * undoStackStyles()
Returns pointer to layer's style undo stack.
void dataChanged()
Data of layer changed.
virtual QStringList subLayers() const
Returns the sublayers of this layer.
virtual QString htmlMetadata() const
Obtain a formatted HTML string containing assorted metadata for this layer.
Q_DECL_DEPRECATED void setMetadataUrlFormat(const QString &metaUrlFormat)
Sets the metadata format of the layer used by QGIS Server in GetCapabilities request.
virtual bool loadNamedStyleFromDatabase(const QString &db, const QString &uri, QString &qml)
Retrieve a named style for this layer from a sqlite database.
void verticalCrsChanged()
Emitted when the verticalCrs() of the layer has changed.
virtual QgsBox3D extent3D() const
Returns the 3D extent of the layer.
static QString extensionPropertyType(PropertyType type)
Returns the extension of a Property.
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
void blendModeChanged(QPainter::CompositionMode blendMode)
Signal emitted when the blend mode is changed, through QgsMapLayer::setBlendMode()
void setName(const QString &name)
Set the display name of the layer.
void setAutoRefreshInterval(int interval)
Sets the auto refresh interval (in milliseconds) for the layer.
virtual bool readSymbology(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)=0
Read the symbology for the current layer from the DOM node supplied.
Q_DECL_DEPRECATED QString metadataUrl() const
Returns the metadata URL of the layer used by QGIS Server in GetCapabilities request.
virtual void setExtent(const QgsRectangle &rect)
Sets the extent.
virtual void resolveReferences(QgsProject *project)
Resolve references to other layers (kept as layer IDs after reading XML) into layer objects.
QString saveNamedMetadata(const QString &uri, bool &resultFlag)
Save the current metadata of this layer as a named metadata (either as a .qmd file on disk or as a re...
QString mDataSource
Data source description string, varies by layer type.
void setAutoRefreshMode(Qgis::AutoRefreshMode mode)
Sets the automatic refresh mode for the layer.
QString refreshOnNotifyMessage() const
Returns the message that should be notified by the provider to triggerRepaint.
virtual bool readSld(const QDomNode &node, QString &errorMessage)
void setMapTipsEnabled(bool enabled)
Enable or disable map tips for this layer.
virtual QString loadDefaultMetadata(bool &resultFlag)
Retrieve the default metadata for this layer if one exists (either as a .qmd file on disk or as a rec...
virtual QString saveSldStyleV2(bool &resultFlag, QgsSldExportContext &exportContext) const
Saves the properties of this layer to an SLD format file.
@ FlagReadExtentFromXml
Read extent from xml and skip get extent from provider.
@ FlagTrustLayerMetadata
Trust layer metadata. Improves layer load time by skipping expensive checks like primary key unicity,...
@ FlagForceReadOnly
Force open as read only.
void setValid(bool valid)
Sets whether layer is valid or not.
Q_DECL_DEPRECATED QString attributionUrl() const
Returns the attribution URL of the layer used by QGIS Server in GetCapabilities request.
void readCommonStyle(const QDomElement &layerElement, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read style data common to all layer types.
void customPropertyChanged(const QString &key)
Emitted when a custom property of the layer has been changed or removed.
virtual QDomDocument exportSldStyleV3(QgsSldExportContext &exportContext) const
Export the properties of this layer as SLD style in a QDomDocument.
QgsMapLayer::ReadFlags mReadFlags
Read flags. It's up to the subclass to respect these when restoring state from XML.
double minimumScale() const
Returns the minimum map scale (i.e.
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
Q_DECL_DEPRECATED QString legendUrl() const
Returns the URL for the layer's legend.
void flagsChanged()
Emitted when layer's flags have been modified.
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
Q_DECL_DEPRECATED void setLegendUrlFormat(const QString &legendUrlFormat)
Sets the format for a URL based layer legend.
void exportNamedMetadata(QDomDocument &doc, QString &errorMsg) const
Export the current metadata of this layer as named metadata in a QDomDocument.
virtual QString saveNamedStyle(const QString &uri, bool &resultFlag, StyleCategories categories=AllStyleCategories)
Save the properties of this layer as a named style (either as a .qml file on disk or as a record in t...
virtual Q_DECL_DEPRECATED void exportSldStyle(QDomDocument &doc, QString &errorMsg) const
Export the properties of this layer as SLD style in a QDomDocument.
void beforeResolveReferences(QgsProject *project)
Emitted when all layers are loaded and references can be resolved, just before the references of this...
void setMapTipTemplate(const QString &mapTipTemplate)
The mapTip is a pretty, html representation for feature information.
Q_DECL_DEPRECATED void setMetadataUrl(const QString &metaUrl)
Sets the metadata URL of the layer used by QGIS Server in GetCapabilities request.
virtual QgsMapLayerElevationProperties * elevationProperties()
Returns the layer's elevation properties.
bool setVerticalCrs(const QgsCoordinateReferenceSystem &crs, QString *errorMessage=nullptr)
Sets the layer's vertical coordinate reference system.
Q_INVOKABLE QStringList customPropertyKeys() const
Returns list of all keys within custom properties.
QgsProject * project() const
Returns the parent project if this map layer is added to a project.
Q_DECL_DEPRECATED void setMetadataUrlType(const QString &metaUrlType)
Set the metadata type of the layer used by QGIS Server in GetCapabilities request MetadataUrlType ind...
bool mapTipsEnabled
Definition qgsmaplayer.h:91
bool readLayerXml(const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags=QgsMapLayer::ReadFlags(), QgsDataProvider *preloadedProvider=nullptr)
Sets state from DOM document.
void setLegend(QgsMapLayerLegend *legend)
Assign a legend controller to the map layer.
double opacity
Definition qgsmaplayer.h:89
virtual QString decodedSource(const QString &source, const QString &dataProvider, const QgsReadWriteContext &context) const
Called by readLayerXML(), used by derived classes to decode provider's specific data source from proj...
void nameChanged()
Emitted when the name has been changed.
virtual QString metadataUri() const
Retrieve the metadata URI for this layer (either as a .qmd file on disk or as a record in the users s...
int autoRefreshInterval
Definition qgsmaplayer.h:82
QgsCoordinateReferenceSystem verticalCrs
Definition qgsmaplayer.h:85
virtual bool writeStyle(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write just the symbology information for the layer into the document.
bool mIsRefreshOnNofifyEnabled
virtual Q_INVOKABLE QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
double mLayerOpacity
Layer opacity.
bool mValid
Indicates if the layer is valid and can be drawn.
@ LayerConfiguration
General configuration: identifiable, removable, searchable, display expression, read-only.
@ Symbology
Symbology.
@ Notes
Layer user notes.
@ Temporal
Temporal properties.
@ Rendering
Rendering: scale visibility, simplify method, opacity.
@ Elevation
Elevation settings.
@ Symbology3D
3D symbology
@ CustomProperties
Custom properties (by plugins for instance)
virtual Q_INVOKABLE void reload()
Synchronises with changes in the datasource.
virtual QDateTime timestamp() const
Time stamp of data source in the moment when data/metadata were loaded by provider.
void setProviderType(const QString &providerType)
Sets the providerType (provider key)
void mapTipsEnabledChanged()
Emitted when map tips are enabled or disabled for the layer.
virtual QString saveDefaultStyle(bool &resultFlag, StyleCategories categories)
Save the properties of this layer as the default style (either as a .qml file on disk or as a record ...
void setRenderer3D(QgsAbstract3DRenderer *renderer)
Sets 3D renderer for the layer.
~QgsMapLayer() override
QString customPropertyHtmlMetadata() const
Returns an HTML fragment containing custom property information, for use in the htmlMetadata() method...
const QgsObjectCustomProperties & customProperties() const
Read all custom properties from layer.
QString generalHtmlMetadata() const
Returns an HTML fragment containing general metadata information, for use in the htmlMetadata() metho...
Q_DECL_DEPRECATED QString metadataUrlType() const
Returns the metadata type of the layer used by QGIS Server in GetCapabilities request.
void writeCommonStyle(QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write style data common to all layer types.
double maximumScale() const
Returns the maximum map scale (i.e.
Q_DECL_DEPRECATED QString keywordList() const
Returns the keyword list of the layer used by QGIS Server in GetCapabilities request.
virtual void setLayerOrder(const QStringList &layers)
Reorders the previously selected sublayers of this layer from bottom to top.
void invalidateWgs84Extent()
Invalidates the WGS84 extent.
QString mapTipTemplate
Definition qgsmaplayer.h:90
Q_DECL_DEPRECATED void setTitle(const QString &title)
Sets the title of the layer used by QGIS Server in GetCapabilities request.
PropertyType
Maplayer has a style and a metadata property.
bool mShouldValidateCrs
true if the layer's CRS should be validated and invalid CRSes are not permitted.
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Custom exception class which is raised when an operation is not supported.
Simple key-value store (keys = strings, values = variants) that supports loading/saving to/from XML i...
void setValue(const QString &key, const QVariant &value)
Add an entry to the store with the specified key.
QStringList keys() const
Returns a list of all stored keys.
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Writes the store contents to an XML node.
void remove(const QString &key)
Removes a key (entry) from the store.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value for the given key.
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from an XML node.
bool contains(const QString &key) const
Returns true if the properties contains a key with the specified name.
Contains information about a PROJ operation.
QString description() const
Description.
Convert from older project file versions to newer.
bool updateRevision(const QgsProjectVersion &version)
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
Translates a string using the Qt QTranslator mechanism.
Describes the version of a project.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
bool removeAttachedFile(const QString &path)
Removes the attached file.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QString baseName() const
Returns the base name of the project file without the path and without extension - derived from fileN...
QString absoluteFilePath() const
Returns full absolute path to the project file if the project is stored in a file system - derived fr...
Holds data provider key, description, and associated shared library file or function pointer informat...
@ SaveLayerMetadata
Indicates that the provider supports saving native layer metadata.
QString getStyleById(const QString &providerKey, const QString &uri, const QString &styleId, QString &errCause)
Gets a layer style defined by styleId.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
bool saveLayerMetadata(const QString &providerKey, const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage)
Saves metadata to the layer corresponding to the specified uri.
bool deleteStyleById(const QString &providerKey, const QString &uri, const QString &styleId, QString &errCause)
Deletes a layer style defined by styleId.
QString loadStoredStyle(const QString &providerKey, const QString &uri, QString &styleName, QString &errCause)
Loads a layer style from the provider storage, reporting its name.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
int listStyles(const QString &providerKey, const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause)
Lists stored layer styles in the provider defined by providerKey and uri.
bool saveStyle(const QString &providerKey, const QString &uri, const QString &qmlStyle, const QString &sldStyle, const QString &styleName, const QString &styleDescription, const QString &uiFileContent, bool useAsDefault, QString &errCause)
Saves a layer style to provider.
Represents a raster layer.
Q_DECL_DEPRECATED bool writeSld(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props=QVariantMap()) const
Writes the symbology of the layer into the document provided in SLD 1.0.0 format.
Allows entering a context category and takes care of leaving this category on deletion of the class.
A container for the context for various read/write operations on objects.
MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory(const QString &category, const QString &details=QString()) const
Push a category to the stack.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
A rectangle specified with double values.
static bool equalToOrGreaterThanMinimumScale(const double scale, const double minScale)
Returns whether the scale is equal to or greater than the minScale, taking non-round numbers into acc...
static bool lessThanMaximumScale(const double scale, const double maxScale)
Returns whether the scale is less than the maxScale, taking non-round numbers into account.
void setMetadataUrls(const QList< QgsServerMetadataUrlProperties::MetadataUrl > &metaUrls)
Sets a the list of metadata URL for the layer.
QList< QgsServerMetadataUrlProperties::MetadataUrl > metadataUrls() const
Returns a list of metadataUrl resources associated for the layer.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
QString exportFilePath() const
Returns the export file path for the SLD.
QStringList errors() const
Returns a list of errors which occurred during the conversion.
void setExtraProperties(const QVariantMap &properties)
Sets the open ended set of properties that can drive/inform the SLD encoding.
void setExportFilePath(const QString &exportFilePath)
Sets the export file path for the SLD to exportFilePath.
QVariantMap extraProperties() const
Returns the open ended set of properties that can drive/inform the SLD encoding.
void pushError(const QString &error)
Pushes a error message generated during the conversion.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
An interface for classes which can visit style entity (e.g.
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based dataset.
Q_DECL_DEPRECATED bool writeSld(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props=QVariantMap()) const
Writes the symbology of the layer into the document provided in SLD 1.1 format.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
static T readFlagAttribute(const QDomElement &element, const QString &attributeName, T defaultValue)
Read a flag value from an attribute of the element.
static QDomElement writeBox3D(const QgsBox3D &box, QDomDocument &doc, const QString &elementName=QStringLiteral("extent3D"))
Encodes a 3D box to a DOM element.
static QgsBox3D readBox3D(const QDomElement &element)
Decodes a DOM element to a 3D box.
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc, const QString &elementName=QStringLiteral("extent"))
Encodes a rectangle to a DOM element.
static QgsRectangle readRectangle(const QDomElement &element)
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
int open(const QString &path)
Opens the database at the specified file path.
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:6512
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6219
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6493
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6302
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:41
#define QgsDebugError(str)
Definition qgslogger.h:40
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
const QgsCoordinateReferenceSystem & crs
Setting options for creating vector data providers.
QString format
Format specification of online resource.