Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. # just the standard initialization
  2. #
  3. from qgis.core import QgsApplication, QgsFeature, QgsGeometry
  4. from qgis.core import QgsMapLayerRegistry, QgsProject
  5.  
  6. from PyQt4.QtGui import QApplication
  7. from PyQt4.QtCore import QFileInfo
  8.  
  9. app = QApplication([])
  10. QgsApplication.setPrefixPath("/usr", True)
  11. QgsApplication.initQgis()
  12.  
  13. project = QgsProject.instance()
  14. project.read(QFileInfo('/home/mario/Documents/GIS/heliconiaBB.qgs'))
  15.  
  16. # import normal distribution from 'random'
  17. #
  18. from random import gauss
  19.  
  20. # all layers are in WGS 84, or I want the code to be independent from the
  21. # layer coordinates reference system
  22.  
  23. # the points in my virtual reality
  24. reality = QgsMapLayerRegistry.instance().mapLayersByName("points")[0]
  25. # the points as measured by my virtual GPS machine, to be simulated here
  26. gps_readings = QgsMapLayerRegistry.instance().mapLayersByName("gps")[0]
  27.  
  28. # the two layers have the same fields set, including 'code'
  29. fields = reality.fields()
  30.  
  31. gps_readings.startEditing()
  32. pr = gps_readings.dataProvider()
  33.  
  34. for feat in reality.getFeatures():
  35. dx = gauss(0.0, 3.0)
  36. dy = gauss(0.0, 3.0)
  37.  
  38. # the following line is just pseudo-code
  39. fake_gps_reading = feat.geometry().asPoint() + (dx, dy)
  40.  
  41. new_feat = QgsFeature(fields)
  42. new_feat['code'] = feat['code']
  43. new_feat.setGeometry(QgsGeometry.fromPoint(fake_gps_reading))
  44. pr.addFeatures( [ new_feat ] )
  45.  
  46. gps_readings.commitChanges()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement