Skip to main content

GeoJSON

Learning Focus

GeoJSON is the standard for representing geographic features (points, lines, polygons) as JSON. Used by mapping libraries, PostGIS, Mapbox, and web APIs.

What is GeoJSON?

GeoJSON (RFC 7946) is a JSON format for encoding geographic data structures:

point.geojson
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [103.8198, 1.3521]
},
"properties": {
"name": "Singapore",
"population": 5900000
}
}

Geometry Types

TypeShapeCoordinates Format
PointSingle location[longitude, latitude]
LineStringPath / route[[lon,lat], [lon,lat], ...]
PolygonClosed area[[[lon,lat], ...]] (ring)
MultiPointMultiple pointsArray of Point coordinates
MultiLineStringMultiple pathsArray of LineString coords
MultiPolygonMultiple areasArray of Polygon coords
GeometryCollectionMixed geometriesArray of geometry objects

FeatureCollection

The most common GeoJSON format — wraps multiple features:

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [103.8198, 1.3521] },
"properties": { "name": "Singapore" }
},
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [100.5018, 13.7563] },
"properties": { "name": "Bangkok" }
}
]
}
Coordinate Order

GeoJSON uses [longitude, latitude] order — the opposite of most GPS systems that use latitude/longitude. This trips up many developers.

PostgreSQL PostGIS

-- Enable PostGIS
CREATE EXTENSION postgis;

CREATE TABLE locations (
id SERIAL PRIMARY KEY,
name TEXT,
geom GEOMETRY(Point, 4326) -- 4326 = WGS 84 (standard GPS)
);

-- Insert a point
INSERT INTO locations (name, geom)
VALUES ('Singapore', ST_SetSRID(ST_MakePoint(103.8198, 1.3521), 4326));

-- Find locations within 100 km of a point
SELECT name FROM locations
WHERE ST_DWithin(
geom::geography,
ST_MakePoint(103.8198, 1.3521)::geography,
100000 -- metres
);

-- Return as GeoJSON
SELECT ST_AsGeoJSON(geom) FROM locations;

Concept Map

Concept Flow

GeoJSON
├── Feature
│ ├── geometry: Point / LineString / Polygon
│ └── properties: any JSON object
│ └── coordinates always [longitude, latitude]
└── FeatureCollection
└── Used by Mapbox, Leaflet, PostGIS, OpenStreetMap

Common Pitfalls

PitfallConsequencePrevention
Lat/lon reversed in coordinatesMarker appears on wrong continentAlways use [longitude, latitude] (RFC 7946)
Missing type fieldLibrary fails to parseEvery geometry and feature needs type
Integer coordinates for precisionRounding to wrong locationUse float coordinates with enough decimals

What's Next