Geographic Information Systems Asked on April 10, 2021
I can rotate polygon features in a shapefile using:
vlayer = iface.activeLayer()
vlayer.startEditing()
for ft in vlayer.getFeatures():
geom = ft.geometry()
pt = ft.geometry().centroid().asPoint()
geom.rotate(45, pt)
vlayer.dataProvider().changeGeometryValues({ 0 : geom })
vlayer.commitChanges()
But if I use this on a memory layer, nothing happens. True
values are printed on the console but the features are not rotated, even after refreshing the canvas and repainting the layer.
Is there a way to do this for memory layers?
A recipe for your intent (deduced from answer Qgis 2.7 (Dev) feature rotation?)
rotation = 45
vlayer = iface.activeLayer()
provider = vlayer.dataProvider()
couples_id_geom = []
for feature in vlayer.getFeatures():
geom = feature.geometry()
centroid = feature.geometry().centroid().asPoint()
geom.rotate(rotation, centroid)
# accumulate args to avoid rotation feature by feature
couples_id_geom.append([feature.id(), geom])
# Change the layer features rotation in one go
provider.changeGeometryValues({
couple_id_geom[0]: couple_id_geom[1] for couple_id_geom in couples_id_geom
})
# Refresh to see the changes
vlayer.triggerRepaint()
Correct answer by ThomasG77 on April 10, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP