Skip to content

Instantly share code, notes, and snippets.

@Rafnuss
Created August 25, 2024 15:07
Show Gist options
  • Save Rafnuss/ea58a8943863b6c1fc1e97469d79ff1c to your computer and use it in GitHub Desktop.
Save Rafnuss/ea58a8943863b6c1fc1e97469d79ff1c to your computer and use it in GitHub Desktop.
# Download eBird GPX track with https://www.faintlake.com/eBird/extension/Enhancements/#E2
# Convert to gpx to geojson (probably can be done in python too)
# Compute total duration from https://www.faintlake.com/eBird/compiler/
# run code below
import json
import gpxpy
import gpxpy.gpx
from datetime import datetime, timedelta
def invert_and_merge_geojson(input_file, output_geojson_file, output_gpx_file, total_duration):
# Read the GeoJSON file
with open(input_file, 'r') as file:
geojson_data = json.load(file)
if 'features' in geojson_data:
# Invert the order of the features
features = geojson_data['features'][::-1]
# Merge all LineString coordinates into one list
merged_coordinates = []
for feature in features:
if feature['geometry']['type'] == 'LineString':
merged_coordinates.extend(feature['geometry']['coordinates'])
# Create a new GeoJSON with a single LineString feature
merged_geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": merged_coordinates
},
"properties": {}
}
]
}
# Save the merged GeoJSON to a file
with open(output_geojson_file, 'w') as file:
json.dump(merged_geojson, file, indent=4)
print(f"Merged GeoJSON saved to {output_geojson_file}")
# Convert to GPX with time information
gpx = gpxpy.gpx.GPX()
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
# Calculate total points and time intervals
num_points = len(merged_coordinates)
total_seconds = total_duration.total_seconds()
time_interval = total_seconds / num_points # Time interval between each point
# Set a starting time for the first point
start_time = datetime.now()
for i, coord in enumerate(merged_coordinates):
lon, lat = coord[0], coord[1]
# Increment time for each point based on calculated interval
point_time = start_time + timedelta(seconds=i * time_interval)
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(lat, lon, time=point_time))
# Save GPX file
with open(output_gpx_file, 'w') as file:
file.write(gpx.to_xml())
print(f"Merged GPX file with precise time data saved to {output_gpx_file}")
else:
print("Invalid GeoJSON format: 'features' not found")
# Usage
input_geojson = '/Users/rafnuss/Downloads/Un-Liech-ing 10 years - eBird Trip Report.geojson' # Replace with your input file path
output_geojson = '/Users/rafnuss/Downloads/output.geojson' # Replace with your desired output file path
output_gpx = '/Users/rafnuss/Downloads/output.gpx' # Replace with your desired output GPX file path
# Specify the total duration of the activity (20 hours and 54 minutes)
total_duration = timedelta(hours=20-3, minutes=54-42)
invert_and_merge_geojson(input_geojson, output_geojson, output_gpx, total_duration)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment