Skip to main content
deleted 161 characters in body
Source Link
ndawson
  • 27.9k
  • 3
  • 62
  • 86

Update: Here's a version to try to avoid the memory errors from storing everything in a single list. Now, when the list has grown to the maximum possible size then the features will beare added to the layer and the list clearedonce per row. It's not as fast as the above method, but less memory hungry:

vl = QgsVectorLayer("Point?crs=", "temporary_points2", "memory")
vl.dataProvider().addAttributes([ QgsField("X_coord", QVariant.Int), QgsField("Y_coord", QVariant.Int), QgsField("Azimuth", QVariant.Int) ])

for row in range(0,rows):
   new_features = []
   for col in range(0,cols):
      rspnt = pixel2coord(col,row)
      fet = QgsFeature()
      fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(rspnt[0], rspnt[1])))
      fet.initAttributes(3)
      fet.setAttribute(0, rspnt[0] )
      fet.setAttribute(1, rspnt[1])
      az = pntxy.azimuth(QgsPoint(rspnt[0], rspnt[1]))
      if az < 0.0:
         azim = 360 + az
      else:
         azim = az
      fet.setAttribute(2, azim)
      try:
         new_features.append(fet)
      except:
         vl.dataProvider().addFeatures( new_features )
         new_features = []

vl.dataProvider().addFeatures( new_features )
vl.updateExtents()
vl.updateFields()    
QgsMapLayerRegistry.instance().addMapLayers([vl])

it will be

Update: Here's a version to try to avoid the memory errors from storing everything in a single list. Now, when the list has grown to the maximum possible size then the features will be added to the layer and the list cleared.

vl = QgsVectorLayer("Point?crs=", "temporary_points2", "memory")
vl.dataProvider().addAttributes([ QgsField("X_coord", QVariant.Int), QgsField("Y_coord", QVariant.Int), QgsField("Azimuth", QVariant.Int) ])

for row in range(0,rows):
   new_features = []
   for col in range(0,cols):
      rspnt = pixel2coord(col,row)
      fet = QgsFeature()
      fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(rspnt[0], rspnt[1])))
      fet.initAttributes(3)
      fet.setAttribute(0, rspnt[0] )
      fet.setAttribute(1, rspnt[1])
      az = pntxy.azimuth(QgsPoint(rspnt[0], rspnt[1]))
      if az < 0.0:
         azim = 360 + az
      else:
         azim = az
      fet.setAttribute(2, azim)
      try:
         new_features.append(fet)
      except:
         vl.dataProvider().addFeatures( new_features )
         new_features = []

vl.dataProvider().addFeatures( new_features )
vl.updateExtents()
vl.updateFields()    
QgsMapLayerRegistry.instance().addMapLayers([vl])

it will be

Update: Here's a version to try to avoid the memory errors from storing everything in a single list. Now, the features are added once per row. It's not as fast as the above method, but less memory hungry:

vl = QgsVectorLayer("Point?crs=", "temporary_points2", "memory")
vl.dataProvider().addAttributes([ QgsField("X_coord", QVariant.Int), QgsField("Y_coord", QVariant.Int), QgsField("Azimuth", QVariant.Int) ])

for row in range(0,rows):
   new_features = []
   for col in range(0,cols):
      rspnt = pixel2coord(col,row)
      fet = QgsFeature()
      fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(rspnt[0], rspnt[1])))
      fet.initAttributes(3)
      fet.setAttribute(0, rspnt[0] )
      fet.setAttribute(1, rspnt[1])
      az = pntxy.azimuth(QgsPoint(rspnt[0], rspnt[1]))
      if az < 0.0:
         azim = 360 + az
      else:
         azim = az
      fet.setAttribute(2, azim)
      new_features.append(fet)

   vl.dataProvider().addFeatures( new_features )
