如何使用jq在单个MultiLineString GeoJSON数组下合并数组?

时间:2018-08-21 15:16:40

标签: json geojson 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]
          ]
        ]
      }
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

简单解决方案的关键是要认识到组合的“坐标”数组可以通过过滤器进行计算:

[.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)]