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
| Type | Shape | Coordinates Format |
|---|---|---|
Point | Single location | [longitude, latitude] |
LineString | Path / route | [[lon,lat], [lon,lat], ...] |
Polygon | Closed area | [[[lon,lat], ...]] (ring) |
MultiPoint | Multiple points | Array of Point coordinates |
MultiLineString | Multiple paths | Array of LineString coords |
MultiPolygon | Multiple areas | Array of Polygon coords |
GeometryCollection | Mixed geometries | Array 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
| Pitfall | Consequence | Prevention |
|---|---|---|
| Lat/lon reversed in coordinates | Marker appears on wrong continent | Always use [longitude, latitude] (RFC 7946) |
Missing type field | Library fails to parse | Every geometry and feature needs type |
| Integer coordinates for precision | Rounding to wrong location | Use float coordinates with enough decimals |
What's Next
- Previous: Binary JSON Formats — Binary serialization alternatives.
- Next: Best Practices — Production JSON conventions.
- Section Overview — Return to the Advanced module index.