如何从一个大的(基于.json的)列表结构中提取路径坐标

2024-04-20 00:00:13 发布

您现在位置:Python中文网/ 问答频道 /正文

对于每一条路,我都试图从与该特定“路”相关联的所有“节点”构建一个坐标列表。你知道吗

我想,既然所有的东西都在这个大列表中(也就是“elements”:),我就可以使用一个“for”循环…但结果是…我不能用这种方式访问“type”:“node”(即坐标所在的位置)。你知道吗

如果我不能使用for循环,如何在“type”:“node”中找到匹配的坐标??你知道吗

列表结构的示例(您可以看到这两个'ways'here的完整集合…我只是在下面缩短了它):

  "elements": [

{
  "type": "way",
  "id": 57935838
  },
  "nodes": [
    279385160,
    1142007444
  ],
  "tags": {
    "highway": "secondary",
    "name": "Kauno g."
  }
},
{
  "type": "way",
  "id": 223130469
  },
  "nodes": [
    470874618,
    2362142222
  ],
  "tags": {
    "highway": "secondary",
    "name": "Agluonos g."
  }
},
{
  "type": "node",
  "id": 470874618,
  "lat": 55.6933076,
  "lon": 21.1517616
},
{
  "type": "node",
  "id": 2362142222,
  "lat": 55.6931543,
  "lon": 21.1514953
},
{
  "type": "node",
  "id": 1142007444,
  "lat": 55.6991153,
  "lon": 21.1647621
},
{
  "type": "node",
  "id": 279385160,
  "lat": 55.7001553,
  "lon": 21.1671538
}
]

如果在元素上使用“for”循环(对于id=57935838},会得到什么:

{u'tags': {u'name': u'Kauno g.', u'highway': u'secondary'}, u'nodes': [279385160, 1142007444], u'type': u'way', u'id': 57935838}

例如:

import json
from urllib2 import urlopen

    all_data_dict = get_overpass_json_data(line_url2)

    with open(outfile, 'w') as geojson_file:
        for item in all_data_dict['elements']:
            print item

例如(来自here的两个点的方式):

>> print all_data_dict

>> {u'elements': [{u'changeset': 29434078, u'uid': 91490, u'tags': {u'bridge': u'yes', u'layer': u'1', u'ref': u'F72', u'surface': u'asphalt', u'highway': u'tertiary'}, u'timestamp': u'2015-03-12T19:56:59Z', u'version': 2, u'user': u'Heinz_V', u'nodes': [1635609339, 1635609329], u'type': u'way', u'id': 150672234, u'center': {u'lat': 27.5894941, u'lon': 85.4801512}}, {u'lat': 27.5894735, u'lon': 85.4800892, u'type': u'node', u'id': 1635609329}, {u'lat': 27.5895146, u'lon': 85.4802131, u'type': u'node', u'id': 1635609339}], u'version': 0.6, u'osm3s': {u'timestamp_osm_base': u'2015-05-10T18:01:02Z', u'copyright': u'The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.'}, u'generator': u'Overpass API'}

Tags: nameidnode列表fordatatypetags
1条回答
网友
1楼 · 发布于 2024-04-20 00:00:13

很难从一个无效的JSON片段、一个模糊的描述和没有代码中找出你想要做什么或者你的问题是什么,但是

我认为你的elements应该是一个列表,里面有一堆dict,每个dict都有一个type。您需要其type是字符串'node'的所有元素。然后,对于它们中的每一个,您需要latlon值。那么,让我们写下:

coords = []
for element in elements:
    if element['type'] == 'node':
        coords.append((element['lat'], element['lon']))

或者,更简单地说:

[(e['lat'], e['lon']) for e in elements if e['type'] == 'node']

但如果我去掉前两个元素中每个nodes键之前的两个}散乱的}键,那么它将进行解析(当然,我只是猜测这是否是实际结构的内容……),我得到以下结果:

[(55.6933076, 21.1517616),
 (55.6931543, 21.1514953),
 (55.6991153, 21.1647621),
 (55.7001553, 21.1671538)]

如果您希望将其作为dict,将每个节点的ID映射到其lat/lon元组,那么同样的想法是:

coords = {}
for element in elements:
    if element['type'] == 'node':
        coords[element['id']] = (element['lat'], element['lon'])

或:

{e['id']: (e['lat'], e['lon']) for e in elements if e['type'] == 'node'}

这给了我:

{279385160: (55.7001553, 21.1671538),
 470874618: (55.6933076, 21.1517616),
 1142007444: (55.6991153, 21.1647621),
 2362142222: (55.6931543, 21.1514953)}

相关问题 更多 >