1

I am trying to create some code to generate 10 random points within a vector layer's districts:

layer=iface.activeLayer()
import ogr
import sys
import random
from shapely import wkb, geometry

Moz_no=layer.featureCount()

for i in xrange(Moz_no):
  district_tmp=layer.getFeatures(i)
  district=wkb.loads(district_tmp.GetGeometryRef().ExportToWkb())

  random_point_count=0

  sys.stdout.write("\r")
  sys.stdout.flush()

  while random_point_count<11:
    maxx, maxy, minx, miny = district.bounds
    random_point=geometry.Point(minx_+(random.random()*(maxx-minx)), (miny+ (random.random()*(maxy-miny))))

However, when I run the code, it gives me this error:

TypeError: QgsVectorLayer.getFeatures(QgsFeatureRequest request=QgsFeatureRequest()): argument 1 has unexpected type 'int'

Could someone please help me, and explain what is going on?

1

The link @underdark used in the comment should give you plenty to review. However, to address your error, the getFeatures command does not take an integer as a parameter. It takes a QgsFeatureRequest object. See the documentation for more details.

Instead of using an index, try something like:

for district_tmp in layer.getFeatures():

There's no need to get the count of features, enumerate through an xrange, and then select the feature by index. Just enumerate through the features.

(Untested code so I might be missing something fundamental in your problem.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.