How to Validate GeoJSON Before Processing It

API or service
Geometry Operations API
Task
GeoJSON geometry → valid or rejected input
Examples
URL
Difficulty
Beginner
Time
7 min

Validate GeoJSON before processing it

You have a GeoJSON geometry from a file, user-drawn shape, database, or another API and need to know whether it is safe to send into a spatial workflow. Validate its JSON structure and coordinate rules first, then use the Geometry Operations API valid operation for a geometry-level boolean result.

Task flow: GeoJSON input → structural checks → geometry validity check → accept or reject.

Validation prevents confusing downstream failures in buffering, intersections, measurements, and map rendering. It does not repair the geometry or explain every possible topology issue; keep the original input and report actionable errors separately.

Check structure and coordinates first

Before calling the API, verify these rules in your application:

  • The value is a GeoJSON geometry such as Point, LineString, Polygon, or MultiPolygon, not an entire Feature unless the operation explicitly accepts one.
  • Positions use [longitude, latitude] order. Longitude is between -180 and 180; latitude is between -90 and 90.
  • A LineString contains at least two positions.
  • Every Polygon ring contains at least four positions and repeats its first position as its last.
  • Nested coordinate arrays match the declared geometry type.
  • Coordinates are finite numbers, not strings, null, NaN, or infinity.

These are format checks. Business validation is separate: confirm that the geometry is in the expected country, is not unexpectedly large, and has enough precision for the task.

Do not silently swap latitude and longitude based only on their numeric ranges. Both orders can appear valid when values fall between -90 and 90; establish the source format explicitly.

Validate a geometry with the API

Send the request as JSON:

POST https://api.geoapify.com/v1/geometry/operation?apiKey=YOUR_API_KEY
Content-Type: application/json

This rectangle is valid:

{
  "operation": "valid",
  "geometry": {
    "type": "Polygon",
    "coordinates": [[
      [13.38, 52.50], [13.42, 52.50],
      [13.42, 52.54], [13.38, 52.54],
      [13.38, 52.50]
    ]]
  }
}
{ "type": "boolean", "data": true }

This structurally parseable ring has no area because every position is identical:

{
  "operation": "valid",
  "geometry": {
    "type": "Polygon",
    "coordinates": [[
      [13.38, 52.50], [13.38, 52.50],
      [13.38, 52.50], [13.38, 52.50]
    ]]
  }
}
{ "type": "boolean", "data": false }

Treat data: false as a completed validity check. A 400 response means the request itself failed validation—for example because a required field or coordinate structure is missing—and should not be converted into a boolean result.