json - 如何在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"]
如何改变值并且只接受浮点值
任何帮助、建议或指导将不胜感激。
解决方案
第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
推荐阅读
- node.js - 在 Raspberry Pi 上恢复互联网连接后,Firestore 侦听器无法正常工作
- java - Mongo Java 与 POJO 连接的内存泄漏导致 OOM
- html - 具有固定高度的背景颜色扰乱了我的文档流程
- ios - 无法初始化对象
- docker - Kubenetes 是否开发用于创建 Pod 对象的代码?
- php - 下载文件而不是在新选项卡中打开它以在 Web 浏览器中查看
- python - bs4:遍历一系列页面并按类将元素收集到 csv 输出中
- c# - WPF中的动态控制切换
- linux - 如何通过 Ansible 获取根设备?
- xsd - 一个 XSD 复杂元素中可以有多个指标吗?