I would like to save x,y,z values for polygon vertices as a geoJSON file using Python in QGIS. I've made several attempts, but all have issues. I've found that the QgsPoint
object must be used as QgsPointXY
does not support Z and M values. Below is a code snippet which includes various tries and their corresponding error messages/issue:
for idx in range(n):
col = int(round((geom.vertexAt(idx).x() - extent.xMinimum()) / xres))
row = int(round((extent.yMaximum() - geom.vertexAt(idx).y()) / yres))
lat = round(self.ur_lat - (row * lat_res), 6)
lon = round(self.ll_lon + (col * lon_res), 6)
alt = computeAltFromLatLon
point=QgsPoint(lon, lat,alt)
lat_lon_points.append(point)
# Error: type object 'QgsGeometry' has no attribute 'fromPolygonZ'
# lat_lon_poly = QgsGeometry.fromPolygonZ([lat_lon_points])
# Error: type object 'QgsGeometry' has no attribute 'fromPolygon'
#lat_lon_poly = QgsGeometry.fromPolygon([lat_lon_points])
# Error: index 0 has type 'QgsPoint' but 'QgsPointXY' is expected
# QgsPointXY does not support Z and M values
#lat_lon_poly = QgsGeometry.fromPolygonXY([lat_lon_points])
# File is successfully written here, but only x,y is saved.
lat_lon_poly = QgsGeometry.fromPolyline([lat_lon_points[0],
lat_lon_points[1],lat_lon_points[2], lat_lon_points[3]])
fet.setGeometry(lat_lon_poly)
# print("Write to GeoJSON: ")
writer.addFeature(fet)
Any suggestions for how I can write a geoJSON file with x,y,z values for each coordinate?