{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agentengineering.it/egms-client/schema/opendata-record-v2.json",
  "title": "opendata-record/v2",
  "description": "One record of open data: an observation, a fact already computed, a map layer, or an event (something that happened: a landslide, a rainfall event). Every record carries its source, licence, time, place and how far to trust it. Not measured is null, never 0. No record is an alert. v2 adds to v1: the event kind; a place that may be absent and how wide it is; how wide a date is, a local time without a zone, an instant that is a convention; values that are yes/no or categories.",
  "type": "object",
  "required": [
    "schema",
    "kind",
    "source",
    "made_by",
    "time",
    "where",
    "reliability"
  ],
  "properties": {
    "schema": {
      "const": "opendata-record/v2"
    },
    "kind": {
      "enum": [
        "observation",
        "fact",
        "layer",
        "event"
      ]
    },
    "id": {
      "type": "string",
      "minLength": 1
    },
    "source": {
      "$ref": "#/$defs/source"
    },
    "made_by": {
      "$ref": "#/$defs/made_by"
    },
    "time": {
      "$ref": "#/$defs/time"
    },
    "where": {
      "description": "Where: a GeoJSON geometry, or null when the source gives no place (a fact or an event only)",
      "oneOf": [
        {
          "type": "null"
        },
        {
          "$ref": "#/$defs/geometry"
        }
      ]
    },
    "where_accuracy_m": {
      "type": [
        "number",
        "null"
      ],
      "minimum": 0,
      "description": "How wide the place is: the true position is within this many metres of `where`. 0 is exact; null when the source does not state it"
    },
    "reliability": {
      "$ref": "#/$defs/reliability"
    },
    "notes": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "label": {
      "description": "What the record is about, in words for people: one text, or one per language ({\"it\": \"Pioggia\", \"en\": \"Rain\"})",
      "oneOf": [
        {
          "type": "string",
          "minLength": 1,
          "maxLength": 80
        },
        {
          "type": "object",
          "minProperties": 1,
          "propertyNames": {
            "pattern": "^[a-z]{2}$"
          },
          "additionalProperties": {
            "type": "string",
            "minLength": 1,
            "maxLength": 80
          }
        }
      ]
    },
    "headline": {
      "type": "string",
      "minLength": 1,
      "description": "In a fact: the name of the value to show first"
    },
    "thing": {
      "type": "string",
      "minLength": 1,
      "description": "The device or station, as a SensorThings Thing"
    },
    "property": {
      "$ref": "#/$defs/property"
    },
    "value": {
      "type": [
        "number",
        "null"
      ],
      "description": "null when not measured; never 0 in its place"
    },
    "unit": {
      "$ref": "#/$defs/unit"
    },
    "meaning": {
      "type": "string",
      "minLength": 1,
      "description": "What the sign or the value says, e.g. 'vertical; positive = up'"
    },
    "quality": {
      "$ref": "#/$defs/quality"
    },
    "question": {
      "type": "string",
      "minLength": 1
    },
    "event": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*$",
      "description": "In an event: what happened, e.g. landslide, rainfall_event"
    },
    "values": {
      "type": "object",
      "minProperties": 1,
      "additionalProperties": {
        "$ref": "#/$defs/value"
      }
    },
    "href": {
      "type": "string",
      "minLength": 1
    },
    "media_type": {
      "enum": [
        "image/tiff; application=geotiff; profile=cloud-optimized",
        "image/tiff; application=geotiff",
        "application/geo+json"
      ]
    },
    "no_data": {
      "type": "string",
      "minLength": 1
    }
  },
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "kind": {
            "const": "observation"
          }
        }
      },
      "then": {
        "required": [
          "property",
          "value",
          "unit"
        ],
        "properties": {
          "where": {
            "$ref": "#/$defs/geometry"
          },
          "question": false,
          "event": false,
          "values": false,
          "href": false,
          "media_type": false,
          "no_data": false
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "fact"
          }
        }
      },
      "then": {
        "required": [
          "question",
          "values"
        ],
        "properties": {
          "value": false,
          "event": false,
          "href": false,
          "media_type": false,
          "no_data": false,
          "quality": false
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "layer"
          }
        }
      },
      "then": {
        "required": [
          "property",
          "unit",
          "href",
          "media_type",
          "no_data"
        ],
        "properties": {
          "where": {
            "$ref": "#/$defs/geometry"
          },
          "value": false,
          "question": false,
          "event": false,
          "values": false,
          "quality": false
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "event"
          }
        }
      },
      "then": {
        "required": [
          "event"
        ],
        "properties": {
          "value": false,
          "unit": false,
          "property": false,
          "question": false,
          "headline": false,
          "href": false,
          "media_type": false,
          "no_data": false,
          "quality": false
        }
      }
    }
  ],
  "$defs": {
    "source": {
      "type": "object",
      "required": [
        "name",
        "licence"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "url": {
          "type": "string",
          "minLength": 1
        },
        "licence": {
          "type": "string",
          "minLength": 1
        },
        "attribution": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    },
    "made_by": {
      "type": "object",
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "version": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    },
    "time": {
      "description": "An instant, a window, or both (an event dated to a day or an interval, with the instant to use for it); null when the source gives no dates",
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "if": {
            "not": {
              "required": [
                "instant"
              ]
            }
          },
          "then": {
            "required": [
              "first",
              "last",
              "exact"
            ]
          },
          "else": {
            "dependentRequired": {
              "first": [
                "last",
                "exact"
              ],
              "last": [
                "first",
                "exact"
              ],
              "exact": [
                "first",
                "last"
              ]
            }
          },
          "properties": {
            "instant": {
              "type": "string",
              "format": "date-time"
            },
            "convention": {
              "type": "string",
              "minLength": 1,
              "description": "Present when the instant is a convention, not a measurement; says which, e.g. 'end of the day, Europe/Rome'"
            },
            "first": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}"
            },
            "last": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}"
            },
            "exact": {
              "type": "boolean",
              "description": "false when the dates are the release name, not the measurements"
            },
            "precision": {
              "enum": [
                "hour",
                "day",
                "interval"
              ],
              "description": "How wide the date is: known to the hour, to the day, or only as an interval of days"
            },
            "days": {
              "type": "integer",
              "minimum": 1,
              "description": "The days the date covers: 1 for an hour or a day"
            },
            "local_time": {
              "type": "string",
              "pattern": "^([01]\\d|2[0-3]):[0-5]\\d$",
              "description": "The time of day as the source writes it, HH:MM, with no time zone"
            }
          },
          "dependentRequired": {
            "convention": [
              "instant"
            ],
            "days": [
              "precision"
            ]
          },
          "additionalProperties": false
        }
      ]
    },
    "position": {
      "type": "array",
      "minItems": 2,
      "maxItems": 3,
      "items": {
        "type": "number"
      },
      "prefixItems": [
        {
          "type": "number",
          "minimum": -180,
          "maximum": 180
        },
        {
          "type": "number",
          "minimum": -90,
          "maximum": 90
        }
      ]
    },
    "geometry": {
      "description": "GeoJSON geometry, longitude and latitude in WGS84",
      "oneOf": [
        {
          "type": "object",
          "required": [
            "type",
            "coordinates"
          ],
          "properties": {
            "type": {
              "const": "Point"
            },
            "coordinates": {
              "$ref": "#/$defs/position"
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "type",
            "coordinates"
          ],
          "properties": {
            "type": {
              "const": "Polygon"
            },
            "coordinates": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "array",
                "minItems": 4,
                "items": {
                  "$ref": "#/$defs/position"
                }
              }
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "type",
            "coordinates"
          ],
          "properties": {
            "type": {
              "const": "MultiPolygon"
            },
            "coordinates": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "array",
                  "minItems": 4,
                  "items": {
                    "$ref": "#/$defs/position"
                  }
                }
              }
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "reliability": {
      "description": "null when nobody says how far to trust the number",
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "required": [
            "method"
          ],
          "properties": {
            "score": {
              "type": [
                "number",
                "null"
              ],
              "minimum": 0,
              "maximum": 1
            },
            "reliable": {
              "type": [
                "boolean",
                "null"
              ]
            },
            "method": {
              "type": "string",
              "minLength": 1
            },
            "reasons": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1
              }
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "property": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*$",
      "description": "What is measured, e.g. rainfall, displacement, velocity, soil_moisture, water_level"
    },
    "unit": {
      "type": "string",
      "minLength": 1,
      "maxLength": 32,
      "description": "A UCUM code, e.g. mm, mm/a, mm/h, kPa, Cel, %"
    },
    "quality": {
      "type": "object",
      "required": [
        "flags"
      ],
      "properties": {
        "flags": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "enum": [
              "ok",
              "gap",
              "flatline",
              "spike",
              "range",
              "unit"
            ]
          }
        }
      },
      "additionalProperties": false
    },
    "quantity": {
      "type": "object",
      "required": [
        "value",
        "unit"
      ],
      "properties": {
        "value": {
          "type": [
            "number",
            "null"
          ]
        },
        "unit": {
          "$ref": "#/$defs/unit"
        },
        "meaning": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    },
    "value": {
      "description": "A named value: a quantity (a number with its unit), a flag (yes or no), or a category (a code or a word, with the scheme it belongs to). null: not known, never 0 or false in its place",
      "oneOf": [
        {
          "$ref": "#/$defs/quantity"
        },
        {
          "type": "object",
          "required": [
            "value"
          ],
          "properties": {
            "value": {
              "type": [
                "boolean",
                "null"
              ]
            },
            "meaning": {
              "type": "string",
              "minLength": 1
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "value",
            "scheme"
          ],
          "properties": {
            "value": {
              "type": [
                "string",
                "integer",
                "null"
              ]
            },
            "scheme": {
              "type": "string",
              "minLength": 1,
              "description": "What the codes or words are, e.g. 'ISTAT region code'"
            },
            "label": {
              "type": "string",
              "minLength": 1,
              "description": "The value in words for people"
            },
            "meaning": {
              "type": "string",
              "minLength": 1
            }
          },
          "additionalProperties": false
        }
      ]
    }
  }
}
