QGIS – How to Show WKT from Attribute Table

attribute-tableMySQLqgiswell-known-text

I have a dataset (from mysql) which I can load into QGIS. One of the columns in the table (and thus in the attribute table) is a WKT column. I am not able to select this column for the geometry.

The WKT looks like this: LINESTRING Z (101438.727 426681.927 -0.555, 101428.442 426687.427 -0.539). What I would like to do is to tell the vector layer to show all line strings based on the WKT in the attribute table.

All I can find was how to get geometries inside the attribute table, but I would like to do it the other way around.

I tried to create a geometry column, but that returns only NULL in my attribute table (and I still don't know how to tell QGIS to look in the attribute table of a geometry object)

What I use:

 geom_from_wkt(  "WKT" )

example below shows <geometrie: LineStringZ> but the attribute table shows NULL.

EDIT

I have a mysql table containing multiple columns and one of them is a WKT column. When I load the table using layer - add layer - vector layer I choose Database and create my connection.

At the moment a new layer is created with my whole mysql table as attribute table, and the layer has no geometry.

I would like to "tell" QGIS that the information regarding the geometry is inside the attribute table, in the column WKT (that is also the name of the column). What I would like to do is to display my spatial information (from mysql) inside QGIS.

The reason I am using text instead of mysql geometries is because the mysql geometries do not support 3D information (like LINESTRING Z).

Best Answer

If it's just for visualization you could use the geometry generator with the same expression:

geom_from_wkt("WKT")

To set it up you may duplicate your layer (so you will get one layer with the original geometry and the copy with the geometry from the WKT) or add a new symbol layer to display both geometry in original layer. Then in the Layer styling panel use the below setting (Symbol layer type : Geometry Generator ; Geometry type : Point/Multipoint):

Alternatively you can use the processing tool "Geometry by expression" (using again the same expression) but this will create a new layer so change on the original layer wont be reflected in this.

enter image description here


EDIT : If you have no geometry in the original layer the best solution is to create a virtual layer based on your table where your SQL query generate the geometry from the WKT column, this way you get a layer that will stay synchronized with the original data.

You may adapt this to get the layer (replace : "Field &" , ... , "Field n" by the name of the field you want to get and mysql_TABLE by the name of your table)

SQL query :

SELECT "Field &" , ... , "Field n" , st_geomfromtext( "WKT") as geometry FROM mysql_TABLE

enter image description here

Related Question