首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jq将数组合并到单个MultiLineString GeoJSON数组下?

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

Stack Overflow用户
提问于 2018-08-21 23:16:40
回答 1查看 116关注 0票数 1

数组中的坐标在多个GeoJSON MultiLineString结构之间划分。我想合并所有坐标,将它们保存在自己的数组中,放在一个单一的MultiLineString结构下。如何使用jq实现这一点?

这是原始文件(出于示例目的而修改)

代码语言:javascript
复制
{
  "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]
          ]
        ]
      }
    }
  ]
}

我想要实现的是:

代码语言:javascript
复制
{
  "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]
          ]
        ]
      }
    }
  ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-21 23:50:57

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

代码语言:javascript
复制
[.features[] | .geometry | .coordinates[]] 

它可以缩写为:

代码语言:javascript
复制
[.features[].geometry.coordinates[]] 

让我们将此数组命名为$combined。然后可以通过更新.features获得解决方案,如下所示:

代码语言:javascript
复制
.features = [.features[0] | (.geometry.coordinates = $combined)]

一个完整的解决方案是:

代码语言:javascript
复制
[.features[].geometry.coordinates[]] as $combined
| .features = [.features[0] | (.geometry.coordinates = $combined)]

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

代码语言:javascript
复制
[.features[].geometry.coordinates[]] as $combined
| .features |= [.[0] | (.geometry.coordinates = $combined)]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51951847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档