vl.updateExtents()
vl.updateFields()    
QgsMapLayerRegistry.instance().addMapLayers([vl])
added 1341 characters in body
Source Link
ndawson
  • 27.9k
  • 3
  • 62
  • 86

Update: Here's a version to try to avoid the memory errors from storing everything in a single list. Now, when the list has grown to the maximum possible size then the features will be added to the layer and the list cleared.

vl = QgsVectorLayer("Point?crs=", "temporary_points2", "memory")
vl.dataProvider().addAttributes([ QgsField("X_coord", QVariant.Int), QgsField("Y_coord", QVariant.Int), QgsField("Azimuth", QVariant.Int) ])

for row in range(0,rows):
   new_features = []
   for col in range(0,cols):
      rspnt = pixel2coord(col,row)
      fet = QgsFeature()
      fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(rspnt[0], rspnt[1])))
      fet.initAttributes(3)
      fet.setAttribute(0, rspnt[0] )
      fet.setAttribute(1, rspnt[1])
      az = pntxy.azimuth(QgsPoint(rspnt[0], rspnt[1]))
      if az < 0.0:
         azim = 360 + az
      else:
         azim = az
      fet.setAttribute(2, azim)
      try:
         new_features.append(fet)
      except:
         vl.dataProvider().addFeatures( new_features )
         new_features = []

vl.dataProvider().addFeatures( new_features )
vl.updateExtents()
vl.updateFields()    
QgsMapLayerRegistry.instance().addMapLayers([vl])

it will be

Update: Here's a version to try to avoid the memory errors from storing everything in a single list. Now, when the list has grown to the maximum possible size then the features will be added to the layer and the list cleared.

vl = QgsVectorLayer("Point?crs=", "temporary_points2", "memory")
vl.dataProvider().addAttributes([ QgsField("X_coord", QVariant.Int), QgsField("Y_coord", QVariant.Int), QgsField("Azimuth", QVariant.Int) ])

for row in range(0,rows):
   new_features = []
   for col in range(0,cols):
      rspnt = pixel2coord(col,row)
      fet = QgsFeature()
      fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(rspnt[0], rspnt[1])))
      fet.initAttributes(3)
      fet.setAttribute(0, rspnt[0] )
      fet.setAttribute(1, rspnt[1])
      az = pntxy.azimuth(QgsPoint(rspnt[0], rspnt[1]))
      if az < 0.0:
         azim = 360 + az
      else:
         azim = az
      fet.setAttribute(2, azim)
      try:
         new_features.append(fet)
      except:
         vl.dataProvider().addFeatures( new_features )
         new_features = []

vl.dataProvider().addFeatures( new_features )
vl.updateExtents()
vl.updateFields()    
QgsMapLayerRegistry.instance().addMapLayers([vl])

it will be

Source Link
ndawson
  • 27.9k
  • 3
  • 62
  • 86

Try this version (modified from gene's response):

vl = QgsVectorLayer("Point?crs=", "temporary_points2", "memory")
vl.dataProvider().addAttributes([ QgsField("X_coord", QVariant.Int), QgsField("Y_coord", QVariant.Int), QgsField("Azimuth", QVariant.Int) ])

new_features = []
for row in range(0,rows):
   for col in range(0,cols):
      rspnt = pixel2coord(col,row)
      fet = QgsFeature()
      fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(rspnt[0], rspnt[1])))
      fet.initAttributes(3)
      fet.setAttribute(0, rspnt[0] )
      fet.setAttribute(1, rspnt[1])
      az = pntxy.azimuth(QgsPoint(rspnt[0], rspnt[1]))
      if az < 0.0:
         azim = 360 + az
      else:
         azim = az
      fet.setAttribute(2, azim)
      new_features.append(fet)

vl.dataProvider().addFeatures( new_features )
vl.updateExtents()
vl.updateFields()    
QgsMapLayerRegistry.instance().addMapLayers([vl])

This version only calls addFeatures once, rather than once per pixel in the loop.