35#include <QRegularExpression>
38using namespace Qt::StringLiterals;
54 if ( layer->extent().isNull() )
74 if ( fullExtent.
width() == 0.0 || fullExtent.
height() == 0.0 )
82 fullExtent.
set( -1.0, -1.0, 1.0, 1.0 );
86 const double padFactor = 1e-8;
87 const double widthPad = fullExtent.
xMinimum() * padFactor;
88 const double heightPad = fullExtent.
yMinimum() * padFactor;
89 const double xmin = fullExtent.
xMinimum() - widthPad;
90 const double xmax = fullExtent.
xMaximum() + widthPad;
91 const double ymin = fullExtent.
yMinimum() - heightPad;
92 const double ymax = fullExtent.
yMaximum() + heightPad;
93 fullExtent.
set( xmin, ymin, xmax, ymax );
111 if ( !providerMetadata )
117 return conn.release();
121 if ( !ex.
what().contains(
"createConnection"_L1 ) )
123 QgsDebugError( u
"Error retrieving database connection for layer %1: %2"_s.arg( layer->
name(), ex.
what() ) );
131 if ( !layer || path.isEmpty() )
135 return parts.value( u
"path"_s ).toString() == path;
159 if ( !layer || newPath.isEmpty() )
163 if ( !parts.contains( u
"path"_s ) )
166 parts.insert( u
"path"_s, newPath );
174 QList< QgsMapLayer * > res = layers;
176 for ( Qgis::LayerType type : order )
178 if ( a->type() == type && b->type() != type )
180 else if ( b->type() == type )
190 QString laundered = name.toLower();
191 const thread_local QRegularExpression sRxSwapChars( u
"\\s"_s );
192 laundered.replace( sRxSwapChars, u
"_"_s );
194 const thread_local QRegularExpression sRxRemoveChars( u
"[^a-zA-Z0-9_]"_s );
195 laundered.replace( sRxRemoveChars, QString() );
213 if ( providerType ==
"wms"_L1 )
217 QVariantMap details = metadata->decodeUri( uri );
218 url = QUrl( details.value( u
"url"_s ).toString() );
221 else if ( providerType ==
"xyzvectortiles"_L1 )
225 QVariantMap details = metadata->decodeUri( uri );
226 url = QUrl( details.value( u
"url"_s ).toString() );
229 return !url.isEmpty() && ( url.host().endsWith(
".openstreetmap.org"_L1 ) || url.host().endsWith(
".osm.org"_L1 ) );
237 return QObject::tr(
"Vector" );
239 return QObject::tr(
"Raster" );
241 return QObject::tr(
"Mesh" );
243 return QObject::tr(
"Point Cloud" );
245 return QObject::tr(
"Annotation" );
247 return QObject::tr(
"Vector Tile" );
249 return QObject::tr(
"Plugin" );
251 return QObject::tr(
"Group" );
253 return QObject::tr(
"Tiled Scene" );
266 if ( title.isEmpty() )
267 title = layer->
name();
268 title =
"<b>" + title +
"</b>";
271 QString layerCrs = layer->
crs().
authid();
276 if (
const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
279 title = QObject::tr(
"%1 (%2)" ).arg( title, layerCrs );
284 if ( !abstract.isEmpty() )
285 parts <<
"<br/>" + abstract.replace(
"\n"_L1,
"<br/>"_L1 );
289 parts << QObject::tr(
"ID: %1" ).arg( layer->
id() );
291 return parts.join(
"<br/>"_L1 );
298 const QString &providerKey,
299 const QString &dataSource,
301 const QString &description,
303 const QString &uiFileContent,
310 QString sldStyle, qmlStyle, qmlError;
314 QDomDocument qmlDocument;
317 if ( !qmlError.isEmpty() )
324 qmlStyle = qmlDocument.toString();
332 if ( !sldContext.
errors().empty() )
340 sldStyle = sldDocument.toString();
344 QString providerSaveStyleError;
345 if ( !
QgsProviderRegistry::instance()->saveStyle( providerKey, dataSource, qmlStyle, sldStyle, name, description, uiFileContent, useAsDefault, providerSaveStyleError ) )
QFlags< SaveStyleFormat > SaveStyleFormats
LayerType
Types of layers that can be added to a map.
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
SourceHierarchyLevel
Defines the structural levels within a data source hierarchy.
Provides common functionality for database based connections.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
double coordinateEpoch() const
Returns the coordinate epoch, as a decimal year.
Contains information about the context in which a coordinate transform is executed.
Custom exception class for Coordinate Reference System related exceptions.
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
QString abstract() const
Returns the abstract of the layerused by QGIS Server in GetCapabilities request.
static QString layerToolTip(const QgsMapLayer *layer)
Returns the consistent tooltip for the given layer.
static bool updateLayerSourcePath(QgsMapLayer *layer, const QString &newPath)
Updates a layer's data source, replacing its data source with a path referring to newPath.
static QgsSaveStyleResult saveLayerStyleToDatabase(QgsMapLayer *layer, const QString &providerKey, const QString &targetDataSource, const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, const Qgis::SaveStyleFormats formats=Qgis::SaveStyleFormats(Qgis::SaveStyleFormat::QML|Qgis::SaveStyleFormat::SLD), QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Saves QML and SLD representations of layer's style to a table in the database identified by providerK...
static bool layerRefersToUri(const QgsMapLayer *layer, const QString &uri, Qgis::SourceHierarchyLevel level=Qgis::SourceHierarchyLevel::Object)
Returns true if a layer and uri point to the same resource at the specified hierarchy level.
static QgsRectangle combinedExtent(const QList< QgsMapLayer * > &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext)
Returns the combined extent of a list of layers.
static QgsAbstractDatabaseProviderConnection * databaseConnection(const QgsMapLayer *layer)
Creates and returns the (possibly nullptr) database connection for a layer.
static QString layerTypeToString(Qgis::LayerType type)
Returns the translated name of the type for a given layer type.
static QList< QgsMapLayer * > sortLayersByType(const QList< QgsMapLayer * > &layers, const QList< Qgis::LayerType > &order)
Sorts a list of map layers by their layer type, respecting the order of types specified.
static QString launderLayerName(const QString &name)
Launders a layer's name, converting it into a format which is general suitable for file names or data...
static bool isOpenStreetMapLayer(QgsMapLayer *layer)
Returns true if the layer is served by OpenStreetMap server.
static bool layerSourceMatchesPath(const QgsMapLayer *layer, const QString &path)
Returns true if the source of the specified layer matches the given path.
static bool isOpenStreetMapUri(const QString &uri, const QString &providerType)
Returns true if the uri for a given provider is served by OpenStreetMap server.
Base class for all map layer types.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
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.
QString source() const
Returns the source for the layer.
@ SldGenerationFailed
Generation of the SLD failed, and was not written to the database.
@ DatabaseWriteFailed
An error occurred when attempting to write to the database.
@ QmlGenerationFailed
Generation of the QML failed, and was not written to the database.
QString providerType() const
Returns the provider type (provider key) for this layer.
QgsCoordinateReferenceSystem crs
QgsMapLayerServerProperties * serverProperties()
Returns QGIS Server Properties for the map layer.
void setDataSource(const QString &dataSource, const QString &baseName=QString(), const QString &provider=QString(), bool loadDefaultStyleFlag=false)
Updates the data source of the layer.
QgsLayerMetadata metadata
QString publicSource(bool hidePassword=false) const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
QFlags< StyleCategory > StyleCategories
virtual QDomDocument exportSldStyleV3(QgsSldExportContext &exportContext) const
Export the properties of this layer as SLD style in a QDomDocument.
Custom exception class which is raised when an operation is not supported.
Custom exception class for provider connection related exceptions.
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.
QString encodeUri(const QString &providerKey, const QVariantMap &parts)
Reassembles a provider data source URI from its component paths (e.g.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
Q_INVOKABLE QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be rounded to the spec...
void set(const QgsPointXY &p1, const QgsPointXY &p2, bool normalize=true)
Sets the rectangle from two QgsPoints.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
void setNull()
Mark a rectangle as being null (holding no spatial information).
Represents the result of saving a style.
QStringList sldWarningMessages
List of warning messages generated while exporting the style to SLD.
QStringList sldErrorMessages
List of error messages generated while exporting the style to SLD.
QString qmlError
Error message generated while saving the style as QML, or an empty string if no error occurred.
QString providerSaveStyleError
Error message reported by the data provider when saving the style, or an empty string if no error occ...
QgsMapLayer::SaveStyleResults saveResult
Flags representing which style formats (QML and/or SLD) were successfully saved.
static const QgsSettingsEntryBool * settingsLayerTreeShowIdInLayerTooltips
Settings entry for hidden ID in layer tooltips.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
QStringList warnings() const
Returns a list of warnings which occurred during the conversion.
QStringList errors() const
Returns a list of errors which occurred during the conversion.
Represents a vector layer which manages a vector based dataset.
static Q_INVOKABLE QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)