15

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 number 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?

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

5 Answers 5

11

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.

4
  • That does make sense and I'll have a chance to try it shortly.
    – Gregory
    Mar 19, 2013 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, 2013 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, 2013 at 6:48
  • 1
    +1 I took the liberty of further clarifying your edit by explicitly mentioning that the output will include the attributes.
    – whuber
    Mar 20, 2013 at 17:48
5

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.

1
  • 1
    The project link is broken. Feb 26, 2019 at 23:37
2

No plug is needed to convert shp to wkt. You just need to export "Save layer as" csv and select "AS_WKT" in the "Geometry" field and that's it. I'm sorry for my English.

1

There is also a tool from SAGA named "Export Simple Features to Well Known Text", that exports vector data to 'well known text' (WKT) simple features format.

tool

After export one can see the following:

export

0

Try this video: https://www.youtube.com/watch?v=N6WOzM00FLk

Not mine but helped me a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.