# Path: roadmaptools/calculate_curvature.py
# def get_curvature(coords):
#     if len(coords) < 3:
#         return [0, 0]  # no curvature on edge
#     else:
#         total_curvature = 0
#         max_curvature = -1
#         length_of_edge = 0
#         for i in range(0, len(coords) - 2):
#             point_a = get_node(coords[i])
#             point_b = get_node(coords[i + 1])
#             point_c = get_node(coords[i + 2])
# 
#             length_c = get_distance_between_coords(point_a, point_b)
#             length_a = get_distance_between_coords(point_b, point_c)
#             length_b = get_distance_between_coords(point_c, point_a)
# 
#             # k = 0.5 * (length_b+length_a+length_c)
#             # area = math.sqrt(k*(k-length_a)*(k-length_b)*(k-length_c))
#             # radius = (length_b*length_a*length_c)/(4*area)
#             angle = calculate_angle_in_degree(length_a, length_b, length_c)
#             distance = length_c + length_a
#             curvature = angle / distance
#             if curvature > max_curvature:
#                 max_curvature = curvature
#             total_curvature += angle
#             length_of_edge += distance
# 
#         return [total_curvature / length_of_edge, max_curvature]
# 
# Path: roadmaptools/init.py

# 
# Path: roadmaptools/printer.py
# def print_info(info, file=sys.stdout, end="\n"):
#     print("[{0}]: {1}".format(datetime.now().strftime('%H:%M:%S'), info), file=file, end=end)


from typing import Dict
from roadmaptools.calculate_curvature import get_curvature
from geojson import LineString, Feature, FeatureCollection
from tqdm import tqdm
from roadmaptools.init import config
from roadmaptools.printer import print_info
import geojson
import networkx as nx
import codecs
import argparse
import sys
import time
import roadmaptools.inout
import roadmaptools.road_structures

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#


thresholds = [0, 0.01, 0.5, 1, 2]


def simplify_geojson(input_file=config.sanitized_geojson_file, output_file=config.simplified_file):
    print_info('Simplifying geoJSON')
    start_time = time.time()

    geojson_file = roadmaptools.inout.load_geojson(input_file)

    print_info("Simplification process started")

    # l_check set True whether you don't want to simplify edges with different number of lanes
    # c_check set True whether you don't want to simplify edges with different curvature
    geojson_out = get_simplified_geojson(geojson_file, l_check=False, c_check=False)


# Path: roadmaptools/calculate_curvature.py
# def get_curvature(coords):
#     if len(coords) < 3:
#         return [0, 0]  # no curvature on edge
#     else:
#         total_curvature = 0
#         max_curvature = -1
#         length_of_edge = 0
#         for i in range(0, len(coords) - 2):
#             point_a = get_node(coords[i])
#             point_b = get_node(coords[i + 1])
#             point_c = get_node(coords[i + 2])
# 
#             length_c = get_distance_between_coords(point_a, point_b)
#             length_a = get_distance_between_coords(point_b, point_c)
#             length_b = get_distance_between_coords(point_c, point_a)
# 
#             # k = 0.5 * (length_b+length_a+length_c)
#             # area = math.sqrt(k*(k-length_a)*(k-length_b)*(k-length_c))
#             # radius = (length_b*length_a*length_c)/(4*area)
#             angle = calculate_angle_in_degree(length_a, length_b, length_c)
#             distance = length_c + length_a
#             curvature = angle / distance
#             if curvature > max_curvature:
#                 max_curvature = curvature
#             total_curvature += angle
#             length_of_edge += distance
# 
#         return [total_curvature / length_of_edge, max_curvature]
# 
# Path: roadmaptools/init.py

# 
# Path: roadmaptools/printer.py
# def print_info(info, file=sys.stdout, end="\n"):
#     print("[{0}]: {1}".format(datetime.now().strftime('%H:%M:%S'), info), file=file, end=end)


# Path: roadmaptools/simplify_graph.py
from typing import Dict
from roadmaptools.calculate_curvature import get_curvature
from geojson import LineString, Feature, FeatureCollection
from tqdm import tqdm
from roadmaptools.init import config
from roadmaptools.printer import print_info
import geojson
import networkx as nx
import codecs
import argparse
import sys
import time
import roadmaptools.inout
import roadmaptools.road_structures

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#


thresholds = [0, 0.01, 0.5, 1, 2]


def simplify_geojson(input_file=config.sanitized_geojson_file, output_file=config.simplified_file):
    print_info('Simplifying geoJSON')
    start_time = time.time()

    geojson_file = roadmaptools.inout.load_geojson(input_file)

    print_info("Simplification process started")

    # l_check set True whether you don't want to simplify edges with different number of lanes
    # c_check set True whether you don't want to simplify edges with different curvature
    geojson_out = get_simplified_geojson(geojson_file, l_check=False, c_check=False)


    print_info('Simplification completed. (%.2f secs)' % (time.time() - start_time))
