Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature : Tile Thumbnail #289

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/api-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ fairpredictor==0.0.26

rasterio==1.3.8
numpy<2.0.0

mercantile==1.2.1

40 changes: 31 additions & 9 deletions backend/core/serializers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,11 +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()
thumbnail_url = serializers.SerializerMethodField()

class Meta:
model = Model
Expand All @@ -66,9 +65,31 @@ def create(self, validated_data):
validated_data["created_by"] = user
return super().create(validated_data)

def get_accuracy(
self, obj
): ## this might have performance problem when db grows bigger , consider adding indexes / view in db
# 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):
training = Training.objects.filter(id=obj.published_training).first()
if training:
return training.accuracy
Expand All @@ -82,7 +103,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):
"""
Expand Down
Loading