UPDATE
This commit is contained in:
+147
-184
@@ -1,10 +1,14 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from django.apps import apps
|
||||
from django.core.paginator import EmptyPage, Paginator
|
||||
from django.db.models import Avg
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
from rest_framework import status
|
||||
from drf_spectacular.utils import (
|
||||
OpenApiExample,
|
||||
OpenApiParameter,
|
||||
OpenApiResponse,
|
||||
extend_schema,
|
||||
inline_serializer,
|
||||
@@ -25,19 +29,20 @@ from .models import (
|
||||
RemoteSensingSubdivisionResult,
|
||||
SoilLocation,
|
||||
)
|
||||
from farm_data.models import SensorData
|
||||
|
||||
from .data_driven_subdivision import DEFAULT_CLUSTER_FEATURES
|
||||
from .serializers import (
|
||||
BlockSubdivisionSerializer,
|
||||
NdviHealthRequestSerializer,
|
||||
NdviHealthResponseSerializer,
|
||||
RemoteSensingCellObservationSerializer,
|
||||
RemoteSensingResponseSerializer,
|
||||
RemoteSensingResultQuerySerializer,
|
||||
RemoteSensingRunResultResponseSerializer,
|
||||
RemoteSensingFarmRequestSerializer,
|
||||
RemoteSensingRunSerializer,
|
||||
RemoteSensingRunStatusResponseSerializer,
|
||||
RemoteSensingSummarySerializer,
|
||||
RemoteSensingSubdivisionResultSerializer,
|
||||
RemoteSensingTriggerSerializer,
|
||||
SoilDataRequestSerializer,
|
||||
SoilLocationResponseSerializer,
|
||||
)
|
||||
@@ -90,7 +95,7 @@ RemoteSensingQueuedEnvelopeSerializer = build_envelope_serializer(
|
||||
"summary": RemoteSensingSummarySerializer(),
|
||||
"cells": drf_serializers.JSONField(),
|
||||
"run": drf_serializers.JSONField(allow_null=True),
|
||||
"task_id": drf_serializers.CharField(),
|
||||
"task_id": drf_serializers.UUIDField(),
|
||||
},
|
||||
),
|
||||
)
|
||||
@@ -98,19 +103,13 @@ RemoteSensingRunStatusEnvelopeSerializer = build_envelope_serializer(
|
||||
"RemoteSensingRunStatusEnvelopeSerializer",
|
||||
RemoteSensingRunStatusResponseSerializer,
|
||||
)
|
||||
RemoteSensingRunResultEnvelopeSerializer = build_envelope_serializer(
|
||||
"RemoteSensingRunResultEnvelopeSerializer",
|
||||
RemoteSensingRunResultResponseSerializer,
|
||||
)
|
||||
|
||||
|
||||
class SoilDataView(APIView):
|
||||
"""
|
||||
ثبت مختصات گوشههای مزرعه و بلوکهای تعریفشده توسط کشاورز.
|
||||
"""
|
||||
|
||||
@extend_schema(
|
||||
tags=["Soil Data"],
|
||||
tags=["Location Data"],
|
||||
summary="خواندن ساختار مزرعه و بلوکها (GET)",
|
||||
description="با ارسال lat و lon، ساختار ذخیرهشده مزرعه، بلوکها و آخرین خلاصه سنجشازدور هر بلوک بازگردانده میشود.",
|
||||
parameters=[
|
||||
@@ -175,7 +174,7 @@ class SoilDataView(APIView):
|
||||
)
|
||||
|
||||
@extend_schema(
|
||||
tags=["Soil Data"],
|
||||
tags=["Location Data"],
|
||||
summary="ثبت مزرعه و بلوکهای کشاورز (POST)",
|
||||
description="مختصات گوشههای مزرعه و boundary هر بلوک کشاورز ذخیره میشود. هیچ subdivision سنکرونی اجرا نمیشود.",
|
||||
request=SoilDataRequestSerializer,
|
||||
@@ -306,7 +305,7 @@ class SoilDataView(APIView):
|
||||
|
||||
class NdviHealthView(APIView):
|
||||
@extend_schema(
|
||||
tags=["Soil Data"],
|
||||
tags=["Location Data"],
|
||||
summary="دریافت NDVI سلامت مزرعه",
|
||||
description="با دریافت farm_uuid، داده NDVI سلامت پوشش گیاهی مزرعه را به صورت مستقل از dashboard برمی گرداند.",
|
||||
request=NdviHealthRequestSerializer,
|
||||
@@ -359,10 +358,10 @@ class NdviHealthView(APIView):
|
||||
|
||||
class RemoteSensingAnalysisView(APIView):
|
||||
@extend_schema(
|
||||
tags=["Soil Data"],
|
||||
tags=["Location Data"],
|
||||
summary="اجرای async تحلیل سنجشازدور و subdivision دادهمحور",
|
||||
description="برای location موجود، pipeline کامل grid + openEO + observation persistence + KMeans clustering در Celery صف میشود و sync اجرا نمیشود.",
|
||||
request=RemoteSensingTriggerSerializer,
|
||||
request=RemoteSensingFarmRequestSerializer,
|
||||
responses={
|
||||
202: build_response(
|
||||
RemoteSensingQueuedEnvelopeSerializer,
|
||||
@@ -381,21 +380,15 @@ class RemoteSensingAnalysisView(APIView):
|
||||
OpenApiExample(
|
||||
"نمونه درخواست remote sensing",
|
||||
value={
|
||||
"lat": 35.6892,
|
||||
"lon": 51.3890,
|
||||
"block_code": "block-1",
|
||||
"start_date": "2025-01-01",
|
||||
"end_date": "2025-01-31",
|
||||
"farm_uuid": "11111111-1111-1111-1111-111111111111",
|
||||
"force_refresh": False,
|
||||
"cluster_count": 3,
|
||||
"selected_features": ["ndvi", "ndwi", "soil_vv_db"],
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def post(self, request):
|
||||
serializer = RemoteSensingTriggerSerializer(data=request.data)
|
||||
serializer = RemoteSensingFarmRequestSerializer(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
return Response(
|
||||
{"code": 400, "msg": "داده نامعتبر.", "data": serializer.errors},
|
||||
@@ -403,37 +396,41 @@ class RemoteSensingAnalysisView(APIView):
|
||||
)
|
||||
|
||||
payload = serializer.validated_data
|
||||
location = _get_location_by_lat_lon(payload["lat"], payload["lon"], prefetch=True)
|
||||
farm = SensorData.objects.select_related("center_location").filter(farm_uuid=payload["farm_uuid"]).first()
|
||||
location = getattr(farm, "center_location", None)
|
||||
if location is None:
|
||||
return Response(
|
||||
{"code": 404, "msg": "location پیدا نشد.", "data": None},
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
|
||||
block_code = str(payload.get("block_code", "") or "").strip()
|
||||
temporal_end = timezone.localdate() - timedelta(days=1)
|
||||
temporal_start = temporal_end - timedelta(days=30)
|
||||
run = RemoteSensingRun.objects.create(
|
||||
soil_location=location,
|
||||
block_code=block_code,
|
||||
chunk_size_sqm=_resolve_chunk_size_for_location(location, block_code),
|
||||
temporal_start=payload["start_date"],
|
||||
temporal_end=payload["end_date"],
|
||||
block_code="",
|
||||
chunk_size_sqm=_resolve_chunk_size_for_location(location, ""),
|
||||
temporal_start=temporal_start,
|
||||
temporal_end=temporal_end,
|
||||
status=RemoteSensingRun.STATUS_PENDING,
|
||||
metadata={
|
||||
"requested_via": "api",
|
||||
"status_label": "pending",
|
||||
"cluster_count": payload.get("cluster_count"),
|
||||
"selected_features": payload.get("selected_features") or [],
|
||||
"requested_cluster_count": None,
|
||||
"selected_features": list(DEFAULT_CLUSTER_FEATURES),
|
||||
"farm_uuid": str(payload["farm_uuid"]),
|
||||
"scope": "all_blocks",
|
||||
},
|
||||
)
|
||||
task_result = run_remote_sensing_analysis_task.delay(
|
||||
soil_location_id=location.id,
|
||||
block_code=block_code,
|
||||
temporal_start=payload["start_date"].isoformat(),
|
||||
temporal_end=payload["end_date"].isoformat(),
|
||||
block_code="",
|
||||
temporal_start=temporal_start.isoformat(),
|
||||
temporal_end=temporal_end.isoformat(),
|
||||
force_refresh=payload.get("force_refresh", False),
|
||||
run_id=run.id,
|
||||
cluster_count=payload.get("cluster_count"),
|
||||
selected_features=payload.get("selected_features"),
|
||||
cluster_count=None,
|
||||
selected_features=list(DEFAULT_CLUSTER_FEATURES),
|
||||
)
|
||||
run.metadata = {**(run.metadata or {}), "task_id": task_result.id}
|
||||
run.save(update_fields=["metadata", "updated_at"])
|
||||
@@ -443,11 +440,11 @@ class RemoteSensingAnalysisView(APIView):
|
||||
"status": "processing",
|
||||
"source": "processing",
|
||||
"location": location_data,
|
||||
"block_code": block_code,
|
||||
"block_code": "",
|
||||
"chunk_size_sqm": run.chunk_size_sqm,
|
||||
"temporal_extent": {
|
||||
"start_date": payload["start_date"].isoformat(),
|
||||
"end_date": payload["end_date"].isoformat(),
|
||||
"start_date": temporal_start.isoformat(),
|
||||
"end_date": temporal_end.isoformat(),
|
||||
},
|
||||
"summary": _empty_remote_sensing_summary(),
|
||||
"cells": [],
|
||||
@@ -460,15 +457,11 @@ class RemoteSensingAnalysisView(APIView):
|
||||
)
|
||||
|
||||
@extend_schema(
|
||||
tags=["Soil Data"],
|
||||
tags=["Location Data"],
|
||||
summary="خواندن نتایج cache شده سنجشازدور و subdivision",
|
||||
description="فقط نتایج ذخیرهشده remote sensing و clustering را برمیگرداند و هیچ پردازش sync اجرا نمیکند.",
|
||||
parameters=[
|
||||
{"name": "lat", "in": "query", "required": True, "schema": {"type": "number"}},
|
||||
{"name": "lon", "in": "query", "required": True, "schema": {"type": "number"}},
|
||||
{"name": "block_code", "in": "query", "required": False, "schema": {"type": "string"}},
|
||||
{"name": "start_date", "in": "query", "required": True, "schema": {"type": "string", "format": "date"}},
|
||||
{"name": "end_date", "in": "query", "required": True, "schema": {"type": "string", "format": "date"}},
|
||||
{"name": "farm_uuid", "in": "query", "required": True, "schema": {"type": "string", "format": "uuid"}},
|
||||
{"name": "page", "in": "query", "required": False, "schema": {"type": "integer", "default": 1}},
|
||||
{"name": "page_size", "in": "query", "required": False, "schema": {"type": "integer", "default": 100}},
|
||||
],
|
||||
@@ -488,7 +481,7 @@ class RemoteSensingAnalysisView(APIView):
|
||||
},
|
||||
)
|
||||
def get(self, request):
|
||||
serializer = RemoteSensingResultQuerySerializer(data=request.query_params)
|
||||
serializer = RemoteSensingFarmRequestSerializer(data=request.query_params)
|
||||
if not serializer.is_valid():
|
||||
return Response(
|
||||
{"code": 400, "msg": "داده نامعتبر.", "data": serializer.errors},
|
||||
@@ -496,31 +489,34 @@ class RemoteSensingAnalysisView(APIView):
|
||||
)
|
||||
|
||||
payload = serializer.validated_data
|
||||
location = _get_location_by_lat_lon(payload["lat"], payload["lon"], prefetch=True)
|
||||
farm = SensorData.objects.select_related("center_location").filter(farm_uuid=payload["farm_uuid"]).first()
|
||||
location = getattr(farm, "center_location", None)
|
||||
if location is None:
|
||||
return Response(
|
||||
{"code": 404, "msg": "location پیدا نشد.", "data": None},
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
|
||||
block_code = str(payload.get("block_code", "") or "").strip()
|
||||
temporal_end = timezone.localdate() - timedelta(days=1)
|
||||
temporal_start = temporal_end - timedelta(days=30)
|
||||
block_code = ""
|
||||
observations = _get_remote_sensing_observations(
|
||||
location=location,
|
||||
block_code=block_code,
|
||||
start_date=payload["start_date"],
|
||||
end_date=payload["end_date"],
|
||||
start_date=temporal_start,
|
||||
end_date=temporal_end,
|
||||
)
|
||||
run = _get_latest_remote_sensing_run(
|
||||
location=location,
|
||||
block_code=block_code,
|
||||
start_date=payload["start_date"],
|
||||
end_date=payload["end_date"],
|
||||
start_date=temporal_start,
|
||||
end_date=temporal_end,
|
||||
)
|
||||
subdivision_result = _get_remote_sensing_subdivision_result(
|
||||
location=location,
|
||||
block_code=block_code,
|
||||
start_date=payload["start_date"],
|
||||
end_date=payload["end_date"],
|
||||
start_date=temporal_start,
|
||||
end_date=temporal_end,
|
||||
)
|
||||
|
||||
if not observations.exists():
|
||||
@@ -532,11 +528,11 @@ class RemoteSensingAnalysisView(APIView):
|
||||
"status": "processing" if processing else "not_found",
|
||||
"source": "processing" if processing else "database",
|
||||
"location": SoilLocationResponseSerializer(location).data,
|
||||
"block_code": block_code,
|
||||
"block_code": "",
|
||||
"chunk_size_sqm": getattr(run, "chunk_size_sqm", None),
|
||||
"temporal_extent": {
|
||||
"start_date": payload["start_date"].isoformat(),
|
||||
"end_date": payload["end_date"].isoformat(),
|
||||
"start_date": temporal_start.isoformat(),
|
||||
"end_date": temporal_end.isoformat(),
|
||||
},
|
||||
"summary": _empty_remote_sensing_summary(),
|
||||
"cells": [],
|
||||
@@ -576,11 +572,11 @@ class RemoteSensingAnalysisView(APIView):
|
||||
"status": "success",
|
||||
"source": "database",
|
||||
"location": SoilLocationResponseSerializer(location).data,
|
||||
"block_code": block_code,
|
||||
"block_code": "",
|
||||
"chunk_size_sqm": observations.first().cell.chunk_size_sqm,
|
||||
"temporal_extent": {
|
||||
"start_date": payload["start_date"].isoformat(),
|
||||
"end_date": payload["end_date"].isoformat(),
|
||||
"start_date": temporal_start.isoformat(),
|
||||
"end_date": temporal_end.isoformat(),
|
||||
},
|
||||
"summary": _build_remote_sensing_summary(observations),
|
||||
"cells": cells_data,
|
||||
@@ -597,54 +593,22 @@ class RemoteSensingAnalysisView(APIView):
|
||||
|
||||
class RemoteSensingRunStatusView(APIView):
|
||||
@extend_schema(
|
||||
tags=["Soil Data"],
|
||||
tags=["Location Data"],
|
||||
summary="وضعیت run تحلیل سنجشازدور",
|
||||
description="وضعیت async pipeline را با شناسه run برمیگرداند.",
|
||||
responses={
|
||||
200: build_response(
|
||||
RemoteSensingRunStatusEnvelopeSerializer,
|
||||
"وضعیت run بازگردانده شد.",
|
||||
),
|
||||
404: build_response(
|
||||
SoilErrorResponseSerializer,
|
||||
"run موردنظر پیدا نشد.",
|
||||
),
|
||||
},
|
||||
)
|
||||
def get(self, request, run_id):
|
||||
run = RemoteSensingRun.objects.filter(pk=run_id).select_related("soil_location").first()
|
||||
if run is None:
|
||||
return Response(
|
||||
{"code": 404, "msg": "run پیدا نشد.", "data": None},
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
|
||||
task_id = (run.metadata or {}).get("task_id")
|
||||
response_payload = {
|
||||
"status": RemoteSensingRunSerializer(run).data["status_label"],
|
||||
"source": "database",
|
||||
"run": RemoteSensingRunSerializer(run).data,
|
||||
"task_id": task_id,
|
||||
}
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": response_payload},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class RemoteSensingRunResultView(APIView):
|
||||
@extend_schema(
|
||||
tags=["Soil Data"],
|
||||
summary="نتیجه نهایی run تحلیل سنجشازدور",
|
||||
description="نتایج observation و subdivision دادهمحور را با شناسه run برمیگرداند.",
|
||||
description="وضعیت async pipeline را با task_id از نوع UUID برمیگرداند. این task_id همان شناسه تسک Celery ذخیرهشده در metadata.run است.",
|
||||
parameters=[
|
||||
{"name": "page", "in": "query", "required": False, "schema": {"type": "integer", "default": 1}},
|
||||
{"name": "page_size", "in": "query", "required": False, "schema": {"type": "integer", "default": 100}},
|
||||
OpenApiParameter(
|
||||
name="run_id",
|
||||
type={"type": "string", "format": "uuid"},
|
||||
location=OpenApiParameter.PATH,
|
||||
required=True,
|
||||
description="شناسه UUID تسک async (task_id).",
|
||||
),
|
||||
],
|
||||
responses={
|
||||
200: build_response(
|
||||
RemoteSensingRunResultEnvelopeSerializer,
|
||||
"نتیجه run بازگردانده شد.",
|
||||
RemoteSensingRunStatusEnvelopeSerializer,
|
||||
"وضعیت run بازگردانده شد و بعد از اتمام، نتیجه نهایی نیز از همین route برگردانده میشود.",
|
||||
),
|
||||
404: build_response(
|
||||
SoilErrorResponseSerializer,
|
||||
@@ -655,93 +619,92 @@ class RemoteSensingRunResultView(APIView):
|
||||
def get(self, request, run_id):
|
||||
page = _safe_positive_int(request.query_params.get("page"), default=1)
|
||||
page_size = min(_safe_positive_int(request.query_params.get("page_size"), default=100), MAX_REMOTE_SENSING_PAGE_SIZE)
|
||||
run = (
|
||||
RemoteSensingRun.objects.filter(pk=run_id)
|
||||
.select_related("soil_location")
|
||||
.first()
|
||||
)
|
||||
run = RemoteSensingRun.objects.filter(metadata__task_id=str(run_id)).select_related("soil_location").first()
|
||||
if run is None:
|
||||
return Response(
|
||||
{"code": 404, "msg": "run پیدا نشد.", "data": None},
|
||||
{"code": 404, "msg": "run با این task_id پیدا نشد.", "data": None},
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
|
||||
location = _get_location_by_lat_lon(run.soil_location.latitude, run.soil_location.longitude, prefetch=True)
|
||||
observations = _get_remote_sensing_observations(
|
||||
location=run.soil_location,
|
||||
block_code=run.block_code,
|
||||
start_date=run.temporal_start,
|
||||
end_date=run.temporal_end,
|
||||
)
|
||||
subdivision_result = getattr(run, "subdivision_result", None)
|
||||
|
||||
if not observations.exists():
|
||||
response_payload = {
|
||||
"status": RemoteSensingRunSerializer(run).data["status_label"],
|
||||
"source": "processing" if run.status in {RemoteSensingRun.STATUS_PENDING, RemoteSensingRun.STATUS_RUNNING} else "database",
|
||||
"location": SoilLocationResponseSerializer(location).data,
|
||||
"block_code": run.block_code,
|
||||
"chunk_size_sqm": run.chunk_size_sqm,
|
||||
"temporal_extent": {
|
||||
"start_date": run.temporal_start.isoformat() if run.temporal_start else None,
|
||||
"end_date": run.temporal_end.isoformat() if run.temporal_end else None,
|
||||
},
|
||||
"summary": _empty_remote_sensing_summary(),
|
||||
"cells": [],
|
||||
"run": RemoteSensingRunSerializer(run).data,
|
||||
"subdivision_result": None,
|
||||
}
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": response_payload},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
paginated_observations = _paginate_observations(
|
||||
observations,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
paginated_assignments = []
|
||||
pagination = {"cells": paginated_observations["pagination"]}
|
||||
if subdivision_result is not None:
|
||||
paginated = _paginate_assignments(
|
||||
subdivision_result,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
paginated_assignments = paginated["items"]
|
||||
pagination["assignments"] = paginated["pagination"]
|
||||
|
||||
subdivision_data = None
|
||||
if subdivision_result is not None:
|
||||
subdivision_data = RemoteSensingSubdivisionResultSerializer(
|
||||
subdivision_result,
|
||||
context={"paginated_assignments": paginated_assignments},
|
||||
).data
|
||||
|
||||
response_payload = {
|
||||
"status": RemoteSensingRunSerializer(run).data["status_label"],
|
||||
"source": "database",
|
||||
"location": SoilLocationResponseSerializer(location).data,
|
||||
"block_code": run.block_code,
|
||||
"chunk_size_sqm": run.chunk_size_sqm,
|
||||
"temporal_extent": {
|
||||
"start_date": run.temporal_start.isoformat() if run.temporal_start else None,
|
||||
"end_date": run.temporal_end.isoformat() if run.temporal_end else None,
|
||||
},
|
||||
"summary": _build_remote_sensing_summary(observations),
|
||||
"cells": RemoteSensingCellObservationSerializer(paginated_observations["items"], many=True).data,
|
||||
"run": RemoteSensingRunSerializer(run).data,
|
||||
"subdivision_result": subdivision_data,
|
||||
}
|
||||
if pagination is not None:
|
||||
response_payload["pagination"] = pagination
|
||||
response_payload = _build_remote_sensing_run_status_payload(run, page=page, page_size=page_size)
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": response_payload},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
def _build_remote_sensing_run_status_payload(run: RemoteSensingRun, *, page: int, page_size: int) -> dict:
|
||||
run_data = RemoteSensingRunSerializer(run).data
|
||||
task_id = (run.metadata or {}).get("task_id")
|
||||
if run.status in {RemoteSensingRun.STATUS_PENDING, RemoteSensingRun.STATUS_RUNNING}:
|
||||
return {
|
||||
"status": run_data["status_label"],
|
||||
"source": "database",
|
||||
"run": run_data,
|
||||
"task_id": task_id,
|
||||
}
|
||||
|
||||
location = _get_location_by_lat_lon(run.soil_location.latitude, run.soil_location.longitude, prefetch=True)
|
||||
observations = _get_remote_sensing_observations(
|
||||
location=run.soil_location,
|
||||
block_code=run.block_code,
|
||||
start_date=run.temporal_start,
|
||||
end_date=run.temporal_end,
|
||||
)
|
||||
subdivision_result = getattr(run, "subdivision_result", None)
|
||||
|
||||
response_payload = {
|
||||
"status": run_data["status_label"],
|
||||
"source": "database",
|
||||
"run": run_data,
|
||||
"task_id": task_id,
|
||||
"location": SoilLocationResponseSerializer(location).data,
|
||||
"block_code": run.block_code,
|
||||
"chunk_size_sqm": run.chunk_size_sqm,
|
||||
"temporal_extent": {
|
||||
"start_date": run.temporal_start.isoformat() if run.temporal_start else None,
|
||||
"end_date": run.temporal_end.isoformat() if run.temporal_end else None,
|
||||
},
|
||||
"summary": _empty_remote_sensing_summary(),
|
||||
"cells": [],
|
||||
"subdivision_result": None,
|
||||
}
|
||||
|
||||
if not observations.exists():
|
||||
return response_payload
|
||||
|
||||
paginated_observations = _paginate_observations(
|
||||
observations,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
paginated_assignments = []
|
||||
pagination = {"cells": paginated_observations["pagination"]}
|
||||
if subdivision_result is not None:
|
||||
paginated = _paginate_assignments(
|
||||
subdivision_result,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
paginated_assignments = paginated["items"]
|
||||
pagination["assignments"] = paginated["pagination"]
|
||||
|
||||
response_payload["summary"] = _build_remote_sensing_summary(observations)
|
||||
response_payload["cells"] = RemoteSensingCellObservationSerializer(
|
||||
paginated_observations["items"],
|
||||
many=True,
|
||||
).data
|
||||
response_payload["pagination"] = pagination
|
||||
|
||||
if subdivision_result is not None:
|
||||
response_payload["subdivision_result"] = RemoteSensingSubdivisionResultSerializer(
|
||||
subdivision_result,
|
||||
context={"paginated_assignments": paginated_assignments},
|
||||
).data
|
||||
|
||||
return response_payload
|
||||
|
||||
|
||||
def _get_location_by_lat_lon(lat, lon, *, prefetch: bool = False):
|
||||
lat_rounded = round(lat, 6)
|
||||
lon_rounded = round(lon, 6)
|
||||
@@ -869,18 +832,18 @@ def _clear_block_analysis_state(
|
||||
subdivision.elbow_plot = None
|
||||
|
||||
|
||||
def _resolve_chunk_size_for_location(location: SoilLocation, block_code: str) -> int | None:
|
||||
def _resolve_chunk_size_for_location(location: SoilLocation, block_code: str) -> int:
|
||||
if block_code:
|
||||
subdivision = location.block_subdivisions.filter(block_code=block_code).first()
|
||||
if subdivision is not None:
|
||||
return subdivision.chunk_size_sqm
|
||||
return int(subdivision.chunk_size_sqm or 900)
|
||||
block_layout = location.block_layout or {}
|
||||
if not block_code:
|
||||
return block_layout.get("analysis_grid_summary", {}).get("chunk_size_sqm")
|
||||
return int(block_layout.get("analysis_grid_summary", {}).get("chunk_size_sqm") or 900)
|
||||
for block in block_layout.get("blocks", []):
|
||||
if block.get("block_code") == block_code:
|
||||
return block.get("analysis_grid_summary", {}).get("chunk_size_sqm")
|
||||
return None
|
||||
return int(block.get("analysis_grid_summary", {}).get("chunk_size_sqm") or 900)
|
||||
return 900
|
||||
|
||||
|
||||
def _get_remote_sensing_observations(*, location, block_code: str, start_date, end_date):
|
||||
|
||||
Reference in New Issue
Block a user