QGIS script doesn't commit attribute changes

المشرف العام

Administrator
طاقم الإدارة
I have a script to generate random points within a polygon, and to give the points the attributes of their host polygon. It generates points, but for some reason it won't commit the changes to the attributes. The weird thing is that the output while running the script clearly shows the feature it is adding has the attributes, but when it's done I'm left with a bunch of featureless points with only an ID.

I have a feeling it's something fundamental I'm missing in committing the attributes.

import random# Prepare new temporary editable memory layerpointLayer = iface.addVectorLayer("Point?crs="+source.crs().toWkt(), "random_points", "memory")founter = -1for f in source.getFeatures(): # Create bounding box from polygon bounds = f.geometry().boundingBox() attributes = f.attributes() xmin = bounds.xMinimum() xmax = bounds.xMaximum() ymin = bounds.yMinimum() ymax = bounds.yMaximum() for i in range(1): inBounds = False while not inBounds: # generate random point 'p' within feature bounds xRandom = xmin + (random.random() * (xmax-xmin)) yRandom = ymin + (random.random() * (ymax-ymin)) p = QgsPoint(xRandom,yRandom) pGeom = QgsGeometry.fromPoint(p) # check if new point is contained in poly # if within bounds, add attributes and exit loop if f.geometry().contains(pGeom): pointFeature = QgsFeature() pointFeature.setGeometry(pGeom) print('source atts: ') print(attributes) pointFeature.setAttributes(attributes) print('point atts: ') print(pointFeature.attributes()) pointLayer.dataProvider().addFeatures([pointFeature]) inBounds = True

أكثر...
 
أعلى