【问题标题】:How to turn json file coordinates in to a coordinates list that python can use如何将json文件坐标转换为python可以使用的坐标列表
【发布时间】:2022-01-07 22:20:15
【问题描述】:

如何转这个:

   dict = {"objects":[
                {
      "type": "node",
      "id": 46049199,
      "lat": 52.2925916,
      "lon": 4.9485062,
      "tags": {
      "traffic_calming": "table"
        }
    },
  {
      "type": "node",
      "id": 46053305,
      "lat": 52.2936706,
       "lon": 4.9469035,
       "tags": {
       "barrier": "gate"
      }
      },
      {
      "type": "node",
        "id": 46053351,
       "lat": 52.2934958,
       "lon": 4.9463902
        }

进入这个

      coordTD2 = {46049199: [52.2925916, 4.9485062], 46053305: [52.2936706, 4.9469035], 4 
        46053351: [52.2934958, 4.9463902]}

【问题讨论】:

  • 到目前为止你尝试了什么?你被困在哪里了?请提供minimal reproducible example,以便更轻松地为您提供帮助。您的任务可以通过迭代您的数据并通过其 dict 键访问相关数据来解决。你知道列表和字典是如何工作的吗?

标签: python json coordinates overpass-api


【解决方案1】:

取出objects列表中的每一个字典,然后挑选出id、lat和lon如下:

dic = {"objects":[
                {
      "type": "node",
      "id": 46049199,
      "lat": 52.2925916,
      "lon": 4.9485062,
      "tags": {
      "traffic_calming": "table"
        }
    },
  {
      "type": "node",
      "id": 46053305,
      "lat": 52.2936706,
       "lon": 4.9469035,
       "tags": {
       "barrier": "gate"
      }
      },
      {
      "type": "node",
        "id": 46053351,
       "lat": 52.2934958,
       "lon": 4.9463902
        },
    ]
}

newdic = {}
for d in dic['objects']:
   newdic[d['id']] = [d['lat'], d['lon']]

print(newdic)

【讨论】:

  • 或作为单行表达式:{item['id']: [item['lat'], item['lon']] for item in d['objects']}
  • 你是救命稻草!
  • 如果这对你有用,请接受答案,如果你喜欢,请点赞。
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多