Skip to content

Commit

Permalink
Merge pull request #157 from hotosm/feature/conflation
Browse files Browse the repository at this point in the history
Feature: Conflation
  • Loading branch information
kshitijrajsharma authored Aug 21, 2023
2 parents 39a597b + d8fd91a commit 06f6daa
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
2 changes: 2 additions & 0 deletions backend/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .views import (
AOIViewSet,
APIStatus,
ConflateGeojson,
DatasetViewSet,
FeedbackAOIViewset,
FeedbackLabelViewset,
Expand Down Expand Up @@ -53,6 +54,7 @@
path("feedback/training/submit/", FeedbackView.as_view()),
path("status/", APIStatus.as_view()),
path("geojson2osm/", geojson2osmconverter, name="geojson2osmconverter"),
path("conflate/", ConflateGeojson, name="Conflate Geojson"),
path("aoi/gpx/<int:aoi_id>/", GenerateGpxView.as_view()),
path(
"feedback-aoi/gpx/<int:feedback_aoi_id>/", GenerateFeedbackAOIGpxView.as_view()
Expand Down
13 changes: 13 additions & 0 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from hot_fair_utilities import polygonize, predict, vectorize
from login.authentication import OsmAuthentication
from login.permissions import IsOsmAuthenticated
from osmconflator import conflate_geojson
from rest_framework import decorators, serializers, status, viewsets
from rest_framework.decorators import api_view
from rest_framework.exceptions import ValidationError
Expand Down Expand Up @@ -364,6 +365,18 @@ def geojson2osmconverter(request):
return HttpResponse(osm_xml, content_type="application/xml")


@api_view(["POST"])
def ConflateGeojson(request):
try:
geojson_data = json.loads(request.body)["geojson"]
except json.JSONDecodeError:
return HttpResponseBadRequest("Invalid input")

conflated_geojson = conflate_geojson(geojson_data, remove_conflated=True)

return Response(conflated_geojson,status = 200)


@api_view(["GET"])
def run_task_status(request, run_id: str):
"""Gives the status of running task from background process
Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ flower==1.2.0
validators==0.20.0
gpxpy==1.5.0
hot-fair-utilities
geojson2osm==0.0.1
geojson2osm==0.0.1
osmconflator==0.0.6
57 changes: 53 additions & 4 deletions frontend/src/components/Layout/Start/Prediction/Prediction.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const Prediction = () => {
let tileBoundaryLayer = null;
const [error, setError] = useState(false);
const [josmLoading, setJosmLoading] = useState(false);
const [conflateLoading, setConflateLoading] = useState(false);

const [feedbackLoading, setFeedbackLoading] = useState(false);
const [predictionZoomlevel, setpredictionZoomlevel] = useState(null);

Expand Down Expand Up @@ -282,11 +284,12 @@ const Prediction = () => {
return;
}

// Remove the "id" and "featuretype" properties from each feature in the "features" array
const modifiedPredictions = {
// Remove the "id", action , duplicate and intersect propertiesproperties from each feature in the "features" array
const postprocessed_predictions = {
...predictions,
features: predictions.features.map((feature) => {
const { id, action, ...newProps } = feature.properties;
const { id, action, duplicate, intersect, ...newProps } =
feature.properties;
return {
...feature,
properties: newProps,
Expand All @@ -296,7 +299,7 @@ const Prediction = () => {

try {
const response = await axios.post("/geojson2osm/", {
geojson: modifiedPredictions,
geojson: postprocessed_predictions,
});
if (response.status === 200) {
const osmUrl = new URL("http://127.0.0.1:8111/load_data");
Expand Down Expand Up @@ -326,6 +329,40 @@ const Prediction = () => {
}
}

async function conflateFeatures() {
setConflateLoading(true);
if (!predictions) {
setError("No predictions available");
return;
}
// Remove the "id" , "action" from each feature in the "features" array
const modifiedPredictions = {
...predictions,
features: predictions.features.map((feature) => {
const { id, action, ...newProps } = feature.properties;
return {
...feature,
properties: newProps,
};
}),
};
try {
const response = await axios.post("/conflate/", {
geojson: modifiedPredictions,
});
if (response.status == 200) {
setPredictions(null);
const updatedPredictions = addIdsToPredictions(response.data);
setPredictions(updatedPredictions);
settotalPredictionsCount(updatedPredictions.features.length);
}
} catch (error) {
setError(error);
} finally {
setConflateLoading(false);
}
}

function MyComponent() {
const map = useMapEvents({
zoomend: (e) => {
Expand Down Expand Up @@ -556,6 +593,18 @@ const Prediction = () => {
</Box>
)}
{error && <Alert severity="error">{error}</Alert>}
{predictions && (
<LoadingButton
variant="contained"
color="secondary"
onClick={conflateFeatures}
loading={conflateLoading}
size="small"
sx={{ mt: 1 }}
>
Conflate OSM Features
</LoadingButton>
)}
{predictions && (
<LoadingButton
variant="contained"
Expand Down

0 comments on commit 06f6daa

Please sign in to comment.