Skip to content

Commit

Permalink
Merge pull request #152 from hotosm/feature/gpx_feedback_aoi
Browse files Browse the repository at this point in the history
Feature : GPX Generator for feedback
  • Loading branch information
kshitijrajsharma authored Aug 14, 2023
2 parents 8f76f43 + 105d7dd commit 9e94555
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
6 changes: 5 additions & 1 deletion backend/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
FeedbackLabelViewset,
FeedbackView,
FeedbackViewset,
GenerateFeedbackAOIGpxView,
GenerateGpxView,
LabelViewSet,
ModelViewSet,
Expand Down Expand Up @@ -49,10 +50,13 @@
path("training/status/<str:run_id>/", run_task_status),
path("training/publish/<int:training_id>/", publish_training),
path("prediction/", PredictionView.as_view()),
path("apply/feedback/", FeedbackView.as_view()),
path("feedback/training/submit/", FeedbackView.as_view()),
path("status/", APIStatus.as_view()),
path("geojson2osm/", geojson2osmconverter, name="geojson2osmconverter"),
path("aoi/gpx/<int:aoi_id>/", GenerateGpxView.as_view()),
path(
"feedback-aoi/gpx/<int:feedback_aoi_id>/", GenerateFeedbackAOIGpxView.as_view()
),
path("workspace/", TrainingWorkspaceView.as_view()),
path(
"workspace/download/<path:lookup_dir>/", TrainingWorkspaceDownloadView.as_view()
Expand Down
29 changes: 29 additions & 0 deletions backend/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import json
import math
import os
from datetime import datetime
from uuid import uuid4
from xml.dom import ValidationErr
from zipfile import ZipFile

import requests
from django.conf import settings
from gpxpy.gpx import GPX, GPXTrack, GPXTrackSegment, GPXWaypoint
from tqdm import tqdm

from .models import AOI, FeedbackAOI, FeedbackLabel, Label
Expand Down Expand Up @@ -243,6 +245,33 @@ def remove_file(path: str) -> None:
os.unlink(path)


def gpx_generator(geom_json):
"""Generates GPX for give geojson geometry
Args:
geom_json (_type_): _description_
Returns:
xml: gpx
"""

gpx = GPX()
gpx_track = GPXTrack()
gpx.tracks.append(gpx_track)
gpx_segment = GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
for point in geom_json["coordinates"][0]:
# Append each point as a GPXWaypoint to the GPXTrackSegment
gpx_segment.points.append(GPXWaypoint(point[1], point[0]))
gpx.creator = "fAIr"
gpx_track.name = "Don't Edit this Boundary"
gpx_track.description = "Map inside this boundary and go back to fAIr UI"
gpx.time = datetime.now()
gpx.link = "https://github.com/hotosm/fAIr"
gpx.link_text = "AI Assisted Mapping - fAIr : HOTOSM"
return gpx.to_xml()


def process_feature(feature, aoi_id, foreign_key_id, feedback=False):
"""Multi thread process of features"""
properties = feature["properties"]
Expand Down
30 changes: 14 additions & 16 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from django_filters.rest_framework import DjangoFilterBackend
from drf_yasg.utils import swagger_auto_schema
from geojson2osm import geojson2osm
from gpxpy.gpx import GPX, GPXTrack, GPXTrackSegment, GPXWaypoint
from hot_fair_utilities import polygonize, predict, vectorize
from login.authentication import OsmAuthentication
from login.permissions import IsOsmAuthenticated
Expand Down Expand Up @@ -66,6 +65,7 @@
download_imagery,
get_dir_size,
get_start_end_download_coords,
gpx_generator,
process_rawdata,
request_rawdata,
)
Expand Down Expand Up @@ -624,21 +624,19 @@ def get(self, request, aoi_id: int):
# Convert the polygon field to GPX format
geom_json = json.loads(aoi.geom.json)
# Create a new GPX object
gpx = GPX()
gpx_track = GPXTrack()
gpx.tracks.append(gpx_track)
gpx_segment = GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
for point in geom_json["coordinates"][0]:
# Append each point as a GPXWaypoint to the GPXTrackSegment
gpx_segment.points.append(GPXWaypoint(point[1], point[0]))
gpx.creator = "fAIr Backend"
gpx_track.name = f"AOI of id {aoi_id} , Don't Edit this Boundary"
gpx_track.description = "This is coming from AI Assisted Mapping - fAIr : HOTOSM , Map inside this boundary and go back to fAIr UI"
gpx.time = datetime.now()
gpx.link = "https://github.com/hotosm/fAIr"
gpx.link_text = "AI Assisted Mapping - fAIr : HOTOSM"
return HttpResponse(gpx.to_xml(), content_type="application/xml")
gpx_xml=gpx_generator(geom_json)
return HttpResponse(gpx_xml, content_type="application/xml")


class GenerateFeedbackAOIGpxView(APIView):
def get(self, request, feedback_aoi_id: int):
aoi = get_object_or_404(FeedbackAOI, id=feedback_aoi_id)
# Convert the polygon field to GPX format
geom_json = json.loads(aoi.geom.json)
# Create a new GPX object
gpx_xml=gpx_generator(geom_json)
return HttpResponse(gpx_xml, content_type="application/xml")



class TrainingWorkspaceView(APIView):
Expand Down

0 comments on commit 9e94555

Please sign in to comment.