Skip to main content

Validating GeoJSON Points Before Drawing a Map

Catch swapped coordinates and distinguish a valid point from a plausible location

Updated 3 min readMarkdown
Validating GeoJSON Points Before Drawing a Map

A travel guide imports a batch of places and one marker lands far from its destination. The import had accepted every coordinate pair because both values were numbers. Maps for planning a trip make the consequence immediate: one misplaced marker can make an otherwise useful route hard to trust. The records in this example are invented. Tripmap itinerary planner

Validate the GeoJSON contract first

A GeoJSON point puts longitude first and latitude second, in decimal degrees. Some source interfaces label fields in the opposite order. Convert between the two at the import boundary, where the source format is still known, and keep the order consistent within your GeoJSON data. RFC 7946, GeoJSON positions

This example accepts the two- and three-value positions used by a simple place importer. It checks that the values are finite and that longitude and latitude lie within their usual geographic ranges.

function isGeoJsonPoint(geometry) {
  if (geometry?.type !== "Point" || !Array.isArray(geometry.coordinates)) {
    return false;
  }

  const { coordinates } = geometry;
  if (coordinates.length !== 2 && coordinates.length !== 3) return false;

  const [longitude, latitude] = coordinates;
  return Array.from(coordinates).every(Number.isFinite)
    && longitude >= -180 && longitude <= 180
    && latitude >= -90 && latitude <= 90;
}
Coordinates Validation result
[108.2, 16.1] Passes the structural and range checks.
[16.1, 108.2] Fails because the latitude is above 90掳.
[48.9, 2.3] Passes even if a source intended the reverse order.
["108.2", 16.1] Fails because longitude is a string.

Review points that pass but look wrong

Both values in [48.9, 2.3] fit either axis, so a range check cannot settle the intended order. For a feed that declares a limited service region, flag points well outside that region for review. Treat proximity as a signal. A ferry stop, an island, or a partner across a boundary can be legitimate.

If the upstream format names the fields, a mapping such as { longitude, latitude } to [longitude, latitude] is deterministic. If the source is ambiguous, preserve the original record and review it with the source owner. Swapping plausible coordinates automatically can replace one wrong point with another.

Keep fixtures for both kinds of error

Test a reversed pair that fails latitude bounds and a reversed pair that passes. Add a string, a missing coordinate, and an optional altitude. Track structural rejections separately from records waiting for geographic review. This gives importers a clear distinction between data they can fix mechanically and places that need human context.

About the author

  • Tripmap is a Vietnam-based travel technology platform on a mission to connect people with the real world through AI, geospatial intelligence, and digital innovation. We build intelligent travel discovery and booking solutions, combining AI agents, mapping technology, and tourism data to make exploring the world easier, smarter, and more accessible. Here, we share our engineering journey, technical insights, and experiences in building the future of travel technology. Built in Vietnam, for the world. 馃寪 https://tripmap.vn

No comments yet. Be the first to comment.