{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aeronautiql.local/specs/nav/airspace-element.schema.json",
  "title": "Aeronautiql airspace element family",
  "description": "Common data model for map airspace. SQL column `class` and GeoJSON property `cls` are the same token. All subtypes inherit AirspaceElement.",
  "$defs": {
    "AirspaceElement": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "ident",
        "name",
        "class",
        "tier",
        "geom_type",
        "geometry",
        "bbox"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "description": "Stable row id within one nav.sqlite build."
        },
        "ident": {
          "type": "string",
          "description": "Chart designator (DEN, R-2901, PCO001, ZDV). Duplicate idents need namespace."
        },
        "name": {
          "type": "string",
          "description": "Display name. PJA uses drop-zone/city, not the NASR ident."
        },
        "class": {
          "$ref": "#/$defs/AirspaceClass"
        },
        "type_code": {
          "type": ["string", "null"],
          "description": "Producer subtype (NASR_PJA, shapefile local type). Not required by the map loader."
        },
        "local_type": {
          "type": ["string", "null"],
          "description": "Producer-specific (CLASS_E2, PJA time-of-use)."
        },
        "namespace": {
          "type": ["string", "null"],
          "description": "ICAO region that owns the volume (K, K2, EG). ARINC Airspace Center lives in owner_ident."
        },
        "owner_ident": {
          "type": ["string", "null"],
          "description": "Host designator: ARINC Airspace Center, class airport ident, or SUA designator."
        },
        "qualified_ident": {
          "type": ["string", "null"],
          "description": "`{namespace}:{ident}` when namespace is set."
        },
        "tier": {
          "type": "integer",
          "minimum": 0,
          "maximum": 2,
          "description": "0 = B/C and SUA/PJA, 1 = D, 2 = E and other."
        },
        "lower_ft": {
          "type": ["integer", "null"],
          "description": "Floor in feet MSL. SFC is 0. Null if unknown."
        },
        "upper_ft": {
          "type": ["integer", "null"],
          "description": "Ceiling in feet MSL only. AGL ceilings must not be stored here."
        },
        "altitude_summary": { "type": ["string", "null"], "description": "Chart label preserving the published altitude references." },
        "lower_raw": { "type": ["string", "null"], "description": "ARINC floor token with padding trimmed." },
        "upper_raw": { "type": ["string", "null"], "description": "ARINC ceiling token with padding trimmed." },
        "lower_unit": { "type": ["string", "null"], "description": "Source floor unit indicator." },
        "upper_unit": { "type": ["string", "null"], "description": "Source ceiling unit indicator." },
        "lower_nominal_ft": { "type": ["integer", "null"], "description": "Nominal floor magnitude in the published reference." },
        "upper_nominal_ft": { "type": ["integer", "null"], "description": "Nominal ceiling magnitude in the published reference." },
        "lower_desc": {
          "type": ["string", "null"],
          "description": "Non-MSL or formatted floor (SFC, AGL)."
        },
        "upper_desc": {
          "type": ["string", "null"],
          "description": "Non-MSL ceiling (5000 AGL, UNLIMITED)."
        },
        "geom_type": {
          "enum": ["Polygon", "MultiPolygon"]
        },
        "geometry": {
          "description": "GeoJSON Polygon or MultiPolygon, lon/lat degrees, rings closed.",
          "type": "object"
        },
        "bbox": {
          "type": "object",
          "required": ["min_lon", "min_lat", "max_lon", "max_lat"],
          "properties": {
            "min_lon": { "type": "number" },
            "min_lat": { "type": "number" },
            "max_lon": { "type": "number" },
            "max_lat": { "type": "number" }
          }
        }
      }
    },
    "AirspaceClass": {
      "type": "string",
      "enum": [
        "B",
        "C",
        "D",
        "E",
        "MOA",
        "R",
        "P",
        "W",
        "A",
        "PJA",
        "DANGER",
        "ADIZ",
        "FIR",
        "ARTCC",
        "OTA"
      ]
    },
    "ClassAirspace": {
      "allOf": [
        { "$ref": "#/$defs/AirspaceElement" },
        {
          "type": "object",
          "properties": {
            "class": { "enum": ["B", "C", "D", "E"] }
          }
        }
      ]
    },
    "SpecialUseAirspace": {
      "allOf": [
        { "$ref": "#/$defs/AirspaceElement" },
        {
          "type": "object",
          "properties": {
            "class": {
              "enum": ["MOA", "R", "P", "W", "A", "PJA", "DANGER", "ADIZ"]
            }
          }
        }
      ]
    },
    "BoundaryAirspace": {
      "allOf": [
        { "$ref": "#/$defs/AirspaceElement" },
        {
          "type": "object",
          "properties": {
            "class": { "enum": ["FIR", "ARTCC", "OTA"] },
            "lower_ft": { "type": "null" },
            "upper_ft": { "type": "null" }
          }
        }
      ]
    },
    "MapAirspaceFeature": {
      "type": "object",
      "required": ["type", "id", "geometry", "properties"],
      "properties": {
        "type": { "const": "Feature" },
        "id": { "type": "string" },
        "geometry": { "type": "object" },
        "properties": {
          "type": "object",
          "required": ["bid", "ident", "name", "cls", "tier"],
          "properties": {
            "bid": { "type": "string", "description": "Same as AirspaceElement.id" },
            "ident": { "type": "string" },
            "name": { "type": "string" },
            "cls": { "$ref": "#/$defs/AirspaceClass" },
            "tier": { "type": "integer" },
            "lower_ft": { "type": "integer" },
            "upper_ft": { "type": "integer" },
            "base_m": { "type": "number" },
            "top_m": { "type": "number" },
            "alt": { "type": "string", "description": "Altitude tag copy (label features only)" },
            "nm": { "type": "string", "description": "Name label copy (name features only)" }
          }
        }
      }
    }
  },
  "oneOf": [
    { "$ref": "#/$defs/ClassAirspace" },
    { "$ref": "#/$defs/SpecialUseAirspace" },
    { "$ref": "#/$defs/BoundaryAirspace" }
  ]
}
