Skip to content

Commit

Permalink
feat: add GPX parser
Browse files Browse the repository at this point in the history
  • Loading branch information
paulschreiber committed Nov 28, 2023
1 parent 149c47a commit cea7396
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions terraso_backend/apps/core/gis/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
logger = structlog.get_logger(__name__)

supported_drivers["KML"] = "rw"
supported_drivers["GPX"] = "rw"


def is_geojson_file_extension(file):
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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:
Expand All @@ -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")

0 comments on commit cea7396

Please sign in to comment.