# code for segmentation and labbelling data
#%% 
from data_loader import load_segments_from_folder

from calculations import *
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

if __name__ == "__main__":
    data = []

    i = 0

    # for each segment
    for segment in load_segments_from_folder(r'directory'):
       
      
        # calculate distance between plots
        segment["distance"] = calculate_distance_serie(segment["lat"], segment["lon"])

        #calculating label base on the difference of covered distance and shortest distance
   
        distance = segment["distance"].sum()
  
        distances = calculate_points_distances_to_center_line(
            segment["lat"], segment["lon"]
        )

        shortest_distance = calculate_distance(segment["lat"].iloc[0], segment["lon"].iloc[0], segment["lat"].iloc[-1], segment["lon"].iloc[-1])

        ratio = distance/shortest_distance

        label = 1

        if ratio < 1.003:
            label = 0
        if ratio > 1.04:
            label = 2

              data.append(ratio)

        i = i + 1
        print(i)
        if i > 10000:
            break

        # image output to directory based on label
        fig = Figure()
       

        
        ax = fig.gca()
        ax.set_aspect('equal', adjustable='box')
        ax.margins(0)
        ax.axis("off")
        ax.set_position((0, 0, 1, 1))

        ax.plot(
            segment["lat"],
            segment["lon"],
            "bo",
            segment["lat"],
            segment["lon"],
            "k",
            markersize=5,
            linewidth=3,
        )

      
    plt.hist(data, density=False, bins=100)  # density=False would make counts
    plt.ylabel('Počet1')
    plt.xlabel('Počet2')
    plt.show()
    
# %%
