Created
September 17, 2025 05:17
-
-
Save Rafnuss/62efc0069d52302ec6b57479c900eb19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import xml.etree.ElementTree as ET | |
| import json | |
| import os | |
| file_path = [ | |
| "~/Downloads/3 kantone for 11 years - eBird Trip Report (2).kml", | |
| "~/Downloads/3 kantone for 11 years - eBird Trip Report (3).kml", | |
| ] | |
| output_geojson_path = "~/Downloads/output1.geojson" | |
| # Your manual folder order | |
| manual_order = [ | |
| "S273670258", | |
| "S273668254", | |
| "S273666758", | |
| "S273664648", | |
| "S273664678", | |
| "S273589521", | |
| "S273574105", | |
| "S273574415", | |
| "S273574453", | |
| "S273574484", | |
| "S273574674", | |
| "S273574250", | |
| "S273574202", | |
| "S273574284", | |
| "S273574637", | |
| "S273574305", | |
| "S273574545", | |
| "S273574519", | |
| "S273574580", | |
| "S273499879", | |
| "S273499902", | |
| "S273495351", | |
| "S273495361", | |
| "S273410386", | |
| "S273396818", | |
| "S273396882", | |
| "S273397062", | |
| "S273396942", | |
| "S273337044", | |
| "S273337134", | |
| ] # replace with your desired order | |
| manual_order.reverse() | |
| ns = {"kml": "http://www.opengis.net/kml/2.2"} | |
| # Collect coordinates per folder | |
| folder_coords = {} | |
| def parse_folder(folder): | |
| folder_name_elem = folder.find("kml:name", ns) | |
| if folder_name_elem is None: | |
| return | |
| folder_name = folder_name_elem.text | |
| if not folder_name.startswith("S"): | |
| return | |
| coords_list = [] | |
| for pm in folder.findall("kml:Placemark", ns): | |
| coords_elem = pm.find(".//kml:coordinates", ns) | |
| if coords_elem is not None: | |
| for c in coords_elem.text.strip().split(): | |
| lon, lat, *_ = map(float, c.split(",")) | |
| coords_list.append([lon, lat]) | |
| if coords_list: | |
| folder_coords[folder_name] = coords_list | |
| for p in file_path: | |
| p = os.path.expanduser(p) # Expand '~' to full home path | |
| if not os.path.isfile(p): | |
| raise FileNotFoundError(f"File not found: {p}") | |
| # Load the KML file | |
| tree = ET.parse(p) | |
| root = tree.getroot() | |
| # Parse top-level folders | |
| for folder in root.findall(".//kml:Folder", ns): | |
| parse_folder(folder) | |
| # Merge coordinates according to manual order | |
| all_coords = [] | |
| for fname in manual_order: | |
| if fname in folder_coords: | |
| all_coords.extend(folder_coords[fname]) | |
| # Create GeoJSON LineString | |
| geojson = { | |
| "type": "FeatureCollection", | |
| "features": [ | |
| { | |
| "type": "Feature", | |
| "geometry": {"type": "LineString", "coordinates": all_coords}, | |
| "properties": {}, | |
| } | |
| ], | |
| } | |
| output_geojson_path = os.path.expanduser(output_geojson_path) | |
| with open(output_geojson_path, "w") as f: | |
| json.dump(geojson, f, indent=2) | |
| print( | |
| f"GeoJSON {output_geojson_path} has been created with {len(all_coords)} points, ordered by manual folder order." | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment