當前位置: 華文星空 > 新聞

從地圖軟件中提取城市的地鐵路線數據

2024-02-07新聞

獲取高德地圖數據

第一步,開啟高德地圖:https:// map.amap.com/subway/ind ex.html

第二步,開啟開發者工具(按 F12 或 ⌥ ⌘ I )。註意,如果你是第一次開啟,瀏覽器可能會阻止你,請允許啟動開發者許可權。

第三步,切換到 Network 分頁。第一次開啟時,這裏可能是空的。

或者也可能發現已經堆滿了非常多內容,你可以點選清空按鈕。方便等會找到想要的數據。

第四步,重新整理頁面,會發現現在已經多了好多東西了。其中我劃紅線看到的 subway.json ,就是我們要找的數據。

不過,目前預設是北京的數據。我想收集廣州的。所以,回到網頁中,切換城市。

然後發現,這裏多了2個檔。第一個的 drw_guangzhou.json 就是我要的。

第五步,把它保存到桌面,作為 subway.json

數據分析:

"l" 是一個陣列,包含了所有路線的數據,也就是網頁中這個黑色導航欄。

遍歷陣列 "l" 中的每個元素:提取 "ln" 路線名稱,"st" 也是一個陣列,裏面是所有站點的數據。然後再遍歷陣列 "st" 中的每個元素,提取裏面的 "n" 站點名稱。

OK。有了思路,就可以開始寫程式碼了。

整理地鐵路線數據

新建一個 python 檔,也放在桌面,執行它

import json # 地圖數據 data_file = 'subway.json' # 匯出的檔 output_file = 'output.json' # 匯入數據 json_data = {} with open ( data_file , 'r' , encoding = "utf-8" ) as file : json_data = json . load ( file ) # 整理數據 lines = [] for item in json_data [ 'l' ]: line_name = item [ 'ln' ] station_names = [ station [ 'n' ] for station in item [ 'st' ]] lines . append ({ line_name : station_names }) # 保存成檔 with open ( output_file , 'w' , encoding = "utf-8" ) as file : json . dump ( lines , file , ensure_ascii = False , indent = 4 )

就得到了這樣一份數據

