From a33143743386678e1c5c4d95038340cf05beb710 Mon Sep 17 00:00:00 2001 From: Mark van der Wal Date: Tue, 28 Apr 2020 12:37:56 +0200 Subject: [PATCH] Finding satellite items. --- FarmmapsApi/Constants.cs | 5 +++ .../HaulmkillingApplication.cs | 38 +++++++++++++++++-- FarmmapsHaulmkilling/Models/Settings.cs | 2 +- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/FarmmapsApi/Constants.cs b/FarmmapsApi/Constants.cs index 30f50f1..1bae5eb 100644 --- a/FarmmapsApi/Constants.cs +++ b/FarmmapsApi/Constants.cs @@ -3,11 +3,16 @@ namespace FarmmapsApiSamples public static class Constants { public const string USERINPUT_ITEMTYPE = "vnd.farmmaps.itemtype.user.input"; + public const string TEMPORAL_ITEMTYPE = "vnd.farmmaps.itemtype.temporal"; public const string GEOTIFF_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff"; public const string GEOTIFF_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff.processed"; public const string CROPFIELD_ITEMTYPE = "vnd.farmmaps.itemtype.cropfield"; public const string SHAPE_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.shape.processed"; public const string SHAPE_ITEMTYPE = "vnd.farmmaps.itemtype.shape"; + public const string VRANBS_TASK = "vnd.farmmaps.task.vranbs"; + public const string VRAHERBICIDE_TASK = "vnd.farmmaps.task.vraherbicide"; + public const string VRAHAULMKILLING_TASK = "vnd.farmmaps.task.vrahaulmkilling"; + public const string SATELLITE_TASK = "vnd.farmmaps.task.satellite"; } } \ No newline at end of file diff --git a/FarmmapsHaulmkilling/HaulmkillingApplication.cs b/FarmmapsHaulmkilling/HaulmkillingApplication.cs index 720f457..209cded 100644 --- a/FarmmapsHaulmkilling/HaulmkillingApplication.cs +++ b/FarmmapsHaulmkilling/HaulmkillingApplication.cs @@ -8,6 +8,8 @@ using FarmmapsHaulmkilling.Models; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using static FarmmapsApiSamples.Constants; + namespace FarmmapsHaulmkilling { public class HaulmkillingApplication : IApplication @@ -78,17 +80,47 @@ namespace FarmmapsHaulmkilling cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode); } - if (!_settings.ProcessedSatellite) + ItemTaskStatus taskStatus; + if (!string.IsNullOrEmpty(_settings.SatelliteTaskCode)) { _logger.LogInformation("Gathering satellite information for cropfield, this might take a while!"); - var taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, "vnd.farmmaps.task.satellite", null, 20); + taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, SATELLITE_TASK, null, 20); - _settings.ProcessedSatellite = taskStatus.State == ItemTaskState.Ok; + if (taskStatus.State == ItemTaskState.Error) + { + _logger.LogError($"Something went wrong when trying to process satellite data; {taskStatus.Message}"); + return; + } + + _settings.SatelliteTaskCode = taskStatus.Code; SaveSettings(); } + else + { + taskStatus = await _farmmapsApiService.GetTaskStatusAsync(_settings.CropfieldItemCode, _settings.SatelliteTaskCode); + } // find ndvi or wdvi satellite data geotiffs + var temporalITem = await _generalService.FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE, + "Cropfield VRA Haulmkilling, Satellite", item => item.SourceTask == SATELLITE_TASK && + taskStatus.Finished >= item.Created && + taskStatus.Finished <= item.Created.Value.AddHours(1)); + + if (temporalITem == null) + { + _logger.LogError($"Temporal item not found"); + return; + } + + var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalITem.Code); + var firstSatelliteItem = satelliteTiffs.FirstOrDefault(); + + if (firstSatelliteItem == null) + { + _logger.LogError($"Satellite item not found"); + return; + } // create haulmkilling application map diff --git a/FarmmapsHaulmkilling/Models/Settings.cs b/FarmmapsHaulmkilling/Models/Settings.cs index 6093495..68925ef 100644 --- a/FarmmapsHaulmkilling/Models/Settings.cs +++ b/FarmmapsHaulmkilling/Models/Settings.cs @@ -3,6 +3,6 @@ namespace FarmmapsHaulmkilling.Models public class Settings { public string CropfieldItemCode { get; set; } - public bool ProcessedSatellite { get; set; } + public string SatelliteTaskCode { get; set; } } } \ No newline at end of file