# 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](https://tripmap.vn/en/my-trips)

## 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](https://www.rfc-editor.org/rfc/rfc7946.html#section-3.1.1)

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.

```js
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.
