While developing a workflow involving spatial data duplication, an analyst requested help with client requirements:
① Convert individual polygons to WKT format in TXT files
② Merge multiple polygons into MultiPolygon WKT format

1713798782717

Understanding WKT

Well-known text (WKT) is a text markup language standardized by the Open Geospatial Consortium (OGC) for representing:

  • Vector geometries (points, linestrings, polygons)
  • Coordinate reference systems
  • Coordinate transformations

Solution via QGIS Plugin

Step 1: Install Get WKT Plugin

  1. Navigate to: Plugins → Manage and Install Plugins...
  2. Search "WKT"
  3. Select "Get WKT" plugin
  4. Click Install Plugin

1713798782717

Step 2: Export Single Feature WKT

  1. Load polygon data
  2. Select target feature
  3. Execute: Plugins → Get WKT → Get WKT String
  4. Copy output to TXT

1713801163354

Limitation: Does not support multi-feature export

Python Script for MultiPolygon Conversion

# -*- coding: utf-8 -*-
import datetime
from osgeo import ogr
from shapely.geometry import MultiPolygon
from shapely.wkt import loads

if __name__ == '__main__':
    # Input/Output paths
    in_shapefile = r"./data-use/shp/Guangdong.shp"
    out_file = r'./results/Guangdong_' + datetime.datetime.now().strftime('%Y%m%d') + '.txt'
    
    # Process shapefile
    driver = ogr.GetDriverByName("ESRI Shapefile")
    dataSource = driver.Open(in_shapefile, 0)
    layer = dataSource.GetLayer()
    
    # Aggregate polygons
    polygons = []
    for feature in layer:
        geom = feature.GetGeometryRef().ExportToWkt()
        polygons.append(loads(geom))
    
    # Generate MultiPolygon WKT
    multi_poly = MultiPolygon(polygons)
    with open(out_file, 'w') as f:
        f.write(multi_poly.wkt)

Validation with QuickWKT Plugin

  1. Install "QuickWKT" plugin
  2. Paste generated WKT
  3. Verify unified MultiPolygon visualization

1713802611662