How can I get the wkt from a geometry object in python? I'd like to be able to do something like the following:

from qgis.core import *
import qgis.utils
layer = qgis.utils.iface.activeLayer()
for feat in layer.getFeatures():
    geom = feat.geometry()
    #print something meaningful about the geom object

I've checked dir(geom), but there doesn't seem to be an option to get a wkt.


The WKt can be obtained from geom as follows: your_string = geom.exportToWkt()


In QGIS 3 there was a backwards incompatible change about this:

exportToWkt() was renamed to asWkt()

So now it is: your_string = geom.asWkt()

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.