[ { "1號線" : [ "西塱" , "坑口" , "花地灣" , "芳村" , "黃沙" , "長壽路" , "陳家祠" , "西門口" , "公園前" , "農講所" , "烈士陵園" , "東山口" , "楊箕" , "體育西路" , "體育中心" , "廣州東站" ] }, { "佛山2號線" : [ "廣州南站" , "林嶽東(2號線)" , "林嶽西" , "石洲" , "仙湧" , "花卉世界" , "登洲" , "灣華" , "石梁" , "魁奇路" , "沙崗" , "石灣" , "張槎" , "智慧新城" , "綠島湖" , "湖湧" , "南莊" ] }, { "2號線" : [ "廣州南站" , "石壁" , "會江" , "南浦" , "洛溪" , "南洲" , "東曉南" , "江泰路" , "昌崗" , "江南西" , "市二宮" , "海珠廣場" , "公園前" , "紀念堂" , "越秀公園" , "廣州火車站" , "三元裏" , "飛翔公園" , "白雲公園" , "白雲文化廣場" , "蕭崗" , "江夏" , "黃邊" , "嘉禾望崗" ] }, { "3號線" : [ "番禺廣場" , "市橋" , "漢溪長隆" , "大石" , "廈滘" , "瀝滘" , "大塘" , "客村" , "廣州塔" , "珠江新城" , "體育西路" , "石牌橋" , "崗頂" , "華師" , "五山" , "天河客運站" ] }, { "3號線(北延段)" : [ "機場北(2號航站樓)" , "機場南(1號航站樓)" , "高增" , "人和" , "龍歸" , "嘉禾望崗" , "白雲大道北" , "永泰" , "同和" , "京溪南方醫院" , "梅花園" , "燕塘" , "廣州東站" , "林和西" , "體育西路" ] }, { "4號線" : [ "黃村" , "車陂" , "車陂南" , "萬勝圍" , "官洲" , "大學城北" , "大學城南" , "新造" , "石碁" , "海傍" , "低湧" , "東湧" , "慶盛" , "黃閣帕蒂德馬斯哈德" , "黃閣" , "蕉門" , "金洲" , "飛沙角" , "廣隆" , "大湧" , "塘坑" , "南橫" , "南沙客運港" ] }, { "5號線" : [ "滘口" , "坦尾" , "中山八" , "西場" , "西村" , "廣州火車站" , "小北" , "淘金" , "區莊" , "動物園" , "楊箕" , "五羊邨" , "珠江新城" , "獵德" , "潭村" , "員村" , "科韻路" , "車陂南" , "東圃" , "三溪" , "魚珠" , "大沙地" , "大沙東" , "文沖" , "雙沙" , "廟頭" , "夏園" , "保盈大道" , "夏港" , "黃埔新港" ] }, { "6號線" : [ "香雪" , "蘿崗" , "蘇元" , "暹崗" , "金峰" , "黃陂" , "高塘石" , "柯木塱" , "龍洞" , "植物園" , "長湴" , "天河客運站" , "燕塘" , "天平架" , "沙河頂" , "黃花崗" , "區莊" , "東山口" , "東湖" , "團一大廣場" , "北京路" , "海珠廣場" , "一德路" , "文化公園" , "黃沙" , "如意坊" , "坦尾" , "河沙" , "沙貝" , "橫沙" , "潯峰崗" ] }, { "7號線" : [ "燕山" , "水西" , "蘿崗" , "科豐路" , "加莊" , "姬堂" , "大沙東" , "裕豐圍" , "長洲" , "深井" , "大學城南" , "板橋" , "員崗" , "南村萬博" , "漢溪長隆" , "鐘村" , "謝村" , "石壁" , "廣州南站" , "大洲" , "陳村北" , "陳村" , "錦龍" , "南湧" , "美的" , "北滘公園" , "美的大道" ] }, { "8號線" : [ "滘心" , "亭崗" , "石井" , "小坪" , "石潭" , "聚龍" , "上步" , "同德" , "鵝掌坦" , "西村" , "彩虹橋" , "陳家祠" , "華林寺" , "文化公園" , "同福西" , "鳳凰新村" , "沙園" , "寶崗大道" , "昌崗" , "曉港" , "中大" , "鷺江" , "客村" , "赤崗" , "磨碟沙" , "新港東" , "琶洲" , "萬勝圍" ] }, { "9號線" : [ "高增" , "清塘" , "清布" , "蓮塘" , "馬鞍山公園" , "花都廣場" , "花果山公園" , "花城路" , "廣州北站" , "花都帕蒂德馬斯哈德" , "飛鵝嶺" ] }, { "13號線" : [ "魚珠" , "裕豐圍" , "雙崗" , "南海神廟" , "夏園" , "南崗" , "沙村" , "白江" , "新塘" , "官湖" , "新沙" ] }, { "14號線" : [ "嘉禾望崗" , "白雲東平" , "夏良" , "太和" , "竹料" , "鐘落潭" , "馬瀝" , "新和" , "太平" , "神崗" , "赤草" , "從化客運站" , "東風" ] }, { "14號線支線(知識城線)" : [ "新和" , "紅衛" , "新南" , "楓下" , "知識城" , "何棠下" , "旺村" , "湯村" , "鎮龍北" , "鎮龍" ] }, { "18號線" : [ "冼村" , "磨碟沙" , "龍潭" , "沙溪" , "南村萬博" , "番禺廣場" , "橫瀝" , "萬頃沙" ] }, { "21號線" : [ "員村" , "天河公園" , "棠東" , "黃村" , "大觀南路" , "天河智慧城" , "神舟路" , "科學城" , "蘇元" , "水西" , "長平" , "金坑" , "鎮龍西" , "鎮龍" , "中新" , "坑貝" , "鳳崗" , "朱村" , "山田" , "鐘崗" , "增城廣場" ] }, { "22號線" : [ "陳頭崗" , "廣州南站" , "市廣路" , "番禺廣場" ] }, { "APM線" : [ "廣州塔" , "海心沙" , "大劇院" , "花城大道" , "婦兒中心" , "黃埔大道" , "天河南" , "體育中心南" , "林和西" ] }, { "廣佛線" : [ "瀝滘" , "南洲" , "石溪" , "燕崗" , "沙園" , "沙湧" , "鶴洞" , "西塱" , "菊樹" , "龍溪" , "金融高新區" , "千燈湖" , "礌崗" , "南桂路" , "桂城" , "朝安" , "普君北路" , "祖廟" , "同濟路" , "季華園" , "魁奇路" , "瀾石" , "世紀蓮" , "東平" , "新城東" ] } ]

繼續擴充套件一下。

我想找出某個站點是否和其他站點有交集,也就是中轉站

篩選包含站點的路線

import json # 前面整理好的數據 data_file = 'output.json' # 要尋找交集的站點名稱 find_stations = '廣州東站' # 匯入檔 with open(data_file, 'r', encoding="utf-8") as file: data = json.load(file) # 搜尋符合的站點資訊 lines = [line for line in data if any(find_stations in stations for stations in line.values())] # 輸出 print(json.dumps(lines, indent=2, ensure_ascii=False))

匯出:

[ { "1號線" : [ "西塱" , "坑口" , "花地灣" , "芳村" , "黃沙" , "長壽路" , "陳家祠" , "西門口" , "公園前" , "農講所" , "烈士陵園" , "東山口" , "楊箕" , "體育西路" , "體育中心" , "廣州東站" ] }, { "3號線(北延段)" : [ "機場北(2號航站樓)" , "機場南(1號航站樓)" , "高增" , "人和" , "龍歸" , "嘉禾望崗" , "白雲大道北" , "永泰" , "同和" , "京溪南方醫院" , "梅花園" , "燕塘" , "廣州東站" , "林和西" , "體育西路" ] } ]

計算站點之間距離