PostGIS Spatial Queries & ST_Transform Coordinate Reference Guide
Coordinates in PostGIS: Numbers Plus a Promise
A PostGIS geometry is a list of numbers plus an SRID — a code that states which coordinate reference system those numbers belong to. The point (139.7671, 35.6812) is Tokyo Station if the SRID is 4326 (WGS84 latitude/longitude). Read the same numbers as EPSG:6677 (Japan Plane Rectangular zone IX) and they describe a spot about 140 km off the coast — the database will not warn you, because as far as it is concerned, you made a promise about the numbers and it believed you. Almost every PostGIS coordinate bug is a broken version of that promise: a wrong SRID, a relabel mistaken for a conversion, or a distance computed in degrees. This guide works through the core functions and the three classic mistakes, with examples you can run.
Essential PostGIS Functions
| Function | What it does |
|---|---|
ST_Transform(geom, srid) | Recomputes the coordinates into another CRS. The numbers change. |
ST_SetSRID(geom, srid) | Relabels the geometry with an SRID. The numbers do not change. |
ST_MakePoint(x, y) | Builds a point. For longitude/latitude data, x is longitude, y is latitude. |
ST_Distance(a, b) | Distance in the units of the CRS for geometry; in metres for geography. |
ST_DWithin(a, b, d) | True if within distance d — and the form that uses a spatial index. |
ST_AsText / ST_AsGeoJSON | Readable output for checking what you actually stored. |
Worked Example 1: WGS84 to a Japanese Plane Grid (EPSG:6677)
Japanese surveying and cadastral work happens in the JGD2011 plane rectangular zones — flat grids in metres. Converting Tokyo Station from WGS84 to zone IX:
SELECT ST_AsText(
ST_Transform(
ST_SetSRID(ST_MakePoint(139.7671, 35.6812), 4326),
6677));
Result: POINT(-5995.185 -35367.23) — that is, 5,995.185 m west and 35,367.230 m south of the zone origin (36°N, 139°50′E), which is exactly where Tokyo Station sits relative to it. Note the construction order: ST_MakePoint builds raw numbers, ST_SetSRID truthfully labels them as 4326 (they are WGS84 values), and only then can ST_Transform recompute them, because a transformation needs to know what the numbers mean before it can change them. Distances and areas computed in 6677 come out in metres and square metres — the practical reason projected CRSs exist. (To look up the definitions of EPSG:4326 and EPSG:6677, use our EPSG Code Finder.)
Worked Example 2: A Real Distance, in Metres
SELECT ST_Distance( ST_MakePoint(139.7671, 35.6812)::geography, ST_MakePoint(135.5023, 34.6937)::geography);
Tokyo Station to Osaka Station returns approximately 403,550 metres — the spheroidal (geodesic) distance, because the geography type computes on the WGS84 ellipsoid. Run the same query on plain geometry in SRID 4326 and you get about 3.6 — degrees, a number with no physical meaning. Rule: on longitude/latitude data, distances come from geography casts or from transforming to a projected CRS — never from raw geometry.
The Three Classic Mistakes
1. Relabelling instead of transforming. ST_SetSRID(geom, 6677) on WGS84 data does not convert anything; it declares metre-grid numbers that are still degree values, placing your data hundreds of kilometres away with no error message. If the numbers must change, the function is ST_Transform. Conversely, data loaded with SRID 0 (unknown) cannot be transformed until you ST_SetSRID it to its true source SRID — the one legitimate everyday use of relabelling.
2. Swapped axes. ST_MakePoint(35.6812, 139.7671) builds a point with longitude 35.68 and latitude 139.77 — an impossible latitude that some queries reject and others silently return nothing for. PostGIS always takes x = longitude first for geographic data, whatever order your source file used.
3. Filtering with ST_Distance instead of ST_DWithin. WHERE ST_Distance(...) < 1000 computes a distance for every row; ST_DWithin(geom, point, 1000) (on a projected CRS, or with geography for metres) can use a GiST index (CREATE INDEX ... USING GIST (geom)) and only computes exact distances for candidates. On a million-row table this is the difference between a scan and a lookup.
Frequently Asked Questions
What is the difference between ST_Transform and ST_SetSRID?
ST_Transform recomputes coordinate values into a different CRS; ST_SetSRID only changes the label, leaving values untouched. Use SetSRID to declare what unlabeled data truly is, and Transform to actually convert it.
Why does my distance query return 3.6 instead of kilometres?
Because the geometry is in EPSG:4326 and ST_Distance on geometry returns CRS units — degrees. Cast to geography for metres on the spheroid, or ST_Transform to a projected CRS and measure there.
Which SRID should I store web map data in?
Store in EPSG:4326 (WGS84) for interchange and geography calculations, and transform to a local projected CRS (such as the JGD2011 zones in Japan) for measurement, CAD-style work and legal/survey outputs. Storage and measurement are different jobs.