4

From the user interface in QGIS, I can change the coordinate display (unit) for a project by going to:

Project > Properties... > General (tab) and under the Coordinate Display section can change the setting for Display coordinates using to Decimal degrees.

How do I do this with pyqgis?

Things tried so far:

  • Searching the results of QSettings().allKeys() but there are 748 - so I may have missed it.

  • QSettings().setValue('QgsCollapsibleGroupBox/QgsProjectPropertiesBase/mCoordinateDisplayGroup/collapsed', True) allows me to collapse and expand the group box that contains the setting - useless, but so close!

  • looking at QSettings().value('qgis/measure/displayunits'), but making changes in the GUI doesn't seem to change the value of this, nor does guessing with QSettings().setValue('qgis/measure/displayunits', 'degrees')

Also tried setting the destination CRS to one that uses decimal degrees, as per AWGIS's suggestion, but again it's made no difference:

QgsMapCanvas().setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
QgsMapSettings().setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))

Bonus points if you can point me to a reference where I could have looked this up myself!

2
  • Not sure if it is what you are looking for but in QGIS 2 you used to do this with canvas.setMapUnits(QGis.Degrees) - however I think in QGIS 3 this has been removed since this is now dictated by the destination CRS.
    – AWGIS
    Jan 18, 2019 at 15:34
  • Thanks @AWGIS I've updated my question to note I've tried setDestinationCrs(), but without any success.
    – Jonny
    Jan 21, 2019 at 10:05

1 Answer 1

3

I know that my answer is not complete but it is adding more context for getting a completely right answer. For example, if you write at Python Console of a recently opened empty project of QGIS 3:

>>>project = QgsProject.instance()
>>>project.crs().postgisSrid()
4326

you got default CRS (Coordinate Reference System) for this empty project. If you load a layer with a EPSG of 32612, automatically it is changed to this one:

>>>project.crs().postgisSrid()
32612

Before doing anything, it can be observed (red rectangles) that coordinate units are meters in, respectively, Coordinate Display (Project Properties|General), Measure Tool and Status bar (bottom).

enter image description here

If manually is changed the setting for Display coordinates using to Decimal degrees (your proposal above) it can be observed, at status bar, that units were changed automatically to decimal degrees, however, CRS and Measure Tool units remain unchanged (below image).

enter image description here

So, I believed that with the setting for Display coordinates used to meters again and following command:

project.setDistanceUnits(QgsUnitTypes.DistanceDegrees)

I would have expected result but, it only has effect on Measure Tool (at below image):

enter image description here

For accessing to status bar:

>>>sb = iface.mainWindow().statusBar()
>>>sb
<PyQt5.QtWidgets.QStatusBar object at 0x7fb40c76b288>

and, it can be corroborated with its 'setVisible' method (True|False values). However, there is not any method (in QStatusBar) for Units or Coordinates in it. It looks like only as a wrapper for this information. So, a possible solution it could be in Menu Bar.

For accessing to menu bar:

>>>mb = iface.mainWindow().menuBar()
>>>mb
<PyQt5.QtWidgets.QMenuBar object at 0x7fb40c778168>

where it can also be corroborated with its 'setVisible' method (True|False values). For getting its respective actions and show up Project Properties Window:

>>>actions = [ item for item in iface.mainWindow().menuBar().actions() ]
>>>actions[0].text() #for corroborating action name
'Pro&ject'
>>>prj_menu = actions[0].menu()
>>>proj_actions = [ item for item in prj_menu.actions() ]
>>>proj_actions[14].text() #for corroborating action name
'&Properties…'
>>>proj_actions[14].trigger()

At this moment, it appears following Dialog:

enter image description here

where it can be changed default value for coordinate display units.

1
  • Thanks for the post - it's useful, appreciated and gives a lot of information but unfortunately still needs a manual step to change the 'display coordinates using' setting to decimal degrees. I'm ideally looking to do this entirely via the backend API.
    – Jonny
    Jan 21, 2019 at 9:48

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.