{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://stac-extensions.github.io/pointcloud/v2.0.0/schema.json#",
  "title": "Point Cloud Extension",
  "description": "STAC Point Cloud Extension for STAC Items and Collections.",
  "type": "object",
  "required": [
    "stac_extensions",
    "type"
  ],
  "properties": {
    "stac_extensions": {
      "type": "array",
      "contains": {
        "const": "https://stac-extensions.github.io/pointcloud/v2.0.0/schema.json"
      }
    }
  },
  "allOf": [
    {
      "$comment": "Item",
      "if": {
        "properties": {
          "type": {
            "const": "Feature"
          }
        }
      },
      "then": {
        "allOf": [
          {
            "properties": {
              "properties": {
                "$ref": "#/definitions/fields"
              }
            }
          },
          {
            "$ref": "#/definitions/validate_assets"
          }
        ],
        "anyOf": [
          {
            "required": [
              "properties"
            ],
            "properties": {
              "properties": {
                "$ref": "#/definitions/require_fields"
              }
            }
          },
          {
            "$ref": "#/definitions/require_assets"
          }
        ]
      }
    },
    {
      "$comment": "Collections",
      "if": {
        "properties": {
          "type": {
            "const": "Collection"
          }
        }
      },
      "then": {
        "allOf": [
          {
            "$ref": "#/definitions/validate_assets"
          },
          {
            "properties": {
              "item_assets": {
                "additionalProperties": {
                  "$ref": "#/definitions/fields"
                }
              }
            }
          }
        ],
        "anyOf": [
          {
            "$ref": "#/definitions/require_assets"
          },
          {
            "required": [
              "item_assets"
            ],
            "properties": {
              "item_assets": {
                "$ref": "#/definitions/asset_contains"
              }
            }
          },
          {
            "required": [
              "summaries"
            ],
            "properties": {
              "summaries": {
                "$ref": "#/definitions/require_fields"
              }
            }
          }
        ]
      }
    }
  ],
  "definitions": {
    "require_fields": {
      "required": [
        "pc:count",
        "pc:type"
      ]
    },
    "require_assets": {
      "required": [
        "assets"
      ],
      "properties": {
        "assets": {
          "$ref": "#/definitions/asset_contains"
        }
      }
    },
    "validate_assets": {
      "properties": {
        "assets": {
          "additionalProperties": {
            "$ref": "#/definitions/fields"
          }
        }
      }
    },
    "asset_contains": {
      "type": "object",
      "not": {
        "additionalProperties": {
          "not": {
            "$ref": "#/definitions/fields"
          }
        }
      }
    },
    "fields": {
      "type": "object",
      "properties": {
        "pc:count": {
          "$ref": "#/definitions/pc:count"
        },
        "pc:type": {
          "$ref": "#/definitions/pc:type"
        },
        "pc:schemas": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/schema"
          }
        },
        "pc:density": {
          "$ref": "#/definitions/pc:density"
        },
        "pc:statistics": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/stats"
          }
        }
      },
      "patternProperties": {
        "^(?!pc:)": {}
      },
      "additionalProperties": false
    },
    "pc:count": {
      "title": "The number of points",
      "type": "integer",
      "minimum": 0
    },
    "pc:type": {
      "title": "Phenomenology type for the point cloud",
      "type": "string",
      "minLength": 1
    },
    "pc:schemas": {
      "title": "A sequential array of Items that define the dimensions and their types",
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/definitions/schema"
      }
    },
    "pc:density": {
      "title": "Number of points per square unit area",
      "type": "number",
      "minimum": 0
    },
    "pc:statistics": {
      "title": "A sequential array of Items mapping to pc:schemas defines per-channel statistics",
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/definitions/stats"
      }
    },
    "schema": {
      "title": "A sequential array of Items that define the dimensions or channels of the point cloud, their types, and their sizes (in full bytes)",
      "type": "object",
      "required": [
        "name",
        "size",
        "type"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "size": {
          "type": "integer"
        },
        "type": {
          "type": "string",
          "enum": [
            "floating",
            "unsigned",
            "signed"
          ]
        }
      }
    },
    "stats": {
      "title": "A sequential array of Items mapping to pc:schemas defines per-channel statistics",
      "type": "object",
      "minProperties": 2,
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "position": {
          "type": "integer"
        },
        "average": {
          "type": "number"
        },
        "count": {
          "type": "integer"
        },
        "maximum": {
          "type": "number"
        },
        "minimum": {
          "type": "number"
        },
        "stddev": {
          "type": "number"
        },
        "variance": {
          "type": "number"
        }
      }
    }
  }
}