Skip to content

Instantly share code, notes, and snippets.

@monperrus
Created March 16, 2025 10:30
Show Gist options
  • Save monperrus/6a2ef620f0c845ebc68475f475a60073 to your computer and use it in GitHub Desktop.
Save monperrus/6a2ef620f0c845ebc68475f475a60073 to your computer and use it in GitHub Desktop.
go over all tx files, if the tcx coordinates are in canada, print the file name
import os
import xml.etree.ElementTree as ET
import glob
def is_in_canada(lat, lon):
# Rough boundaries for Canada
# Latitude: Between 41.7° and 83.1° N
# Longitude: Between -141.0° and -52.6° W
return (41.7 <= float(lat) <= 83.1) and (-141.0 <= float(lon) <= -52.6)
def check_tcx_file(file_path):
try:
tree = ET.parse(file_path)
root = tree.getroot()
# Handle namespace in TCX files
ns = {'ns': 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2'}
# Find all position elements
positions = root.findall('.//ns:Position', ns)
points_in_canada = 0
for pos in positions:
lat = pos.find('ns:LatitudeDegrees', ns).text
lon = pos.find('ns:LongitudeDegrees', ns).text
if is_in_canada(lat, lon):
points_in_canada += 1
return points_in_canada >= 50
except Exception as e:
print(f"Error processing {file_path}: {str(e)}")
return False
# Find all TCX files in current directory and subdirectories
tcx_files = glob.glob('**/*.tcx', recursive=True)
# Check each file
for file_path in tcx_files:
if check_tcx_file(file_path):
print(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment