如何在 JQ 中翻转线条和更改键 第 1 部分第 2 部分组合起来

问题描述

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

我的第一个问题是我在json中的坐标,原来是一个GeoJson,排序错了,我的long先到lat,因此解析谷歌地图时无法正确读取。

杰森:

{
  "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"]

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

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

解决方法

第 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