首页 > 解决方案 > 如何在JQ中翻转线和改变键

问题描述

我正在创建我校园的交互式地图,我的想法是在此链接中复制我在 uMaps 上所做的事情。geojson 是从 UMap 下载的,我正在使用它附带的坐标。

我的第一个问题是我在 json 中的坐标,最初是一个 GeoJson,排序错误,我的 long 排在第一位,然后是 lat,因此在解析 Google Maps 时无法正确读取。

杰森:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "Almoxarifado / Patrimônio"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -52.163317,
              -32.075462
            ],
            [
              -52.163884,
              -32.075467
            ],
            [
              -52.163883,
              -32.075336
            ],
            [
              -52.163321,
              -32.075332
            ],
            [
              -52.163317,
              -32.075462
            ]
          ]
        ]
      }
    },
   {
    ...
   },
   {
    ...
   },
   ...
  ]
}

所以,我必须翻转坐标线以正确放入我的 Google Maps Api。

我的第二个问题是将“类型”键更改为“层”,以便在我的应用程序中更好地分离层。

我试过了:

.features[] | .["type"]  |= .["new value"]

如何改变值并且只接受浮点值

任何帮助、建议或指导将不胜感激。

标签: jsonkeyjqreverselatitude-longitude

解决方案


第1部分

翻转坐标线

为了清晰和易于测试,让我们定义一个辅助函数:

# input: a JSON object with a coordinates array of arrays of pairs
def flip: .coordinates |= map(map(reverse)) ;

甚至更好——在调用之前reverse,检查数组是否有预期的长度,例如:

def flip:
  .coordinates
  |= map(map(if length == 2 then reverse 
             else error("incorrect length: \(length)")
             end)) ;

为了翻转坐标,我们现在可以简单地写:

.features[].geometry |= flip

第2部分

将“类型”键更改为“层”

{layer: .type} + .
| del(.type)

把它放在一起

{layer:.type} + .
| del(.type)
| .features[].geometry |= flip

推荐阅读