diff --git a/FarmmapsHaulmkilling/HaulmkillingApplication.cs b/FarmmapsHaulmkilling/HaulmkillingApplication.cs
index 209cded..272873d 100644
--- a/FarmmapsHaulmkilling/HaulmkillingApplication.cs
+++ b/FarmmapsHaulmkilling/HaulmkillingApplication.cs
@@ -1,3 +1,4 @@
+using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -7,7 +8,7 @@ using FarmmapsApi.Services;
using FarmmapsHaulmkilling.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
-
+using Newtonsoft.Json.Linq;
using static FarmmapsApiSamples.Constants;
namespace FarmmapsHaulmkilling
@@ -59,7 +60,7 @@ namespace FarmmapsHaulmkilling
}
var agents = await _haulmkillingService.GetHaulmkillingAgents();
- if (agents == null)
+ if (agents == null || agents.Count == 0)
{
_logger.LogError("No valid agents found for haulmkilling");
return;
@@ -81,7 +82,7 @@ namespace FarmmapsHaulmkilling
}
ItemTaskStatus taskStatus;
- if (!string.IsNullOrEmpty(_settings.SatelliteTaskCode))
+ if (string.IsNullOrEmpty(_settings.SatelliteTaskCode))
{
_logger.LogInformation("Gathering satellite information for cropfield, this might take a while!");
taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, SATELLITE_TASK, null, 20);
@@ -102,29 +103,48 @@ namespace FarmmapsHaulmkilling
}
// 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 &&
taskStatus.Finished >= item.Created &&
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;
}
- var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalITem.Code);
+ var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalItem.Code);
var firstSatelliteItem = satelliteTiffs.FirstOrDefault();
if (firstSatelliteItem == null)
{
- _logger.LogError($"Satellite item not found");
+ _logger.LogError("Satellite item not found");
return;
}
-
- // create haulmkilling application map
-
- // download application map
+
+ // must be ndvi or wdvi
+ var inputType = (firstSatelliteItem.Data["layers"] as JArray)?[0]["name"].ToString();
+ 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()
diff --git a/FarmmapsHaulmkilling/HaulmkillingService.cs b/FarmmapsHaulmkilling/HaulmkillingService.cs
index 19ba565..5c7ee74 100644
--- a/FarmmapsHaulmkilling/HaulmkillingService.cs
+++ b/FarmmapsHaulmkilling/HaulmkillingService.cs
@@ -1,9 +1,13 @@
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
+using FarmmapsApi.Models;
using FarmmapsApi.Services;
using FarmmapsHaulmkilling.Models;
using Microsoft.Extensions.Logging;
+using static FarmmapsApi.Extensions;
+using static FarmmapsApiSamples.Constants;
namespace FarmmapsHaulmkilling
{
@@ -21,6 +25,10 @@ namespace FarmmapsHaulmkilling
_generalService = generalService;
}
+ ///
+ /// Gets the list of available haulmkilling agents
+ ///
+ /// List of haulmkilling agents
public async Task> GetHaulmkillingAgents()
{
var itemType = "vnd.farmmaps.package.vra.haulmkilling";
@@ -32,5 +40,57 @@ namespace FarmmapsHaulmkilling
return item.Data.ContainsKey("agents") ? item.Data["agents"].ToObject>() : null;
}
+
+ ///
+ /// Creates an haulmkilling application map
+ ///
+ /// The context cropfield item to use
+ /// The geotiff item to use
+ /// WDVI or NDVI
+ /// One of the available agents
+ /// One of the available options
+ /// Haulmkilling application map item
+ public async Task- 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;
+ }
}
}
\ No newline at end of file
diff --git a/FarmmapsHerbicide/HerbicideApplication.cs b/FarmmapsHerbicide/HerbicideApplication.cs
index fc189c6..c2c3909 100644
--- a/FarmmapsHerbicide/HerbicideApplication.cs
+++ b/FarmmapsHerbicide/HerbicideApplication.cs
@@ -144,14 +144,14 @@ namespace FarmmapsHerbicide
}
// create appliance map
- _logger.LogInformation("Calculating appliance map");
+ _logger.LogInformation("Calculating application map");
var applianceMapItem = await _herbicideService.CalculateApplicationMapAsync(cropfieldItem, _liberatorTarweAgent, tiffInputItem, tiffInputExtraItem);
if (applianceMapItem == null)
{
return;
}
- _logger.LogInformation("Downloading appliance map");
+ _logger.LogInformation("Downloading application map");
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(DownloadFolder,
$"{applianceMapItem.Name}_{Guid.NewGuid():N}.zip"));
}