I'm writing a plugin where once a layer is selected in a combo box, it shows the layer's linear unit of measurement (meters, feet, etc.) in a QLineEdit box.

def Function(self):
    currLyr = self.cmbLyrSelect.currentLayer()
    currLyrUnit = currLyr.sourceCrs().mapUnits()

This works and mapUnits() return a 0, which is meters as the layer is projected. So, to get meters as string, I use instead


But QGIS returns an Attribute Error saying 'DistanceUnit' object has no attribute 'encodeUnit'.

What's the correct syntax in using encodeUnit()?

sourceCrs() https://qgis.org/pyqgis/3.16/core/QgsCoordinateTransform.html?highlight=sourcecrs#qgis.core.QgsCoordinateTransform.sourceCrs

mapUnits() https://qgis.org/pyqgis/3.16/core/QgsCoordinateReferenceSystem.html#qgis.core.QgsCoordinateReferenceSystem.mapUnits

DistanceUnit() https://qgis.org/pyqgis/3.16/core/QgsUnitTypes.html#qgis.core.QgsUnitTypes.DistanceUnit

encodeUnit() https://qgis.org/pyqgis/3.16/core/QgsUnitTypes.html#qgis.core.QgsUnitTypes.encodeUnit

1 Answer 1


You can get it with the following code :

  • Thanks for the tip! This worked self.linEditBox.setText(QgsUnitTypes.encodeUnit(currLyrUnit))
    – BallpenMan
    May 27, 2022 at 8:56

Your Answer

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

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