# How to convert a shapefile to WKT?

I'm trying to convert a shapefile into the simple Well Known Text (WKT) format in the example below (from http://en.wikipedia.org/wiki/Well-known_text).

``````MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),
((20 35, 45 20, 30 5, 10 10, 10 30, 20 35),
(30 20, 20 25, 20 15, 30 20)))
``````

I know that I use QGIS to save the shapefile as a CSV, and I can use ogr2ogr to do any numer of conversions. In fact `ogr2ogr -f CSV out.wkt source.shp -lco GEOMETRY=AS_WKT` gets me as close as I've come so far, but not quite there. Any suggestions?

p.s. As noted by Mapperz, this thread is very similar to this one. That thread, while it provided the ogr2ogr approach that I noted above, did not solve this particular challenge. The ogr2ogr output I'm getting looks like this (I've truncated the lines). Apparently, I just haven't figured out how to get ogr2ogr to use multipolygon.

``````WKT,AREA,PERIMETER,PINUPOND_,PINUPOND_I,CODE
"POLYGON ((-120.630531650950061 50.838562484449184, . . .
"POLYGON ((-123.206067372148397 51.038984074378327, . . .
``````
• – Mapperz Mar 19 '13 at 15:10
• What is the problem with the ogr2ogr output? – til_b Mar 19 '13 at 15:22
• @til_b: addressed in edit. – Gregory Mar 19 '13 at 15:40
• Is your shape MULTIPOLYGON? – Jürgen Zornig Mar 19 '13 at 16:04
• @JürgenZornig: How can I tell? It would make sense for it to be a multipolygon, but I'd like to verify that. – Gregory Mar 19 '13 at 17:34

Well Known Text is not meant for saving layers like shape files that consists of many objects. WKT defines how to represent geometry of one object. That geometry could be single or multi part. Multi part geometries mean that geometry of one object consists of many parts. For example Hawaiian Islands could be represented as one object but it consists of many geometries.

Shape file is a collection of these kinds of objects. Objects usually have also some attribute information that is also part of a shape file.

Your ogr2ogr approach converts these objects to csv format with WKT geometry (and text representations of the attributes). If you really want to combine all the geometries to one MULTIPOLYGON you have to first convert all the objects to one multi geometry. You can do that in QGIS. Select from Vector menu Geometry Tools and then Singleparts to Multiparts. Then you can convert that to wkt with ogr2ogr.

Hopefully this makes sense.

• That does make sense and I'll have a chance to try it shortly. – Gregory Mar 19 '13 at 16:09
• Okay, in QGIS, I used Vector/Geometry Tools/Single parts to multipart. I saved the output as a new shapefile, but when I loaded that file into QGIS it appears blank and the attribute table is completely empty (perhaps void would be a better description, as there are no headers, either). – Gregory Mar 19 '13 at 17:30
• Did you @pyrogerg select any unique field when the tool requested that? That means the tool "groups" all features that have the same value in that field. If you don't have any field that you want to unique you can just create one with Field calculator. You can access that from the attribute table. Make sure that the layer is in edit mode (pen icon). Then select the Field Calculator and "calculate" for example value 1 to every row of a new field. – LauriK Mar 20 '13 at 6:48
• +1 I took the liberty of further clarifying your edit by explicitly mentioning that the output will include the attributes. – whuber Mar 20 '13 at 17:48

I wrote a QGIS plugin that saves immediately the active layer to a .wkt file.

The name of the plugin is "WktExport". You can search for it in the default QGIS plugin panel.

BTW, the link to the plugin page is this:

https://plugins.qgis.org/plugins/wktexport/

"WktExport" plugin for QGIS creates a folder, named "WktExport" where the original SHP is, and insert in the folder a .wkt file containing the elements in WKT ascii format of all objects of SHP (but also KML, or other original file format).

The name of the .wkt has inside the date and time of saving, so saving it many times, you will have many versions of the same file.

This could be useful to create animations of a carthographic working progress or to document the current status of a file.

Please, send a message to the list qgis-developer, especially if you find wktexport useful.