mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2026-05-10 17:40:00 +01:00
151 lines
5.1 KiB
Python
151 lines
5.1 KiB
Python
import xml.etree.ElementTree as ET
|
|
|
|
from position_utils import which_area # file-type import, not module type.
|
|
|
|
def run_limit_tests(which_area_func):
|
|
# Boundary limits derived from GPX data
|
|
west_limit = 13.72476763 # 1626-22 [cite: 1]
|
|
east_limit = 13.86031535 # 1626-256 [cite: 3]
|
|
|
|
print("Running Border Logic Tests...")
|
|
|
|
# 1. Invalid point to the West
|
|
res, area = which_area_func(47.69, west_limit - 0.01)
|
|
print(f"Test West: Expected (False, 'None'), Got ({res}, '{area}')")
|
|
|
|
# 2. Invalid point to the East
|
|
res, area = which_area_func(47.70, east_limit + 0.01)
|
|
print(f"Test East: Expected (False, 'None'), Got ({res}, '{area}')")
|
|
|
|
|
|
def generate_test_data():
|
|
offset = 0.0005
|
|
|
|
# 6 base points extracted from the track to ensure we cover the whole E/W span
|
|
# Coordinates from source GPX
|
|
|
|
base_coords = [
|
|
(47.69048595, 13.72476763 + 2* offset), # Far West (1626-22)
|
|
(47.68532296, 13.73512238), # Monotonic section 1 (1626-13)
|
|
(47.67441976, 13.76998479), # Central dip (1626-308)
|
|
(47.68909077, 13.79091566), # Rising section (1626-294)
|
|
(47.70536617, 13.82402883), # Northern peak (1626-278)
|
|
(47.70002466, 13.86031535 - 2* offset) # Far East (1626-256)
|
|
]
|
|
|
|
test_points = []
|
|
|
|
for lat, lon in base_coords:
|
|
# Generate North point (Area 1626)
|
|
test_points.append({'lat': lat + offset, 'lon': lon + offset, 'area': '1626', 'desc': 'North_Test'})
|
|
# Generate South point (Area 1623)
|
|
test_points.append({'lat': lat - offset, 'lon': lon - offset, 'area': '1623', 'desc': 'South_Test'})
|
|
|
|
return test_points
|
|
|
|
def export_test_gpx(test_points, filename="test_points.gpx"):
|
|
gpx = ET.Element("gpx", version="1.1", creator="Python Script",
|
|
xmlns="http://www.topografix.com/GPX/1/1")
|
|
|
|
for pt in test_points:
|
|
wpt = ET.SubElement(gpx, "wpt", lat=str(pt['lat']), lon=str(pt['lon']))
|
|
name = ET.SubElement(wpt, "name")
|
|
name.text = f"{pt['area']}_{pt['desc']}"
|
|
desc = ET.SubElement(wpt, "desc")
|
|
desc.text = f"Expected Area: {pt['area']}"
|
|
|
|
tree = ET.ElementTree(gpx)
|
|
tree.write(filename, encoding="utf-8", xml_declaration=True)
|
|
print(f"Successfully created {filename} with 12 test waypoints.")
|
|
|
|
def run_12_point_test(which_area_func):
|
|
points = generate_test_data()
|
|
export_test_gpx(points)
|
|
|
|
passed = 0
|
|
|
|
print(f"{'Lat':<12} | {'Lon':<12} | {'Expected':<10} | {'Result'}")
|
|
print("-" * 55)
|
|
|
|
for pt in points:
|
|
valid, area = which_area_func(pt['lat'], pt['lon'])
|
|
status = "PASS" if (valid and area == pt['area']) else "FAIL"
|
|
if status == "PASS": passed += 1
|
|
print(f"{pt['lat']:<12.6f} | {pt['lon']:<12.6f} | {pt['area']:<10} | {status}")
|
|
|
|
print(f"\nSummary: {passed}/12 points passed.")
|
|
|
|
|
|
import random
|
|
import time
|
|
import xml.etree.ElementTree as ET
|
|
from position_utils import which_area
|
|
|
|
def generate_performance_test(num_points=200):
|
|
# Border limits derived from GPX data
|
|
west_lon = 13.72476763 # Point 1626-22
|
|
east_lon = 13.86031535 # Point 1626-256
|
|
|
|
# Calculate the span to create a square N/S range
|
|
span = east_lon - west_lon
|
|
|
|
# Midpoint of the border (approximate center for the test square)
|
|
mid_lat = 47.69
|
|
min_lat = mid_lat - (span / 2)
|
|
max_lat = mid_lat + (span / 2)
|
|
|
|
test_points = []
|
|
for _ in range(num_points):
|
|
lat = random.uniform(min_lat, max_lat)
|
|
lon = random.uniform(west_lon, east_lon)
|
|
test_points.append((lat, lon))
|
|
|
|
return test_points
|
|
|
|
def export_random_points_gpx(points_with_results, filename="random_test_points.gpx"):
|
|
root = ET.Element("gpx", version="1.1", creator="PerformanceTester",
|
|
xmlns="http://www.topografix.com/GPX/1/1")
|
|
|
|
for lat, lon, area, valid in points_with_results:
|
|
wpt = ET.SubElement(root, "wpt", lat=f"{lat:.8f}", lon=f"{lon:.8f}")
|
|
name = ET.SubElement(wpt, "name")
|
|
name.text = f"{area}"
|
|
desc = ET.SubElement(wpt, "desc")
|
|
desc.text = f"Valid: {valid}"
|
|
|
|
tree = ET.ElementTree(root)
|
|
tree.write(filename, encoding="utf-8", xml_declaration=True)
|
|
print(f"Random points exported to {filename}")
|
|
|
|
def run_performance_test():
|
|
# 1. Generate coordinates
|
|
coords = generate_performance_test(200)
|
|
results = []
|
|
|
|
# 2. Start Timing
|
|
start_time = time.perf_counter()
|
|
|
|
for lat, lon in coords:
|
|
valid, area = which_area(lat, lon)
|
|
results.append((lat, lon, area, valid))
|
|
|
|
end_time = time.perf_counter()
|
|
|
|
# 3. Output results
|
|
total_time_ms = (end_time - start_time) * 1000
|
|
avg_time_us = (total_time_ms * 1000) / len(coords)
|
|
|
|
print(f"--- Performance Results ---")
|
|
print(f"Total time for {len(coords)} points: {total_time_ms:.4f} ms")
|
|
print(f"Average time per lookup: {avg_time_us:.2f} microseconds")
|
|
|
|
# 4. Export for visualization
|
|
export_random_points_gpx(results)
|
|
|
|
if __name__ == "__main__":
|
|
run_limit_tests(which_area)
|
|
run_12_point_test(which_area)
|
|
run_performance_test()
|
|
|
|
|