pgLatLon is a spatial database extension for the PostgreSQL object-relational database management system providing geographic data types and spatial indexing for the WGS-84 spheroid.

While many other spatial databases still use imprecise bounding boxes for many operations, pgLatLon aims to support more precise calculations for all implemented geographic operators. Efficient indexing of geographic objects is provided using space-filling fractal curves. Optimizations on bit level (including logarithmic compression) allow for a highly memory-efficient non-overlapping index suitable for huge datasets.

pgLatLon is a lightweight solution as it only depends on PostgreSQL itself (and a C compiler for building).

Unlike competing spatial extensions for PostgreSQL, pgLatLon is available under the permissive MIT/X11 license to avoid problems with viral licenses like the GPLv2/v3.

Installation and Documentation

pgLatLon uses PostgreSQL's extension building infrastructure PGXS to allow for an easy installation:

  1. Download the extension by clicking on one of the links in the “Download” section below.
  2. Extract the tar.gz file.
  3. Run “make install”.

Documentation can be found here or be downloaded as an HTML file.

Example usage

Create a table with a column that stores a geographic position (an epoint) in column "location":

CREATE TABLE tbl (id serial4 PRIMARY KEY, location epoint NOT NULL);

Create a spatial index for column "location":

CREATE INDEX tbl_location_key ON tbl USING gist (location);

Perform a nearest-neighbor search using the previously created index:

SELECT * FROM tbl ORDER BY location <-> 'N12.34 E56.78'::epoint LIMIT 10;





  • 2016-10-31: Version 0.10
    • Added safety margins for distance calculation on index lookups (fixes bug that caused nearest-neighbor search to fail in certain cases)
    • Improved fair_distance function (see README for properties)
  • 2016-10-25: Version 0.9
    • Bugfix in type casts from epoint and ebox to ecluster (missing bounding circle calculation, requires reimport of database to be fixed)
    • New fair_distance function (see README)
  • 2016-10-21: Version 0.8
    • Fixed broken functions GeoJSON_to_epoint and GeoJSON_to_ecluster (NOTE: after update, manually recreate any indices using these functions)
  • 2016-09-26: Version 0.7
    • Improved accuracy of vector projection for distance calculation by considering local approximation of WGS-84 spheroid instead of sphere
  • 2016-09-22: Version 0.6
    • Bugfix in distance calculation involving the ecluster data type
  • 2016-09-12: Version 0.5
    • Changed behavior of "contains?" operator (@>) which fixes wrong behavior regarding certain corner cases
    • Added "contains?" operator for ebox type
  • 2016-09-09: Version 0.4
    • Added overlap operator (&&) and distance operator (<->) for ebox and ecluster data types
    • Added "contains?" (@>) and "is contained in?" (<@) operators
    • Bug causing table scans instead of index usage for nearest-neighbor searches of clusters (including polygons) has been fixed
  • 2016-09-02: Version 0.3
    • Added lossy overlap operator (&&+) for faster selections (where false positives are tolerable)
    • Bug causing table scans instead of index usage when using the overlap operator (&&) with a ecircle query object on ecluster columns has been fixed
  • 2016-08-22: Version 0.2
    • Improved calculation speed of approximated distances on the WGS-84 ellipsoid
  • 2016-08-21: Initial release of version 0.1