0

i was create a simple export code using pyqgis3 and atlas print composer map layout on QGIS3.

my question is about map scale.

if i use this line :

mapScale = map.scale()

i take the scale from map and i can use this number on grid interval as i want.

but after generate atlas mode and using this settings for atlas scale

layout.referenceMap().setAtlasScalingMode(QgsLayoutItemMap.Auto)

then the map scale change.

i want to take with some way to take the scale from the atlas features after generate or how to use expression on define IntervalX like this .

expression123 = "$scale*10"
qgs_layout_item_map_grid = QgsLayoutItemMapGrid("map 1", map)
#qgs_layout_item_map_grid.intervalY(50)
#qgs_layout_item_map_grid.setIntervalX(50)

i try to use this but without success.

qgs_layout_item_map_grid.dataDefinedProperties().property(QgsLayoutItemMapGrid.intervalY).setExpressionString(expression
2

The part where you were directly stucked should be related to the following

newGrid = QgsLayoutItemMapGrid ('My new grid', referenceMap)
referenceMap.grids().addGrid(newGrid)

You can find below a long correction if you want to use existing grid (if already in your layout) or by adding a new grid

project = QgsProject.instance()
manager = project.layoutManager()
layout = manager.layoutByName('mygridname')
referenceMap = layout.referenceMap()

# We supposed you set a grid manually here
firstGrid = referenceMap.grid()
# You can also use referenceMap.grids().asList() if more than one grid

# Else, you can add the grid with
newGrid = QgsLayoutItemMapGrid ('My new grid', referenceMap)
referenceMap.grids().addGrid(newGrid)

# INFO: We continue with `firstGrid` variable but you could
# replace all the following code by using `newGrid` instead

# To get the non dynamic properties for intervalX & intervalY
firstGrid.intervalX()
firstGrid.intervalY()

# To activate the grid
firstGrid.setEnabled(True)

# Your expression as a string
expressionScale = '@map_scale / 100'
# To get the dynamic properties for intervalX e.g QgsLayoutObject.MapGridIntervalX
# & intervalY e.g QgsLayoutObject.MapGridIntervalY
dataDefinedMapGridIntervalX = firstGrid.dataDefinedProperties().property(QgsLayoutObject.MapGridIntervalX)
dataDefinedMapGridIntervalY = firstGrid.dataDefinedProperties().property(QgsLayoutObject.MapGridIntervalY)

# Set the expression
dataDefinedMapGridIntervalX.setExpressionString(expressionScale)
dataDefinedMapGridIntervalY.setExpressionString(expressionScale)

# Activate both properties
dataDefinedMapGridIntervalX.setActive(True)
dataDefinedMapGridIntervalY.setActive(True)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.