3

Is there a way to access the status and the XY-coordinates from a GPS-connection established by the GPS-widget in QGIS 3 via Python?

I want to establish the connection with the widget to have better control of the connection (like changing ports) and status checks, but need the coordinates inside a Python plugin.

Taras
  • 32,823
  • 4
  • 66
  • 137
Firefly
  • 355
  • 2
  • 12
  • This is a good one! There is a panel to view GPS in the menu View- Panels- GPS information. There must be a way to get that info. – Cary H Dec 21 '18 at 19:17
  • Seems to be a bigger issue.. even in the QGIS mailinglist I got no reaction to this question. – Firefly Dec 22 '18 at 09:23
  • 1
    When keeping the map always centered over the current position, it is possible to get the coordinates with canvas = iface.mapCanvas() canvas.center() . – Firefly Dec 28 '18 at 21:19
  • 1
    https://gis.stackexchange.com/questions/187985/accessing-gps-from-qgis-2-14-1-python-2-7-windows10 This is a QGIS 2 response but we can look for the same function in QGIS 3.4 docs – Cary H Feb 20 '19 at 16:13

1 Answers1

2

Thanks to the comment of @CaryH I found a solution.

For QGIS 2:

QgsGpsConnectionRegistry().instance()

For QGIS 3:

QgsApplication.gpsConnectionRegistry()

So using the following code

connectionRegistry = QgsApplication.gpsConnectionRegistry()
connectionList = connectionRegistry.connectionList()
GPSInfo = connectionList[0].currentGPSInformation()

After that it is possible to access all information of the widget-gps-connection using for example

GPSInfo.elevation
GPSInfo.latitude

All available commands are listed at https://qgis.org/pyqgis/3.2/core/Gps/QgsGpsInformation.html

Taras
  • 32,823
  • 4
  • 66
  • 137
Firefly
  • 355
  • 2
  • 12