Spatial data interoperability is crucial in modern GIS workflows. Whether you're importing data from external sources, exporting results for visualization, or integrating with web applications, understanding geometry input/output formats is essential. PostGIS supports a comprehensive range of formats, making it easy to work with spatial data across different platforms and applications.
Why Do Geometry Formats Matter?
Geometry I/O formats allow spatial data to be read, stored, and shared across different systems and applications. Whether you're exporting a dataset to a web application, visualizing data in QGIS, processing data in Python, or sharing results with stakeholders, choosing the right format ensures compatibility and efficiency.
PostGIS supports industry-standard formats that enable seamless integration with virtually any GIS software, web mapping platform, or spatial analysis tool.
Well-Known Text (WKT) - Human-Readable Format
WKT is a text-based format that represents geometries in a human-readable way. It's perfect for debugging, manual data entry, and educational purposes.
Creating and Reading WKT
-- Convert geometry to WKT
SELECT ST_AsText(geom) AS wkt_geometry
FROM landmarks;
-- Create geometry from WKT
SELECT ST_GeomFromText('POINT(77.59 12.97)', 4326) AS point_geom;
-- More complex geometries
SELECT ST_GeomFromText('LINESTRING(77.58 12.96, 77.60 12.98, 77.62 13.00)', 4326);
SELECT ST_GeomFromText('POLYGON((77.58 12.96, 77.60 12.96, 77.60 12.98, 77.58 12.98, 77.58 12.96))', 4326);
Geometry Type | WKT Example | Use Case |
---|---|---|
Point | POINT(77.59 12.97) |
Single locations |
LineString | LINESTRING(77.58 12.96, 77.60 12.98) |
Paths, routes |
Polygon | POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)) |
Areas, boundaries |
MultiPoint | MULTIPOINT((0 0), (1 1)) |
Multiple locations |
MultiPolygon | MULTIPOLYGON(((0 0,1 0,1 1,0 1,0 0)),((2 2,3 2,3 3,2 3,2 2))) |
Complex areas |
WKT Advantages and Use Cases
- Human-readable: Easy to understand and debug
- Manual editing: Can be typed or edited by hand
- Educational: Great for learning spatial concepts
- Copy-paste friendly: Easy to share between tools
- SQL-friendly: Works well in database queries
Well-Known Binary (WKB) - Compact Binary Format
WKB is a binary format that provides compact storage and fast processing. It's ideal for internal storage and network transmission.
-- Convert geometry to WKB (hexadecimal representation)
SELECT ST_AsBinary(geom) AS wkb_geometry,
encode(ST_AsBinary(geom), 'hex') AS wkb_hex
FROM landmarks;
-- Create geometry from WKB
SELECT ST_GeomFromWKB('\x0101000020E6100000000000000000F03F0000000000000040');
-- Using hex-encoded WKB
SELECT ST_GeomFromWKB(decode('0101000020E6100000000000000000F03F0000000000000040', 'hex'));
Format | Size | Speed | Human Readable | Best Use |
---|---|---|---|---|
WKT | Large | Slow | Yes | Debugging, manual entry |
WKB | Compact | Fast | No | Storage, network transfer |
GeoJSON - Web-Friendly Format
GeoJSON is a JSON-based format that's become the standard for web mapping applications and APIs.
-- Convert geometry to GeoJSON
SELECT ST_AsGeoJSON(geom) AS geojson_geometry
FROM landmarks;
-- Create geometry from GeoJSON
SELECT ST_GeomFromGeoJSON('{"type":"Point","coordinates":[77.59,12.97]}');
-- GeoJSON with properties (using row_to_json)
SELECT json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(geom)::json,
'properties', json_build_object(
'name', name,
'type', landmark_type
)
) AS feature_geojson
FROM landmarks;
-- Complete FeatureCollection
SELECT json_build_object(
'type', 'FeatureCollection',
'features', json_agg(
json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(geom)::json,
'properties', json_build_object('name', name)
)
)
) AS feature_collection
FROM landmarks;
GeoJSON Structure Examples
Geometry Type | GeoJSON Example |
---|---|
Point | {"type":"Point","coordinates":[77.59,12.97]} |
LineString | {"type":"LineString","coordinates":[[77.58,12.96],[77.60,12.98]]} |
Polygon | {"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,1],[0,0]]]} |
GeoJSON Use Cases
- Web mapping: Leaflet, Mapbox, OpenLayers
- APIs: REST services and microservices
- JavaScript applications: Frontend mapping applications
- Data exchange: Between web services
- Visualization: D3.js and other visualization libraries
GML - Geography Markup Language
GML is an XML-based format used in OGC standards and enterprise systems.
-- Convert geometry to GML
SELECT ST_AsGML(geom) AS gml_geometry
FROM landmarks;
-- Create geometry from GML
SELECT ST_GeomFromGML('<gml:Point><gml:coordinates>77.59,12.97</gml:coordinates></gml:Point>');
-- GML with different versions
SELECT ST_AsGML(2, geom) AS gml_v2,
ST_AsGML(3, geom) AS gml_v3
FROM landmarks;
GML Characteristics
Feature | Description | Use Case |
---|---|---|
XML-based | Structured markup language | Enterprise systems |
OGC Standard | Official OGC specification | Interoperability |
Schema validation | Can be validated against XSD | Data quality assurance |
Metadata support | Rich attribute support | Complex data models |
KML - Keyhole Markup Language
KML is designed for visualization in Google Earth and similar platforms.
-- Convert geometry to KML
SELECT ST_AsKML(geom) AS kml_geometry
FROM landmarks;
-- Create geometry from KML
SELECT ST_GeomFromKML('<Point><coordinates>77.59,12.97</coordinates></Point>');
-- KML with styling and descriptions
SELECT ST_AsKML(geom, 15) AS detailed_kml -- 15 decimal places
FROM landmarks;
KML Applications
- Google Earth: Primary visualization platform
- Presentations: Visual overlays for reports
- Public engagement: Sharing spatial data with non-GIS users
- Mobile apps: Location-based applications
Format Conversion and Validation
Converting Between Formats
-- Chain conversions
SELECT ST_AsGeoJSON(ST_GeomFromText('POINT(77.59 12.97)', 4326)) AS wkt_to_geojson;
-- Convert WKB to GML
SELECT ST_AsGML(ST_GeomFromWKB(decode('0101000020E6100000000000000000F03F0000000000000040','hex')));
-- Batch conversion example
SELECT
name,
ST_AsText(geom) AS wkt,
ST_AsGeoJSON(geom) AS geojson,
ST_AsKML(geom) AS kml
FROM landmarks;
Format Validation
-- Validate geometries before conversion
SELECT
name,
ST_IsValid(geom) AS is_valid,
ST_IsValidReason(geom) AS validation_reason
FROM landmarks;
-- Fix invalid geometries
UPDATE landmarks
SET geom = ST_MakeValid(geom)
WHERE NOT ST_IsValid(geom);
-- Validate specific format inputs
SELECT ST_IsValid(ST_GeomFromGeoJSON('{"type":"Point","coordinates":[77.59,12.97]}'));
Performance Considerations
Format | Parse Speed | Generate Speed | Size | Best For |
---|---|---|---|---|
WKB | Fastest | Fastest | Smallest | Internal storage, APIs |
WKT | Medium | Medium | Large | Debugging, human reading |
GeoJSON | Medium | Medium | Medium | Web applications |
GML | Slow | Slow | Largest | Enterprise integration |
KML | Slow | Slow | Large | Visualization |
Real-World Integration Examples
Web API Integration
-- Create REST API endpoint response
CREATE OR REPLACE FUNCTION get_landmarks_geojson()
RETURNS json AS $$
BEGIN
RETURN (
SELECT json_build_object(
'type', 'FeatureCollection',
'features', json_agg(
json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(geom)::json,
'properties', json_build_object(
'id', id,
'name', name,
'category', category
)
)
)
)
FROM landmarks
);
END;
$$ LANGUAGE plpgsql;
Data Import Pipeline
-- Import from various formats
CREATE TABLE imported_data (
id SERIAL PRIMARY KEY,
source_format TEXT,
geom GEOMETRY,
properties JSONB
);
-- Insert from different sources
INSERT INTO imported_data (source_format, geom, properties)
VALUES
('WKT', ST_GeomFromText('POINT(77.59 12.97)', 4326), '{"name": "Location A"}'),
('GeoJSON', ST_GeomFromGeoJSON('{"type":"Point","coordinates":[77.60,12.98]}'), '{"name": "Location B"}'),
('KML', ST_GeomFromKML('77.61,12.99 '), '{"name": "Location C"}');
Best Practices
- Choose the right format for your use case:
- WKB for internal storage and performance
- GeoJSON for web applications
- WKT for debugging and manual editing
- KML for visualization and public sharing
- Always validate input data: Use ST_IsValid() before processing
- Handle coordinate precision: Consider decimal places for your application
- Include SRID information: Always specify coordinate reference systems
- Optimize for your workflow: Cache converted formats if used repeatedly
- Consider file size: Choose compact formats for large datasets
Format Selection Guide
Use Case | Recommended Format | Alternative | Reason |
---|---|---|---|
Web mapping | GeoJSON | WKT | Native JavaScript support |
Database storage | WKB | Native geometry | Compact and fast |
Debugging | WKT | GeoJSON | Human readable |
Google Earth | KML | GeoJSON | Native support |
Enterprise systems | GML | GeoJSON | Standards compliance |
API responses | GeoJSON | WKT | Web standard |