Skip to content

Records of open data

A record is one piece of open data in a shape every source can write and the application can read: an observation, a fact or a layer. The schema is opendata-record-v1.json (JSON Schema 2020-12). Why it exists, and what comes next, is in the design.

Version 2, opendata-record-v2.json, adds the kind event, a place that may be absent and how wide it is, how wide a date is, and values that are yes/no or a category. A record says which version it follows in its schema field; v1 is not changed. Both files are served at their id: https://agentengineering.it/egms-client/schema/opendata-record-v1.json and .../opendata-record-v2.json. Examples of v2, from ispra-idrogeo and rainfall-thresholds, are in examples/records-v2.

Real examples of v1 are in examples/records: two EGMS points (one not measured), an EGMS map, an e-ITALICA threshold (rainfall-thresholds), the radar reliability at a point (dpc-radar) and a simulated rain gauge.

The answers of egms_client.facts become records with egms_client.facts.to_records:

from egms_client.facts import to_records, velocity

records = to_records(velocity(tiffs, 14.1205, 40.8225, release="2020-2025"))

A CSV with the columns of the template (tempo, lon, lat, grandezza, valore, unità, fonte, licenza, or the same in English) reads as observations; an empty valore is not measured.

Check a file from the command line:

pip install "egms-client[records]"
egms records examples/records/*

egms_client.records

Records of open data: one shape for every source the application reads.

A record is a JSON object valid against schema/opendata-record-v1.json or schema/opendata-record-v2.json: its schema field says which. It is an observation (one value at one place and time), a fact (an answer already computed) or a layer (a map). Every record says its source, licence, time window, place and how far to trust it.

v2 adds what a dated landslide or a rainfall event needs, and v1 could not say: the kind event (something that happened); a place that may be absent (where: null) and how wide it is (where_accuracy_m); how wide a date is (time.precision, time.days), a time of day with no zone (time.local_time) and an instant that is a convention, not a measurement (time.convention); values that are yes/no or a category. v1 is not changed: a v1 record is checked against v1.

The schema is the contract between sources. A library, an IoT gateway or a CSV from a municipality writes records; the application reads records. No source imports another.

Some rules a JSON Schema cannot say. :func:problems checks them too:

  • not measured is null, never 0 or NaN;
  • a signed property (a velocity, a displacement) says what its sign means;
  • the time window does not end before it starts;
  • nothing is an alert or a risk;
  • in v2: a date said to be an interval covers more than one day, days is the days from first to last, and a place that is absent has no accuracy.

Validation needs the extra records (jsonschema).

RecordError

Bases: ValueError

A record is not valid against opendata-record/v1. problems says why.

schema cached

schema(version: str = SCHEMA) -> dict[str, Any]

The JSON Schema of a version: "opendata-record/v1" (default) or /v2.

problems

problems(record: Any) -> list[str]

Everything wrong with one record, as short sentences; empty if it is valid.

validate

validate(record: Any) -> dict[str, Any]

Return the record if it is valid; raise :class:RecordError if not.

read

read(path: str | Path) -> list[dict[str, Any]]

The records in a file: one object, a list of objects, JSON Lines (.jsonl) or CSV.

Every record is validated; a file with an invalid one raises :class:RecordError, with the position of the record in the file (for a CSV, its line).

from_csv

from_csv(text: str, *, made_by: str) -> list[dict[str, Any]]

Observations from a CSV with the columns of the template, one per line.

The columns are tempo, lon, lat, grandezza, valore, unità, fonte, licenza (or time, lon, lat, property, value, unit, source, licence), and optionally significato, sensore, etichetta. The separator may be a comma or a semicolon, and a decimal a comma, as a spreadsheet saves it. An empty valore is not measured: null. tempo is a date (a day) or a date and time with its time zone. The records are not validated here: :func:read does it.