WGS 84: how a spatial database knows where a location is
The big idea: coordinates need a shared reference system to identify a location unambiguously. WGS 84 provides a global reference for describing positions on Earth. A spatial database uses this reference information to interpret geographical coordinates correctly.
Think of the difference between storing a number and storing a measurement: 25 is incomplete if you do not know whether it means metres, kilometres or degrees. Similarly, a pair of geographical coordinates needs information about the system in which it was measured.
1. What is WGS 84?
WGS 84 stands for World Geodetic System 1984. It is a global geodetic reference system used by GPS. Geodesy is the science of measuring and representing Earth, including its shape and positions on its surface.
Earth is not a perfect sphere. WGS 84 uses a smooth mathematical model called an ellipsoid, slightly flattened at the poles. It also establishes how its reference system is positioned and oriented relative to Earth.
This provides a consistent foundation for describing locations using latitude and longitude. The ellipsoid is a reference surface: it does not reproduce individual hills, buildings or valleys.
The name includes 1984, but the reference framework has been refined since its introduction.
How does WGS 84 represent three dimensions?
A position can be described using latitude, longitude and ellipsoidal height. Latitude and longitude locate the position horizontally relative to the reference ellipsoid. Ellipsoidal height supplies the third dimension: how far the position lies above or below that ellipsoid, measured along the line perpendicular to its surface.
Illustrative three-dimensional position:
Latitude: 52.2300°
Longitude: 21.0100°
Ellipsoidal height: 120.0 metres
Reference: WGS 84Two positions can have the same latitude and longitude but different heights. For example, positions on different floors of a building could have approximately the same horizontal coordinates while being separated vertically.
Ellipsoidal height is not the same as height above mean sea level. The WGS 84 ellipsoid is a smooth mathematical surface. A geoid is a gravity-based reference surface that approximates global mean sea level. Converting between ellipsoidal height and a sea-level-related height requires information about the separation between these surfaces, usually supplied by a geoid model.
WGS 84 also supports an Earth-centred, Earth-fixed Cartesian representation, using three coordinates called X, Y and Z, measured in metres:
- Origin: Earth's centre of mass.
- X-axis: points towards the intersection of the equator and the reference meridian.
- Y-axis: lies in the equatorial plane, towards 90° east longitude.
- Z-axis: points towards the reference north pole.
Earth-fixed means that the coordinate frame rotates with Earth. In this representation, X, Y and Z are distances along three perpendicular axes; Z alone is not the position's height above the surface.
Latitude, longitude and ellipsoidal height can be converted to Earth-centred X, Y and Z, and vice versa. They are two ways of representing the same three-dimensional position.
The database examples below use only two dimensions. They record horizontal locations and do not store height. WGS 84's three-dimensional foundation does not require every application to retain all three dimensions.
2. Latitude and longitude: what do the numbers mean?
Consider this illustrative location in Warsaw:
Latitude: 52.2300°
Longitude: 21.0100°
Reference: WGS 84| Coordinate | Meaning | Common signed range | Our example |
|---|---|---|---|
| Latitude | Angular position north or south of the equator. | −90° to +90°; north is positive. | 52.2300° north. |
| Longitude | Angular position east or west of the reference meridian. | −180° to +180°; east is positive. | 21.0100° east. |
These values are angles, not distances. A longitude of 21.0100° does not mean that the location is 21.0100 kilometres east of an origin.
For technical precision, geodetic latitude is measured using the line perpendicular to the reference ellipsoid at the location. Because the ellipsoid is not a sphere, this line does not generally pass through Earth's centre.
Coordinate order matters
People commonly write coordinates as latitude, longitude. However, the PostGIS point constructor used below expects longitude, latitude: its x value is longitude and its y value is latitude.
Labelled coordinates:
latitude = 52.2300, longitude = 21.0100
The same location in PostGIS point notation:
POINT(21.0100 52.2300)The formal EPSG:4326 axis order is latitude followed by longitude, while some software interfaces use longitude followed by latitude. Always check the convention used by the particular function or format.
Swapping these values can produce a valid coordinate pair for an entirely different location. A database may accept the values without detecting your mistake.
3. How does this connect to a spatial database?
A spatial database can store geographical objects such as:
- Points: a shop, bus stop or delivery location.
- Lines: a road or recorded journey.
- Polygons: a park boundary or delivery zone.
The object describes the spatial structure. Its coordinate reference system (CRS) explains how its coordinate values relate to real locations.
For example, POINT(21.0100 52.2300) alone does not establish whether the numbers represent geographical angles or coordinates in a local engineering grid.
What is EPSG:4326?
EPSG:4326 identifies a widely used two-dimensional geographical CRS based on WGS 84, with latitude and longitude expressed in degrees. EPSG codes identify defined coordinate reference systems.
In PostGIS, this identifier is commonly recorded as SRID 4326. SRID means Spatial Reference System Identifier.
SRID=4326;POINT(21.0100 52.2300)This is a readable representation of a point together with its spatial reference identifier. The coordinate order shown follows PostGIS's longitude–latitude convention.
WGS 84 and EPSG:4326 are related, but they are not identical terms: WGS 84 is the broader geodetic reference system; EPSG:4326 identifies a particular two-dimensional CRS based on it.
4. What might the stored data look like?
Imagine an application that helps students find nearby study spaces. These fictional records use illustrative coordinates around Warsaw:
| place_id | name | Location shown in readable form |
|---|---|---|
| 1 | Study Space A | SRID=4326;POINT(21.0100 52.2300) |
| 2 | Study Space B | SRID=4326;POINT(21.0200 52.2350) |
| 3 | Study Space C | SRID=4326;POINT(21.1000 52.3000) |
The spatial value is stored using the database's spatial representation. The text above makes it readable to a person; it is not a claim that the database internally stores the value as that exact string.
A conventional table could store latitude and longitude in two numerical columns. Spatial capabilities add specialised types, operations and indexes, allowing the database to answer questions such as “Which study spaces are within 1,000 metres of this student?”
5. A worked spatial query
The following example uses PostGIS, an extension that adds spatial capabilities to PostgreSQL. It assumes PostGIS is already enabled.
Create the table and add locations
CREATE TABLE study_places (
place_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
location geography(Point, 4326) NOT NULL
);
INSERT INTO study_places (place_id, name, location)
VALUES
(
1,
'Study Space A',
ST_SetSRID(
ST_MakePoint(21.0100, 52.2300),
4326
)::geography
),
(
2,
'Study Space B',
ST_SetSRID(
ST_MakePoint(21.0200, 52.2350),
4326
)::geography
),
(
3,
'Study Space C',
ST_SetSRID(
ST_MakePoint(21.1000, 52.3000),
4326
)::geography
);ST_MakePoint(longitude, latitude)constructs a point.ST_SetSRID(..., 4326)labels the point with its CRS. It does not change the coordinate numbers.::geographyconverts the value to PostGIS's geography type, which supports geographical calculations that account for Earth's curvature.geography(Point, 4326)restricts the column to points using the specified spatial reference system.
Find places within 1,000 metres
SELECT name
FROM study_places
WHERE ST_DWithin(
location,
ST_SetSRID(
ST_MakePoint(21.0120, 52.2310),
4326
)::geography,
1000
);The second point represents the student's illustrative location. For geography inputs, ST_DWithin interprets the distance in metres and uses a spheroidal calculation by default.
The query asks whether each stored location is within 1,000 metres of the student. This measures geographical proximity, not walking distance along roads. Walking distance requires information about the route network.
Support efficient searches
CREATE INDEX study_places_location_idx
ON study_places
USING GIST (location);This creates a spatial index. ST_DWithin can use an appropriate index to narrow the candidate records before completing the distance test. Whether the query planner uses it depends on factors such as table size and how many records the query is likely to return.
6. Why degrees must not be treated as metres
Lines of longitude converge towards the poles. Consequently, a one-degree change in longitude corresponds to a larger ground distance near the equator than near the poles.
This means there is no single, globally correct conversion from a longitude difference in degrees to a distance in metres.
Common programming mistake: assuming that assigning SRID 4326 makes every distance function return metres. The result also depends on the spatial data type and the function used.
| PostGIS input | Behaviour of ST_Distance | Consequence |
|---|---|---|
geometry using SRID 4326 | Calculates a planar distance using degree-valued coordinates. | The result is not a ground distance in metres. |
geography using SRID 4326 | Calculates a geodesic distance in metres, using the ellipsoid by default. | Suitable for geographical distance calculations. |
geometry in a suitable projected CRS with metre units | Calculates a planar distance in metres. | Useful for local analysis when the projection's distortion is acceptable. |
A map projection represents Earth's curved surface on a plane. Every projection introduces distortion. Having coordinates measured in metres does not, by itself, guarantee accurate ground distances.
7. Labelling coordinates is different from transforming them
Suppose a dataset uses a different CRS. You cannot correctly convert it to WGS 84 simply by changing its SRID to 4326.
- Assigning a CRS declares how the existing coordinate numbers should be interpreted.
- Transforming coordinates calculates coordinate numbers in a target CRS so that they represent the same real-world location, within the accuracy of the transformation.
In PostGIS, ST_SetSRID assigns the identifier, while ST_Transform performs the coordinate transformation.
For example, relabelling a distance of 100 feet as “100 metres” does not convert it. Likewise, changing a spatial reference label does not convert the underlying coordinates. The correct source CRS must be known before a meaningful transformation can be performed.
8. Why is WGS 84 useful?
- A shared global reference: applications can exchange positions using a common geographical framework.
- Compatibility with GPS: GPS-derived locations can be incorporated into geographical datasets using the appropriate WGS 84-based representation.
- Combining datasets: locations from different sources can be compared once their reference systems and coordinate conventions are correctly aligned.
However, WGS 84 does not guarantee that a measurement is accurate. A poorly measured position remains poorly measured even when it is correctly labelled. Similarly, storing additional decimal places does not improve the accuracy of the original observation.
9. Check your understanding
- What does the identifier 4326 represent in the PostGIS examples?
- Why can swapping latitude and longitude produce an incorrect location without causing a database error?
- Why should a distance calculated directly from degree-valued coordinates not be interpreted as metres?
- What is the difference between assigning a coordinate reference system and transforming coordinates?
- The identifier 4326: it identifies the two-dimensional WGS 84 geographical CRS, EPSG:4326.
- Swapping latitude and longitude: the two values can remain within valid numerical ranges after they are swapped, while describing a different position. The database cannot necessarily infer the intended location.
- Degrees and metres: the coordinates are angles. In particular, the ground distance represented by a longitude difference varies with latitude, so a planar result in degrees is not a distance in metres.
- Assigning and transforming: assigning a CRS labels the existing values. Transforming coordinates calculates values in another CRS that represent the same real-world location.
Sources and further reading
- National Geospatial-Intelligence Agency: WGS 84 — the reference system and its relationship to GPS.
- PROJ: frequently asked questions — coordinate reference systems and axis-order conventions.
- PostGIS: ST_MakePoint — constructing points and coordinate order.
- PostGIS: ST_SetSRID and ST_Transform — assigning reference information and transforming coordinates.
- PostGIS: ST_DWithin and ST_Distance — proximity queries, distance calculations and units.