2020-04-08 18:53:08 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-04-08 18:39:38 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using FarmmapsApi;
|
2020-04-08 18:53:08 +00:00
|
|
|
using FarmmapsApi.Models;
|
2020-04-08 18:39:38 +00:00
|
|
|
using FarmmapsApi.Services;
|
2020-04-08 18:53:08 +00:00
|
|
|
using FarmmapsHerbicide.Models;
|
2020-04-08 18:39:38 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-04-08 18:53:08 +00:00
|
|
|
using static FarmmapsApiSamples.Constants;
|
2020-04-08 18:39:38 +00:00
|
|
|
|
|
|
|
namespace FarmmapsHerbicide
|
|
|
|
{
|
|
|
|
public class HerbicideApplication : IApplication
|
|
|
|
{
|
|
|
|
private const string DownloadFolder = "Downloads";
|
|
|
|
|
|
|
|
private readonly ILogger<HerbicideApplication> _logger;
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
private readonly HerbicideService _herbicideService;
|
|
|
|
private readonly GeneralService _generalService;
|
2020-04-08 18:53:08 +00:00
|
|
|
|
|
|
|
private HerbicideAgent _liberatorTarweAgent;
|
2020-04-08 18:39:38 +00:00
|
|
|
|
|
|
|
public HerbicideApplication(ILogger<HerbicideApplication> logger, FarmmapsApiService farmmapsApiService,
|
|
|
|
GeneralService generalService, HerbicideService herbicideService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_farmmapsApiService = farmmapsApiService;
|
|
|
|
_generalService = generalService;
|
|
|
|
_herbicideService = herbicideService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task RunAsync()
|
|
|
|
{
|
2020-04-08 18:53:08 +00:00
|
|
|
if (!Directory.Exists(DownloadFolder))
|
|
|
|
Directory.CreateDirectory(DownloadFolder);
|
|
|
|
|
|
|
|
// !! this call is needed the first time an api is called with a fresh clientid and secret !!
|
|
|
|
await _farmmapsApiService.GetCurrentUserCodeAsync();
|
|
|
|
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
|
2020-04-14 18:50:27 +00:00
|
|
|
|
|
|
|
var agents = await _herbicideService.GetHerbicideAgents();
|
|
|
|
_liberatorTarweAgent = agents.SingleOrDefault(a => a.Name.Equals("Liberator"));
|
2020-04-08 18:53:08 +00:00
|
|
|
|
2020-06-10 13:24:08 +00:00
|
|
|
// await SingleLutumTiffFlow(roots);
|
2020-04-08 18:53:08 +00:00
|
|
|
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 _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Herbicide LUTUM", 2020,
|
|
|
|
@"{""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]]]}");
|
|
|
|
|
|
|
|
_logger.LogInformation("Uploading data");
|
|
|
|
var filePath = Path.Combine("Data", "Lutum.tiff");
|
|
|
|
var inputItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, filePath, "Lutum.tiff");
|
|
|
|
if(inputItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError($"Failed to upload data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation("Calculating appliance map");
|
|
|
|
var applianceMapItem = await _herbicideService.CalculateApplicationMapAsync(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 _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Herbicide van den borne", 2020,
|
|
|
|
@"{ ""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 ] ] ] }");
|
|
|
|
|
|
|
|
// upload data
|
|
|
|
_logger.LogInformation("Uploading lutum data");
|
|
|
|
var inputItem = await _generalService.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 _generalService.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 _generalService.ShapeToGeotiff(inputItem);
|
|
|
|
if (tiffInputItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError($"Failed to convert shape to tiff for inputItem");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tiffInputExtraItem = await _generalService.ShapeToGeotiff(inputExtraItem);
|
|
|
|
if (tiffInputExtraItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError($"Failed to convert shape to tiff for inputExtraItem");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create appliance map
|
2020-05-05 14:08:32 +00:00
|
|
|
_logger.LogInformation("Calculating application map");
|
2020-04-08 18:53:08 +00:00
|
|
|
var applianceMapItem = await _herbicideService.CalculateApplicationMapAsync(cropfieldItem, _liberatorTarweAgent, tiffInputItem, tiffInputExtraItem);
|
|
|
|
if (applianceMapItem == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:08:32 +00:00
|
|
|
_logger.LogInformation("Downloading application map");
|
2020-04-08 18:53:08 +00:00
|
|
|
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(DownloadFolder,
|
|
|
|
$"{applianceMapItem.Name}_{Guid.NewGuid():N}.zip"));
|
2020-04-08 18:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|