I am developing a small tool to complement GPS data with mutual distances, and I need to simulate some input data. So I thought I would introduce a normally distributed error on the (known) positions, to simulate the GPS measurements. To make things easier, my simulated GPS measurements will have independent errors on the two coordinates, both averaging to 3m.
I want to do this within QGIS, and my code looks like this:
# just the standard initialization
#
from qgis.core import QgsApplication, QgsFeature, QgsGeometry
from qgis.core import QgsMapLayerRegistry, QgsProject
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QFileInfo
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
project = QgsProject.instance()
project.read(QFileInfo('/home/mario/Documents/GIS/heliconiaBB.qgs'))
# import normal distribution from 'random'
#
from random import gauss
# all layers are in WGS 84, or I want the code to be independent from the
# layer coordinates reference system
# the points in my virtual reality
reality = QgsMapLayerRegistry.instance().mapLayersByName("points")[0]
# the points as measured by my virtual GPS machine, to be simulated here
gps_readings = QgsMapLayerRegistry.instance().mapLayersByName("gps")[0]
# the two layers have the same fields set, including 'code'
fields = reality.fields()
gps_readings.startEditing()
pr = gps_readings.dataProvider()
for feat in reality.getFeatures():
dx = gauss(0.0, 3.0)
dy = gauss(0.0, 3.0)
# the following line is just pseudo-code
fake_gps_reading = feat.geometry().asPoint() + (dx, dy)
new_feat = QgsFeature(fields)
new_feat['code'] = feat['code']
new_feat.setGeometry(QgsGeometry.fromPoint(fake_gps_reading))
pr.addFeatures( [ new_feat ] )
gps_readings.commitChanges()
I do not know how to add the offset (in metres) to the point location, which is in degrees. I would be quite happy with the converse functionality as offered by QgsDistanceArea
, which gives me the distance in metres between points on an ellipsoid given their latitude and longitude.