Created
May 8, 2026 12:39
-
-
Save aspose-com-gists/2eb00dc45d5ebf86d7e5f955ae68528f to your computer and use it in GitHub Desktop.
GPX to GEOJSON Conversion Tutorial in Python
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
| # -*- coding: utf-8 -*- | |
| import sys | |
| import json | |
| from aspose.gis import GisFile, GisException | |
| def convert_gpx_to_geojson(input_path: str, output_path: str) -> None: | |
| try: | |
| # Load GPX file | |
| gpx = GisFile.open(input_path) | |
| # Convert to GeoJSON string | |
| geojson_str = gpx.to_geojson() | |
| # Write GeoJSON to file | |
| with open(output_path, "w", encoding="utf-8") as out_file: | |
| out_file.write(geojson_str) | |
| # Simple validation | |
| with open(output_path, "r", encoding="utf-8") as in_file: | |
| data = json.load(in_file) | |
| if data.get("type") != "FeatureCollection": | |
| raise ValueError("Invalid GeoJSON output.") | |
| print(f"Conversion successful: {output_path}") | |
| except (GisException, OSError, ValueError) as e: | |
| print(f"Error during conversion: {e}", file=sys.stderr) | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| # Example usage | |
| convert_gpx_to_geojson("sample.gpx", "sample.geojson") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment