3

In QGIS, would it be possible to automatically assign the current map scale to a feature created during an edit session and save it into a field in the attribute table?

I have a series of OS maps of different scales set up to allow automatic zooming in and out. Users will digitise features onto a study area and may choose to do this at different scales - for example a large polygon of a general, non specific, area at a small scale, and a point location for which the location is known exactly and is digitised at a large scale. To allow me to determine the potential accuracy of the features being digitised (after the session) I would like to be able to automatically assign the current map scale to the attribute table when completing a feature.

Ideally it would happen automatically on finishing each feature, so that there is minimal user interaction (the people using the system are non-specialists), but other suggestions welcome that could be adapted into a reasonably slick workflow.

1 Answer 1

2

One method is to use a short Python code in the Python console which will write the map scale to the attribute table. You would first need to create a new field to accept the scale values and then replace Map_Scale (which is the field name I used) from the second last line to whatever field name you used:

layer = qgis.utils.iface.activeLayer()
canvas = qgis.utils.iface.mapCanvas()
    
# Top 2 lines need only be entered once
# Copy the 3 lines below, paste into the console after creating/selecting the feature

for feature in layer.selectedFeatures():
    idx = layer.fieldNameIndex('Map_Scale') 
    layer.changeAttributeValue(feature.id(), idx, canvas.scale()) 

The code adds the map scale to the field of the selected feature(s). Therefore, once a feature is created, it has to be selected and then paste the code into the console.

I admit it's not very slick but hopefully others can also advise!


Update:

Inspired by and credit to @gcarrillo in a recent post

You could apply a shortcut key to the above code instead of having to type or paste anything in the Python Console after each creation. Paste the following code into the Python Console and make sure Edit mode is on for your layer. Now you can select a feature and simply press Ctrl + 1 to update the Map_Scale field with the current scale:

layer = qgis.utils.iface.activeLayer()
canvas = qgis.utils.iface.mapCanvas()

def scale():
    for feature in layer.selectedFeatures():
        idx = layer.fieldNameIndex('Map_Scale') 
        layer.changeAttributeValue(feature.id(), idx, canvas.scale()) 

from PyQt4.QtGui import QShortcut, QKeySequence
from PyQt4.QtCore import Qt
shortcut = QShortcut(QKeySequence(Qt.ControlModifier + Qt.Key_1), iface.mainWindow())
shortcut.setContext(Qt.ApplicationShortcut)
shortcut.activated.connect(scale)

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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