QGIS API Documentation 3.39.0-Master (52f98f8c831)
Loading...
Searching...
No Matches
qgsmaplayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayer.cpp - description
3 -------------------
4 begin : Fri Jun 28 2002
5 copyright : (C) 2002 by Gary E.Sherman
6 email : sherman at mrcc.com
7***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19#include "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 "qgsmaplayerlegend.h"
32#include "qgspathresolver.h"
34#include "qgsproject.h"
35#include "qgsproviderregistry.h"
36#include "qgsrasterlayer.h"
37#include "qgsreadwritecontext.h"
38#include "qgsrectangle.h"
39#include "qgssldexportcontext.h"
40#include "qgsvectorlayer.h"
41#include "qgsxmlutils.h"
42#include "qgsstringutils.h"
43#include "qgsmessagelog.h"
46#include "qgsprovidermetadata.h"
47#include "qgslayernotesutils.h"
48#include "qgsdatums.h"
49#include "qgsprojoperation.h"
50#include "qgsthreadingutils.h"
51#include "qgsunittypes.h"
52
53#include <QDir>
54#include <QDomDocument>
55#include <QDomElement>
56#include <QDomImplementation>
57#include <QDomNode>
58#include <QFile>
59#include <QFileInfo>
60#include <QLocale>
61#include <QTextStream>
62#include <QUrl>
63#include <QTimer>
64#include <QStandardPaths>
65#include <QUuid>
66#include <QRegularExpression>
67
68#include <sqlite3.h>
69
71{
72 switch ( type )
73 {
74 case Metadata:
75 return QStringLiteral( ".qmd" );
76
77 case Style:
78 return QStringLiteral( ".qml" );
79 }
80 return QString();
81}
82
84 const QString &lyrname,
85 const QString &source )
86 : mDataSource( source )
87 , mLayerName( lyrname )
88 , mLayerType( type )
89 , mServerProperties( std::make_unique<QgsMapLayerServerProperties>( this ) )
90 , mUndoStack( new QUndoStack( this ) )
91 , mUndoStackStyles( new QUndoStack( this ) )
92 , mStyleManager( new QgsMapLayerStyleManager( this ) )
93 , mRefreshTimer( new QTimer( this ) )
94{
95 mID = generateId( lyrname );
98 connect( mRefreshTimer, &QTimer::timeout, this, [this]
99 {
100
101 switch ( mAutoRefreshMode )
102 {
104 break;
106 triggerRepaint( true );
107 break;
109 reload();
110 break;
111 }
112 } );
113}
114
116{
117 if ( project() && project()->pathResolver().writePath( mDataSource ).startsWith( "attachment:" ) )
118 {
120 }
121
122 delete m3DRenderer;
123 delete mLegend;
124 delete mStyleManager;
125}
126
127void QgsMapLayer::clone( QgsMapLayer *layer ) const
128{
130
131 QgsDebugMsgLevel( QStringLiteral( "Cloning layer '%1'" ).arg( name() ), 3 );
132 layer->setBlendMode( blendMode() );
133
134 const auto constStyles = styleManager()->styles();
135 for ( const QString &s : constStyles )
136 {
137 layer->styleManager()->addStyle( s, styleManager()->style( s ) );
138 }
139
140 layer->setName( name() );
141
142 if ( layer->dataProvider() && layer->dataProvider()->elevationProperties() )
143 {
145 layer->mExtent3D = mExtent3D;
146 else
147 layer->mExtent2D = mExtent2D;
148 }
149
150 layer->setMaximumScale( maximumScale() );
151 layer->setMinimumScale( minimumScale() );
153 layer->setLegendUrl( legendUrl() );
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
257void QgsMapLayer::setShortName( const QString &shortName )
258{
260
261 mServerProperties->setShortName( shortName );
262}
263
265{
267
268 return mServerProperties->shortName();
269}
270
271void QgsMapLayer::setTitle( const QString &title )
272{
274
275 mServerProperties->setTitle( title );
276}
277
278QString QgsMapLayer::title() const
279{
281
282 return mServerProperties->title();
283}
284
285void QgsMapLayer::setAbstract( const QString &abstract )
286{
288
289 mServerProperties->setAbstract( abstract );
290}
291
293{
295
296 return mServerProperties->abstract();
297}
298
299void QgsMapLayer::setKeywordList( const QString &keywords )
300{
302
303 mServerProperties->setKeywordList( keywords );
304}
305
307{
309
310 return mServerProperties->keywordList();
311}
312
313void QgsMapLayer::setDataUrl( const QString &dataUrl )
314{
316
317 mServerProperties->setDataUrl( dataUrl );
318}
319
320QString QgsMapLayer::dataUrl() const
321{
323
324 return mServerProperties->dataUrl();
325}
326
327void QgsMapLayer::setDataUrlFormat( const QString &dataUrlFormat )
328{
330
331 mServerProperties->setDataUrlFormat( dataUrlFormat );
332}
333
335{
337
338 return mServerProperties->dataUrlFormat();
339}
340
341void QgsMapLayer::setAttribution( const QString &attrib )
342{
344
345 mServerProperties->setAttribution( attrib );
346}
347
349{
351
352 return mServerProperties->attribution();
353}
354
355void QgsMapLayer::setAttributionUrl( const QString &attribUrl )
356{
358
359 mServerProperties->setAttributionUrl( attribUrl );
360}
361
363{
365
366 return mServerProperties->attributionUrl();
367
368}
369
370void QgsMapLayer::setMetadataUrl( const QString &metaUrl )
371{
373
374 QList<QgsMapLayerServerProperties::MetadataUrl> urls = serverProperties()->metadataUrls();
375 if ( urls.isEmpty() )
376 {
377 const QgsMapLayerServerProperties::MetadataUrl newItem = QgsMapLayerServerProperties::MetadataUrl( metaUrl, QLatin1String(), QLatin1String() );
378 urls.prepend( newItem );
379 }
380 else
381 {
382 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
383 const QgsMapLayerServerProperties::MetadataUrl newItem( metaUrl, old.type, old.format );
384 urls.prepend( newItem );
385 }
387}
388
390{
392
393 if ( mServerProperties->metadataUrls().isEmpty() )
394 {
395 return QLatin1String();
396 }
397 else
398 {
399 return mServerProperties->metadataUrls().first().url;
400 }
401}
402
403void QgsMapLayer::setMetadataUrlType( const QString &metaUrlType )
404{
406
407 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
408 if ( urls.isEmpty() )
409 {
410 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), metaUrlType, QLatin1String() );
411 urls.prepend( newItem );
412 }
413 else
414 {
415 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
416 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, metaUrlType, old.format );
417 urls.prepend( newItem );
418 }
419 mServerProperties->setMetadataUrls( urls );
420}
421
423{
425
426 if ( mServerProperties->metadataUrls().isEmpty() )
427 {
428 return QLatin1String();
429 }
430 else
431 {
432 return mServerProperties->metadataUrls().first().type;
433 }
434}
435
436void QgsMapLayer::setMetadataUrlFormat( const QString &metaUrlFormat )
437{
439
440 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
441 if ( urls.isEmpty() )
442 {
443 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), QLatin1String(), metaUrlFormat );
444 urls.prepend( newItem );
445 }
446 else
447 {
448 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst( );
449 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, old.type, metaUrlFormat );
450 urls.prepend( newItem );
451 }
452 mServerProperties->setMetadataUrls( urls );
453}
454
456{
458
459 if ( mServerProperties->metadataUrls().isEmpty() )
460 {
461 return QString();
462 }
463 else
464 {
465 return mServerProperties->metadataUrls().first().format;
466 }
467}
468
469QString QgsMapLayer::publicSource( bool hidePassword ) const
470{
472
473 // Redo this every time we're asked for it, as we don't know if
474 // dataSource has changed.
475 QString safeName = QgsDataSourceUri::removePassword( mDataSource, hidePassword );
476 return safeName;
477}
478
479QString QgsMapLayer::source() const
480{
482
483 return mDataSource;
484}
485
487{
489
490 return mExtent2D.isNull() ? mExtent3D.toRectangle() : mExtent2D;
491}
492
494{
496
497 return mExtent3D;
498}
499
500void QgsMapLayer::setBlendMode( const QPainter::CompositionMode blendMode )
501{
503
504 if ( mBlendMode == blendMode )
505 return;
506
507 mBlendMode = blendMode;
510}
511
512QPainter::CompositionMode QgsMapLayer::blendMode() const
513{
514 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
516
517 return mBlendMode;
518}
519
520void QgsMapLayer::setOpacity( double opacity )
521{
523
525 return;
527 emit opacityChanged( opacity );
529}
530
532{
533 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
535
536 return mLayerOpacity;
537}
538
539bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags, QgsDataProvider *preloadedProvider )
540{
542
543 mPreloadedProvider.reset( preloadedProvider );
544
545 bool layerError;
547
548 QDomNode mnl;
549 QDomElement mne;
550
551 // read provider
552 QString provider;
553 mnl = layerElement.namedItem( QStringLiteral( "provider" ) );
554 mne = mnl.toElement();
555 provider = mne.text();
556
557 // set data source
558 mnl = layerElement.namedItem( QStringLiteral( "datasource" ) );
559 mne = mnl.toElement();
560 const QString dataSourceRaw = mne.text();
561 mDataSource = provider.isEmpty() ? dataSourceRaw : QgsProviderRegistry::instance()->relativeToAbsoluteUri( provider, dataSourceRaw, context );
562
563 // if the layer needs authentication, ensure the master password is set
564 const thread_local QRegularExpression rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
565 if ( rx.match( mDataSource ).hasMatch()
567 {
568 return false;
569 }
570
571 mDataSource = decodedSource( mDataSource, provider, context );
572
573 // Set the CRS from project file, asking the user if necessary.
574 // Make it the saved CRS to have WMS layer projected correctly.
575 // We will still overwrite whatever GDAL etc picks up anyway
576 // further down this function.
577 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
578 mne = mnl.toElement();
579
581 CUSTOM_CRS_VALIDATION savedValidation;
582
583 const QDomNode srsNode = layerElement.namedItem( QStringLiteral( "srs" ) );
584 mCRS.readXml( srsNode );
585 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) );
587 mCRS.validate();
588 savedCRS = mCRS;
589
590 // Do not validate any projections in children, they will be overwritten anyway.
591 // No need to ask the user for a projections when it is overwritten, is there?
594
595 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Layer" ), mne.text() );
596
597 // the internal name is just the data source basename
598 //QFileInfo dataSourceFileInfo( mDataSource );
599 //internalName = dataSourceFileInfo.baseName();
600
601 // set ID
602 mnl = layerElement.namedItem( QStringLiteral( "id" ) );
603 if ( ! mnl.isNull() )
604 {
605 mne = mnl.toElement();
606 if ( ! mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz)
607 {
608 const QString newId = mne.text();
609 if ( newId != mID )
610 {
611 mID = mne.text();
612 emit idChanged( mID );
613 }
614 }
615 }
616
617 // set name
618 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
619 mne = mnl.toElement();
620
621 //name can be translated
622 setName( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );
623
624 // now let the children grab what they need from the Dom node.
625 layerError = !readXml( layerElement, context );
626
627 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
628 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
629
630 // overwrite CRS with what we read from project file before the raster/vector
631 // file reading functions changed it. They will if projections is specified in the file.
632 // FIXME: is this necessary? Yes, it is (autumn 2019)
634 mCRS = savedCRS;
635
636 //vertical CRS
637 {
639 const QDomNode verticalCrsNode = layerElement.firstChildElement( QStringLiteral( "verticalCrs" ) );
640 if ( !verticalCrsNode.isNull() )
641 {
642 verticalCrs.readXml( verticalCrsNode );
643 }
644 mVerticalCrs = verticalCrs;
645 }
646 rebuildCrs3D();
647
648 //legendUrl
649 const QDomElement legendUrlElem = layerElement.firstChildElement( QStringLiteral( "legendUrl" ) );
650 if ( !legendUrlElem.isNull() )
651 {
652 mLegendUrl = legendUrlElem.text();
653 mLegendUrlFormat = legendUrlElem.attribute( QStringLiteral( "format" ), QString() );
654 }
655
656 serverProperties()->readXml( layerElement );
657
658 if ( serverProperties()->metadataUrls().isEmpty() )
659 {
660 // metadataUrl is still empty, maybe it's a QGIS Project < 3.22
661 // keep for legacy
662 const QDomElement metaUrlElem = layerElement.firstChildElement( QStringLiteral( "metadataUrl" ) );
663 if ( !metaUrlElem.isNull() )
664 {
665 const QString url = metaUrlElem.text();
666 const QString type = metaUrlElem.attribute( QStringLiteral( "type" ), QString() );
667 const QString format = metaUrlElem.attribute( QStringLiteral( "format" ), QString() );
668 const QgsMapLayerServerProperties::MetadataUrl newItem( url, type, format );
669 mServerProperties->setMetadataUrls( QList<QgsMapLayerServerProperties::MetadataUrl>() << newItem );
670 }
671 }
672
673 // mMetadata.readFromLayer( this );
674 const QDomElement metadataElem = layerElement.firstChildElement( QStringLiteral( "resourceMetadata" ) );
675 mMetadata.readMetadataXml( metadataElem );
676
677 setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ), QStringLiteral( "0" ) ).toInt() );
678 if ( layerElement.hasAttribute( QStringLiteral( "autoRefreshMode" ) ) )
679 {
680 setAutoRefreshMode( qgsEnumKeyToValue( layerElement.attribute( QStringLiteral( "autoRefreshMode" ) ), Qgis::AutoRefreshMode::Disabled ) );
681 }
682 else
683 {
684 setAutoRefreshMode( layerElement.attribute( QStringLiteral( "autoRefreshEnabled" ), QStringLiteral( "0" ) ).toInt() ? Qgis::AutoRefreshMode::RedrawOnly : Qgis::AutoRefreshMode::Disabled );
685 }
686 setRefreshOnNofifyMessage( layerElement.attribute( QStringLiteral( "refreshOnNotifyMessage" ), QString() ) );
687 setRefreshOnNotifyEnabled( layerElement.attribute( QStringLiteral( "refreshOnNotifyEnabled" ), QStringLiteral( "0" ) ).toInt() );
688
689 // geographic extent is read only if necessary
691 {
692 const QDomNode wgs84ExtentNode = layerElement.namedItem( QStringLiteral( "wgs84extent" ) );
693 if ( !wgs84ExtentNode.isNull() )
694 mWgs84Extent = QgsXmlUtils::readRectangle( wgs84ExtentNode.toElement() );
695 }
696
697 mLegendPlaceholderImage = layerElement.attribute( QStringLiteral( "legendPlaceholderImage" ) );
698
699 if ( verticalCrs() != oldVerticalCrs )
700 emit verticalCrsChanged();
701 if ( mCrs3D != oldCrs3D )
702 emit crs3DChanged();
703
704 return ! layerError;
705} // bool QgsMapLayer::readLayerXML
706
707
708bool QgsMapLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
709{
711
712 Q_UNUSED( layer_node )
713 Q_UNUSED( context )
714 // NOP by default; children will over-ride with behavior specific to them
715
716 // read Extent
718 {
719 const QDomNode extent3DNode = layer_node.namedItem( QStringLiteral( "extent3D" ) );
720 if ( extent3DNode.isNull() )
721 {
722 const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
723 if ( !extentNode.isNull() )
724 {
725 mExtent2D = QgsXmlUtils::readRectangle( extentNode.toElement() );
726 }
727 }
728 else
729 {
730 mExtent3D = QgsXmlUtils::readBox3D( extent3DNode.toElement() );
731 }
732 }
733
734 return true;
735} // void QgsMapLayer::readXml
736
737
738bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const
739{
741
742 if ( !mExtent3D.isNull() && dataProvider() && dataProvider()->elevationProperties() && dataProvider()->elevationProperties()->containsElevationData() )
743 layerElement.appendChild( QgsXmlUtils::writeBox3D( mExtent3D, document ) );
744 else if ( !mExtent2D.isNull() )
745 layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent2D, document ) );
746
747 if ( const QgsRectangle lWgs84Extent = wgs84Extent( true ); !lWgs84Extent.isNull() )
748 {
749 layerElement.appendChild( QgsXmlUtils::writeRectangle( lWgs84Extent, document, QStringLiteral( "wgs84extent" ) ) );
750 }
751
752 layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( mRefreshTimer->interval() ) );
753 layerElement.setAttribute( QStringLiteral( "autoRefreshMode" ), qgsEnumValueToKey( mAutoRefreshMode ) );
754 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyEnabled" ), mIsRefreshOnNofifyEnabled ? 1 : 0 );
755 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyMessage" ), mRefreshOnNofifyMessage );
756
757 // ID
758 QDomElement layerId = document.createElement( QStringLiteral( "id" ) );
759 const QDomText layerIdText = document.createTextNode( id() );
760 layerId.appendChild( layerIdText );
761
762 layerElement.appendChild( layerId );
763
764 if ( mVerticalCrs.isValid() )
765 {
766 QDomElement verticalSrsNode = document.createElement( QStringLiteral( "verticalCrs" ) );
767 mVerticalCrs.writeXml( verticalSrsNode, document );
768 layerElement.appendChild( verticalSrsNode );
769 }
770
771 // data source
772 QDomElement dataSource = document.createElement( QStringLiteral( "datasource" ) );
773 const QgsDataProvider *provider = dataProvider();
774 const QString providerKey = provider ? provider->name() : QString();
775 const QString srcRaw = encodedSource( source(), context );
776 const QString src = providerKey.isEmpty() ? srcRaw : QgsProviderRegistry::instance()->absoluteToRelativeUri( providerKey, srcRaw, context );
777 const QDomText dataSourceText = document.createTextNode( src );
778 dataSource.appendChild( dataSourceText );
779 layerElement.appendChild( dataSource );
780
781 // layer name
782 QDomElement layerName = document.createElement( QStringLiteral( "layername" ) );
783 const QDomText layerNameText = document.createTextNode( name() );
784 layerName.appendChild( layerNameText );
785 layerElement.appendChild( layerName );
786
787 // layer short name
788
789 // TODO -- ideally this would be in QgsMapLayerServerProperties::writeXml, but that's currently
790 // only called for SOME map layer subclasses!
791 if ( !mServerProperties->shortName().isEmpty() )
792 {
793 QDomElement layerShortName = document.createElement( QStringLiteral( "shortname" ) );
794 const QDomText layerShortNameText = document.createTextNode( mServerProperties->shortName() );
795 layerShortName.appendChild( layerShortNameText );
796 layerElement.appendChild( layerShortName );
797 }
798
799 // layer title
800 if ( !mServerProperties->title().isEmpty() )
801 {
802 QDomElement layerTitle = document.createElement( QStringLiteral( "title" ) );
803 const QDomText layerTitleText = document.createTextNode( mServerProperties->title() );
804 layerTitle.appendChild( layerTitleText );
805
806 if ( mServerProperties->title() != mServerProperties->wfsTitle() )
807 {
808 layerTitle.setAttribute( "wfs", mServerProperties->wfsTitle() );
809 }
810
811 layerElement.appendChild( layerTitle );
812 }
813
814 // layer abstract
815 if ( !mServerProperties->abstract().isEmpty() )
816 {
817 QDomElement layerAbstract = document.createElement( QStringLiteral( "abstract" ) );
818 const QDomText layerAbstractText = document.createTextNode( mServerProperties->abstract() );
819 layerAbstract.appendChild( layerAbstractText );
820 layerElement.appendChild( layerAbstract );
821 }
822
823 // layer keyword list
824 const QStringList keywordStringList = mServerProperties->keywordList().split( ',' );
825 if ( !keywordStringList.isEmpty() )
826 {
827 QDomElement layerKeywordList = document.createElement( QStringLiteral( "keywordList" ) );
828 for ( int i = 0; i < keywordStringList.size(); ++i )
829 {
830 QDomElement layerKeywordValue = document.createElement( QStringLiteral( "value" ) );
831 const QDomText layerKeywordText = document.createTextNode( keywordStringList.at( i ).trimmed() );
832 layerKeywordValue.appendChild( layerKeywordText );
833 layerKeywordList.appendChild( layerKeywordValue );
834 }
835 layerElement.appendChild( layerKeywordList );
836 }
837
838 // layer dataUrl
839 const QString aDataUrl = mServerProperties->dataUrl();
840 if ( !aDataUrl.isEmpty() )
841 {
842 QDomElement layerDataUrl = document.createElement( QStringLiteral( "dataUrl" ) );
843 const QDomText layerDataUrlText = document.createTextNode( aDataUrl );
844 layerDataUrl.appendChild( layerDataUrlText );
845 layerDataUrl.setAttribute( QStringLiteral( "format" ), mServerProperties->dataUrlFormat() );
846 layerElement.appendChild( layerDataUrl );
847 }
848
849 // layer legendUrl
850 const QString aLegendUrl = legendUrl();
851 if ( !aLegendUrl.isEmpty() )
852 {
853 QDomElement layerLegendUrl = document.createElement( QStringLiteral( "legendUrl" ) );
854 const QDomText layerLegendUrlText = document.createTextNode( aLegendUrl );
855 layerLegendUrl.appendChild( layerLegendUrlText );
856 layerLegendUrl.setAttribute( QStringLiteral( "format" ), legendUrlFormat() );
857 layerElement.appendChild( layerLegendUrl );
858 }
859
860 // layer attribution
861 const QString aAttribution = mServerProperties->attribution();
862 if ( !aAttribution.isEmpty() )
863 {
864 QDomElement layerAttribution = document.createElement( QStringLiteral( "attribution" ) );
865 const QDomText layerAttributionText = document.createTextNode( aAttribution );
866 layerAttribution.appendChild( layerAttributionText );
867 layerAttribution.setAttribute( QStringLiteral( "href" ), mServerProperties->attributionUrl() );
868 layerElement.appendChild( layerAttribution );
869 }
870
871 // timestamp if supported
872 if ( timestamp() > QDateTime() )
873 {
874 QDomElement stamp = document.createElement( QStringLiteral( "timestamp" ) );
875 const QDomText stampText = document.createTextNode( timestamp().toString( Qt::ISODate ) );
876 stamp.appendChild( stampText );
877 layerElement.appendChild( stamp );
878 }
879
880 layerElement.appendChild( layerName );
881
882 // zorder
883 // This is no longer stored in the project file. It is superfluous since the layers
884 // are written and read in the proper order.
885
886 // spatial reference system id
887 QDomElement mySrsElement = document.createElement( QStringLiteral( "srs" ) );
888 mCRS.writeXml( mySrsElement, document );
889 layerElement.appendChild( mySrsElement );
890
891 // layer metadata
892 QDomElement myMetadataElem = document.createElement( QStringLiteral( "resourceMetadata" ) );
893 mMetadata.writeMetadataXml( myMetadataElem, document );
894 layerElement.appendChild( myMetadataElem );
895
896 layerElement.setAttribute( QStringLiteral( "legendPlaceholderImage" ), mLegendPlaceholderImage );
897
898 // now append layer node to map layer node
899 return writeXml( layerElement, document, context );
900}
901
902void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &document,
903 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
904{
906
907 // save categories
908 const QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::StyleCategories>();
909 const QString categoriesKeys( metaEnum.valueToKeys( static_cast<int>( categories ) ) );
910 layerElement.setAttribute( QStringLiteral( "styleCategories" ), categoriesKeys );
911
912 if ( categories.testFlag( Rendering ) )
913 {
914 // use scale dependent visibility flag
915 layerElement.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
916 layerElement.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
917 layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );
918 }
919
920 if ( categories.testFlag( Symbology3D ) )
921 {
922 if ( m3DRenderer )
923 {
924 QDomElement renderer3DElem = document.createElement( QStringLiteral( "renderer-3d" ) );
925 renderer3DElem.setAttribute( QStringLiteral( "type" ), m3DRenderer->type() );
926 m3DRenderer->writeXml( renderer3DElem, context );
927 layerElement.appendChild( renderer3DElem );
928 }
929 }
930
931 if ( categories.testFlag( LayerConfiguration ) )
932 {
933 // flags
934 // this code is saving automatically all the flags entries
935 QDomElement layerFlagsElem = document.createElement( QStringLiteral( "flags" ) );
936 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
937 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
938 {
939 const bool flagValue = mFlags.testFlag( it.key() );
940 QDomElement flagElem = document.createElement( it.value() );
941 flagElem.appendChild( document.createTextNode( QString::number( flagValue ) ) );
942 layerFlagsElem.appendChild( flagElem );
943 }
944 layerElement.appendChild( layerFlagsElem );
945 }
946
947 if ( categories.testFlag( Temporal ) )
948 {
950 properties->writeXml( layerElement, document, context );
951 }
952
953 if ( categories.testFlag( Elevation ) )
954 {
956 properties->writeXml( layerElement, document, context );
957 }
958
959 if ( categories.testFlag( Notes ) && QgsLayerNotesUtils::layerHasNotes( this ) )
960 {
961 QDomElement notesElem = document.createElement( QStringLiteral( "userNotes" ) );
962 notesElem.setAttribute( QStringLiteral( "value" ), QgsLayerNotesUtils::layerNotes( this ) );
963 layerElement.appendChild( notesElem );
964 }
965
966 // custom properties
967 if ( categories.testFlag( CustomProperties ) )
968 {
969 writeCustomProperties( layerElement, document );
970 }
971}
972
973
974bool QgsMapLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const
975{
977
978 Q_UNUSED( layer_node )
979 Q_UNUSED( document )
980 Q_UNUSED( context )
981 // NOP by default; children will over-ride with behavior specific to them
982
983 return true;
984}
985
986QString QgsMapLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
987{
989
990 Q_UNUSED( context )
991 return source;
992}
993
994QString QgsMapLayer::decodedSource( const QString &source, const QString &dataProvider, const QgsReadWriteContext &context ) const
995{
997
998 Q_UNUSED( context )
999 Q_UNUSED( dataProvider )
1000 return source;
1001}
1002
1004{
1006
1008 if ( m3DRenderer )
1009 m3DRenderer->resolveReferences( *project );
1010}
1011
1012
1013void QgsMapLayer::readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith )
1014{
1016
1017 const QgsObjectCustomProperties oldKeys = mCustomProperties;
1018
1019 mCustomProperties.readXml( layerNode, keyStartsWith );
1020
1021 for ( const QString &key : mCustomProperties.keys() )
1022 {
1023 if ( !oldKeys.contains( key ) || mCustomProperties.value( key ) != oldKeys.value( key ) )
1024 {
1025 emit customPropertyChanged( key );
1026 }
1027 }
1028}
1029
1030void QgsMapLayer::writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const
1031{
1033
1034 mCustomProperties.writeXml( layerNode, doc );
1035}
1036
1037void QgsMapLayer::readStyleManager( const QDomNode &layerNode )
1038{
1040
1041 const QDomElement styleMgrElem = layerNode.firstChildElement( QStringLiteral( "map-layer-style-manager" ) );
1042 if ( !styleMgrElem.isNull() )
1043 mStyleManager->readXml( styleMgrElem );
1044 else
1045 mStyleManager->reset();
1046}
1047
1048void QgsMapLayer::writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const
1049{
1051
1052 if ( mStyleManager )
1053 {
1054 QDomElement styleMgrElem = doc.createElement( QStringLiteral( "map-layer-style-manager" ) );
1055 mStyleManager->writeXml( styleMgrElem );
1056 layerNode.appendChild( styleMgrElem );
1057 }
1058}
1059
1061{
1063
1064 return mMapTipTemplate;
1065}
1066
1067void QgsMapLayer::setMapTipTemplate( const QString &mapTip )
1068{
1070
1071 if ( mMapTipTemplate == mapTip )
1072 return;
1073
1074 mMapTipTemplate = mapTip;
1075 emit mapTipTemplateChanged();
1076}
1077
1079{
1081
1082 if ( mMapTipsEnabled == enabled )
1083 return;
1084
1085 mMapTipsEnabled = enabled;
1086 emit mapTipsEnabledChanged();
1087}
1088
1090{
1092
1093 return mMapTipsEnabled;
1094}
1095
1097{
1099 if ( layerReadFlags & QgsMapLayer::FlagTrustLayerMetadata )
1100 {
1102 }
1103 if ( layerReadFlags & QgsMapLayer::FlagForceReadOnly )
1104 {
1106 }
1107
1108 if ( layerReadFlags & QgsMapLayer::FlagReadExtentFromXml )
1109 {
1110 const QDomNode extent3DNode = layerNode.namedItem( QStringLiteral( "extent3D" ) );
1111 if ( extent3DNode.isNull() )
1112 {
1113 const QDomNode extentNode = layerNode.namedItem( QStringLiteral( "extent" ) );
1114 if ( !extentNode.isNull() )
1115 {
1117 }
1118 }
1119 else
1120 {
1122 }
1123 }
1124
1125 return flags;
1126}
1127
1129{
1130 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
1132
1133 return mValid;
1134}
1135
1136#if 0
1137void QgsMapLayer::connectNotify( const char *signal )
1138{
1139 Q_UNUSED( signal )
1140 QgsDebugMsgLevel( "QgsMapLayer connected to " + QString( signal ), 3 );
1141} // QgsMapLayer::connectNotify
1142#endif
1143
1144bool QgsMapLayer::isInScaleRange( double scale ) const
1145{
1146 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1148
1149 return !mScaleBasedVisibility ||
1150 ( ( mMinScale == 0 || mMinScale * Qgis::SCALE_PRECISION < scale )
1151 && ( mMaxScale == 0 || scale < mMaxScale ) );
1152}
1153
1155{
1156 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1158
1159 return mScaleBasedVisibility;
1160}
1161
1163{
1165
1166 return mAutoRefreshMode != Qgis::AutoRefreshMode::Disabled;;
1167}
1168
1170{
1172
1173 return mAutoRefreshMode;
1174}
1175
1177{
1179
1180 return mRefreshTimer->interval();
1181}
1182
1184{
1186
1187 if ( interval <= 0 )
1188 {
1189 mRefreshTimer->stop();
1190 mRefreshTimer->setInterval( 0 );
1192 }
1193 else
1194 {
1195 mRefreshTimer->setInterval( interval );
1196 }
1197 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1198}
1199
1206
1208{
1210
1211 if ( mode == mAutoRefreshMode )
1212 return;
1213
1214 mAutoRefreshMode = mode;
1215 switch ( mAutoRefreshMode )
1216 {
1218 mRefreshTimer->stop();
1219 break;
1220
1223 if ( mRefreshTimer->interval() > 0 )
1224 mRefreshTimer->start();
1225 break;
1226 }
1227
1228 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1229}
1230
1232{
1234
1235 return mMetadata;
1236}
1237
1239{
1241
1242 mMinScale = scale;
1243}
1244
1246{
1248
1249 return mMinScale;
1250}
1251
1253{
1255
1256 mMaxScale = scale;
1257}
1258
1260{
1262
1263 mScaleBasedVisibility = enabled;
1264}
1265
1267{
1269
1270 return mMaxScale;
1271}
1272
1273QStringList QgsMapLayer::subLayers() const
1274{
1276
1277 return QStringList();
1278}
1279
1280void QgsMapLayer::setLayerOrder( const QStringList &layers )
1281{
1283
1284 Q_UNUSED( layers )
1285}
1286
1287void QgsMapLayer::setSubLayerVisibility( const QString &name, bool vis )
1288{
1290
1291 Q_UNUSED( name )
1292 Q_UNUSED( vis )
1293}
1294
1296{
1298
1299 return false;
1300}
1301
1303{
1304 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1306
1307 return mCRS;
1308}
1309
1311{
1313
1314 switch ( mCRS.type() )
1315 {
1316 case Qgis::CrsType::Vertical: // would hope this never happens!
1317 QgsDebugError( QStringLiteral( "Layer has a vertical CRS set as the horizontal CRS!" ) );
1318 return mCRS;
1319
1321 return mCRS.verticalCrs();
1322
1334 break;
1335 }
1336 return mVerticalCrs;
1337}
1338
1340{
1342
1343 return mCrs3D.isValid() ? mCrs3D : mCRS;
1344}
1345
1346void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal )
1347{
1349 if ( mCRS == srs )
1350 return;
1351
1352 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
1353 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
1354
1355 mCRS = srs;
1356
1358 {
1359 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( name() ) );
1360 mCRS.validate();
1361 }
1362
1363 rebuildCrs3D();
1364
1365 if ( emitSignal )
1366 emit crsChanged();
1367
1368 // Did vertical crs also change as a result of this? If so, emit signal
1369 if ( oldVerticalCrs != verticalCrs() )
1370 emit verticalCrsChanged();
1371 if ( oldCrs3D != mCrs3D )
1372 emit crs3DChanged();
1373}
1374
1376{
1378 bool res = true;
1379 if ( crs.isValid() )
1380 {
1381 // validate that passed crs is a vertical crs
1382 switch ( crs.type() )
1383 {
1385 break;
1386
1399 if ( errorMessage )
1400 *errorMessage = QObject::tr( "Specified CRS is a %1 CRS, not a Vertical CRS" ).arg( qgsEnumValueToKey( crs.type() ) );
1401 return false;
1402 }
1403 }
1404
1405 if ( crs != mVerticalCrs )
1406 {
1407 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
1408 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
1409
1410 switch ( mCRS.type() )
1411 {
1413 if ( crs != oldVerticalCrs )
1414 {
1415 if ( errorMessage )
1416 *errorMessage = QObject::tr( "Layer CRS is a Compound CRS, specified Vertical CRS will be ignored" );
1417 return false;
1418 }
1419 break;
1420
1422 if ( crs != oldVerticalCrs )
1423 {
1424 if ( errorMessage )
1425 *errorMessage = QObject::tr( "Layer CRS is a Geographic 3D CRS, specified Vertical CRS will be ignored" );
1426 return false;
1427 }
1428 break;
1429
1431 if ( crs != oldVerticalCrs )
1432 {
1433 if ( errorMessage )
1434 *errorMessage = QObject::tr( "Layer CRS is a Geocentric CRS, specified Vertical CRS will be ignored" );
1435 return false;
1436 }
1437 break;
1438
1440 if ( mCRS.hasVerticalAxis() && crs != oldVerticalCrs )
1441 {
1442 if ( errorMessage )
1443 *errorMessage = QObject::tr( "Layer CRS is a Projected 3D CRS, specified Vertical CRS will be ignored" );
1444 return false;
1445 }
1446 break;
1447
1457 break;
1458 }
1459
1460 mVerticalCrs = crs;
1461 res = rebuildCrs3D( errorMessage );
1462
1463 // only emit signal if vertical crs was actually changed, so eg if mCrs is compound
1464 // then we haven't actually changed the vertical crs by this call!
1465 if ( verticalCrs() != oldVerticalCrs )
1466 emit verticalCrsChanged();
1467 if ( mCrs3D != oldCrs3D )
1468 emit crs3DChanged();
1469 }
1470 return res;
1471}
1472
1474{
1476
1477 const QgsDataProvider *lDataProvider = dataProvider();
1478 return lDataProvider ? lDataProvider->transformContext() : QgsCoordinateTransformContext();
1479}
1480
1481QString QgsMapLayer::formatLayerName( const QString &name )
1482{
1483 QString layerName( name );
1484 layerName.replace( '_', ' ' );
1486 return layerName;
1487}
1488
1489QString QgsMapLayer::baseURI( PropertyType type ) const
1490{
1492
1493 QString myURI = publicSource();
1494
1495 // first get base path for delimited text, spatialite and OGR layers,
1496 // as in these cases URI may contain layer name and/or additional
1497 // information. This also strips prefix in case if VSIFILE mechanism
1498 // is used
1499 if ( providerType() == QLatin1String( "ogr" ) || providerType() == QLatin1String( "delimitedtext" )
1500 || providerType() == QLatin1String( "gdal" ) || providerType() == QLatin1String( "spatialite" ) )
1501 {
1502 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( providerType(), myURI );
1503 myURI = components["path"].toString();
1504 }
1505
1506 QFileInfo myFileInfo( myURI );
1507 QString key;
1508
1509 if ( myFileInfo.exists() )
1510 {
1511 // if file is using the /vsizip/ or /vsigzip/ mechanism, cleanup the name
1512 if ( myURI.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
1513 myURI.chop( 3 );
1514 else if ( myURI.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
1515 myURI.chop( 4 );
1516 else if ( myURI.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) )
1517 myURI.chop( 4 );
1518 else if ( myURI.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) )
1519 myURI.chop( 7 );
1520 else if ( myURI.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
1521 myURI.chop( 4 );
1522 myFileInfo.setFile( myURI );
1523 // get the file name for our .qml style file
1524 key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1525 }
1526 else
1527 {
1528 key = publicSource();
1529 }
1530
1531 return key;
1532}
1533
1535{
1537
1538 return baseURI( PropertyType::Metadata );
1539}
1540
1541QString QgsMapLayer::saveDefaultMetadata( bool &resultFlag )
1542{
1544
1545 if ( const QgsProviderMetadata *metadata = QgsProviderRegistry::instance()->providerMetadata( providerType() ) )
1546 {
1547 if ( metadata->providerCapabilities() & QgsProviderMetadata::SaveLayerMetadata )
1548 {
1549 try
1550 {
1551 QString errorMessage;
1552 resultFlag = QgsProviderRegistry::instance()->saveLayerMetadata( providerType(), mDataSource, mMetadata, errorMessage );
1553 if ( resultFlag )
1554 return tr( "Successfully saved default layer metadata" );
1555 else
1556 return errorMessage;
1557 }
1558 catch ( QgsNotSupportedException &e )
1559 {
1560 resultFlag = false;
1561 return e.what();
1562 }
1563 }
1564 }
1565
1566 // fallback default metadata saving method, for providers which don't support (or implement) saveLayerMetadata
1567 return saveNamedMetadata( metadataUri(), resultFlag );
1568}
1569
1570QString QgsMapLayer::loadDefaultMetadata( bool &resultFlag )
1571{
1573
1574 return loadNamedMetadata( metadataUri(), resultFlag );
1575}
1576
1578{
1580
1581 return baseURI( PropertyType::Style );
1582}
1583
1590
1591bool QgsMapLayer::loadNamedMetadataFromDatabase( const QString &db, const QString &uri, QString &qmd )
1592{
1594
1595 return loadNamedPropertyFromDatabase( db, uri, qmd, PropertyType::Metadata );
1596}
1597
1598bool QgsMapLayer::loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml )
1599{
1601
1602 return loadNamedPropertyFromDatabase( db, uri, qml, PropertyType::Style );
1603}
1604
1605bool QgsMapLayer::loadNamedPropertyFromDatabase( const QString &db, const QString &uri, QString &xml, QgsMapLayer::PropertyType type )
1606{
1608
1609 QgsDebugMsgLevel( QStringLiteral( "db = %1 uri = %2" ).arg( db, uri ), 4 );
1610
1611 bool resultFlag = false;
1612
1613 // read from database
1616
1617 int myResult;
1618
1619 QgsDebugMsgLevel( QStringLiteral( "Trying to load style or metadata for \"%1\" from \"%2\"" ).arg( uri, db ), 4 );
1620
1621 if ( db.isEmpty() || !QFile( db ).exists() )
1622 return false;
1623
1624 myResult = database.open_v2( db, SQLITE_OPEN_READONLY, nullptr );
1625 if ( myResult != SQLITE_OK )
1626 {
1627 return false;
1628 }
1629
1630 QString mySql;
1631 switch ( type )
1632 {
1633 case Metadata:
1634 mySql = QStringLiteral( "select qmd from tbl_metadata where metadata=?" );
1635 break;
1636
1637 case Style:
1638 mySql = QStringLiteral( "select qml from tbl_styles where style=?" );
1639 break;
1640 }
1641
1642 statement = database.prepare( mySql, myResult );
1643 if ( myResult == SQLITE_OK )
1644 {
1645 QByteArray param = uri.toUtf8();
1646
1647 if ( sqlite3_bind_text( statement.get(), 1, param.data(), param.length(), SQLITE_STATIC ) == SQLITE_OK &&
1648 sqlite3_step( statement.get() ) == SQLITE_ROW )
1649 {
1650 xml = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( statement.get(), 0 ) ) );
1651 resultFlag = true;
1652 }
1653 }
1654 return resultFlag;
1655}
1656
1657
1658QString QgsMapLayer::loadNamedStyle( const QString &uri, bool &resultFlag, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
1659{
1661
1662 return loadNamedStyle( uri, resultFlag, false, categories, flags );
1663}
1664
1665QString QgsMapLayer::loadNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &namedPropertyExists, bool &propertySuccessfullyLoaded, StyleCategories categories, Qgis::LoadStyleFlags flags )
1666{
1668
1669 QgsDebugMsgLevel( QStringLiteral( "uri = %1 myURI = %2" ).arg( uri, publicSource() ), 4 );
1670
1671 namedPropertyExists = false;
1672 propertySuccessfullyLoaded = false;
1673 if ( uri.isEmpty() )
1674 return QString();
1675
1676 QDomDocument myDocument( QStringLiteral( "qgis" ) );
1677
1678 // location of problem associated with errorMsg
1679 int line, column;
1680 QString myErrorMessage;
1681
1682 QFile myFile( uri );
1683 if ( myFile.open( QFile::ReadOnly ) )
1684 {
1685 QgsDebugMsgLevel( QStringLiteral( "file found %1" ).arg( uri ), 2 );
1686 namedPropertyExists = true;
1687
1688 // read file
1689 propertySuccessfullyLoaded = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
1690 if ( !propertySuccessfullyLoaded )
1691 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1692 myFile.close();
1693 }
1694 else
1695 {
1696 const QFileInfo project( QgsProject::instance()->fileName() );
1697 QgsDebugMsgLevel( QStringLiteral( "project fileName: %1" ).arg( project.absoluteFilePath() ), 4 );
1698
1699 QString xml;
1700 switch ( type )
1701 {
1702 case QgsMapLayer::Style:
1703 {
1704 if ( loadNamedStyleFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1705 ( project.exists() && loadNamedStyleFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1706 loadNamedStyleFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1707 {
1708 namedPropertyExists = true;
1709 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1710 if ( !propertySuccessfullyLoaded )
1711 {
1712 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1713 }
1714 }
1715 else
1716 {
1718 {
1719 myErrorMessage = tr( "Style not found in database" );
1720 }
1721 }
1722 break;
1723 }
1725 {
1726 if ( loadNamedMetadataFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1727 ( project.exists() && loadNamedMetadataFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1728 loadNamedMetadataFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1729 {
1730 namedPropertyExists = true;
1731 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1732 if ( !propertySuccessfullyLoaded )
1733 {
1734 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1735 }
1736 }
1737 else
1738 {
1739 myErrorMessage = tr( "Metadata not found in database" );
1740 }
1741 break;
1742 }
1743 }
1744 }
1745
1746 if ( !propertySuccessfullyLoaded )
1747 {
1748 return myErrorMessage;
1749 }
1750
1751 switch ( type )
1752 {
1753 case QgsMapLayer::Style:
1754 propertySuccessfullyLoaded = importNamedStyle( myDocument, myErrorMessage, categories );
1755 if ( !propertySuccessfullyLoaded )
1756 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1757 break;
1759 propertySuccessfullyLoaded = importNamedMetadata( myDocument, myErrorMessage );
1760 if ( !propertySuccessfullyLoaded )
1761 myErrorMessage = tr( "Loading metadata file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1762 break;
1763 }
1764 return myErrorMessage;
1765}
1766
1767bool QgsMapLayer::importNamedMetadata( QDomDocument &document, QString &errorMessage )
1768{
1770
1771 const QDomElement myRoot = document.firstChildElement( QStringLiteral( "qgis" ) );
1772 if ( myRoot.isNull() )
1773 {
1774 errorMessage = tr( "Root <qgis> element could not be found" );
1775 return false;
1776 }
1777
1778 return mMetadata.readMetadataXml( myRoot );
1779}
1780
1781bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMessage, QgsMapLayer::StyleCategories categories )
1782{
1784
1785 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "qgis" ) );
1786 if ( myRoot.isNull() )
1787 {
1788 myErrorMessage = tr( "Root <qgis> element could not be found" );
1789 return false;
1790 }
1791
1792 // get style file version string, if any
1793 const QgsProjectVersion fileVersion( myRoot.attribute( QStringLiteral( "version" ) ) );
1794 const QgsProjectVersion thisVersion( Qgis::version() );
1795
1796 if ( thisVersion > fileVersion )
1797 {
1798 QgsProjectFileTransform styleFile( myDocument, fileVersion );
1799 styleFile.updateRevision( thisVersion );
1800 }
1801
1802 // Get source categories
1803 const QgsMapLayer::StyleCategories sourceCategories = QgsXmlUtils::readFlagAttribute( myRoot, QStringLiteral( "styleCategories" ), QgsMapLayer::AllStyleCategories );
1804
1805 //Test for matching geometry type on vector layers when applying, if geometry type is given in the style
1806 if ( ( sourceCategories.testFlag( QgsMapLayer::Symbology ) || sourceCategories.testFlag( QgsMapLayer::Symbology3D ) ) &&
1807 ( categories.testFlag( QgsMapLayer::Symbology ) || categories.testFlag( QgsMapLayer::Symbology3D ) ) )
1808 {
1809 if ( type() == Qgis::LayerType::Vector && !myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).isNull() )
1810 {
1811 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( this );
1812 const Qgis::GeometryType importLayerGeometryType = static_cast<Qgis::GeometryType>( myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).text().toInt() );
1813 if ( importLayerGeometryType != Qgis::GeometryType::Unknown && vl->geometryType() != importLayerGeometryType )
1814 {
1815 myErrorMessage = tr( "Cannot apply style with symbology to layer with a different geometry type" );
1816 return false;
1817 }
1818 }
1819 }
1820
1822 return readSymbology( myRoot, myErrorMessage, context, categories ); // TODO: support relative paths in QML?
1823}
1824
1825void QgsMapLayer::exportNamedMetadata( QDomDocument &doc, QString &errorMsg ) const
1826{
1828
1829 QDomImplementation DomImplementation;
1830 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1831 QDomDocument myDocument( documentType );
1832
1833 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1834 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1835 myDocument.appendChild( myRootNode );
1836
1837 if ( !mMetadata.writeMetadataXml( myRootNode, myDocument ) )
1838 {
1839 errorMsg = QObject::tr( "Could not save metadata" );
1840 return;
1841 }
1842
1843 doc = myDocument;
1844}
1845
1846void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
1847{
1849
1850 QDomImplementation DomImplementation;
1851 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1852 QDomDocument myDocument( documentType );
1853
1854 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1855 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1856 myDocument.appendChild( myRootNode );
1857
1858 if ( !writeSymbology( myRootNode, myDocument, errorMsg, context, categories ) ) // TODO: support relative paths in QML?
1859 {
1860 errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1861 return;
1862 }
1863
1864 /*
1865 * Check to see if the layer is vector - in which case we should also export its geometryType
1866 * to avoid eventually pasting to a layer with a different geometry
1867 */
1868 if ( type() == Qgis::LayerType::Vector )
1869 {
1870 //Getting the selectionLayer geometry
1871 const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( this );
1872 const QString geoType = QString::number( static_cast<int>( vl->geometryType() ) );
1873
1874 //Adding geometryinformation
1875 QDomElement layerGeometryType = myDocument.createElement( QStringLiteral( "layerGeometryType" ) );
1876 const QDomText type = myDocument.createTextNode( geoType );
1877
1878 layerGeometryType.appendChild( type );
1879 myRootNode.appendChild( layerGeometryType );
1880 }
1881
1882 doc = myDocument;
1883}
1884
1885QString QgsMapLayer::saveDefaultStyle( bool &resultFlag )
1886{
1888
1889 return saveDefaultStyle( resultFlag, AllStyleCategories );
1890}
1891
1892QString QgsMapLayer::saveDefaultStyle( bool &resultFlag, StyleCategories categories )
1893{
1895
1896 return saveNamedStyle( styleURI(), resultFlag, categories );
1897}
1898
1899QString QgsMapLayer::saveNamedMetadata( const QString &uri, bool &resultFlag )
1900{
1902
1903 return saveNamedProperty( uri, QgsMapLayer::Metadata, resultFlag );
1904}
1905
1906QString QgsMapLayer::loadNamedMetadata( const QString &uri, bool &resultFlag )
1907{
1909
1910 bool metadataExists = false;
1911 bool metadataSuccessfullyLoaded = false;
1912 const QString message = loadNamedProperty( uri, QgsMapLayer::Metadata, metadataExists, metadataSuccessfullyLoaded );
1913
1914 // TODO QGIS 4.0 -- fix API for loadNamedMetadata so we can return metadataExists too
1915 ( void )metadataExists;
1916 resultFlag = metadataSuccessfullyLoaded;
1917 return message;
1918}
1919
1920QString QgsMapLayer::saveNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag, StyleCategories categories )
1921{
1923
1924 // check if the uri is a file or ends with .qml/.qmd,
1925 // which indicates that it should become one
1926 // everything else goes to the database
1927 QString filename;
1928
1929 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1930 if ( vlayer && vlayer->providerType() == QLatin1String( "ogr" ) )
1931 {
1932 QStringList theURIParts = uri.split( '|' );
1933 filename = theURIParts[0];
1934 }
1935 else if ( vlayer && vlayer->providerType() == QLatin1String( "gpx" ) )
1936 {
1937 QStringList theURIParts = uri.split( '?' );
1938 filename = theURIParts[0];
1939 }
1940 else if ( vlayer && vlayer->providerType() == QLatin1String( "delimitedtext" ) )
1941 {
1942 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
1943 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
1944 if ( filename.isEmpty() )
1945 filename = uri;
1946 }
1947 else
1948 {
1949 filename = uri;
1950 }
1951
1952 QString myErrorMessage;
1953 QDomDocument myDocument;
1954 switch ( type )
1955 {
1956 case Metadata:
1957 exportNamedMetadata( myDocument, myErrorMessage );
1958 break;
1959
1960 case Style:
1961 const QgsReadWriteContext context;
1962 exportNamedStyle( myDocument, myErrorMessage, context, categories );
1963 break;
1964 }
1965
1966 const QFileInfo myFileInfo( filename );
1967 if ( myFileInfo.exists() || filename.endsWith( QgsMapLayer::extensionPropertyType( type ), Qt::CaseInsensitive ) )
1968 {
1969 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
1970 if ( !myDirInfo.isWritable() )
1971 {
1972 resultFlag = false;
1973 return tr( "The directory containing your dataset needs to be writable!" );
1974 }
1975
1976 // now construct the file name for our .qml or .qmd file
1977 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1978
1979 QFile myFile( myFileName );
1980 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1981 {
1982 QTextStream myFileStream( &myFile );
1983 // save as utf-8 with 2 spaces for indents
1984 myDocument.save( myFileStream, 2 );
1985 myFile.close();
1986 resultFlag = true;
1987 switch ( type )
1988 {
1989 case Metadata:
1990 return tr( "Created default metadata file as %1" ).arg( myFileName );
1991
1992 case Style:
1993 return tr( "Created default style file as %1" ).arg( myFileName );
1994 }
1995
1996 }
1997 else
1998 {
1999 resultFlag = false;
2000 switch ( type )
2001 {
2002 case Metadata:
2003 return tr( "ERROR: Failed to created default metadata file as %1. Check file permissions and retry." ).arg( myFileName );
2004
2005 case Style:
2006 return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName );
2007 }
2008 }
2009 }
2010 else
2011 {
2012 const QString qml = myDocument.toString();
2013
2014 // read from database
2017
2018 int myResult = database.open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ) );
2019 if ( myResult != SQLITE_OK )
2020 {
2021 return tr( "User database could not be opened." );
2022 }
2023
2024 QByteArray param0 = uri.toUtf8();
2025 QByteArray param1 = qml.toUtf8();
2026
2027 QString mySql;
2028 switch ( type )
2029 {
2030 case Metadata:
2031 mySql = QStringLiteral( "create table if not exists tbl_metadata(metadata varchar primary key,qmd varchar)" );
2032 break;
2033
2034 case Style:
2035 mySql = QStringLiteral( "create table if not exists tbl_styles(style varchar primary key,qml varchar)" );
2036 break;
2037 }
2038
2039 statement = database.prepare( mySql, myResult );
2040 if ( myResult == SQLITE_OK )
2041 {
2042 if ( sqlite3_step( statement.get() ) != SQLITE_DONE )
2043 {
2044 resultFlag = false;
2045 switch ( type )
2046 {
2047 case Metadata:
2048 return tr( "The metadata table could not be created." );
2049
2050 case Style:
2051 return tr( "The style table could not be created." );
2052 }
2053 }
2054 }
2055
2056 switch ( type )
2057 {
2058 case Metadata:
2059 mySql = QStringLiteral( "insert into tbl_metadata(metadata,qmd) values (?,?)" );
2060 break;
2061
2062 case Style:
2063 mySql = QStringLiteral( "insert into tbl_styles(style,qml) values (?,?)" );
2064 break;
2065 }
2066 statement = database.prepare( mySql, myResult );
2067 if ( myResult == SQLITE_OK )
2068 {
2069 if ( sqlite3_bind_text( statement.get(), 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
2070 sqlite3_bind_text( statement.get(), 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
2071 sqlite3_step( statement.get() ) == SQLITE_DONE )
2072 {
2073 resultFlag = true;
2074 switch ( type )
2075 {
2076 case Metadata:
2077 myErrorMessage = tr( "The metadata %1 was saved to database" ).arg( uri );
2078 break;
2079
2080 case Style:
2081 myErrorMessage = tr( "The style %1 was saved to database" ).arg( uri );
2082 break;
2083 }
2084 }
2085 }
2086
2087 if ( !resultFlag )
2088 {
2089 QString mySql;
2090 switch ( type )
2091 {
2092 case Metadata:
2093 mySql = QStringLiteral( "update tbl_metadata set qmd=? where metadata=?" );
2094 break;
2095
2096 case Style:
2097 mySql = QStringLiteral( "update tbl_styles set qml=? where style=?" );
2098 break;
2099 }
2100 statement = database.prepare( mySql, myResult );
2101 if ( myResult == SQLITE_OK )
2102 {
2103 if ( sqlite3_bind_text( statement.get(), 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
2104 sqlite3_bind_text( statement.get(), 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
2105 sqlite3_step( statement.get() ) == SQLITE_DONE )
2106 {
2107 resultFlag = true;
2108 switch ( type )
2109 {
2110 case Metadata:
2111 myErrorMessage = tr( "The metadata %1 was updated in the database." ).arg( uri );
2112 break;
2113
2114 case Style:
2115 myErrorMessage = tr( "The style %1 was updated in the database." ).arg( uri );
2116 break;
2117 }
2118 }
2119 else
2120 {
2121 resultFlag = false;
2122 switch ( type )
2123 {
2124 case Metadata:
2125 myErrorMessage = tr( "The metadata %1 could not be updated in the database." ).arg( uri );
2126 break;
2127
2128 case Style:
2129 myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( uri );
2130 break;
2131 }
2132 }
2133 }
2134 else
2135 {
2136 resultFlag = false;
2137 switch ( type )
2138 {
2139 case Metadata:
2140 myErrorMessage = tr( "The metadata %1 could not be inserted into database." ).arg( uri );
2141 break;
2142
2143 case Style:
2144 myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( uri );
2145 break;
2146 }
2147 }
2148 }
2149 }
2150
2151 return myErrorMessage;
2152}
2153
2154QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag, StyleCategories categories )
2155{
2157
2158 return saveNamedProperty( uri, QgsMapLayer::Style, resultFlag, categories );
2159}
2160
2161void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
2162{
2163
2164 return exportSldStyleV2( doc, errorMsg, QgsSldExportContext() );
2165}
2166
2167void QgsMapLayer::exportSldStyleV2( QDomDocument &doc, QString &errorMsg, const QgsSldExportContext &exportContext ) const
2168{
2170
2171 QDomDocument myDocument = QDomDocument();
2172
2173 const QDomNode header = myDocument.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
2174 myDocument.appendChild( header );
2175
2176 const QgsVectorLayer *vlayer = qobject_cast<const QgsVectorLayer *>( this );
2177 const QgsRasterLayer *rlayer = qobject_cast<const QgsRasterLayer *>( this );
2178 if ( !vlayer && !rlayer )
2179 {
2180 errorMsg = tr( "Could not save symbology because:\n%1" )
2181 .arg( tr( "Only vector and raster layers are supported" ) );
2182 return;
2183 }
2184
2185 // Create the root element
2186 QDomElement root = myDocument.createElementNS( QStringLiteral( "http://www.opengis.net/sld" ), QStringLiteral( "StyledLayerDescriptor" ) );
2187 QDomElement layerNode;
2188 if ( vlayer )
2189 {
2190 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.1.0" ) );
2191 root.setAttribute( QStringLiteral( "xsi:schemaLocation" ), QStringLiteral( "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" ) );
2192 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
2193 root.setAttribute( QStringLiteral( "xmlns:se" ), QStringLiteral( "http://www.opengis.net/se" ) );
2194 root.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
2195 root.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
2196 myDocument.appendChild( root );
2197
2198 // Create the NamedLayer element
2199 layerNode = myDocument.createElement( QStringLiteral( "NamedLayer" ) );
2200 root.appendChild( layerNode );
2201 }
2202
2203 // note: Only SLD 1.0 version is generated because seems none is using SE1.1.0 at least for rasters
2204 if ( rlayer )
2205 {
2206 // Create the root element
2207 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0.0" ) );
2208 root.setAttribute( QStringLiteral( "xmlns:gml" ), QStringLiteral( "http://www.opengis.net/gml" ) );
2209 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
2210 root.setAttribute( QStringLiteral( "xmlns:sld" ), QStringLiteral( "http://www.opengis.net/sld" ) );
2211 myDocument.appendChild( root );
2212
2213 // Create the NamedLayer element
2214 layerNode = myDocument.createElement( QStringLiteral( "UserLayer" ) );
2215 root.appendChild( layerNode );
2216 }
2217
2218 QVariantMap props;
2219
2220 QVariant context;
2221 context.setValue( exportContext );
2222
2223 props[ QStringLiteral( "SldExportContext" ) ] = context;
2224
2226 {
2227 props[ QStringLiteral( "scaleMinDenom" ) ] = QString::number( mMinScale );
2228 props[ QStringLiteral( "scaleMaxDenom" ) ] = QString::number( mMaxScale );
2229 }
2230
2231 if ( vlayer )
2232 {
2233 if ( !vlayer->writeSld( layerNode, myDocument, errorMsg, props ) )
2234 {
2235 errorMsg = tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
2236 return;
2237 }
2238 }
2239
2240 if ( rlayer )
2241 {
2242 if ( !rlayer->writeSld( layerNode, myDocument, errorMsg, props ) )
2243 {
2244 errorMsg = tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
2245 return;
2246 }
2247 }
2248
2249 doc = myDocument;
2250}
2251
2252QString QgsMapLayer::saveSldStyle( const QString &uri, bool &resultFlag ) const
2253{
2254 QgsSldExportContext context;
2255 context.setExportFilePath( uri );
2256 return saveSldStyleV2( resultFlag, context );
2257}
2258
2259QString QgsMapLayer::saveSldStyleV2( bool &resultFlag, const QgsSldExportContext &exportContext ) const
2260{
2262
2263 const QgsMapLayer *mlayer = qobject_cast<const QgsMapLayer *>( this );
2264
2265 const QString uri { exportContext.exportFilePath() };
2266
2267 // check if the uri is a file or ends with .sld,
2268 // which indicates that it should become one
2269 QString filename;
2270 if ( mlayer->providerType() == QLatin1String( "ogr" ) )
2271 {
2272 QStringList theURIParts = uri.split( '|' );
2273 filename = theURIParts[0];
2274 }
2275 else if ( mlayer->providerType() == QLatin1String( "gpx" ) )
2276 {
2277 QStringList theURIParts = uri.split( '?' );
2278 filename = theURIParts[0];
2279 }
2280 else if ( mlayer->providerType() == QLatin1String( "delimitedtext" ) )
2281 {
2282 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
2283 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
2284 if ( filename.isEmpty() )
2285 filename = uri;
2286 }
2287 else
2288 {
2289 filename = uri;
2290 }
2291
2292 const QFileInfo myFileInfo( filename );
2293 if ( myFileInfo.exists() || filename.endsWith( QLatin1String( ".sld" ), Qt::CaseInsensitive ) )
2294 {
2295 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
2296 if ( !myDirInfo.isWritable() )
2297 {
2298 resultFlag = false;
2299 return tr( "The directory containing your dataset needs to be writable!" );
2300 }
2301
2302 // now construct the file name for our .sld style file
2303 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld";
2304
2305 QString errorMsg;
2306 QDomDocument myDocument;
2307
2308 QgsSldExportContext context { exportContext };
2309 context.setExportFilePath( myFileName );
2310
2311 mlayer->exportSldStyleV2( myDocument, errorMsg, context );
2312
2313 if ( !errorMsg.isNull() )
2314 {
2315 resultFlag = false;
2316 return errorMsg;
2317 }
2318
2319 QFile myFile( myFileName );
2320 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
2321 {
2322 QTextStream myFileStream( &myFile );
2323 // save as utf-8 with 2 spaces for indents
2324 myDocument.save( myFileStream, 2 );
2325 myFile.close();
2326 resultFlag = true;
2327 return tr( "Created default style file as %1" ).arg( myFileName );
2328 }
2329 }
2330
2331 resultFlag = false;
2332 return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename );
2333
2334}
2335
2336QString QgsMapLayer::loadSldStyle( const QString &uri, bool &resultFlag )
2337{
2339
2340 resultFlag = false;
2341
2342 QDomDocument myDocument;
2343
2344 // location of problem associated with errorMsg
2345 int line, column;
2346 QString myErrorMessage;
2347
2348 QFile myFile( uri );
2349 if ( myFile.open( QFile::ReadOnly ) )
2350 {
2351 // read file
2352 resultFlag = myDocument.setContent( &myFile, true, &myErrorMessage, &line, &column );
2353 if ( !resultFlag )
2354 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
2355 myFile.close();
2356 }
2357 else
2358 {
2359 myErrorMessage = tr( "Unable to open file %1" ).arg( uri );
2360 }
2361
2362 if ( !resultFlag )
2363 {
2364 return myErrorMessage;
2365 }
2366
2367 // check for root SLD element
2368 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "StyledLayerDescriptor" ) );
2369 if ( myRoot.isNull() )
2370 {
2371 myErrorMessage = QStringLiteral( "Error: StyledLayerDescriptor element not found in %1" ).arg( uri );
2372 resultFlag = false;
2373 return myErrorMessage;
2374 }
2375
2376 // now get the style node out and pass it over to the layer
2377 // to deserialise...
2378 const QDomElement namedLayerElem = myRoot.firstChildElement( QStringLiteral( "NamedLayer" ) );
2379 if ( namedLayerElem.isNull() )
2380 {
2381 myErrorMessage = QStringLiteral( "Info: NamedLayer element not found." );
2382 resultFlag = false;
2383 return myErrorMessage;
2384 }
2385
2386 QString errorMsg;
2387 resultFlag = readSld( namedLayerElem, errorMsg );
2388 if ( !resultFlag )
2389 {
2390 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, errorMsg );
2391 return myErrorMessage;
2392 }
2393
2394 return QString();
2395}
2396
2397bool QgsMapLayer::readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
2398{
2400
2401 Q_UNUSED( node )
2402 Q_UNUSED( errorMessage )
2403 Q_UNUSED( context )
2404 Q_UNUSED( categories )
2405 return false;
2406}
2407
2408bool QgsMapLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
2409 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
2410{
2412
2413 Q_UNUSED( node )
2414 Q_UNUSED( doc )
2415 Q_UNUSED( errorMessage )
2416 Q_UNUSED( context )
2417 Q_UNUSED( categories )
2418 return false;
2419}
2420
2421
2422void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2423 bool loadDefaultStyleFlag )
2424{
2426
2428
2430 if ( loadDefaultStyleFlag )
2431 {
2433 }
2434
2436 {
2438 }
2439 setDataSource( dataSource, baseName, provider, options, flags );
2440}
2441
2442void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2443 const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag )
2444{
2446
2448 if ( loadDefaultStyleFlag )
2449 {
2451 }
2452
2454 {
2456 }
2457 setDataSource( dataSource, baseName, provider, options, flags );
2458}
2459
2460void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2462{
2464
2467 {
2469 }
2470 setDataSourcePrivate( dataSource, baseName, provider, options, flags );
2471 emit dataSourceChanged();
2472 emit dataChanged();
2474}
2475
2476
2477void QgsMapLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider,
2479{
2481
2482 Q_UNUSED( dataSource )
2483 Q_UNUSED( baseName )
2484 Q_UNUSED( provider )
2485 Q_UNUSED( options )
2486 Q_UNUSED( flags )
2487}
2488
2489
2491{
2493
2494 return mProviderKey;
2495}
2496
2497void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
2498 QgsMapLayer::StyleCategories categories )
2499{
2501
2502 if ( categories.testFlag( Symbology3D ) )
2503 {
2504 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "3D Symbology" ) );
2505
2506 QgsAbstract3DRenderer *r3D = nullptr;
2507 QDomElement renderer3DElem = layerElement.firstChildElement( QStringLiteral( "renderer-3d" ) );
2508 if ( !renderer3DElem.isNull() )
2509 {
2510 const QString type3D = renderer3DElem.attribute( QStringLiteral( "type" ) );
2512 if ( meta3D )
2513 {
2514 r3D = meta3D->createRenderer( renderer3DElem, context );
2515 }
2516 }
2517 setRenderer3D( r3D );
2518 }
2519
2520 if ( categories.testFlag( CustomProperties ) )
2521 {
2522 // read custom properties before passing reading further to a subclass, so that
2523 // the subclass can also read custom properties
2524 readCustomProperties( layerElement );
2525 }
2526
2527 // use scale dependent visibility flag
2528 if ( categories.testFlag( Rendering ) )
2529 {
2530 setScaleBasedVisibility( layerElement.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
2531 if ( layerElement.hasAttribute( QStringLiteral( "minimumScale" ) ) )
2532 {
2533 // older element, when scales were reversed
2534 setMaximumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
2535 setMinimumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
2536 }
2537 else
2538 {
2539 setMaximumScale( layerElement.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
2540 setMinimumScale( layerElement.attribute( QStringLiteral( "minScale" ) ).toDouble() );
2541 }
2542 }
2543
2544 if ( categories.testFlag( LayerConfiguration ) )
2545 {
2546 // flags
2547 const QDomElement flagsElem = layerElement.firstChildElement( QStringLiteral( "flags" ) );
2548 LayerFlags flags = mFlags;
2549 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
2550 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
2551 {
2552 const QDomNode flagNode = flagsElem.namedItem( it.value() );
2553 if ( flagNode.isNull() )
2554 continue;
2555 const bool flagValue = flagNode.toElement().text() == "1" ? true : false;
2556 if ( flags.testFlag( it.key() ) && !flagValue )
2557 flags &= ~it.key();
2558 else if ( !flags.testFlag( it.key() ) && flagValue )
2559 flags |= it.key();
2560 }
2561 setFlags( flags );
2562 }
2563
2564 if ( categories.testFlag( Temporal ) )
2565 {
2566 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Temporal" ) );
2567
2569 properties->readXml( layerElement.toElement(), context );
2570 }
2571
2572 if ( categories.testFlag( Elevation ) )
2573 {
2574 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Elevation" ) );
2575
2577 properties->readXml( layerElement.toElement(), context );
2578 }
2579
2580 if ( categories.testFlag( Notes ) )
2581 {
2582 const QDomElement notesElem = layerElement.firstChildElement( QStringLiteral( "userNotes" ) );
2583 if ( !notesElem.isNull() )
2584 {
2585 const QString notes = notesElem.attribute( QStringLiteral( "value" ) );
2586 QgsLayerNotesUtils::setLayerNotes( this, notes );
2587 }
2588 }
2589}
2590
2592{
2594
2595 return mUndoStack;
2596}
2597
2599{
2601
2602 return mUndoStackStyles;
2603}
2604
2606{
2608
2609 return mCustomProperties.keys();
2610}
2611
2612void QgsMapLayer::setCustomProperty( const QString &key, const QVariant &value )
2613{
2615
2616 if ( !mCustomProperties.contains( key ) || mCustomProperties.value( key ) != value )
2617 {
2618 mCustomProperties.setValue( key, value );
2619 emit customPropertyChanged( key );
2620 }
2621}
2622
2624{
2626
2627 mCustomProperties = properties;
2628 for ( const QString &key : mCustomProperties.keys() )
2629 {
2630 emit customPropertyChanged( key );
2631 }
2632}
2633
2635{
2637
2638 return mCustomProperties;
2639}
2640
2641QVariant QgsMapLayer::customProperty( const QString &value, const QVariant &defaultValue ) const
2642{
2643 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
2645
2646 return mCustomProperties.value( value, defaultValue );
2647}
2648
2649void QgsMapLayer::removeCustomProperty( const QString &key )
2650{
2652
2653 if ( mCustomProperties.contains( key ) )
2654 {
2655 mCustomProperties.remove( key );
2656 emit customPropertyChanged( key );
2657 }
2658}
2659
2660int QgsMapLayer::listStylesInDatabase( QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError )
2661{
2663
2664 return QgsProviderRegistry::instance()->listStyles( mProviderKey, mDataSource, ids, names, descriptions, msgError );
2665}
2666
2667QString QgsMapLayer::getStyleFromDatabase( const QString &styleId, QString &msgError )
2668{
2670
2671 return QgsProviderRegistry::instance()->getStyleById( mProviderKey, mDataSource, styleId, msgError );
2672}
2673
2674bool QgsMapLayer::deleteStyleFromDatabase( const QString &styleId, QString &msgError )
2675{
2677
2679}
2680
2681void QgsMapLayer::saveStyleToDatabase( const QString &name, const QString &description,
2682 bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories )
2683{
2685
2686 QString sldStyle, qmlStyle;
2687 QDomDocument qmlDocument, sldDocument;
2688 QgsReadWriteContext context;
2689 exportNamedStyle( qmlDocument, msgError, context, categories );
2690 if ( !msgError.isNull() )
2691 {
2692 return;
2693 }
2694 qmlStyle = qmlDocument.toString();
2695
2696 this->exportSldStyle( sldDocument, msgError );
2697 if ( !msgError.isNull() )
2698 {
2699 return;
2700 }
2701 sldStyle = sldDocument.toString();
2702
2704 mDataSource, qmlStyle, sldStyle, name,
2705 description, uiFileContent, useAsDefault, msgError );
2706}
2707
2708QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &resultFlag, bool loadFromLocalDB, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
2709{
2711
2712 QString returnMessage;
2713 QString qml, errorMsg;
2714 QString styleName;
2715 if ( !loadFromLocalDB && dataProvider() && dataProvider()->styleStorageCapabilities().testFlag( Qgis::ProviderStyleStorageCapability::LoadFromDatabase ) )
2716 {
2718 }
2719
2720 // Style was successfully loaded from provider storage
2721 if ( !qml.isEmpty() )
2722 {
2723 QDomDocument myDocument( QStringLiteral( "qgis" ) );
2724 myDocument.setContent( qml );
2725 resultFlag = importNamedStyle( myDocument, errorMsg );
2726 returnMessage = QObject::tr( "Loaded from Provider" );
2727 }
2728 else
2729 {
2731
2732 bool styleExists = false;
2733 bool styleSuccessfullyLoaded = false;
2734
2735 returnMessage = loadNamedProperty( theURI, PropertyType::Style, styleExists, styleSuccessfullyLoaded, categories, flags );
2736
2737 // TODO QGIS 4.0 -- fix API for loadNamedStyle so we can return styleExists too
2738 ( void )styleExists;
2739 resultFlag = styleSuccessfullyLoaded;
2740 }
2741
2742 if ( ! styleName.isEmpty() )
2743 {
2744 styleManager()->renameStyle( styleManager()->currentStyle(), styleName );
2745 }
2746
2747 if ( resultFlag )
2748 emit styleLoaded( categories );
2749
2750 return returnMessage;
2751}
2752
2759
2761{
2763
2764 return false;
2765}
2766
2768{
2770
2771 return false;
2772}
2773
2775{
2777
2778 return true;
2779}
2780
2782{
2784
2785 // invalid layers are temporary? -- who knows?!
2786 if ( !isValid() )
2787 return false;
2788
2789 if ( mProviderKey == QLatin1String( "memory" ) )
2790 return true;
2791
2792 const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( mProviderKey, mDataSource );
2793 const QString path = sourceParts.value( QStringLiteral( "path" ) ).toString();
2794 if ( path.isEmpty() )
2795 return false;
2796
2797 // check if layer path is inside one of the standard temporary file locations for this platform
2798 const QStringList tempPaths = QStandardPaths::standardLocations( QStandardPaths::TempLocation );
2799 for ( const QString &tempPath : tempPaths )
2800 {
2801 if ( path.startsWith( tempPath ) )
2802 return true;
2803 }
2804
2805 return false;
2806}
2807
2808void QgsMapLayer::setValid( bool valid )
2809{
2811
2812 if ( mValid == valid )
2813 return;
2814
2815 mValid = valid;
2816 emit isValidChanged();
2817}
2818
2820{
2822
2823 if ( legend == mLegend )
2824 return;
2825
2826 delete mLegend;
2827 mLegend = legend;
2828
2829 if ( mLegend )
2830 {
2831 mLegend->setParent( this );
2832 connect( mLegend, &QgsMapLayerLegend::itemsChanged, this, &QgsMapLayer::legendChanged, Qt::UniqueConnection );
2833 }
2834
2835 emit legendChanged();
2836}
2837
2839{
2841
2842 return mLegend;
2843}
2844
2846{
2848
2849 return mStyleManager;
2850}
2851
2853{
2855
2856 if ( renderer == m3DRenderer )
2857 return;
2858
2859 delete m3DRenderer;
2860 m3DRenderer = renderer;
2861 emit renderer3DChanged();
2862 emit repaintRequested();
2864}
2865
2867{
2869
2870 return m3DRenderer;
2871}
2872
2873void QgsMapLayer::triggerRepaint( bool deferredUpdate )
2874{
2876
2877 if ( mRepaintRequestedFired )
2878 return;
2879 mRepaintRequestedFired = true;
2880 emit repaintRequested( deferredUpdate );
2881 mRepaintRequestedFired = false;
2882}
2883
2890
2892{
2894
2895 mMetadata = metadata;
2896// mMetadata.saveToLayer( this );
2897 emit metadataChanged();
2898}
2899
2901{
2903
2904 return QString();
2905}
2906
2907QDateTime QgsMapLayer::timestamp() const
2908{
2910
2911 return QDateTime();
2912}
2913
2921
2923{
2924 updateExtent( extent );
2925}
2926
2928{
2930
2931 updateExtent( extent );
2932}
2933
2934bool QgsMapLayer::isReadOnly() const
2935{
2937
2938 return true;
2939}
2940
2942{
2944
2945 return mOriginalXmlProperties;
2946}
2947
2948void QgsMapLayer::setOriginalXmlProperties( const QString &originalXmlProperties )
2949{
2951
2952 mOriginalXmlProperties = originalXmlProperties;
2953}
2954
2955QString QgsMapLayer::generateId( const QString &layerName )
2956{
2957 // Generate the unique ID of this layer
2958 const QString uuid = QUuid::createUuid().toString();
2959 // trim { } from uuid
2960 QString id = layerName + '_' + uuid.mid( 1, uuid.length() - 2 );
2961 // Tidy the ID up to avoid characters that may cause problems
2962 // elsewhere (e.g in some parts of XML). Replaces every non-word
2963 // character (word characters are the alphabet, numbers and
2964 // underscore) with an underscore.
2965 // Note that the first backslash in the regular expression is
2966 // there for the compiler, so the pattern is actually \W
2967 const thread_local QRegularExpression idRx( QStringLiteral( "[\\W]" ) );
2968 id.replace( idRx, QStringLiteral( "_" ) );
2969 return id;
2970}
2971
2973{
2975
2976 return true;
2977}
2978
2980{
2982
2983 return mapTipsEnabled() && !mMapTipTemplate.isEmpty();
2984}
2985
2986void QgsMapLayer::setProviderType( const QString &providerType )
2987{
2989
2991}
2992
2993QSet<QgsMapLayerDependency> QgsMapLayer::dependencies() const
2994{
2996
2997 return mDependencies;
2998}
2999
3000bool QgsMapLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
3001{
3003
3004 QSet<QgsMapLayerDependency> deps;
3005 const auto constODeps = oDeps;
3006 for ( const QgsMapLayerDependency &dep : constODeps )
3007 {
3008 if ( dep.origin() == QgsMapLayerDependency::FromUser )
3009 deps << dep;
3010 }
3011
3012 mDependencies = deps;
3013 emit dependenciesChanged();
3014 return true;
3015}
3016
3018{
3020
3021 QgsDataProvider *lDataProvider = dataProvider();
3022
3023 if ( !lDataProvider )
3024 return;
3025
3026 if ( enabled && !isRefreshOnNotifyEnabled() )
3027 {
3028 lDataProvider->setListening( enabled );
3029 connect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
3030 }
3031 else if ( !enabled && isRefreshOnNotifyEnabled() )
3032 {
3033 // we don't want to disable provider listening because someone else could need it (e.g. actions)
3034 disconnect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
3035 }
3036 mIsRefreshOnNofifyEnabled = enabled;
3037}
3038
3040{
3042
3043 if ( QgsMapLayerStore *store = qobject_cast<QgsMapLayerStore *>( parent() ) )
3044 {
3045 return qobject_cast<QgsProject *>( store->parent() );
3046 }
3047 return nullptr;
3048}
3049
3050void QgsMapLayer::onNotified( const QString &message )
3051{
3053
3054 if ( refreshOnNotifyMessage().isEmpty() || refreshOnNotifyMessage() == message )
3055 {
3057 emit dataChanged();
3058 }
3059}
3060
3061QgsRectangle QgsMapLayer::wgs84Extent( bool forceRecalculate ) const
3062{
3064
3066
3067 if ( ! forceRecalculate && ! mWgs84Extent.isNull() )
3068 {
3069 wgs84Extent = mWgs84Extent;
3070 }
3071 else if ( ! mExtent2D.isNull() || ! mExtent3D.isNull() )
3072 {
3074 transformer.setBallparkTransformsAreAppropriate( true );
3075 try
3076 {
3077 if ( mExtent2D.isNull() )
3078 wgs84Extent = transformer.transformBoundingBox( mExtent3D.toRectangle() );
3079 else
3080 wgs84Extent = transformer.transformBoundingBox( mExtent2D );
3081 }
3082 catch ( const QgsCsException &cse )
3083 {
3084 QgsMessageLog::logMessage( tr( "Error transforming extent: %1" ).arg( cse.what() ) );
3086 }
3087 }
3088 return wgs84Extent;
3089}
3090
3091void QgsMapLayer::updateExtent( const QgsRectangle &extent ) const
3092{
3094
3095 if ( extent == mExtent2D )
3096 return;
3097
3098 mExtent2D = extent;
3099
3100 // do not update the wgs84 extent if we trust layer metadata
3102 return;
3103
3104 mWgs84Extent = wgs84Extent( true );
3105}
3106
3107void QgsMapLayer::updateExtent( const QgsBox3D &extent ) const
3108{
3110
3111 if ( extent == mExtent3D )
3112 return;
3113
3114 if ( extent.isNull() )
3115 {
3116 if ( !extent.toRectangle().isNull() )
3117 {
3118 // bad 3D extent param but valid in 2d --> update 2D extent
3119 updateExtent( extent.toRectangle() );
3120 }
3121 else
3122 {
3123 QgsDebugMsgLevel( QStringLiteral( "Unable to update extent with empty parameter" ), 1 );
3124 }
3125 }
3126 else
3127 {
3128 mExtent3D = extent;
3129
3130 // do not update the wgs84 extent if we trust layer metadata
3132 return;
3133
3134 mWgs84Extent = wgs84Extent( true );
3135 }
3136}
3137
3138bool QgsMapLayer::rebuildCrs3D( QString *error )
3139{
3140 bool res = true;
3141 if ( !mCRS.isValid() )
3142 {
3144 }
3145 else if ( !mVerticalCrs.isValid() )
3146 {
3147 mCrs3D = mCRS;
3148 }
3149 else
3150 {
3151 switch ( mCRS.type() )
3152 {
3156 mCrs3D = mCRS;
3157 break;
3158
3160 {
3161 QString tempError;
3162 mCrs3D = mCRS.hasVerticalAxis() ? mCRS : QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3163 res = mCrs3D.isValid();
3164 break;
3165 }
3166
3168 // nonsense situation
3170 res = false;
3171 break;
3172
3181 {
3182 QString tempError;
3183 mCrs3D = QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3184 res = mCrs3D.isValid();
3185 break;
3186 }
3187 }
3188 }
3189 return res;
3190}
3191
3193{
3195
3196 // do not update the wgs84 extent if we trust layer metadata
3198 return;
3199
3200 mWgs84Extent = QgsRectangle();
3201}
3202
3204{
3206
3207 QString metadata = QStringLiteral( "<h1>" ) + tr( "General" ) + QStringLiteral( "</h1>\n<hr>\n" ) + QStringLiteral( "<table class=\"list-view\">\n" );
3208
3209 // name
3210 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + name() + QStringLiteral( "</td></tr>\n" );
3211
3212 QString path;
3213 bool isLocalPath = false;
3214 if ( dataProvider() )
3215 {
3216 // local path
3217 QVariantMap uriComponents = QgsProviderRegistry::instance()->decodeUri( dataProvider()->name(), publicSource() );
3218 if ( uriComponents.contains( QStringLiteral( "path" ) ) )
3219 {
3220 path = uriComponents[QStringLiteral( "path" )].toString();
3221 QFileInfo fi( path );
3222 if ( fi.exists() )
3223 {
3224 isLocalPath = true;
3225 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" );
3226
3227 QDateTime lastModified = fi.lastModified();
3228 QString lastModifiedFileName;
3229 QSet<QString> sidecarFiles = QgsFileUtils::sidecarFilesForPath( path );
3230 if ( fi.isFile() )
3231 {
3232 qint64 fileSize = fi.size();
3233 if ( !sidecarFiles.isEmpty() )
3234 {
3235 lastModifiedFileName = fi.fileName();
3236 QStringList sidecarFileNames;
3237 for ( const QString &sidecarFile : sidecarFiles )
3238 {
3239 QFileInfo sidecarFi( sidecarFile );
3240 fileSize += sidecarFi.size();
3241 if ( sidecarFi.lastModified() > lastModified )
3242 {
3243 lastModified = sidecarFi.lastModified();
3244 lastModifiedFileName = sidecarFi.fileName();
3245 }
3246 sidecarFileNames << sidecarFi.fileName();
3247 }
3248 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" );
3249 }
3250 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" );
3251 }
3252 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" );
3253 }
3254 }
3255 if ( uriComponents.contains( QStringLiteral( "url" ) ) )
3256 {
3257 const QString url = uriComponents[QStringLiteral( "url" )].toString();
3258 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" );
3259 }
3260 }
3261
3262 // data source
3263 if ( publicSource() != path || !isLocalPath )
3264 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Source" ) + QStringLiteral( "</td><td>%1" ).arg( publicSource() != path ? publicSource() : path ) + QStringLiteral( "</td></tr>\n" );
3265
3266 // provider
3267 if ( dataProvider() )
3268 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Provider" ) + QStringLiteral( "</td><td>%1" ).arg( dataProvider()->name() ) + QStringLiteral( "</td></tr>\n" );
3269
3270 metadata += QLatin1String( "</table>\n<br><br>" );
3271
3272 // custom properties
3273 if ( const auto keys = customPropertyKeys(); !keys.isEmpty() )
3274 {
3275 metadata += QStringLiteral( "<h1>" ) + tr( "Custom Properties" ) + QStringLiteral( "</h1>\n<hr>\n" );
3276 metadata += QLatin1String( "<table class=\"list-view\">\n<tbody>" );
3277 for ( const QString &key : keys )
3278 {
3279 // keys prefaced with _ are considered private/internal details
3280 if ( key.startsWith( '_' ) )
3281 continue;
3282
3283 const QVariant propValue = customProperty( key );
3284 QString stringValue;
3285 if ( propValue.type() == QVariant::List || propValue.type() == QVariant::StringList )
3286 {
3287 for ( const QString &s : propValue.toStringList() )
3288 {
3289 stringValue += "<p style=\"margin: 0;\">" + s.toHtmlEscaped() + "</p>";
3290 }
3291 }
3292 else
3293 {
3294 stringValue = propValue.toString().toHtmlEscaped();
3295
3296 //if the result string is empty but propValue is not, the conversion has failed
3297 if ( stringValue.isEmpty() && !QgsVariantUtils::isNull( propValue ) )
3298 stringValue = tr( "<i>value cannot be displayed</i>" );
3299 }
3300
3301 metadata += QStringLiteral( "<tr><td class=\"highlight\">%1</td><td>%2</td></tr>" ).arg( key.toHtmlEscaped(), stringValue );
3302 }
3303 metadata += QLatin1String( "</tbody></table>\n" );
3304 metadata += QLatin1String( "<br><br>\n" );
3305 }
3306
3307 return metadata;
3308}
3309
3311{
3313
3314 QString metadata = QStringLiteral( "<h1>" ) + tr( "Coordinate Reference System (CRS)" ) + QStringLiteral( "</h1>\n<hr>\n" );
3315 metadata += QLatin1String( "<table class=\"list-view\">\n" );
3316
3317 // Identifier
3319 if ( !c.isValid() )
3320 metadata += QStringLiteral( "<tr><td colspan=\"2\" class=\"highlight\">" ) + tr( "Unknown" ) + QStringLiteral( "</td></tr>\n" );
3321 else
3322 {
3323 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + c.userFriendlyIdentifier( Qgis::CrsIdentifierType::FullString ) + QStringLiteral( "</td></tr>\n" );
3324
3325 // map units
3326 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Units" ) + QStringLiteral( "</td><td>" )
3327 + ( c.isGeographic() ? tr( "Geographic (uses latitude and longitude for coordinates)" ) : QgsUnitTypes::toString( c.mapUnits() ) )
3328 + QStringLiteral( "</td></tr>\n" );
3329
3330 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Type" ) + QStringLiteral( "</td><td>" ) + QgsCoordinateReferenceSystemUtils::crsTypeToString( c.type() ) + QStringLiteral( "</td></tr>\n" );
3331
3332 // operation
3333 const QgsProjOperation operation = c.operation();
3334 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Method" ) + QStringLiteral( "</td><td>" ) + operation.description() + QStringLiteral( "</td></tr>\n" );
3335
3336 // celestial body
3337 try
3338 {
3339 const QString celestialBody = c.celestialBodyName();
3340 if ( !celestialBody.isEmpty() )
3341 {
3342 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Celestial Body" ) + QStringLiteral( "</td><td>" ) + celestialBody + QStringLiteral( "</td></tr>\n" );
3343 }
3344 }
3345 catch ( QgsNotSupportedException & )
3346 {
3347
3348 }
3349
3350 QString accuracyString;
3351 // dynamic crs with no epoch?
3352 if ( c.isDynamic() && std::isnan( c.coordinateEpoch() ) )
3353 {
3354 accuracyString = tr( "Based on a dynamic CRS, but no coordinate epoch is set. Coordinates are ambiguous and of limited accuracy." );
3355 }
3356
3357 // based on datum ensemble?
3358 try
3359 {
3360 const QgsDatumEnsemble ensemble = c.datumEnsemble();
3361 if ( ensemble.isValid() )
3362 {
3363 QString id;
3364 if ( !ensemble.code().isEmpty() )
3365 id = QStringLiteral( "<i>%1</i> (%2:%3)" ).arg( ensemble.name(), ensemble.authority(), ensemble.code() );
3366 else
3367 id = QStringLiteral( "<i>%</i>”" ).arg( ensemble.name() );
3368
3369 if ( ensemble.accuracy() > 0 )
3370 {
3371 accuracyString = tr( "Based on %1, which has a limited accuracy of <b>at best %2 meters</b>." ).arg( id ).arg( ensemble.accuracy() );
3372 }
3373 else
3374 {
3375 accuracyString = tr( "Based on %1, which has a limited accuracy." ).arg( id );
3376 }
3377 }
3378 }
3379 catch ( QgsNotSupportedException & )
3380 {
3381
3382 }
3383
3384 if ( !accuracyString.isEmpty() )
3385 {
3386 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Accuracy" ) + QStringLiteral( "</td><td>" ) + accuracyString + QStringLiteral( "</td></tr>\n" );
3387 }
3388
3389 // static/dynamic
3390 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)" ) );
3391
3392 // coordinate epoch
3393 if ( !std::isnan( c.coordinateEpoch() ) )
3394 {
3395 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Coordinate Epoch" ) + QStringLiteral( "</td><td>%1</td></tr>\n" ).arg( qgsDoubleToString( c.coordinateEpoch(), 3 ) );
3396 }
3397 }
3398
3399 metadata += QLatin1String( "</table>\n<br><br>\n" );
3400 return metadata;
3401}
static QString version()
Version string.
Definition qgis.cpp:258
@ FullString
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions.
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition qgis.h:5163
@ 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.
@ 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:274
@ Unknown
Unknown types.
LayerType
Types of layers that can be added to a map.
Definition qgis.h:114
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
QFlags< MapLayerProperty > MapLayerProperties
Map layer properties.
Definition qgis.h:1921
QFlags< LoadStyleFlag > LoadStyleFlags
Flags for loading layer styles.
Definition qgis.h:170
@ 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:1931
@ 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 may to participate in 3D view.
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:338
bool isNull() const
Test if the box is null (holding no spatial information).
Definition qgsbox3d.cpp:289
static QString crsTypeToString(Qgis::CrsType type)
Returns a translated string representing a CRS type.
This class represents a coordinate reference system (CRS).
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
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.
Class for doing transforms between two map 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.
@ FlagLoadDefaultStyle
Reset the layer's style to the default for the datasource.
@ FlagTrustDataSource
Trust datasource config (primary key unicity, geometry type and srid, etc). Improves provider load ti...
@ ForceReadOnly
Open layer in a read-only mode (since QGIS 3.28)
@ SkipGetExtent
Skip the extent from provider.
virtual QString name() const =0
Returns a provider name.
QFlags< ReadFlag > ReadFlags
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
QgsError is container for error messages (report).
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.
This class models dependencies with or between map layers.
Base class for storage of map layer elevation properties.
The QgsMapLayerLegend class is 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.
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.
void writeXml(QDomElement &mgrElement) const
Write configuration (for project saving)
void reset()
Reset the style manager to a basic state - with one default style which is set as current.
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.
void readXml(const QDomElement &mgrElement)
Read configuration (for project loading)
Base class for storage of map layer temporal properties.
Base class for all map layer types.
Definition qgsmaplayer.h:75
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:79
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.
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 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.
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.
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:84
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:82
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
QString mLegendUrl
WMS legend.
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...
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:78
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:81
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:85
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.
QString mLegendUrlFormat
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.
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()
virtual QString saveSldStyleV2(bool &resultFlag, const QgsSldExportContext &exportContext) const
Saves the properties of this layer to an SLD format file.
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...
@ 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.
QgsMapLayer::ReadFlags mReadFlags
Read flags. It's up to the subclass to respect these when restoring state from XML.
void setDataSource(const QString &dataSource, const QString &baseName, const QString &provider, bool loadDefaultStyleFlag=false)
Updates the data source of the layer.
double minimumScale() const
Returns the minimum map scale (i.e.
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
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...
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 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:89
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:87
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:80
QgsCoordinateReferenceSystem verticalCrs
Definition qgsmaplayer.h:83
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 (since QGIS 3.20)
@ Temporal
Temporal properties (since QGIS 3.14)
@ Rendering
Rendering: scale visibility, simplify method, opacity.
@ Elevation
Elevation settings (since QGIS 3.18)
@ 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
const QgsObjectCustomProperties & customProperties() const
Read all custom properties from layer.
virtual void exportSldStyleV2(QDomDocument &doc, QString &errorMsg, const QgsSldExportContext &exportContext) const
Export the properties of this layer as SLD style in a QDomDocument.
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:88
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 QgsDataProvider::ReadFlags providerReadFlags(const QDomNode &layerNode, QgsMapLayer::ReadFlags layerReadFlags)
Returns provider read flag deduced from layer read flags layerReadFlags and a dom node layerNode that...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
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.
Class to 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
The derived translate() translates with QTranslator and qm file the sourceText.
A class to describe 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 (since QGIS 3.20)
QString absoluteToRelativeUri(const QString &providerKey, const QString &uri, const QgsReadWriteContext &context) const
Converts absolute path(s) to relative path(s) in the given provider-specific URI.
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.
QString relativeToAbsoluteUri(const QString &providerKey, const QString &uri, const QgsReadWriteContext &context) const
Converts relative path(s) to absolute path(s) in the given provider-specific URI.
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.
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.
The class is used as a container of context for various read/write operations on other 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.
bool isNull() const
Test if the rectangle is null (holding no spatial information).
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.
The QgsSldExportContext class holds SLD export options and other information related to SLD export of...
QString exportFilePath() const
Returns the export file path for the SLD.
void setExportFilePath(const QString &exportFilePath)
Sets the export file path for the SLD to exportFilePath.
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 data sets.
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:5675
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:5382
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:5656
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5465
CONSTLATIN1STRING geoEpsgCrsAuthId()
Geographic coord sys from EPSG authority.
Definition qgis.h:5954
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38
#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.