I have a shapefile with polygons and want to add an attribute field with the WKT in QGIS 3.4.

The function geom_to_wkt in the Field Calculator isn't working and I don't know how to do it. Any ideas?

  • 3
    Welcome to GIS SE ! Have you tried to update with : geom_to_wkt( $geometry ) ? – J. Monticolo Sep 25 '19 at 12:11
  • 2
    Make sure you use a string field type with a high length. – Joseph Sep 25 '19 at 12:13
  • I have complex polygons with long wkt. How do I know how big the new field "wkt" has to be?? With 254 length most of the wkt are incomplete. – Elena Lopez Sep 26 '19 at 5:55
  • 1
    Just use an other format gpkg for example. Shapefile is limited to 254 characters, no way to change this... – YoLecomte Mar 24 '20 at 16:21

You need to both (i) create a new string attribute with a high character limit and (ii) use the field calculator to convert the geometry to its WKT representation.

Use the field calculator as shown in the screenshots below to do this (select what is here "Neues Feld anlegen" instead of "Vorhandenes Feld erneuern" to create a new attribute for the results of the calculation):

field calculator for new text field

The field will hold the geometry as seen in the preview above:

enter image description here

With Python/PyQGIS, the same approach would look as follows:

params = {
'INPUT' : <input layer / file path for input layer>,
'FIELD_TYPE' : 2, # String
'FIELD_LENGTH' : 100, # String character limit
'NEW_FIELD' : 1, # 1 = new field, 0 = existing field
'FORMULA' : 'geom_to_wkt( $geometry )', # formula
'OUTPUT' : <file path for output layer>
processing.run('qgis:fieldcalculator', params)

I can suggest using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer...

Let's assume there is a polygon layer 'districts' with its attribute table accordingly, see image below.


With the following Query by means of QGIS native geometry function geom_to_wkt(), it is possible to achieve the result

SELECT *, geom_to_wkt(geometry) AS wkt
FROM "districts"

The output Virtual Layer will maintain original geometry and all initial attributes including a new, namely 'wkt'.


The alternative query will contain ST_AsText() parameter.

It returns the Well-Known Text representation of the geometry/geography. Optional argument may be used to reduce the maximum number of decimal digits after floating-point used in the output (defaults to 15).

SELECT *, ST_AsText(geometry) AS wkt
FROM "districts"


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.