【问题标题】:Restrict json schema to Polygon from GeoJson从 GeoJson 将 json 模式限制为 Polygon
【发布时间】:2019-01-18 13:50:30
【问题描述】:

我正在构建一个具有属性boundary 的 JSON 模式。我正在引用运行良好的 GeoJson 模式。现在我想将我的边界限制为 Polygon 类型,它是来自 GeoJson 架构的 enum

如何做到这一点?

这是我的架构的相关部分:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "plot": {
      "type": "object",
      "properties": {
        "boundary": {
          "description": "The boundary of the plot",
          "title": "Plot boundary",
          "additionalProperties": false,
          "required": [
            "type",
            "coordinates",
            "crs"
          ],
          "TODO": "Restrict to (multi)polygons only.",
          "$ref": "http://json.schemastore.org/geojson"
        }      
      }
    }
  }
}

这是我的验证 json:

{
  "plot":
  {
    "boundary": {
      "crs": {
        "type": "name",
        "properties": {
          "name": "EPSG:3857"
        }
      },
      "coordinates": [],
      "type": "MultiPolygon"
    }    
  }
}

【问题讨论】:

  • 一张便条。您引用的 geojson 架构在 Draft-4 中,而您拥有的架构是 Draft-7。未定义将不同草稿的架构引用到您自己的行为。
  • 所以我也应该将架构更改为 Draft-7 吗?我可以这样做,但我的问题仅限于多边形怎么样?
  • 您的架构显示它是草稿 7,但 geojson 架构是草稿 4。而不是引用它,您应该复制所需的位,并根据需要将它们更新为 draft-7。此外,您不应该在架构中的对象中不能将 $ref 与任何其他键一起使用。其他键将被忽略。 (草稿 8 的变化)

标签: json geojson jsonschema


【解决方案1】:

看来我使用了错误的 geojson。 我更改了我的代码,现在它按预期工作,新模式也是 Draft-7。 这是我更新的代码:

        "boundary": {
          "title": "The boundary of the plot",
          "anyOf": [
            {
              "$ref": "http://geojson.org/schema/MultiPolygon.json"
            },
            {
              "$ref": "http://geojson.org/schema/Polygon.json"
            }
          ],
          "additionalProperties": false

            "geoLocation": {
              "title": "Front door geolocation",
              "$ref": "http://geojson.org/schema/Point.json",
              "additionalProperties": false
            },

JSON 可能是:

  "boundary":
  {
    "type": "Polygon",
    "coordinates": [
      [
        [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0],
        [100.0, 0.0]
      ]
    ]
  }

    "geoLocation": {
      "coordinates": [125.25, 135.255],
      "type": "Point"
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-22
    • 2011-08-02
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多