FarmMapsApiClient_KB34_MAST/FarmmapsApiSamples/HerbicideService.cs

351 lines
15 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
using FarmmapsApiSamples.Models;
using Google.Apis.Upload;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static FarmmapsApi.Extensions;
using static FarmmapsApiSamples.Constants;
namespace FarmmapsApiSamples
{
public class HerbicideService : ITestFlow
{
private readonly ILogger<HerbicideService> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private HerbicideAgent liberatorTarweAgent;
private readonly string _downloadFolder = "Downloads";
public HerbicideService(ILogger<HerbicideService> logger, FarmmapsApiService farmmapsApiService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
liberatorTarweAgent = new HerbicideAgent()
{
Middel = "Liberator",
Grondsoort = "Klei",
ExtraProductType = "OS",
ActieveStofGehalte = "diflu100+flufenacet400",
MinDosis = 0.33f,
MaxDosis = 0.6f,
A = 0.007f,
B = 0.25f,
C = 1.05f,
D = 97f,
E = 3f,
P = 1f,
Gewas = "Tarwe",
Opmerking = "Org. stof arme grond",
TankMix = string.Empty
};
}
public async Task TestFlow(List<UserRoot> roots)
{
if (!Directory.Exists(_downloadFolder))
Directory.CreateDirectory(_downloadFolder);
// await SingleLutumTiffFlow(roots);
await MultiVanDenBorneShapeFlow(roots);
}
private async Task SingleLutumTiffFlow(List<UserRoot> roots)
{
var myDrive = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDrive == null)
{
_logger.LogError("Could not find a needed root item");
return;
}
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot == null)
{
_logger.LogError("Could not find a needed root item");
return;
}
_logger.LogInformation("Creating cropfield");
var cropfieldItem = await CreateCropfieldSingleLutumItemAsync(myDrive.Code);
_logger.LogInformation("Uploading data");
var inputItem = await UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, Path.Combine("Data", "Lutum.tiff"), "Lutum.tiff");
if(inputItem == null)
{
_logger.LogError($"Failed to upload data");
return;
}
_logger.LogInformation("Calculating appliance map");
var applianceMapItem = await CalculateApplianceMapAsync(cropfieldItem, liberatorTarweAgent, inputItem);
if (applianceMapItem == null)
{
return;
}
_logger.LogInformation("Downloading appliance map");
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(_downloadFolder,
$"{applianceMapItem.Name}_{Guid.NewGuid():N}.zip"));
}
private async Task MultiVanDenBorneShapeFlow(List<UserRoot> roots)
{
var myDrive = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDrive == null)
{
_logger.LogError("Could not find a needed root item");
return;
}
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot == null)
{
_logger.LogError("Could not find a needed root item");
return;
}
_logger.LogInformation("Creating cropfield");
var cropfieldItem = await CreateCropfieldVanDenBorneAsync(myDrive.Code);
// upload data
_logger.LogInformation("Uploading lutum data");
var inputItem = await UploadZipWithShapeAsync(uploadedRoot, Path.Combine("Data", "vd_born_lutum.zip"), "Lutum");
if(inputItem == null)
{
_logger.LogError($"Failed to upload lutum data");
return;
}
_logger.LogInformation("Uploading organic matter data");
var inputExtraItem = await UploadZipWithShapeAsync(uploadedRoot, Path.Combine("Data", "vd_born_om.zip"), "OS");
if(inputExtraItem == null)
{
_logger.LogError($"Failed to upload organic matter data");
return;
}
// transform shape to geotiff as nbstask only supports tiff input item
var tiffInputItem = await ShapeToGeotiff(inputItem);
if (tiffInputItem == null)
{
_logger.LogError($"Failed to convert shape to tiff for inputItem");
return;
}
var tiffInputExtraItem = await ShapeToGeotiff(inputExtraItem);
if (tiffInputExtraItem == null)
{
_logger.LogError($"Failed to convert shape to tiff for inputExtraItem");
return;
}
// create appliance map
_logger.LogInformation("Calculating appliance map");
var applianceMapItem = await CalculateApplianceMapAsync(cropfieldItem, liberatorTarweAgent, tiffInputItem, tiffInputExtraItem);
if (applianceMapItem == null)
{
return;
}
_logger.LogInformation("Downloading appliance map");
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(_downloadFolder,
$"{applianceMapItem.Name}_{Guid.NewGuid():N}.zip"));
}
private async Task<Item> CreateCropfieldSingleLutumItemAsync(string parentItemCode)
{
var currentYear = new DateTime(DateTime.UtcNow.Year, 1, 1);
var cropfieldItemRequest = new ItemRequest()
{
ParentCode = parentItemCode,
ItemType = CROPFIELD_ITEMTYPE,
Name = "Cropfield VRA Herbicide LUTUM",
DataDate = currentYear,
DataEndDate = currentYear.AddMonths(3),
Data = JObject.Parse("{}"),
Geometry = JObject.Parse(
@"{""type"":""Polygon"",""coordinates"":[[[4.617786844284247,52.22533706956424],[4.618642601314543,52.225938364585989],[4.6192153806397,52.22563988897754],[4.619192414656403,52.2256242822442],[4.620306732153958,52.225031745661528],[4.620542019225217,52.22519855319158],[4.621157509147853,52.22487436515405],[4.623387917230182,52.22367660757213],[4.624563444939009,52.22304740241544],[4.624562779355982,52.223046635247019],[4.624534908813479,52.22302596787506],[4.627873021330343,52.221240670658399],[4.627504935938338,52.220104419135129],[4.627324878706837,52.22020569669098],[4.627320696113512,52.22020660117888],[4.626707169518044,52.22053923770041],[4.624700376420229,52.221619047547488],[4.623471571183885,52.22227447969577],[4.623471511010673,52.22227500174403],[4.623468838689317,52.22228052566992],[4.617786844284247,52.22533706956424]]]}")
};
return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
}
private async Task<Item> CreateCropfieldVanDenBorneAsync(string parentItemCode)
{
var currentYear = new DateTime(DateTime.UtcNow.Year, 1, 1);
var cropfieldItemRequest = new ItemRequest()
{
ParentCode = parentItemCode,
ItemType = CROPFIELD_ITEMTYPE,
Name = "Cropfield VRA Herbicide van den borne",
DataDate = currentYear,
DataEndDate = currentYear.AddMonths(3),
Data = JObject.Parse("{}"),
Geometry = JObject.Parse(
@"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 5.833813406930386, 52.662044400144573 ], [ 5.835448040282574, 52.662663232992713 ], [ 5.836017878606523, 52.662878934715643 ], [ 5.836051608552347, 52.66284538659815 ], [ 5.836752490453029, 52.662168665385018 ], [ 5.837926180228544, 52.662604908046319 ], [ 5.839506445610824, 52.661074514977884 ], [ 5.839509138399547, 52.661071906335934 ], [ 5.83609382583743, 52.659780657270744 ], [ 5.834864415345847, 52.661000863063798 ], [ 5.833813406930386, 52.662044400144573 ] ] ] }")
};
return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
}
private async Task<Item> UploadDataAsync(UserRoot root, string itemType, string filePath, string itemName)
{
var startUpload = DateTime.UtcNow;
var result = await _farmmapsApiService.UploadFile(filePath, root.Code,
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
if (result.Progress.Status == UploadStatus.Failed)
return null;
return await FindChildItemAsync(root.Code, itemType, itemName,
i => i.Created >= startUpload &&
i.Name.ToLower().Contains(itemName.ToLower()));
}
private async Task<Item> UploadZipWithShapeAsync(UserRoot root, string filePath, string itemName)
{
var startUpload = DateTime.UtcNow;
var result = await _farmmapsApiService.UploadFile(filePath, root.Code,
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
if (result.Progress.Status == UploadStatus.Failed)
return null;
var zipName = Path.GetFileNameWithoutExtension(filePath);
Item shapeItem = null;
await PollTask(TimeSpan.FromSeconds(3), async source =>
{
var uploadedFilesChildren = await _farmmapsApiService.GetItemChildrenAsync(root.Code);
var zipItems = uploadedFilesChildren.Where(i => i.Name.Contains(zipName));
var childrenTasks = new List<Task<List<Item>>>();
foreach (var zipItem in zipItems)
{
childrenTasks.Add(_farmmapsApiService.GetItemChildrenAsync(zipItem.Code,
SHAPE_PROCESSED_ITEMTYPE));
}
List<Item>[] items = await Task.WhenAll(childrenTasks);
if (items.Length > 0)
{
var flatItems = items.SelectMany(i => i).Where(i => i.Name.Contains(itemName)).ToList();
if (flatItems.Count > 0)
{
shapeItem = flatItems.Where(i => i.Created >= startUpload).OrderByDescending(i => i.Created).First();
source.Cancel();
}
}
});
return shapeItem;
}
private async Task<Item> CalculateApplianceMapAsync(Item cropfieldItem, HerbicideAgent agent, params Item[] inputItem)
{
if (inputItem.Length == 0)
return null;
var taskRequest = new TaskRequest()
{
TaskType = "vnd.farmmaps.task.vraherbicide"
};
taskRequest.attributes["inputCode"] = inputItem[0].Code;
taskRequest.attributes["agent"] = JsonConvert.SerializeObject(agent);
if (inputItem.Length > 1)
taskRequest.attributes["extraInputCode"] = inputItem[1].Code;
var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskRequest);
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
{
_logger.LogInformation("Checking vraherbicide 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 = $"VRAHerbicide {agent.Middel}";
var applianceMapItem = await 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 VRAHerbicide geotiff child item under cropfield");
return null;
}
return applianceMapItem;
}
private async Task<Item> ShapeToGeotiff(Item shapeItemCode)
{
var shapeToGeotiffRequest = new TaskRequest()
{
TaskType = "vnd.farmmaps.task.shapetogeotiff"
};
var taskCode = await _farmmapsApiService.QueueTaskAsync(shapeItemCode.Code, shapeToGeotiffRequest);
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
{
_logger.LogInformation("Checking shapetogeotiff task status");
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(shapeItemCode.Code, taskCode);
if (itemTaskStatus.IsFinished)
tokenSource.Cancel();
});
_logger.LogInformation("Data shape converted to geotiff");
// the parent of the shape item is now the tiff item
shapeItemCode = await _farmmapsApiService.GetItemAsync(shapeItemCode.Code);
return await _farmmapsApiService.GetItemAsync(shapeItemCode.ParentCode);
}
private async Task<Item> FindChildItemAsync(string parentCode, string itemType, string containsName,
Func<Item,bool> filter = null, int maxTries = 10)
{
Item dataItem = null;
int tries = 0;
await PollTask(TimeSpan.FromSeconds(3), async source =>
{
_logger.LogInformation($"Trying to get {containsName} data");
var uploadedFilesChildren = await _farmmapsApiService.GetItemChildrenAsync(parentCode, itemType);
if (uploadedFilesChildren.Count > 0)
{
Func<Item, bool> func = filter ?? (i => i.Name.ToLower().Contains(containsName.ToLower()));
dataItem = uploadedFilesChildren.FirstOrDefault(func);
source.Cancel();
} else if (tries == maxTries)
{
source.Cancel();
}
tries++;
});
if (dataItem == null)
{
_logger.LogError("dataItem not found");
return null;
}
_logger.LogInformation($"Found {containsName} item");
return dataItem;
}
}
}