Creating a QgsGeometry
using a WKT/WKB string is a fairly standard procedure using pyqgis:
wkt = 'MULTIPOLYGON (((0 0, 1 1, 1 0, 0 0)))'
wkb = '0106000000010000000103000000010000000400000000000000000000000000000000000000000000000000f03f000000000000f03f000000000000f03f000000000000000000000000000000000000000000000000'
geometry_wkt = QgsGeometry.fromWkt(wkt)
print(geometry_wkt) # <QgsGeometry: MultiPolygon (((0 0, 1 1, 1 0, 0 0)))>
geometry_wkb = QgsGeometry()
geometry_wkb.fromWkb(bytes.fromhex(wkb))
print(geometry_wkb) # <QgsGeometry: MultiPolygon (((0 0, 1 1, 1 0, 0 0)))>
But now I have data in the extended WKT/WKB format which also embeds a spatial reference identifier. I could't find a way to create a QgsGeometry
from such a string. If I just use the same as above I always get <QgsGeometry: null>
as result.
ewkt = 'SRID=4326;POLYGON((0 0,1 0,1,0 1,0 0))'
ewkb = '0103000020E6100000010000000500000000000000000000000000000000000000000000000000F03F0000000000000000000000000000F03F000000000000F03F0000000000000000000000000000F03F00000000000000000000000000000000'
geometry_ewkt = QgsGeometry.fromWkt(ewkt)
print(geometry_ewkt) # <QgsGeometry: null>
geometry_ewkb = QgsGeometry()
geometry_ewkb.fromWkb(bytes.fromhex(ewkb))
print(geometry_ewkb) # <QgsGeometry: null>
Is there any way to create those geometries using pyqgis API?
If not, I probably could extract the relevant wkt/wkb parts from the extended representation. I guess for EWKT it would be just dropping everything until the ;
. But what do I do to convert EWKB string to WKB string?