2

I went through different Stack Overflow questions, tutorials and the official PyQGIS cookbook. Somehow I cannot make a basic manipulation of the geometry of a layer's features.

This is the code I try to run. All that happens is that the layer is loaded into the project totally unchanged and the print(new) gives me the old coordinate values (but the print(geometry()) inside the loop gives me the correct new geometry). Is it because it is a .kmz file?

import os 
from qgis.core import (
    QgsVectorLayer
)

layer = QgsVectorLayer("/my_layer.kmz", "myLayer", "ogr")
layer.startEditing()

for f in layer.getFeatures():
    id = f.id()
    geom = f.geometry()
    geom.translate(10,10)
    f.setGeometry(geom)
    layer.dataProvider().changeGeometryValues({ id : geom })
    print(f.geometry())

layer.commitChanges()

#Check if new geometry was written into layer
for f in layer.getFeatures():
    new = f.geometry()
    print(new)

QgsProject.instance().addMapLayers([layer])

I am using QGIS 3.16

2
  • Did you try the same code using a "/my_layer.shp" instead of "/my_layer.kmz" after saving kmz to shp to confirm the issue is with the kmz format not elsewhere?
    – ThomasG77
    Jun 14, 2021 at 12:12
  • You may have to export your layer as a shapefile or geopackage so that it will allow you to edit it. I have not edited a kmz in the past.
    – Cary H
    Jun 14, 2021 at 14:08

1 Answer 1

1

You code is not working because of your input data format.

I've tested your code is working with shapefile and GPKG but it does not work with KML and KMZ files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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