从python中的图形中提取点

时间:2019-02-11 17:40:26

标签: python algorithm matplotlib graph

我一直在寻找一种方法来从我在Python中绘制的4个图形中提取点,以便在这些点上运行Ramer Douglas peucker算法。理想情况下,数据将以一系列x和y坐标表示,因为当前数据集的表示方式不同。每个图由大约2000个点组成,但在数据集中未将其格式化为X和Y坐标。如果有人有任何建议,我已经附上了用于绘制图形以及数据集的代码。

干杯!

1 个答案:

答案 0 :(得分:1)

插入代码行plt.gca().lines[-1].get_xydata()(在创建绘图的行的下方:plt.plot(distances, alts, color='blue')的下方)将允许您为绘制的每条线的所有点提取xy坐标。

然后,我创建了一个空列表line_coords,以存储四行中每行的x-y坐标,然后使用RDP算法遍历这四组坐标中的每组:

rdp_res = []
for line in line_coords:
    rdp_res.append(rdp(line))

列表rdp_res包含四行中每行的RDP算法输出:

[array([[4.92025194e+00, 3.80000000e+01],
        [5.24522347e+01, 3.80000000e+01],
        [8.59726863e+01, 3.77000000e+01],
        ...,
        [3.07740662e+04, 3.60000000e+01],
        [3.08035662e+04, 3.60000000e+01],
        [3.08075068e+04, 3.59000000e+01]]),
 array([[4.92025194e+00, 3.80000000e+01],
        [5.24522347e+01, 3.80000000e+01],
        [8.59726863e+01, 3.77000000e+01],
        ...,
        [2.81487733e+04, 5.20000000e+01],
        [2.81536662e+04, 5.18000000e+01],
        [2.82000946e+04, 5.18000000e+01]]),
 array([[4.92025194e+00, 3.80000000e+01],
        [5.24522347e+01, 3.80000000e+01],
        [8.59726863e+01, 3.77000000e+01],
        ...,
        [2.37758154e+04, 1.26000000e+01],
        [2.37973123e+04, 1.30000000e+01],
        [2.38301772e+04, 1.38000000e+01]]),
 array([[4.92025194e+00, 3.80000000e+01],
        [5.24522347e+01, 3.80000000e+01],
        [8.59726863e+01, 3.77000000e+01],
        ...,
        [2.59717233e+04, 1.83600000e+02],
        [2.60321544e+04, 1.83400000e+02],
        [2.60884831e+04, 1.83400000e+02]])]

我们可以比较每行中的坐标数:

line_coords[0].shape, line_coords[1].shape, line_coords[2].shape, line_coords[3].shape

((1167, 2), (2133, 2), (2869, 2), (3597, 2))

在每行上运行RDP之后剩余的坐标数:

rdp_res[0].shape, rdp_res[1].shape, rdp_res[2].shape, rdp_res[3].shape

((1080, 2), (1947, 2), (2643, 2), (3360, 2))

下面,我粘贴了您的原始代码,并进行了所有修改:

"""
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))