4

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
5

First, create a new text field as commented above with a high numbers of chararcters. Then use the Field Calculator to convert the geometry to WKT as described in the comments:

field calculator for new text field

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

enter image description here

1

Tested on QGIS 2.18 and QGIS 3.4

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.

input

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'.

output_1

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"

output_2

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.