3

I have shapefile representing roads as polylines, I want to add 1 arc degree to lat & lot of every point constructing each poly line:

from qgis.PyQt.QtCore import *

layer = iface.addVectorLayer("file_path.shp", "my_layer", "ogr")
iter = layer.getFeatures()
for feature in iter:
    geom = feature.geometry().asPolyline()
    new_geom = []
    for point in geom:
        lon = point[0]
        lat = point[1]
        lon = lon + 1  # or whatever calculation doesn't matter
        lat = lat + 1
        new_geom.append(QgsPoint(lon, lat))
    layer.dataProvider().changeGeometryValues({feature.id(): QgsGeometry.fromPolyline(new_geom)})
crs = layer.crs()
crs.createFromId(4326)
layer.setCrs(crs)
layer.beginEditCommand("edit")
layer.endEditCommand() 

When I load the shapefile after executing this script and print all the geoms, I see the editing took place.

However QGIS doesn't display the file (it's not a CRS issue) probably because in the properties->metadata of the file I see in the min/max values, old values before the editing took place.

What I'm doing wrong here?

3

Your code worked for me too. I think you should only apply a slight edit when importing the layer:

layer = QgsVectorLayer("file_path.shp", "my_layer", "ogr")

instead of the line you provided. Doing this, you will load the layer into QGIS, but it won't be shown in the Layers Panel until you will insert the following line at the end of the script:

QgsMapLayerRegistry.instance().addMapLayer(layer)

Applying these edits, your code will become:

from qgis.PyQt.QtCore import *

layer = QgsVectorLayer("file_path.shp", "my_layer", "ogr")
iter = layer.getFeatures()
for feature in iter:
    geom = feature.geometry().asPolyline()
    new_geom = []
    for point in geom:
        lon = point[0]
        lat = point[1]
        lon = lon + 1  # or whatever calculation doesn't matter
        lat = lat + 1
        new_geom.append(QgsPoint(lon, lat))
    layer.dataProvider().changeGeometryValues({feature.id(): QgsGeometry.fromPolyline(new_geom)})
crs = layer.crs()
crs.createFromId(4326)
layer.setCrs(crs)
layer.beginEditCommand("edit")
layer.endEditCommand()

QgsMapLayerRegistry.instance().addMapLayer(layer)
  • Still the same: It's not loading the file to the registry and when I import it manually it doesn't display + I see old values in the properties-metadata-min max values – michael Feb 12 '17 at 13:28
  • Hmm, I thinked your problem was about the displaying of the file after the editing of the geometries (in fact, your original code didn't show the changes until refreshing the canvas). With my edits I be able to directly see the edited layer, so I think it should be an answer to your question. Instead, about the issue of the wrong values in the metadata, I can't confirm your issue because I see a change in the min-max values. You may probably edit your question by adding more informations about this. – mgri Feb 12 '17 at 13:38

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.