"""
File for importing route data from a json file
"""
import json
import os
import matplotlib.pyplot as plt
from rdp import rdp
import numpy as np
def get_data(file_name):
"""
method to retrieve JSON data from "file"
:param file_name: string representing file in which JSON data is stored
:return data: Pyhonic data created from JSON file information
"""
with open(os.path.join(os.sys.path[0], file_name), "r") as data_file:
data = json.load(data_file) # load data from JSON file
return data
if __name__== "__main__":
file_name = 'json_data.json'
routes = get_data(file_name)
print("Total Time")
print(routes[0]["totalTime"])
print("Total Distance")
print(routes[0]["totalDistance"])
routesPos = 0
edgePos = 0
edgeDistance = 0
alts = []
distances = []
line_coords = []
while routesPos < len(routes):
while edgePos < len(routes[routesPos]["edges"]):
edgeDistance = edgeDistance + routes[routesPos]["edges"][edgePos]["edgeDistance"]
distances.append(edgeDistance)
alts.append(routes[routesPos]["edges"][edgePos]["endLocation"]["alt"])
edgePos += 1
plt.plot(distances, alts, color='blue')
coords = plt.gca().lines[-1].get_xydata() # Get coords for most recently plotted line
line_coords.append(coords)
edgeDistance = 0
routesPos += 1
edgePos = 0
rdp_res = []
for line in line_coords:
rdp_res.append(rdp(line))
插入代码行
plt.gca().lines[-1].get_xydata()
(就在创建绘图的那一行下方:plt.plot(distances, alts, color='blue')
)将允许您提取所绘制的每条线的所有点的x-y坐标。在然后我创建了一个空列表
line_coords
,用于存储四条线中每一条的x-y坐标,然后使用RDP算法迭代这四组坐标:列表
^{pr2}$rdp_res
包含RDP算法对四行中每一行的输出:我们可以比较每行的坐标数:
每行运行RDP后剩余的坐标数:
下面我粘贴了您的原始代码,包括我的所有修改:
相关问题 更多 >
编程相关推荐