Private
Public Access
1
0

list of courses api

This commit is contained in:
2024-02-28 09:34:14 +01:00
parent 9851871ecb
commit eb126856a7
4 changed files with 89 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import time
from django.db import IntegrityError
import uuid
from django.conf import settings
import math
import geocoder
@@ -120,6 +121,50 @@ xmlns_uris_dict = {'gx': 'http://www.google.com/kml/ext/2.2',
'': "http://www.opengis.net/kml/2.2"}
def get_polar_angle(point, reference_point):
"""
Calculate the polar angle of a point with respect to a reference point.
"""
try:
delta_x = point.longitude - reference_point.longitude
delta_y = point.latitude - reference_point.latitude
except AttributeError:
delta_x = point['longitude'] - reference_point.longitude
delta_y = point['latitude'] - reference_point.latitude
return math.atan2(delta_y, delta_x)
class Coordinate:
def __init__(self, latitude, longitude):
self.latitude = latitude
self.longitude = longitude
def get_coordinates_centerpoint(coordinates):
try:
centroid_latitude = sum(coord.latitude for coord in coordinates) / len(coordinates)
centroid_longitude = sum(coord.longitude for coord in coordinates) / len(coordinates)
centroid = Coordinate(centroid_latitude, centroid_longitude)
except AttributeError:
centroid_latitude = sum(coord['latitude'] for coord in coordinates) / len(coordinates)
centroid_longitude = sum(coord['longitude'] for coord in coordinates) / len(coordinates)
centroid = Coordinate(centroid_latitude, centroid_longitude)
return centroid
def sort_coordinates_ccw(coordinates):
"""
Sort coordinates in counterclockwise order.
"""
# Find the reference point (centroid)
centroid = get_coordinates_centerpoint(coordinates)
# Sort coordinates based on polar angle with respect to the centroid
sorted_coords = sorted(coordinates, key=lambda coord: get_polar_angle(coord, centroid))
return sorted_coords
def crewnerdcourse(doc):
courses = []
for course in doc:
@@ -162,6 +207,8 @@ def get_polygons(polygonpms):
'latitude': float(coordinates[1]),
})
points = sort_coordinates_ccw(points)
polygons.append({
'name': name,
'points': points
@@ -206,6 +253,7 @@ def coursetokml(course):
coordinates.text = ''
points = GeoPoint.objects.filter(
polygon=polygon).order_by("order_in_poly")
points = sort_coordinates_ccw(points)
for point in points:
coordinates.text += '{lon},{lat},0 '.format(
lat=point.latitude,