What does WKT stand for?
WKT stands for Well-Known Text and is a text representation of geographic features, including points, lines, and polygons, used in GIS (Geographic Information Systems). WKT is widely used to describe the geometry of geographic features in vector data, as well as in many GIS software applications and web mapping APIs.
What is WKT data? and what is it used for?
WKT uses a standard syntax to represent the geometries, making it a human-readable and easy-to-use format for data exchange. For example, a point can be represented in WKT as: “POINT(x y)” where x and y are the coordinates of the point. Similarly, a polygon can be represented in WKT as: “POLYGON((x1 y1, x2 y2, x3 y3, …, x1 y1))”, where each pair of x and y values represents a vertex of the polygon.
The use of WKT makes it easy to share and exchange geographic information between different GIS software and platforms, making it a key component of the GIS landscape.
What are the types of WKT?
Here is a table showing WKT examples of various geometries, along with a description of each:
|Geometry Type||WKT Example||Description|
|Point||POINT(30 10)||A single point with x coordinate 30 and y coordinate 10|
|LineString||LINESTRING(30 10, 10 30, 40 40)||A line connecting three points with x and y coordinates (30 10), (10 30), and (40 40)|
|Polygon||POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))||A polygon with five vertices and an interior ring with x and y coordinates (30 10), (40 40), (20 40), (10 20), and (30 10)|
|MultiPoint||MULTIPOINT((10 40), (40 30), (20 20), (30 10))||A collection of four points with x and y coordinates (10 40), (40 30), (20 20), and (30 10)|
|MultiLineString||MULTILINESTRING((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))||A collection of two line strings, each connecting multiple points|
|MultiPolygon||MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))||A collection of two polygons|
|GeometryCollection||GEOMETRYCOLLECTION(POINT(10 40), LINESTRING(30 10, 10 30, 40 40), POLYGON((30 10, 40 40, 20 40, 10 20, 30 10)))||A collection of point, linestring, and polygon geometries|
|Point ZM||POINT ZM (1 2 3 4)||A point with x, y, z, and m values of 1, 2, 3, and 4, respectively|
|Point M||POINT M (1 2 3)||A point with x, y, and m values of 1, 2, and 3, respectively|
Note: The x and y values in these examples are just for illustration purposes and don’t represent real-world coordinates.
WKT for Coordinate systems
Also known as “WKT for CRS” (Coordinate Reference System), is a text representation of coordinate system definitions that can be used to exchange information between GIS software and other systems. Note that a coordinate system is not sorted within the WKT definition of the geometry!
GEODCRS["WGS 84", DATUM["World Geodetic System 1984", ELLIPSOID["WGS 84", 6378137, 298.257223563, LENGTHUNIT["metre", 1]]], CS[ellipsoidal, 2], AXIS["Latitude (lat)", north, ORDER], AXIS["Longitude (lon)", east, ORDER], ANGLEUNIT["degree", 0.0174532925199433]]
Creating WKT in GIS software
The process for creating WKT in GIS software depends on the specific software you are using. Here are some general steps for creating WKT in some common GIS software:
- Start QGIS and open a new map document.
- Add any necessary data layers to the map view.
- Right-click on a layer and select “Save As.”
- In the “Save Vector Layer As” dialog box, select “Comma Separated Values” as the format.
- In the “Geometry” section, select “Well Known Text (WKT)” as the “Type” option.
- Click “OK” and QGIS will create a new .csv file with the WKT representation of the layer’s geometries.
- Start ArcGIS and open a new map document.
- Add any necessary data layers to the map view.
- Right-click on a layer and select “Data” > “Export Data.”
- In the “Export Data” dialog box, select “Well Known Text (WKT)” as the format.
- Choose a location to save the WKT file and click “OK.”
- ArcGIS will create a new .txt file with the WKT representation of the layer’s geometries.
PostGIS and WKT
PostGIS is a spatial database extension for the PostgreSQL database management system, and it provides support for storing, querying, and manipulating geographic data. Here are the steps to create WKT in PostGIS:
- Connect to a PostgreSQL database with PostGIS enabled.
- Create a table to store the WKT data:
CREATE TABLE wkt_data ( id serial PRIMARY KEY, geom geometry(Point, 4326), name text );
- Insert some WKT data into the table:
INSERT INTO wkt_data (geom, name) VALUES (ST_GeomFromText('POINT(30 10)', 4326), 'Point 1'), (ST_GeomFromText('POINT(40 20)', 4326), 'Point 2');
- Select the WKT representation of the geometries:
SELECT ST_AsText(geom), name FROM wkt_data;
This will return the following output:
POINT(30 10) | Point 1 POINT(40 20) | Point 2
Note that the
ST_AsText functions are used to convert between WKT and PostGIS geometry data types. The first argument to
ST_GeomFromText is the WKT representation of the geometry, and the second argument is the SRID (Spatial Reference ID) of the coordinate system used for the geometry. The
ST_AsText function returns the WKT representation of a PostGIS geometry.
Ogr2ogr and WKT
ogr2ogr is a command line tool that is part of the GDAL (Geospatial Data Abstraction Library) library. It can be used to convert between various vector data formats, including WKT. Here are the steps to create a WKT file using ogr2ogr:
- Open a command prompt or terminal window.
- Navigate to the directory where you want to create the WKT file.
- Run the following command to create a WKT file from a source vector data file (e.g. Shapefile, GeoJSON, etc.):
ogr2ogr -f "CSV" output.csv input.shp -lco GEOMETRY=AS_WKT
This will convert the source vector data file (input.shp) to a comma-separated values (CSV) file (output.csv) with the geometries represented as WKT. The
-f option specifies the output format, and the
-lco option sets a layer creation option, in this case to specify that the geometries should be represented as WKT.
Note that the input and output file formats can be different, and ogr2ogr supports many different vector data formats. You can refer to the ogr2ogr documentation for more information on the supported formats and options.
limitations to using WKT compared to other geospatial formats
Yes, there are some limitations to using WKT compared to other geospatial formats. While WKT is human-readable and widely supported, it has certain drawbacks that can make other formats more suitable for specific use cases. Some of these limitations include:
- Size and verbosity: WKT can be quite verbose, especially when representing complex geometries with numerous coordinates. This can result in larger file sizes compared to more compact formats like Well-Known Binary (WKB) or binary formats like Shapefiles.
- Lack of support for non-spatial attributes: WKT only represents geometries and doesn’t store non-spatial attributes associated with those geometries. Formats like GeoJSON, Shapefiles, or KML allow for the inclusion of properties or attributes alongside the spatial data.
- No coordinate reference system (CRS) information: WKT doesn’t inherently include information about the coordinate reference system, which is crucial for accurate spatial analysis and projections. This means you need to keep track of the CRS separately. In contrast, formats like Shapefiles or GeoJSON can store CRS information within the file itself.
- Limited support for advanced geometries: WKT is suitable for representing basic geometric types, but it may not be suitable for more advanced types or features, like curves or 3D geometries, which are supported by other formats like GML.
- Less suited for web applications: Due to its verbose nature and lack of support for non-spatial attributes, WKT is not as well-suited for web applications as formats like GeoJSON or TopoJSON, which can be easily parsed and rendered by modern web mapping libraries like Leaflet or Mapbox GL.
These limitations should be taken into account when deciding whether to use WKT or another geospatial format for a specific project or application. The choice will depend on factors such as data complexity, storage and transmission efficiency, and compatibility with the tools and libraries you plan to use.
How can I validate if my WKT data is correctly formatted?
To validate if your WKT data is correctly formatted, you can use geospatial libraries or online tools that can parse WKT and check for any syntax or structural errors. Here are a few ways to validate your WKT data:
Using Python and the Shapely library:
from shapely import wkt def validate_wkt(wkt_string): try: geometry = wkt.loads(wkt_string) return True except Exception as e: print("Error:", e) return False wkt_data = "POINT (30 10)" result = validate_wkt(wkt_data) if result: print("WKT is valid.") else: print("WKT is not valid.")
There are several online tools where you can paste your WKT data and check for validity. One such tool is Wicket (https://arthur-e.github.io/Wicket/), which also allows you to visualize the geometries on a map.
Desktop GIS software:
GIS software like QGIS or ArcGIS can import and display WKT data. If your WKT data is correctly formatted, it should be successfully imported and visualized in the GIS software.
Remember that while these methods can help you identify formatting or syntax errors in your WKT data, they may not detect issues related to the coordinate reference system or the semantics of your spatial data. It’s essential to understand the context and nature of your spatial data to ensure its accuracy and consistency.