How to Generate WKT from Latitude-Longitude Coordinates Using Python

When working with geospatial data, you often encounter various data formats, one of which is the Well-Known Text (WKT). WKT is a text markup language for representing vector geometry objects such as points, lines, and polygons. In this article, we’ll explore how to create WKT strings from latitude-longitude coordinates using Python’s Shapely library.

I recently dove into the world of geospatial functions while documenting Apache Pinot’s geospatial capabilities, which sparked my interest in WKT. Here, we’ll break down the process step-by-step and learn how to use Shapely to generate WKT from a list of latitude-longitude coordinates.

Prerequisites Installing the Shapely Library

Before we get started, ensure you have Python installed on your system. To use Shapely, install it via pip:

codepip install shapely

This library makes it incredibly easy to work with geometric objects and convert them into WKT format.

Understanding Latitude-Longitude Coordinates

Let’s begin with a list of coordinates. Each pair represents a geographical point:

codelat_longs = [
(50.854457, 4.377184), # Brussels
(52.518172, 13.407759), # Berlin
(50.072651, 14.435935), # Prague
(48.853033, 2.349553), # Paris
(50.854457, 4.377184) # Brussels (Closing the polygon)
]

These points outline a simple polygon. For geospatial data, the first and last coordinates must be identical to create a closed polygon.

Creating a Polygon and Generating WKT

Using Shapely, we can turn these coordinates into a polygon object. Let’s see how it’s done:

Import the Necessary Module

Start by importing the Polygon class from the Shapely library:

codefrom shapely.geometry import Polygon

Create the Polygon Object

Pass the list of coordinates to the Polygon class to create a polygon object:

codepolygon = Polygon(lat_longs)

Convert the Polygon to WKT

Now that we have a polygon object, converting it to WKT is as simple as accessing its wkt property:

codeprint(polygon.wkt)

Output:

codePOLYGON ((50.854457 4.377184, 52.518172 13.407759, 50.072651 14.435935, 48.853033 2.349553, 50.854457 4.377184))

The output is a valid WKT representation of the polygon formed by our coordinates.

Why Use WKT?

WKT is widely used because it is:

  • Human-Readable: You can easily understand the structure of the geometry just by looking at the text.
  • Interoperable: Supported by many geospatial tools, databases, and programming languages.
  • Efficient: A lightweight and concise way to describe geometries.

Practical Applications

  • Storing Geometries in Databases: Databases like PostgreSQL with PostGIS extensions support WKT for geospatial queries.
  • Visualization: Convert WKT into geometries that can be rendered on maps.
  • Data Exchange: Share geospatial data between systems in a standardized format.

Advanced Generating WKT for Other Geometries

Shapely supports a variety of geometric shapes. Here’s how you can generate WKT for other types of geometries:

For a Point:

codefrom shapely.geometry import Point
point = Point(50.854457, 4.377184)
print(point.wkt)

Output:

codePOINT (50.854457 4.377184)

For a LineString:

codefrom shapely.geometry import LineString
line = LineString(lat_longs)
print(line.wkt)

Output:

codeLINESTRING (50.854457 4.377184, 52.518172 13.407759, 50.072651 14.435935, 48.853033 2.349553, 50.854457 4.377184)

Conclusion

In this guide, we’ve demonstrated how to generate a WKT string from a list of latitude-longitude coordinates using Python’s Shapely library. By following these steps, you can easily handle geospatial data and leverage WKT for storage, visualization, or data sharing.

Whether you’re processing geographic information for research, development, or data analysis, Shapely is a powerful tool that simplifies working with geometries.

Related blog posts