簡體   English   中英

將特征數據加載到 Vega-Lite

[英]Loading feature data into Vega-Lite

我正在嘗試使用 vega-lite plot 區域形狀,但報告了以下區域。

[警告] 從通道“shape”中刪除 {"type":"geojson"},因為它不包含任何數據字段、數據、值或信號。

數據已成功加載,因為我可以在 vega-lite 編輯器/查看器中看到它,我在下面和vega-lite 片段的鏈接中包含了一些記錄。

我認為問題在於指出數據中的正確字段。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {
        "OBJECTID": 4691,
        "WD21CD": "E05011118",
        "WD21NM": "Acocks Green",
        "WD21NMW": " ",
        "BNG_E": 412052,
        "BNG_N": 282830,
        "LONG": -1.8241,
        "LAT": 52.4433,
        "SHAPE_Length": 0.0924,
        "SHAPE_Area": 0.0005,
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": [
            [
              [
                [-1.8093, 52.4454],
                [-1.8173, 52.4324],
                [-1.8253, 52.4293],
                [-1.839, 52.4341],
                [-1.8355, 52.4382],
                [-1.831, 52.4483],
                [-1.8345, 52.4522],
                [-1.824, 52.4571],
                [-1.8139, 52.4547],
                [-1.8093, 52.4454]
              ]
            ]
          ]
        }
      },
      {
        "OBJECTID": 4692,
        "WD21CD": "E05011119",
        "WD21NM": "Allens Cross",
        "WD21NMW": " ",
        "BNG_E": 401463,
        "BNG_N": 279538,
        "LONG": -1.9799,
        "LAT": 52.4138,
        "SHAPE_Length": 0.0959,
        "SHAPE_Area": 0.0002,
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": [
            [
              [
                [-1.9618, 52.4212],
                [-1.9717, 52.4163],
                [-1.9758, 52.4079],
                [-1.9835, 52.4095],
                [-1.9978, 52.4097],
                [-1.986, 52.4167],
                [-1.975, 52.4205],
                [-1.9807, 52.4247],
                [-1.9754, 52.4268],
                [-1.9679, 52.4214],
                [-1.9618, 52.4212]
              ]
            ]
          ]
        }
      }
    ],
    "format": {"type": "json"}
  },
  "mark": "geoshape",
  "encoding": {"shape": {"type": "geojson"}},
  "width": 500,
  "height": 500
}

原來我的 json 格式不正確。 我曾在 R 中使用 jsonlite::toJSON 將簡單特征轉換為 json,但應該使用 geojsonsf::sf_geojson()。 前者只包括feature.properties 的內容。

有關詳細信息,請參閱將數據轉換為包含所有對象的 Json

正確的代碼如下,注意數據定義,這里定義了FeatureCollection和features。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": {
      "type": "FeatureCollection",
      "features": [
        {
          "type": "Feature",
          "properties": {
            "OBJECTID": 4691,
            "WD21CD": "E05011118",
            "WD21NM": "Acocks Green",
            "BNG_E": 412052,
            "BNG_N": 282830,
            "LONG": -1.82411,
            "LAT": 52.443321,
            "SHAPE_Length": 0.09237554909129655,
            "SHAPE_Area": 0.000522111171395259
          },
          "geometry": {
            "type": "MultiPolygon",
            "coordinates": [
              [
                [
                  [-1.809263305999934, 52.44539377400008],
                  [-1.817314732999932, 52.43236735700003],
                  [-1.825263312999937, 52.42930558300003],
                  [-1.839003390999949, 52.434050100000036],
                  [-1.809263305999934, 52.44539377400008]
                ]
              ]
            ]
          }
        },
        {
          "type": "Feature",
          "properties": {
            "OBJECTID": 4692,
            "WD21CD": "E05011119",
            "WD21NM": "Allens Cross",
            "WD21NMW": " ",
            "BNG_E": 401463,
            "BNG_N": 279538,
            "LONG": -1.97991,
            "LAT": 52.413849,
            "SHAPE_Length": 0.09592937334184568,
            "SHAPE_Area": 0.00023886389782556888
          },
          "geometry": {
            "type": "MultiPolygon",
            "coordinates": [
              [
                [
                  [-1.961811859999955, 52.42117633300006],
                  [-1.971725540999955, 52.41631029900003],
                  [-1.975847800999929, 52.40792152500006],
                  [-1.983516194999936, 52.409539153000026],
                  [-1.967944932999956, 52.42144882300005],
                  [-1.961811859999955, 52.42117633300006]
                ]
              ]
            ]
          }
        }
      ]
    },
    "format": {"type": "json", "property": "features"}
  },
  "mark": {"type": "geoshape", "strokeWidth": "1", "stroke": "White"},
  "encoding": {
    "tooltip": {"field": "properties.WD21NM"},
    "shape": {"type": "geojson"}
  },
  "width": 500,
  "height": 500
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM