[Qgis-developer] grab Z value

Paulo Pires paulopires16 at gmail.com
Sat Sep 26 06:15:38 PDT 2015


Hello, see my code, please. I intend to maintain Z value on shapefile, so:
I write first on canvas... even I give the Z value, it will write Z = 0.0,
ok...

feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromWkt("LINESTRING (151620 319350 10, 151710
319350 10)"))
layerCanvas.dataProvider().addFeatures([feat])
layerCanvas.commitChanges()
self.iface.mapCanvas().refresh()

So, for grab the Z value, I go to the shapefile and update Z...

driver = ogr.GetDriverByName('ESRI Shapefile')
dataSource = driver.Open(myfilepath, 1)
layer = dataSource.GetLayer()

oldGeom = ogr.CreateGeometryFromWkt("LINESTRING (151620 319350 0, 151710
319350 0)")
newGeom = ogr.CreateGeometryFromWkt("LINESTRING (151620 319350 10, 151710
319350 10)")

# write shapefile
for feature in layer:
	if feature.GetGeometryRef() is not None:
		geom = feature.GetGeometryRef()

		if geom.Distance(oldGeom) <= 0.1:

			# delete oldGeom
			featureId = feature.GetFID()
			layer.DeleteFeature(featureId)
			
			# create newGeom
			feature.SetGeometry(newGeom)
			feature.SetFID(featureId)                    
			layer.CreateFeature(feature)
			
			feature.Destroy()
			
			msg = "+++++++++++++++"
			# refresh
			self.getShpData()
			self.iface.mapCanvas().refresh()
			break
		   
dataSource.Destroy()

In canvas, I see the right Z :)
But when I edit something, I lost that geometry...
In point geometry, that's all ok, but in linestring, it disappears...
Thanks



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/grab-Z-value-tp5226284.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.


More information about the Qgis-developer mailing list