0

I was looking for a way to extract points from 4 graphs i have plotted in Python in order to run the Ramer Douglas peucker algorithm on the points. Ideally the data would be presented in a series of x and y co-ordinates as the current data set is presented differently. Each graph is made up of around 2000 points but they are not formatted as X and Y co-ordinates in the data set. I have attached the code used to plot the graphs as well as the data set if anyone has any suggestions.

Cheers!!

1 Answer 1

1

Inserting the line of code plt.gca().lines[-1].get_xydata() (right below the line where you create a plot: plt.plot(distances, alts, color='blue')) will allow you to extract the x-y coordinates for all points of each line you plot.

I then created an empty list, line_coords, to store the x-y coordinates for each of the four lines, and then iterated over each of these four sets of coordinates with the RDP algorithm:

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

The list rdp_res contains the RDP algorithm's output for each of the four lines:

[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]])]

We can compare the number of coordinates in each line:

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

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

With the number of coordinates remaining after running RDP on each line:

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

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

Below I've pasted in your original code with all of my modifications included:

"""
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))
Sign up to request clarification or add additional context in comments.

1 Comment

You're quite welcome. And sure, the coordinates for all points in each line are in the line_coords list. For example, if you want to see the points in the first line, you could just run line_coords[0] to see a list of all x-y coords in that line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.