Eerdere aanpassingen NBS en zonering verwerkt

This commit is contained in:
Riepma
2020-12-11 14:02:01 +01:00
parent 2196036135
commit 02708ba01d
7 changed files with 429 additions and 215 deletions

View File

@@ -9,6 +9,7 @@ using FarmmapsApi.Services;
using FarmmapsNbs.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static FarmmapsApiSamples.Constants;
namespace FarmmapsNbs
@@ -16,12 +17,15 @@ namespace FarmmapsNbs
public class NbsApplication : IApplication
{
private const string DownloadFolder = "Downloads";
private const string SettingsFile = "settings.json";
private readonly ILogger<NbsApplication> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly NitrogenService _nitrogenService;
private readonly GeneralService _generalService;
private Settings _settings;
public NbsApplication(ILogger<NbsApplication> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService, NitrogenService nitrogenService)
{
@@ -40,6 +44,10 @@ namespace FarmmapsNbs
if (!Directory.Exists(DownloadFolder))
Directory.CreateDirectory(DownloadFolder);
LoadSettings();
// !! 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();
@@ -59,6 +67,8 @@ namespace FarmmapsNbs
private async Task Process(List<UserRoot> roots, NitrogenInput input)
{
// !!specify if you are using an already created cropfield:
bool useCreatedCropfield = false;
var plantingDate = input.PlantingDate;
var measurementDate = input.MeasurementDate;
@@ -75,24 +85,83 @@ namespace FarmmapsNbs
_logger.LogError("Could not find a needed root item");
return;
}
var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
$"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None));
// Use already created cropfield or create new one
Item cropfieldItem;
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.CropfieldItemCode))
{
_logger.LogInformation("Creating cropfield");
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
$"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None));
_settings.CropfieldItemCode = cropfieldItem.Code;
SaveSettings();
}
else
{
_logger.LogInformation("Cropfield already exists, trying to get it");
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
}
var geotiffItem = (Item)null;
if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
_logger.LogInformation("input = tiff data");
var dataPath = Path.Combine("Data", input.File);
geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath,
Path.GetFileNameWithoutExtension(input.File));
// No file input, use most recent satellite image
if (string.IsNullOrEmpty(input.File))
{
_logger.LogInformation("No specific data given, retrieving most recent satellite image");
if (geotiffItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
// check if satellite task not yet done, do here and save taskcode
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode))
{
var satelliteTaskCode = await _generalService.RunSatelliteTask(cropfieldItem);
_settings.SatelliteTaskCode = satelliteTaskCode;
SaveSettings();
}
// Select a particular satellite item from satelliteTask
Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode);
// must be wdvi[1]
var inputType = (satalliteItem.Data["layers"] as JArray)?[1]["name"].ToString();
if (string.IsNullOrEmpty(inputType))
{
_logger.LogError("Could not get the input type name from the satellite item");
return;
}
// download the geotiff
_logger.LogInformation("Downloading geotiff file");
await _farmmapsApiService.DownloadItemAsync(satalliteItem.Code,
Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}.zip"));
// overwrite measurement date by date of satellite item
measurementDate = satalliteItem.DataDate.Value;
geotiffItem = satalliteItem;
}
// (geo)tiff input:
else if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
_logger.LogInformation("input = tiff data");
var dataPath = Path.Combine("Data", input.File);
geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath,
Path.GetFileNameWithoutExtension(input.File));
if (geotiffItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
}
// json/shape input
else {
var isGeoJson = input.File.Contains("json");
var dataPath = Path.Combine("Data", input.File);
@@ -117,6 +186,12 @@ namespace FarmmapsNbs
Path.Combine(DownloadFolder, $"{input.OutputFileName}.input_geotiff.zip"));
}
_logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}");
var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem);
var targetNData = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate,
@@ -139,7 +214,7 @@ namespace FarmmapsNbs
_logger.LogInformation("Calculating uptake map");
var uptakeMapItem =
await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate,
measurementDate, input.InputVariable);
measurementDate, input.InputVariable, input.InputLayerName);
if (uptakeMapItem == null)
{
_logger.LogError("Something went wrong with creating the uptakeMap");
@@ -154,8 +229,7 @@ namespace FarmmapsNbs
_logger.LogInformation("Calculating application map");
var applicationMapItem =
await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate,
measurementDate,
input.InputVariable, targetNData.TargetN);
measurementDate, input.InputVariable, targetNData.TargetN, input.InputLayerName);
if (applicationMapItem == null)
{
@@ -189,5 +263,28 @@ namespace FarmmapsNbs
}
// Functions to save previously created cropfields
private void LoadSettings()
{
if (File.Exists(SettingsFile))
{
var jsonText = File.ReadAllText(SettingsFile);
_settings = JsonConvert.DeserializeObject<Settings>(jsonText);
}
else
{
_settings = new Settings();
}
}
private void SaveSettings()
{
if (_settings == null)
return;
var json = JsonConvert.SerializeObject(_settings);
File.WriteAllText(SettingsFile, json);
}
}
}