From cea73963457bb9201f8ca8439be381862fe0fa59 Mon Sep 17 00:00:00 2001 From: Paul Schreiber Date: Tue, 21 Nov 2023 14:53:52 -0500 Subject: [PATCH] feat: add GPX parser --- terraso_backend/apps/core/gis/parsers.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/terraso_backend/apps/core/gis/parsers.py b/terraso_backend/apps/core/gis/parsers.py index 17e90f5c9..115bacdf4 100644 --- a/terraso_backend/apps/core/gis/parsers.py +++ b/terraso_backend/apps/core/gis/parsers.py @@ -28,6 +28,7 @@ logger = structlog.get_logger(__name__) supported_drivers["KML"] = "rw" +supported_drivers["GPX"] = "rw" def is_geojson_file_extension(file): @@ -56,6 +57,10 @@ def is_kmz_file_extension(file): return file.name.endswith(".kmz") +def is_gpx_file_extension(file): + return file.name.endswith(".gpx") + + def parse_kml_file(file): gdf = gpd.read_file(file, driver="KML") return json.loads(gdf.to_json()) @@ -106,6 +111,11 @@ def parse_shapefile(file): return json.loads(gdf_transformed.to_json()) +def parse_gpx_file(file): + gdf = gpd.read_file(file, driver="GPX") + return json.loads(gdf.to_json()) + + def parse_file_to_geojson(file): if is_shape_file_extension(file): try: @@ -131,5 +141,11 @@ def parse_file_to_geojson(file): except Exception as e: logger.error("Error parsing geojson file", error=e) raise ValidationError("invalid_geojson_file") + elif is_gpx_file_extension(file): + try: + return parse_gpx_file(file) + except Exception as e: + logger.error("Error parsing gpx file", error=e) + raise ValidationError("invalid_gpx_file") else: raise ValidationError("invalid_file_type")