如何使用 jq 将数组合并到一个 MultiLineString GeoJSON 数组下?

How to consolidate arrays under one single MultiLineString GeoJSON array using jq?

数组中的坐标在多个 GeoJSON MultiLineString 结构之间划分。我想合并所有坐标,将它们维护在一个单独的 MultiLineString 结构下的自己的数组中。我如何使用 jq 执行此操作?

这是原始文件(为了示例目的而修改)

{
  "type": "FeatureCollection",
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
  },

  "features": [{
      "type": "Feature",
      "geometry": {
        "type": "MultiLineString",
        "coordinates": [
          [
            [-82.619152670413143, 29.568340757283536, 0.0],
            [-82.619147188198966, 29.568355832670516, 0.0],
            [-82.607558975018591, 29.580299204829011, 0.0]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiLineString",
        "coordinates": [
          [
            [-82.619152670413513, 29.568340757283536, 0.0],
            [-82.619490683489488, 29.568318912277654, 0.0],
            [-82.629348688631055, 29.569000553128618, 0.0]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiLineString",
        "coordinates": [
          [
            [-82.629348688631055, 29.569000553128618, 0.0],
            [-82.62943243076478, 29.568922074598046, 0.0],
            [-82.623065167913538, 29.56611193045412, 0.0]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiLineString",
        "coordinates": [
          [
            [-82.618039923193663, 29.563657436904819, 0.0],
            [-82.618306111861301, 29.565336028000189, 0.0],
            [-82.619152670413669, 29.568340757283639, 0.0]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiLineString",
        "coordinates": [
          [
            [-82.62306516791385, 29.566111930454156, 0.0],
            [-82.618758856449034, 29.563742939021793, 0.0],
            [-82.618212862210015, 29.563577318475456, 0.0]
          ]
        ]
      }
    }
  ]
}

我想要达到的目标:

{
  "type": "FeatureCollection",
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
  },

  "features": [{
      "type": "Feature",
      "geometry": {
        "type": "MultiLineString",
        "coordinates": [
          [
            [-82.619152670413143, 29.568340757283536, 0.0],
            [-82.619147188198966, 29.568355832670516, 0.0],
            [-82.607558975018591, 29.580299204829011, 0.0]
          ],
          [
            [-82.619152670413513, 29.568340757283536, 0.0],
            [-82.619490683489488, 29.568318912277654, 0.0],
            [-82.629348688631055, 29.569000553128618, 0.0]
          ],
          [
            [-82.629348688631055, 29.569000553128618, 0.0],
            [-82.62943243076478, 29.568922074598046, 0.0],
            [-82.623065167913538, 29.56611193045412, 0.0]
          ],
          [
            [-82.618039923193663, 29.563657436904819, 0.0],
            [-82.618306111861301, 29.565336028000189, 0.0],
            [-82.619152670413669, 29.568340757283639, 0.0]
          ],
          [
            [-82.62306516791385, 29.566111930454156, 0.0],
            [-82.618758856449034, 29.563742939021793, 0.0],
            [-82.618212862210015, 29.563577318475456, 0.0]
          ]
        ]
      }
    }
  ]
}

简单解决方案的关键是认识到组合的 "coordinates" 数组可以由过滤器计算:

[.features[] | .geometry | .coordinates[]] 

可简写为:

[.features[].geometry.coordinates[]] 

我们称这个数组为$combined。然后可以通过更新 .features 来获得解决方案,如下所示:

.features = [.features[0] | (.geometry.coordinates = $combined)]

一个完整的解决方案是:

[.features[].geometry.coordinates[]] as $combined
| .features = [.features[0] | (.geometry.coordinates = $combined)]

这可以使用 |= 运算符进一步简化:

[.features[].geometry.coordinates[]] as $combined
| .features |= [.[0] | (.geometry.coordinates = $combined)]