QgsLayoutItemMap class
Layout graphical items for displaying a map.
Contents
Base classes
- class QgsLayoutItem
- Base class for graphical items within a QgsLayout.
Public types
- enum AtlasScalingMode { Fixed, Predefined, Auto }
- Scaling modes used for the serial rendering (atlas)
- enum MapItemFlag { ShowPartialLabels = 1 << 0 }
- Various flags that affect drawing of map items.
Public static functions
- static auto create(QgsLayout* layout) -> QgsLayoutItemMap*
- Returns a new map item for the specified layout.
Constructors, destructors, conversion operators
- QgsLayoutItemMap(QgsLayout* layout) explicit
- Constructor for QgsLayoutItemMap, with the specified parent layout.
Public functions
- void addLabelBlockingItem(QgsLayoutItem* item)
- Sets the specified layout item as a "label blocking item" for this map.
- void assignFreeId()
- Sets the map id() to a number not yet used in the layout.
- auto atlasDriven() const -> bool
- Returns whether the map extent is set to follow the current atlas feature.
-
auto atlasMargin(QgsLayoutObject::
PropertyValueType valueType = QgsLayoutObject:: EvaluatedValue) -> double - Returns the margin size (percentage) used when the map is in atlas mode.
- auto atlasScalingMode() const -> AtlasScalingMode
- Returns the current atlas scaling mode.
- auto containsAdvancedEffects() const -> bool override
- Returns true if the item contains contents with blend modes or transparency effects which can only be reproduced by rastering the item.
- auto containsWmsLayer() const -> bool
- Returns true if the map contains a WMS layer.
- auto createExpressionContext() const -> QgsExpressionContext override
- This method needs to be reimplemented in all classes which implement this interface and return an expression context.
- auto crs() const -> QgsCoordinateReferenceSystem
- Returns coordinate reference system used for rendering the map.
- auto displayName() const -> QString override
- Gets item display name.
- auto drawAnnotations() const -> bool
- Returns whether annotations are drawn within the map.
- auto extent() const -> QgsRectangle
- Returns the current map extent.
- void finalizeRestoreFromXml() override
- Called after all pending items have been restored from XML.
- auto followVisibilityPreset() const -> bool
- Returns whether the map should follow a map theme.
- auto followVisibilityPresetName() const -> QString
- Preset name that decides which layers and layer styles are used for map rendering.
- auto grid() -> QgsLayoutItemMapGrid*
- Returns the map item's first grid.
- auto grids() -> QgsLayoutItemMapGridStack*
- Returns the map item's grid stack, which is used to control how grids are drawn over the map's contents.
- auto icon() const -> QIcon override
- Returns the item's icon.
- auto isLabelBlockingItem(QgsLayoutItem* item) const -> bool
- Returns true if the specified item is a "label blocking item".
- auto itemFlags() const -> QgsLayoutItem::Flags override
- Returns the item's flags, which indicate how the item behaves.
- auto keepLayerSet() const -> bool
- Returns whether a stored layer set should be used or the current layer set from the project associated with the layout.
- auto keepLayerStyles() const -> bool
- Returns whether current styles of layers should be overridden by previously stored styles.
- auto labelMargin() const -> QgsLayoutMeasurement
- Returns the margin from the map edges in which no labels may be placed.
- auto layers() const -> QList<QgsMapLayer*>
- Returns the stored layer set.
- auto layersToRender(const QgsExpressionContext* context = nullptr) const -> QList<QgsMapLayer*>
- Returns a list of the layers which will be rendered within this map item, considering any locked layers, linked map theme, and data defined settings.
- auto layerStyleOverrides() const -> QMap<QString, QString>
- Returns stored overrides of styles for layers.
- auto mapFlags() const -> QgsLayoutItemMap::MapItemFlags
- Returns the map item's flags, which control how the map content is drawn.
-
auto mapRotation(QgsLayoutObject::
PropertyValueType valueType = QgsLayoutObject:: EvaluatedValue) const -> double - Returns the rotation used for drawing the map within the layout item, in degrees clockwise.
- auto mapSettings(const QgsRectangle& extent, QSizeF size, double dpi, bool includeLayerSettings) const -> QgsMapSettings
- Returns map settings that will be used for drawing of the map.
- auto mapUnitsToLayoutUnits() const -> double
- Returns the conversion factor from map units to layout units.
- void moveContent(double dx, double dy) override
- Moves the content of the item, by a specified dx and dy in layout units.
- auto numberExportLayers() const -> int override
- Returns the number of layers that this item requires for exporting during layered exports (e.g.
- auto overview() -> QgsLayoutItemMapOverview*
- Returns the map item's first overview.
- auto overviews() -> QgsLayoutItemMapOverviewStack*
- Returns the map item's overview stack, which is used to control how overviews are drawn over the map's contents.
- auto presetCrs() const -> QgsCoordinateReferenceSystem
- Returns the map's preset coordinate reference system.
- void removeLabelBlockingItem(QgsLayoutItem* item)
- Removes the specified layout item from the map's "label blocking items".
- auto requiresRasterization() const -> bool override
- Returns true if the item is drawn in such a way that forces the whole layout to be rasterized when exporting to vector formats.
- auto scale() const -> double
- Returns the map scale.
- void setAtlasDriven(bool enabled)
- Sets whether the map extent will follow the current atlas feature.
- void setAtlasMargin(double margin)
- Sets the margin size (percentage) used when the map is in atlas mode.
- void setAtlasScalingMode(AtlasScalingMode mode)
- Sets the current atlas scaling mode.
- void setCrs(const QgsCoordinateReferenceSystem& crs)
- Sets the map's preset crs (coordinate reference system).
- void setDrawAnnotations(bool draw)
- Sets whether annotations are drawn within the map.
- void setExtent(const QgsRectangle& extent)
- Sets a new extent for the map.
- void setFollowVisibilityPreset(bool follow)
- Sets whether the map should follow a map theme.
- void setFollowVisibilityPresetName(const QString& name)
- Sets preset name for map rendering.
- void setFrameStrokeWidth(QgsLayoutMeasurement width) override
- Sets the frame stroke width.
- void setKeepLayerSet(bool enabled)
- Sets whether the stored layer set should be used or the current layer set of the associated project.
- void setKeepLayerStyles(bool enabled)
- Sets whether current styles of layers should be overridden by previously stored styles.
- void setLabelMargin(const QgsLayoutMeasurement& margin)
- Sets the margin from the map edges in which no labels may be placed.
- void setLayers(const QList<QgsMapLayer*>& layers)
- Sets the stored layers set.
- void setLayerStyleOverrides(const QMap<QString, QString>& overrides)
- Sets the stored overrides of styles for layers.
- void setMapFlags(QgsLayoutItemMap::MapItemFlags flags)
- Sets the map item's flags, which control how the map content is drawn.
- void setMapRotation(double rotation)
- Sets the rotation for the map - this does not affect the layout item shape, only the way the map is drawn within the item.
- void setMoveContentPreviewOffset(double dx, double dy) override
- Sets temporary offset for the item, by a specified dx and dy in layout units.
- void setScale(double scale, bool forceUpdate = true)
- Sets new map scale and changes only the map extent.
- void storeCurrentLayerStyles()
- Stores the current project layer styles into style overrides.
- auto visibleExtentPolygon() const -> QPolygonF
- Returns a polygon representing the current visible map extent, considering map extents and rotation.
- void zoomContent(double factor, QPointF point) override
- Zooms content of item.
- void zoomToExtent(const QgsRectangle& extent)
- Zooms the map so that the specified extent is fully visible within the map item.
Signals
- void extentChanged()
- Is emitted when the map's extent changes.
- void layerStyleOverridesChanged()
- Emitted when layer style overrides are changed...
- void mapRotationChanged(double newRotation)
- Is emitted when the map's rotation changes.
- void preparedForAtlas()
- Is emitted when the map has been prepared for atlas rendering, just before actual rendering.
Public slots
- void updateBoundingRect()
- Updates the bounding rect of this item. Call this function before doing any changes related to annotation out of the map rectangle.
Protected functions
- void draw(QgsLayoutItemRenderContext& context) override
- Draws the item's contents using the specified item render context.
- auto isDrawing() const -> bool
- True if a draw is already in progress.
- auto mapToItemCoords(QPointF mapCoords) const -> QPointF
- Transforms map coordinates to item coordinates (considering rotation and move offset)
- auto readPropertiesFromElement(const QDomElement& element, const QDomDocument& document, const QgsReadWriteContext& context) -> bool override
- Sets item state from a DOM element.
- auto requestedExtent() const -> QgsRectangle
- Calculates the extent to request and the yShift of the top-left point in case of rotation.
- auto transformedMapPolygon() const -> QPolygonF
- Returns extent that considers rotation and shift with mOffsetX / mOffsetY.
- auto writePropertiesToElement(QDomElement& element, QDomDocument& document, const QgsReadWriteContext& context) const -> bool override
- Stores item state within an XML DOM element.
Enum documentation
enum QgsLayoutItemMap:: AtlasScalingMode
Scaling modes used for the serial rendering (atlas)
| Enumerators | |
|---|---|
| Fixed |
The current scale of the map is used for each feature of the atlas. |
| Predefined |
A scale is chosen from the predefined scales. The smallest scale from the list of scales where the atlas feature is fully visible is chosen. |
| Auto |
The extent is adjusted so that each feature is fully visible. A margin is applied around the center |
enum QgsLayoutItemMap:: MapItemFlag
Various flags that affect drawing of map items.
| Enumerators | |
|---|---|
| ShowPartialLabels |
Whether to draw labels which are partially outside of the map view. |
Function documentation
static QgsLayoutItemMap* QgsLayoutItemMap:: create(QgsLayout* layout)
Returns a new map item for the specified layout.
The caller takes responsibility for deleting the returned object.
void QgsLayoutItemMap:: addLabelBlockingItem(QgsLayoutItem* item)
Sets the specified layout item as a "label blocking item" for this map.
Items which are marked as label blocking items prevent any map labels from being placed in the area of the map item covered by the item.
void QgsLayoutItemMap:: assignFreeId()
Sets the map id() to a number not yet used in the layout.
The existing id() is kept if it is not in use.
bool QgsLayoutItemMap:: atlasDriven() const
Returns whether the map extent is set to follow the current atlas feature.
| Returns | true if map will follow the current atlas feature. |
|---|
double QgsLayoutItemMap:: atlasMargin(QgsLayoutObject:: PropertyValueType valueType = QgsLayoutObject:: EvaluatedValue)
Returns the margin size (percentage) used when the map is in atlas mode.
| Parameters | |
|---|---|
| valueType | controls whether the returned value is the user specified atlas margin, or the current evaluated atlas margin (which may be affected by data driven atlas margin settings). |
| Returns | margin size in percentage to leave around the atlas feature's extent |
AtlasScalingMode QgsLayoutItemMap:: atlasScalingMode() const
Returns the current atlas scaling mode.
| Returns | the current scaling mode |
|---|
This controls how the map's extents are calculated for the current atlas feature when an atlas composition is enabled.
bool QgsLayoutItemMap:: containsAdvancedEffects() const override
Returns true if the item contains contents with blend modes or transparency effects which can only be reproduced by rastering the item.
Subclasses should ensure that implemented overrides of this method also check the base class result.
QgsExpressionContext QgsLayoutItemMap:: createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an expression context.
QgsCoordinateReferenceSystem QgsLayoutItemMap:: crs() const
Returns coordinate reference system used for rendering the map.
This will match the presetCrs() if that is set, or if a preset CRS is not set then the map's CRS will follow the composition's project's CRS.
QString QgsLayoutItemMap:: displayName() const override
Gets item display name.
This is the item's id if set, and if not, a user-friendly string identifying item type.
bool QgsLayoutItemMap:: drawAnnotations() const
Returns whether annotations are drawn within the map.
QgsRectangle QgsLayoutItemMap:: extent() const
Returns the current map extent.
void QgsLayoutItemMap:: finalizeRestoreFromXml() override
Called after all pending items have been restored from XML.
Items can use this method to run steps which must take place after all items have been restored to the layout, such as connecting to signals emitted by other items, which may not have existed in the layout at the time readXml() was called. E.g. a scalebar can use this to connect to its linked map item after restoration from XML.
bool QgsLayoutItemMap:: followVisibilityPreset() const
Returns whether the map should follow a map theme.
If true, the layers and layer styles will be used from given preset name (configured with setFollowVisibilityPresetName() method). This means when preset's settings are changed, the new settings are automatically picked up next time when rendering, without having to explicitly update them. At most one of the flags keepLayerSet() and followVisibilityPreset() should be enabled at any time since they are alternative approaches - if both are enabled, following map theme has higher priority. If neither is enabled (or if preset name is not set), map will use the same configuration as the map canvas uses.
QString QgsLayoutItemMap:: followVisibilityPresetName() const
Preset name that decides which layers and layer styles are used for map rendering.
It is only used when followVisibilityPreset() returns true.
QgsLayoutItemMapGrid* QgsLayoutItemMap:: grid()
Returns the map item's first grid.
This is a convenience function.
QgsLayoutItemMapGridStack* QgsLayoutItemMap:: grids()
Returns the map item's grid stack, which is used to control how grids are drawn over the map's contents.
bool QgsLayoutItemMap:: isLabelBlockingItem(QgsLayoutItem* item) const
Returns true if the specified item is a "label blocking item".
Items which are marked as label blocking items prevent any map labels from being placed in the area of the map item covered by the item.
QgsLayoutItem::Flags QgsLayoutItemMap:: itemFlags() const override
Returns the item's flags, which indicate how the item behaves.
bool QgsLayoutItemMap:: keepLayerSet() const
Returns whether a stored layer set should be used or the current layer set from the project associated with the layout.
This is just a GUI flag, and itself does not change which layers are rendered in the map. Instead, use setLayers() to control which layers are rendered.
bool QgsLayoutItemMap:: keepLayerStyles() const
Returns whether current styles of layers should be overridden by previously stored styles.
QgsLayoutMeasurement QgsLayoutItemMap:: labelMargin() const
Returns the margin from the map edges in which no labels may be placed.
If the margin is 0 then labels can be placed right up to the edge (and possibly overlapping the edge) of the map.
QList<QgsMapLayer*> QgsLayoutItemMap:: layers() const
Returns the stored layer set.
If empty, the current project layers will be used instead.
QMap<QString, QString> QgsLayoutItemMap:: layerStyleOverrides() const
Returns stored overrides of styles for layers.
QgsLayoutItemMap::MapItemFlags QgsLayoutItemMap:: mapFlags() const
Returns the map item's flags, which control how the map content is drawn.
double QgsLayoutItemMap:: mapRotation(QgsLayoutObject:: PropertyValueType valueType = QgsLayoutObject:: EvaluatedValue) const
Returns the rotation used for drawing the map within the layout item, in degrees clockwise.
| Parameters | |
|---|---|
| valueType | controls whether the returned value is the user specified rotation, or the current evaluated rotation (which may be affected by data driven rotation settings). |
QgsMapSettings QgsLayoutItemMap:: mapSettings(const QgsRectangle& extent,
QSizeF size,
double dpi,
bool includeLayerSettings) const
Returns map settings that will be used for drawing of the map.
If includeLayerSettings is true, than settings specifically relating to map layers and map layer styles will be calculated. This can be expensive to calculate, so if they are not required in the map settings (e.g. for map settings which are used for scale related calculations only) then includeLayerSettings should be false.
double QgsLayoutItemMap:: mapUnitsToLayoutUnits() const
Returns the conversion factor from map units to layout units.
This is calculated using the width of the map item and the width of the current visible map extent.
void QgsLayoutItemMap:: moveContent(double dx,
double dy) override
Moves the content of the item, by a specified dx and dy in layout units.
The default implementation has no effect.
int QgsLayoutItemMap:: numberExportLayers() const override
Returns the number of layers that this item requires for exporting during layered exports (e.g.
SVG). Returns 0 if this item is to be placed on the same layer as the previous item, 1 if it should be placed on its own layer, and >1 if it requires multiple export layers.
Items which require multiply layers should check QgsLayoutContext::currentExportLayer() during their rendering to determine which layer should be drawn.
QgsLayoutItemMapOverview* QgsLayoutItemMap:: overview()
Returns the map item's first overview.
| Returns | pointer to first overview for map item |
|---|
This is a convenience function.
QgsLayoutItemMapOverviewStack* QgsLayoutItemMap:: overviews()
Returns the map item's overview stack, which is used to control how overviews are drawn over the map's contents.
| Returns | pointer to overview stack |
|---|
QgsCoordinateReferenceSystem QgsLayoutItemMap:: presetCrs() const
Returns the map's preset coordinate reference system.
If set, this CRS will be used to render the map regardless of any project CRS setting. If the returned CRS is not valid then the project CRS will be used to render the map.
void QgsLayoutItemMap:: removeLabelBlockingItem(QgsLayoutItem* item)
Removes the specified layout item from the map's "label blocking items".
Items which are marked as label blocking items prevent any map labels from being placed in the area of the map item covered by the item.
bool QgsLayoutItemMap:: requiresRasterization() const override
Returns true if the item is drawn in such a way that forces the whole layout to be rasterized when exporting to vector formats.
double QgsLayoutItemMap:: scale() const
Returns the map scale.
The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
void QgsLayoutItemMap:: setAtlasDriven(bool enabled)
Sets whether the map extent will follow the current atlas feature.
| Parameters | |
|---|---|
| enabled | set to true if the map extents should be set by the current atlas feature. |
void QgsLayoutItemMap:: setAtlasMargin(double margin)
Sets the margin size (percentage) used when the map is in atlas mode.
| Parameters | |
|---|---|
| margin | size in percentage to leave around the atlas feature's extent |
void QgsLayoutItemMap:: setAtlasScalingMode(AtlasScalingMode mode)
Sets the current atlas scaling mode.
| Parameters | |
|---|---|
| mode | atlas scaling mode to set |
This controls how the map's extents are calculated for the current atlas feature when an atlas composition is enabled.
void QgsLayoutItemMap:: setCrs(const QgsCoordinateReferenceSystem& crs)
Sets the map's preset crs (coordinate reference system).
If a valid CRS is set, this CRS will be used to render the map regardless of any project CRS setting. If the CRS is not valid then the project CRS will be used to render the map.
void QgsLayoutItemMap:: setDrawAnnotations(bool draw)
Sets whether annotations are drawn within the map.
void QgsLayoutItemMap:: setExtent(const QgsRectangle& extent)
Sets a new extent for the map.
This method may change the width or height of the map item to ensure that the extent exactly matches the specified extent, with no overlap or margin. This method implicitly alters the map scale.
void QgsLayoutItemMap:: setFollowVisibilityPreset(bool follow)
Sets whether the map should follow a map theme.
See followVisibilityPreset() for more details.
void QgsLayoutItemMap:: setFollowVisibilityPresetName(const QString& name)
Sets preset name for map rendering.
See followVisibilityPresetName() for more details.
void QgsLayoutItemMap:: setFrameStrokeWidth(QgsLayoutMeasurement width) override
Sets the frame stroke width.
void QgsLayoutItemMap:: setKeepLayerSet(bool enabled)
Sets whether the stored layer set should be used or the current layer set of the associated project.
This is just a GUI flag, and itself does not change which layers are rendered in the map. Instead, use setLayers() to control which layers are rendered.
void QgsLayoutItemMap:: setKeepLayerStyles(bool enabled)
Sets whether current styles of layers should be overridden by previously stored styles.
void QgsLayoutItemMap:: setLabelMargin(const QgsLayoutMeasurement& margin)
Sets the margin from the map edges in which no labels may be placed.
If the margin is 0 then labels can be placed right up to the edge (and possibly overlapping the edge) of the map.
void QgsLayoutItemMap:: setLayers(const QList<QgsMapLayer*>& layers)
Sets the stored layers set.
If empty, the current project layers will be used instead.
void QgsLayoutItemMap:: setLayerStyleOverrides(const QMap<QString, QString>& overrides)
Sets the stored overrides of styles for layers.
void QgsLayoutItemMap:: setMapFlags(QgsLayoutItemMap::MapItemFlags flags)
Sets the map item's flags, which control how the map content is drawn.
void QgsLayoutItemMap:: setMapRotation(double rotation)
Sets the rotation for the map - this does not affect the layout item shape, only the way the map is drawn within the item.
Rotation is in degrees, clockwise.
void QgsLayoutItemMap:: setMoveContentPreviewOffset(double dx,
double dy) override
Sets temporary offset for the item, by a specified dx and dy in layout units.
This is useful for live updates when moving item content in a QgsLayoutView. The default implementation has no effect.
void QgsLayoutItemMap:: setScale(double scale,
bool forceUpdate = true)
Sets new map scale and changes only the map extent.
The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
QPolygonF QgsLayoutItemMap:: visibleExtentPolygon() const
Returns a polygon representing the current visible map extent, considering map extents and rotation.
| Returns | polygon with the four corner points representing the visible map extent. The points are clockwise, starting at the top-left point |
|---|
If the map rotation is 0, the result is the same as currentMapExtent
void QgsLayoutItemMap:: zoomContent(double factor,
QPointF point) override
Zooms content of item.
| Parameters | |
|---|---|
| factor | zoom factor, where > 1 results in a zoom in and < 1 results in a zoom out |
| point | item point for zoom center |
Does nothing by default.
void QgsLayoutItemMap:: zoomToExtent(const QgsRectangle& extent)
Zooms the map so that the specified extent is fully visible within the map item.
This method will not change the width or height of the map, and may result in an overlap or margin from the specified extent. This method implicitly alters the map scale.
void QgsLayoutItemMap:: extentChanged() signal
Is emitted when the map's extent changes.
void QgsLayoutItemMap:: layerStyleOverridesChanged() signal
Emitted when layer style overrides are changed...
a means to let associated legend items know they should update
void QgsLayoutItemMap:: mapRotationChanged(double newRotation) signal
Is emitted when the map's rotation changes.
void QgsLayoutItemMap:: draw(QgsLayoutItemRenderContext& context) override protected
Draws the item's contents using the specified item render context.
Note that the context's painter has been scaled so that painter units are pixels. Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units.
bool QgsLayoutItemMap:: readPropertiesFromElement(const QDomElement& element,
const QDomDocument& document,
const QgsReadWriteContext& context) override protected
Sets item state from a DOM element.
| Parameters | |
|---|---|
| element | is the DOM element for the item |
| document | DOM document |
| context | read write context |
Note that item subclasses should not rely on all other items being present in the layout at the time this method is called. Instead, any connections and links to other items must be made in the finalizeRestoreFromXml() method. E.g. when restoring a scalebar, the connection to the linked map's signals should be implemented in finalizeRestoreFromXml(), not readPropertiesFromElement().
bool QgsLayoutItemMap:: writePropertiesToElement(QDomElement& element,
QDomDocument& document,
const QgsReadWriteContext& context) const override protected
Stores item state within an XML DOM element.
| Parameters | |
|---|---|
| element | is the DOM element to store the item's properties in |
| document | DOM document |
| context | read write context |