Hands On

What Is a WKT File and how to Create One?

By Sayna Parsi | 11 May 2020

Well-known text (WKT) is a human readable representation for spatial objects like points, lines, or enclosed areas on a map. I came across WKT files as I was getting started with a Geofencing project. With WKT, you can specify an area that’s interesting to you and then see if some geolocation information like a lat/long pair from a mobile device falls into that area.

Understanding WKT point formatting

WKT formats points in (long, lat) order.

This was surprising to me as I had previously seen geographical points in the (lat, long) order, so I looked into why WKT formats points the other way around. The reason WKT has such order is that latitude represents north/south-ness of a point and longitude represents east/west-ness of a point. So really, WKT is trying to refer to a point by its (x,y) coordinate, and that translates to (long, lat). GeoJSON follows the same order as WKT in this case, so if you are familiar with GeoJSON, this might not be news for you.

Take this following point:

{ lat: 37, lng: -121 }

Latitudes represent the “y” value of the point, and longitudes represent the “x” value. Therefore, when we translate this point to WKT representation, it’ll look like the following:

POINT (-121 37)

How to create a WKT file

There are many applications like QGIS that let you create WKT files. But I will assume you only have access to your laptop laptop right now and you want to create a WKT file from scratch - which is absolutely possible!

All you need to do is create a new file, and add the wkt extension to it. For example I will call mine bermuda.wkt. If you want to do this from the command line you could say:

touch bermuda.wkt

Bermuda Triangle is a mythical region where a number of aircrafts and ships are said to have disappeared under mysterious circumstances. The exact coordinates of it are loosely defined, but for this example I will make a guess for where this mythical triangle might be. In order to add the Bermuda Triangle to your WKT file, you can open your file in a text editor like TextEdit or Atom. You can see below that I have listed four points to describe my triangle. The reason for having four points actually has nothing to do with this triangle’s mysterious history. The last point is a duplicate of the first point in order to tell WKT that our polygon is an enclosed area. When creating a polygon, you want to have your longitudes and latitudes separated by a space and you want different points comma separated. Finally, you want to wrap your points in a pair of parentheses because WKT likes things that way. If you are looking for a visual tool that helps you create your geometric shapes, Wicket might come in useful. 

POLYGON ((-80 25, -65 18, -64 32, -80 25))

If you get started with HERE APIs and decide to add more attributes to your geocoordinates, you can specify them like the following. Just make sure that they are tab delimited, or your layer won’t work. So here, I’m giving my triangle a name before I upload it as a layer.

NAME        WKT
BERMUDA    POLYGON ((-80 25, -65 18, -64 32, -80 25))

You can create multiple polygons too:

NAME        WKT
TwoTriangles    MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),
((15 5, 10 20, 5 10, 15 5)))

Polygons and Points aren’t the only things you can specify with WKT, if you are interested in learning more, please refer to Wikipedia. Have fun mapping and if you think I have incorrectly specified the Bermuda Triangle’s coordinates, feel free to reach out.