From 19b3e45019d9d5c5a042167626598b8f4d824fb1 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Tue, 8 Oct 2024 14:26:21 +0200 Subject: [PATCH 1/4] Add centroid lat and lon --- backend/core/serializers.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/backend/core/serializers.py b/backend/core/serializers.py index 3f4cc556..629a0ba9 100644 --- a/backend/core/serializers.py +++ b/backend/core/serializers.py @@ -1,11 +1,11 @@ +import mercantile from django.conf import settings +from login.models import OsmUser from rest_framework import serializers from rest_framework_gis.serializers import ( GeoFeatureModelSerializer, # this will be used if we used to serialize as geojson ) -from login.models import OsmUser - from .models import * # from .tasks import train_model @@ -50,6 +50,7 @@ class ModelSerializer( ): # serializers are used to translate models objects to api created_by = UserSerializer(read_only=True) accuracy = serializers.SerializerMethodField() + tile = serializers.SerializerMethodField() class Meta: model = Model @@ -66,6 +67,17 @@ def create(self, validated_data): validated_data["created_by"] = user return super().create(validated_data) + def get_tile(self, obj): + aoi = AOI.objects.filter(dataset=obj.dataset).first() + if aoi and aoi.geom: + centroid = aoi.geom.centroid.coords + try: + tile = mercantile.tile(centroid[0], centroid[1], zoom=18) + return [tile.x, tile.y, 18] + except: + pass + return None + def get_accuracy( self, obj ): ## this might have performance problem when db grows bigger , consider adding indexes / view in db @@ -82,7 +94,8 @@ class ModelCentroidSerializer(GeoFeatureModelSerializer): class Meta: model = Model geo_field = "geometry" - fields = ("mid", "name", "geometry") + fields = ("mid", "geometry") + # fields = ("mid", "name", "geometry") def get_geometry(self, obj): """ From db90cce93066b7d0d6a0cebc71b15b17ea323665 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Tue, 8 Oct 2024 14:26:40 +0200 Subject: [PATCH 2/4] Add mercantile requirements --- backend/api-requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/api-requirements.txt b/backend/api-requirements.txt index 17187cea..e74dc0a7 100644 --- a/backend/api-requirements.txt +++ b/backend/api-requirements.txt @@ -23,5 +23,5 @@ fairpredictor==0.0.26 rasterio==1.3.8 numpy<2.0.0 - +mercantile==1.2.1 From d9d4f62ea5bd4aadbb7cea386c235fa88a3f4b07 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Tue, 8 Oct 2024 14:53:51 +0200 Subject: [PATCH 3/4] Added thumnail_url --- backend/core/serializers.py | 41 ++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/backend/core/serializers.py b/backend/core/serializers.py index 629a0ba9..c49a72a0 100644 --- a/backend/core/serializers.py +++ b/backend/core/serializers.py @@ -45,12 +45,10 @@ class Meta: ] -class ModelSerializer( - serializers.ModelSerializer -): # serializers are used to translate models objects to api +class ModelSerializer(serializers.ModelSerializer): created_by = UserSerializer(read_only=True) accuracy = serializers.SerializerMethodField() - tile = serializers.SerializerMethodField() + thumbnail_url = serializers.SerializerMethodField() class Meta: model = Model @@ -67,20 +65,31 @@ def create(self, validated_data): validated_data["created_by"] = user return super().create(validated_data) - def get_tile(self, obj): - aoi = AOI.objects.filter(dataset=obj.dataset).first() - if aoi and aoi.geom: - centroid = aoi.geom.centroid.coords - try: - tile = mercantile.tile(centroid[0], centroid[1], zoom=18) - return [tile.x, tile.y, 18] - except: - pass + def get_training(self, obj): + if not hasattr(self, "_cached_training"): + self._cached_training = Training.objects.filter( + id=obj.published_training + ).first() + return self._cached_training + + def get_thumbnail_url(self, obj): + training = Training.objects.filter(id=obj.published_training).first() + + if training: + if training.source_imagery: + aoi = AOI.objects.filter(dataset=obj.dataset).first() + if aoi and aoi.geom: + centroid = ( + aoi.geom.centroid.coords + ) ## Centroid can be stored in db table if required when project grows bigger + try: + tile = mercantile.tile(centroid[0], centroid[1], zoom=18) + return training.source_imagery.format(x=tile.x, y=tile.y, z=18) + except Exception as ex: + pass return None - def get_accuracy( - self, obj - ): ## this might have performance problem when db grows bigger , consider adding indexes / view in db + def get_accuracy(self, obj): training = Training.objects.filter(id=obj.published_training).first() if training: return training.accuracy From a15bf2a1a4171d189f11ac0b0817c012b0a38fd1 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Tue, 8 Oct 2024 14:55:25 +0200 Subject: [PATCH 4/4] Commented get_training function for now --- backend/core/serializers.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/backend/core/serializers.py b/backend/core/serializers.py index c49a72a0..fbcd1efc 100644 --- a/backend/core/serializers.py +++ b/backend/core/serializers.py @@ -65,12 +65,12 @@ def create(self, validated_data): validated_data["created_by"] = user return super().create(validated_data) - def get_training(self, obj): - if not hasattr(self, "_cached_training"): - self._cached_training = Training.objects.filter( - id=obj.published_training - ).first() - return self._cached_training + # def get_training(self, obj): + # if not hasattr(self, "_cached_training"): + # self._cached_training = Training.objects.filter( + # id=obj.published_training + # ).first() + # return self._cached_training def get_thumbnail_url(self, obj): training = Training.objects.filter(id=obj.published_training).first()