2

I am using QGIS Desktop 2.5.0-Master with GDAL/OGR 1.11.0 on a windows 7.

I am working on a little pyQGis script that should modify the z-coordinate of points in a vector shapefile based on a value read in a raster (DEM). I really want to edit the geometry and not to create/update a feature attribute that hold the z-coordinate.

When executing the code below, the execution of the script goes through without throwing any error, but it seems that the changes to the wkb geometry are not committed...

I went then into the QGIS python console to directly check the outputs of my for loop on the selected features in the vector layer. What I saw is that the call to "geom_ogr.SetPoint(0,newX,newY,newZ)" was really resulting in a change of the geom_ogr.GetX(), geom_ogr.GetY() and geom_ogr.GetZ() values. However, once out of the for loop, I can't commit those changes in the shapefile...

Is there a reason for not being able to write permanently new geometry information in a point vector layer ??

--------PLUGIN CODE---------

vector_layer=vector ->definition of a parameter used in the GUI proposed by QGIS for tools
raster_layer=raster ->definition of a parameter used in the GUI proposed by QGIS for tools

from qgis.core import *
from PyQt4.QtCore import *
from osgeo import ogr

vector_path = vector_layer
raster_path = raster_layer

vector = QgsVectorLayer(vector_path,"vector_lyr","ogr")
if vector.isValid()!=True:
    print("impossible de charger la couche vecteur")

raster = QgsRasterLayer(raster_path,"raster_lyr")
if raster.isValid()!=True:
    print("impossible de charger la couche raster")

selection = QgsFeatureRequest()
selection.setFilterRect(raster.extent())

vector.startEditing()

for f in vector.getFeatures(selection):
    wkb = f.geometry().asWkb()
    geom_ogr = ogr.CreateGeometryFromWkb(wkb)
    altitudeDic raster.dataProvider().identify(QgsPoint(f.geometry().asPoint().x(),f.geometry().asPoint().y()),QgsRaster.IdentifyFormatValue)
    z = altitudeDic.results()[1]
    geom_ogr.SetPoint(0,geom_ogr.GetX(),geom_ogr.GetY(),z)

 vector.commitChanges()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.