Added create applicationmap.

fixed to flow.
This commit is contained in:
Mark van der Wal 2020-05-05 16:08:32 +02:00
parent a331437433
commit 38cb1e27a5
3 changed files with 94 additions and 14 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -7,7 +8,7 @@ using FarmmapsApi.Services;
using FarmmapsHaulmkilling.Models; using FarmmapsHaulmkilling.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static FarmmapsApiSamples.Constants; using static FarmmapsApiSamples.Constants;
namespace FarmmapsHaulmkilling namespace FarmmapsHaulmkilling
@ -59,7 +60,7 @@ namespace FarmmapsHaulmkilling
} }
var agents = await _haulmkillingService.GetHaulmkillingAgents(); var agents = await _haulmkillingService.GetHaulmkillingAgents();
if (agents == null) if (agents == null || agents.Count == 0)
{ {
_logger.LogError("No valid agents found for haulmkilling"); _logger.LogError("No valid agents found for haulmkilling");
return; return;
@ -81,7 +82,7 @@ namespace FarmmapsHaulmkilling
} }
ItemTaskStatus taskStatus; ItemTaskStatus taskStatus;
if (!string.IsNullOrEmpty(_settings.SatelliteTaskCode)) if (string.IsNullOrEmpty(_settings.SatelliteTaskCode))
{ {
_logger.LogInformation("Gathering satellite information for cropfield, this might take a while!"); _logger.LogInformation("Gathering satellite information for cropfield, this might take a while!");
taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, SATELLITE_TASK, null, 20); taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, SATELLITE_TASK, null, 20);
@ -102,29 +103,48 @@ namespace FarmmapsHaulmkilling
} }
// find ndvi or wdvi satellite data geotiffs // find ndvi or wdvi satellite data geotiffs
var temporalITem = await _generalService.FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE, var temporalItem = await _generalService.FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE,
"Cropfield VRA Haulmkilling, Satellite", item => item.SourceTask == SATELLITE_TASK && "Cropfield VRA Haulmkilling, Satellite", item => item.SourceTask == SATELLITE_TASK &&
taskStatus.Finished >= item.Created && taskStatus.Finished >= item.Created &&
taskStatus.Finished <= item.Created.Value.AddHours(1)); taskStatus.Finished <= item.Created.Value.AddHours(1));
if (temporalITem == null) if (temporalItem == null)
{ {
_logger.LogError($"Temporal item not found"); _logger.LogError("Temporal item not found");
return; return;
} }
var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalITem.Code); var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalItem.Code);
var firstSatelliteItem = satelliteTiffs.FirstOrDefault(); var firstSatelliteItem = satelliteTiffs.FirstOrDefault();
if (firstSatelliteItem == null) if (firstSatelliteItem == null)
{ {
_logger.LogError($"Satellite item not found"); _logger.LogError("Satellite item not found");
return; return;
} }
// create haulmkilling application map // must be ndvi or wdvi
var inputType = (firstSatelliteItem.Data["layers"] as JArray)?[0]["name"].ToString();
// download application map if (string.IsNullOrEmpty(inputType))
{
_logger.LogError("Could not get the input type name from the satellite item");
return;
}
var selectedAgent = agents[0];
var selectedOption = selectedAgent.SupportedOptions[0];
_logger.LogInformation("Calculating application map");
var applianceMapItem = await _haulmkillingService.CalculateApplicationMapAsync(cropfieldItem,
firstSatelliteItem, inputType, selectedAgent.Name, selectedOption);
if (applianceMapItem == null)
{
return;
}
_logger.LogInformation("Downloading application map");
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(DownloadFolder,
$"{applianceMapItem.Name}_{Guid.NewGuid():N}.zip"));
} }
private void LoadSettings() private void LoadSettings()

View File

@ -1,9 +1,13 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services; using FarmmapsApi.Services;
using FarmmapsHaulmkilling.Models; using FarmmapsHaulmkilling.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using static FarmmapsApi.Extensions;
using static FarmmapsApiSamples.Constants;
namespace FarmmapsHaulmkilling namespace FarmmapsHaulmkilling
{ {
@ -21,6 +25,10 @@ namespace FarmmapsHaulmkilling
_generalService = generalService; _generalService = generalService;
} }
/// <summary>
/// Gets the list of available haulmkilling agents
/// </summary>
/// <returns>List of haulmkilling agents</returns>
public async Task<List<HaulmkillingAgent>> GetHaulmkillingAgents() public async Task<List<HaulmkillingAgent>> GetHaulmkillingAgents()
{ {
var itemType = "vnd.farmmaps.package.vra.haulmkilling"; var itemType = "vnd.farmmaps.package.vra.haulmkilling";
@ -32,5 +40,57 @@ namespace FarmmapsHaulmkilling
return item.Data.ContainsKey("agents") ? item.Data["agents"].ToObject<List<HaulmkillingAgent>>() : null; return item.Data.ContainsKey("agents") ? item.Data["agents"].ToObject<List<HaulmkillingAgent>>() : null;
} }
/// <summary>
/// Creates an haulmkilling application map
/// </summary>
/// <param name="cropfieldItem">The context cropfield item to use</param>
/// <param name="inputItem">The geotiff item to use</param>
/// <param name="inputType">WDVI or NDVI</param>
/// <param name="agentName">One of the available agents</param>
/// <param name="selectedOption">One of the available options</param>
/// <returns>Haulmkilling application map item</returns>
public async Task<Item> CalculateApplicationMapAsync(Item cropfieldItem, Item inputItem, string inputType,
string agentName, string selectedOption)
{
var taskRequest = new TaskRequest()
{
TaskType = "vnd.farmmaps.task.vrahaulmkilling"
};
taskRequest.attributes["inputCode"] = inputItem.Code;
taskRequest.attributes["inputType"] = inputType;
taskRequest.attributes["agentName"] = agentName;
taskRequest.attributes["selectedOption"] = selectedOption;
var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskRequest);
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
{
_logger.LogInformation("Checking vrahaulmkilling task status");
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
if (itemTaskStatus.IsFinished)
tokenSource.Cancel();
});
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
if (itemTask.State == ItemTaskState.Error)
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null;
}
var itemName = $"VRAHaulmkilling {agentName}";
var applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
i.Name.ToLower().Contains(itemName.ToLower()));
if (applianceMapItem == null)
{
_logger.LogError("Could not find the VRAHaulmkilling geotiff child item under cropfield");
return null;
}
return applianceMapItem;
}
} }
} }

View File

@ -144,14 +144,14 @@ namespace FarmmapsHerbicide
} }
// create appliance map // create appliance map
_logger.LogInformation("Calculating appliance map"); _logger.LogInformation("Calculating application map");
var applianceMapItem = await _herbicideService.CalculateApplicationMapAsync(cropfieldItem, _liberatorTarweAgent, tiffInputItem, tiffInputExtraItem); var applianceMapItem = await _herbicideService.CalculateApplicationMapAsync(cropfieldItem, _liberatorTarweAgent, tiffInputItem, tiffInputExtraItem);
if (applianceMapItem == null) if (applianceMapItem == null)
{ {
return; return;
} }
_logger.LogInformation("Downloading appliance map"); _logger.LogInformation("Downloading application map");
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(DownloadFolder, await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(DownloadFolder,
$"{applianceMapItem.Name}_{Guid.NewGuid():N}.zip")); $"{applianceMapItem.Name}_{Guid.NewGuid():N}.zip"));
} }