2020-04-08 18:23:22 +00:00
|
|
|
using System;
|
2020-04-08 19:43:03 +00:00
|
|
|
using System.Collections.Generic;
|
2020-04-08 18:23:22 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using FarmmapsApi;
|
2020-04-08 19:43:03 +00:00
|
|
|
using FarmmapsApi.Models;
|
2020-04-08 18:23:22 +00:00
|
|
|
using FarmmapsApi.Services;
|
2020-04-08 19:43:03 +00:00
|
|
|
using FarmmapsNbs.Models;
|
2020-04-08 18:23:22 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-04-08 19:43:03 +00:00
|
|
|
using Newtonsoft.Json;
|
2020-07-06 11:12:44 +00:00
|
|
|
using static FarmmapsApiSamples.Constants;
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 18:39:38 +00:00
|
|
|
namespace FarmmapsNbs
|
2020-04-08 18:23:22 +00:00
|
|
|
{
|
|
|
|
public class NbsApplication : IApplication
|
|
|
|
{
|
|
|
|
private const string DownloadFolder = "Downloads";
|
|
|
|
|
|
|
|
private readonly ILogger<NbsApplication> _logger;
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
private readonly NitrogenService _nitrogenService;
|
|
|
|
private readonly GeneralService _generalService;
|
|
|
|
|
|
|
|
public NbsApplication(ILogger<NbsApplication> logger, FarmmapsApiService farmmapsApiService,
|
|
|
|
GeneralService generalService, NitrogenService nitrogenService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_farmmapsApiService = farmmapsApiService;
|
|
|
|
_generalService = generalService;
|
|
|
|
_nitrogenService = nitrogenService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task RunAsync()
|
|
|
|
{
|
2020-04-08 19:43:03 +00:00
|
|
|
var nitrogenInputJson = File.ReadAllText("NitrogenInput.json");
|
2020-08-19 11:40:42 +00:00
|
|
|
//var nitrogenInputJson = File.ReadAllText("fivefieldsinput.json");
|
2020-04-08 19:43:03 +00:00
|
|
|
List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson);
|
|
|
|
|
|
|
|
if (!Directory.Exists(DownloadFolder))
|
|
|
|
Directory.CreateDirectory(DownloadFolder);
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
// !! 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-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
foreach (var input in nitrogenInputs)
|
|
|
|
{
|
|
|
|
try
|
2020-04-08 18:23:22 +00:00
|
|
|
{
|
2020-04-08 19:43:03 +00:00
|
|
|
await Process(roots, input);
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
2020-04-08 19:43:03 +00:00
|
|
|
catch (Exception ex)
|
2020-04-08 18:23:22 +00:00
|
|
|
{
|
2020-04-08 19:43:03 +00:00
|
|
|
_logger.LogError(ex.Message);
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
2020-04-08 19:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
private async Task Process(List<UserRoot> roots, NitrogenInput input)
|
|
|
|
{
|
|
|
|
var plantingDate = input.PlantingDate;
|
|
|
|
var measurementDate = input.MeasurementDate;
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
|
|
|
|
if (uploadedRoot == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
return;
|
|
|
|
}
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
|
|
|
|
if (myDriveRoot == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
return;
|
|
|
|
}
|
2020-10-06 13:50:05 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
|
2020-04-10 11:12:15 +00:00
|
|
|
$"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None));
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-06-15 18:50:43 +00:00
|
|
|
var isGeoJson = input.File.Contains("json");
|
|
|
|
var dataPath = Path.Combine("Data", input.File);
|
|
|
|
var shapeItem = isGeoJson ?
|
2020-07-06 11:12:44 +00:00
|
|
|
await _generalService.UploadDataAsync(uploadedRoot, SHAPE_PROCESSED_ITEMTYPE, dataPath, Path.GetFileNameWithoutExtension(input.File)):
|
2020-06-15 18:50:43 +00:00
|
|
|
await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, Path.GetFileNameWithoutExtension(input.File));
|
|
|
|
|
2020-04-28 14:35:14 +00:00
|
|
|
if (shapeItem == null)
|
2020-04-08 19:43:03 +00:00
|
|
|
{
|
2020-06-15 18:50:43 +00:00
|
|
|
_logger.LogError("Could not find item for uploaded data");
|
2020-04-08 19:43:03 +00:00
|
|
|
return;
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
2020-04-08 19:43:03 +00:00
|
|
|
|
|
|
|
_logger.LogInformation($"Converting shape to geotiff");
|
2020-04-28 14:35:14 +00:00
|
|
|
var geotiffItem = await _generalService.ShapeToGeotiff(shapeItem);
|
|
|
|
if (geotiffItem == null)
|
2020-04-08 18:23:22 +00:00
|
|
|
{
|
2020-04-28 14:35:14 +00:00
|
|
|
_logger.LogError("Something went wrong with shape to geotiff transformation");
|
2020-04-08 19:43:03 +00:00
|
|
|
return;
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
2020-04-08 19:43:03 +00:00
|
|
|
|
2020-08-19 11:40:42 +00:00
|
|
|
_logger.LogInformation("Downloading geotiff file");
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(geotiffItem.Code,
|
|
|
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.input_geotiff.zip"));
|
|
|
|
|
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
_logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}");
|
|
|
|
var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem);
|
2020-04-08 20:17:39 +00:00
|
|
|
var targetNData = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate,
|
2020-04-10 11:12:15 +00:00
|
|
|
measurementDate, input.PotatoPurposeType, input.TargetYield);
|
2020-04-08 20:17:39 +00:00
|
|
|
|
|
|
|
if (targetNData == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Something went wrong with TargetN calculation");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation($"TargetN: {targetNData.TargetN}");
|
|
|
|
|
|
|
|
var targetNDataPath = Path.Combine(DownloadFolder, $"{input.OutputFileName}.targetn.json");
|
|
|
|
await File.WriteAllTextAsync(targetNDataPath, JsonConvert.SerializeObject(targetNData, Formatting.Indented));
|
2020-04-08 19:43:03 +00:00
|
|
|
|
|
|
|
_logger.LogInformation("Calculating uptake map");
|
|
|
|
var uptakeMapItem =
|
2020-04-28 14:35:14 +00:00
|
|
|
await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate,
|
|
|
|
measurementDate, input.InputVariable);
|
2020-07-07 19:45:39 +00:00
|
|
|
if (uptakeMapItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Something went wrong with creating the uptakeMap");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
_logger.LogInformation("Downloading uptake map");
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code,
|
2020-04-08 20:17:39 +00:00
|
|
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip"));
|
2020-08-19 11:40:42 +00:00
|
|
|
_logger.LogInformation("UptakeMap downloaded to {0}", Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip"));
|
2020-04-08 19:43:03 +00:00
|
|
|
|
2020-04-28 14:35:14 +00:00
|
|
|
_logger.LogInformation("Calculating application map");
|
2020-04-29 12:58:30 +00:00
|
|
|
var applicationMapItem =
|
2020-04-28 14:35:14 +00:00
|
|
|
await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate,
|
2020-04-08 19:43:03 +00:00
|
|
|
measurementDate,
|
2020-04-28 14:35:14 +00:00
|
|
|
input.InputVariable, targetNData.TargetN);
|
2020-04-08 19:43:03 +00:00
|
|
|
|
2020-07-07 19:45:39 +00:00
|
|
|
if (applicationMapItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Something went wrong with creating the applicationMap");
|
|
|
|
return;
|
|
|
|
}
|
2020-04-28 14:35:14 +00:00
|
|
|
_logger.LogInformation("Downloading application map");
|
2020-04-29 12:58:30 +00:00
|
|
|
await _farmmapsApiService.DownloadItemAsync(applicationMapItem.Code,
|
2020-04-08 20:17:39 +00:00
|
|
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.application.zip"));
|
2020-08-19 11:40:42 +00:00
|
|
|
_logger.LogInformation("Application map can be found in {0}", Path.Combine(DownloadFolder, $"{input.OutputFileName}.application.zip"));
|
|
|
|
|
|
|
|
//transforming tiff to shape
|
|
|
|
var tiffItem = applicationMapItem;
|
|
|
|
|
|
|
|
if (tiffItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find item for uploaded data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_logger.LogInformation($"Converting geotiff to shape");
|
|
|
|
var taskmap = await _generalService.GeotiffToShape(tiffItem);
|
|
|
|
if (taskmap == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Something went wrong with geotiff to shape transformation");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading taskmap");
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(taskmap.Code,
|
|
|
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.taskmap.zip"));
|
|
|
|
|
|
|
|
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|