2

Curious if the following code has a blatant error that has been overlooked with respect to rotating a single polygon feature.

The *.geometry().rotate(...) returns a '0', indicating it was successful - yet the display and data-source do not depict any rotation; even after reload / refresh of layer, legend, and canvas.

Either the problem is in syntax, or maybe (because this is a Dev release) a not yet fully operational function?

inputShapeRef.startEditing()
featureSearch = QgsFeatureRequest().setFilterFid(int(inputShapeID))
featureRef = inputShapeRef.getFeatures(featureSearch)
for feature in featureRef:
    feature.geometry().rotate(self.MaxRotation,self.baseCentroid)
inputShapeRef.commitChanges()

UPDATE: Single line of code was missing, and the following is the updated script. Sucessfully rotates the shape to the desired angle; quite rapidly!

    inputShapeRef.startEditing()
    featureSearch = QgsFeatureRequest().setFilterFid(int(inputShapeID))
    featureRef = inputShapeRef.getFeatures(featureSearch)
    for feature in featureRef:
        feature.geometry().rotate(self.MaxRotation,self.baseCentroid)
        inputShapeRef.changeGeometry(feature.id(),feature.geometry())  # <=== + = success
    inputShapeRef.commitChanges()

FINAL THOUGHTS / QUESTIONS:

Does anyone know if this functionality carries over to an entire layer, the layers extent, or selection of multiple features? The SAGA translate processing function fulfills this need - but is data-thirsty. The low data-overhead and rapid return of this new single feature rotation would be highly beneficial for an entire layer or selection of multiple features therein.

For example, say a point array needs to be rotated in sync with an associated bounding polygon - rather then rotate each point individually, is there a means of rotating the entire array simultaneously without creating a new dataset?

  • I would probably send this as a bug report as either of your reasons could very well be correct. – Joseph Feb 16 '15 at 10:09
  • No bug (fortunately), just missing a line of code - as Luigi kindly shared below. Updated question to reflect these changes. – Katalpa Feb 16 '15 at 13:02
4

Your are not modifying vector, but features "copies" get with getFeatures. You need to get the feature.id() and updateGeometry using dataProvider of the vector or directly using methods of the vector layer (inside an editing session)

you can find reference of the methods in: for QgsVectorDataProvider: virtual bool changeGeometryValues (QgsGeometryMap &geometry_map) (you have references how to do in pyqgis cookbook) After have to triggerRepaint

for QgsVectorLayer: bool changeGeometry (QgsFeatureId fid, QgsGeometry *geom) after commitChanges, no need of triggerRepaint

regards, Luigi Pirelli

  • Belle indizi! Updated original question with missing lines of code. – Katalpa Feb 16 '15 at 13:00

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.