in KPIApplication possible to (1) add an operation, e.g. applying fertilizer and (2) add a cropfield characteristic containing cropyield.

This commit is contained in:
2023-10-04 17:21:05 +02:00
parent f04cc239d5
commit fedd363075
6 changed files with 221 additions and 77 deletions

View File

@@ -22,18 +22,18 @@ namespace FarmmapsKPI
private readonly ILogger<KPIApplication> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly KPIService _dataDownloadService;
private readonly KPIService _kpiService;
private readonly GeneralService _generalService;
private Settings _settings;
public KPIApplication(ILogger<KPIApplication> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService, KPIService dataDownloadService)
GeneralService generalService, KPIService kpiService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_generalService = generalService;
_dataDownloadService = dataDownloadService;
_kpiService = kpiService;
}
public async Task RunAsync()
@@ -70,6 +70,9 @@ namespace FarmmapsKPI
// !!specify if you are using an already created cropfield:
bool useCreatedCropfield = input.UseCreatedCropfield;
bool useCreatedCropRecording = input.UseCreatedCropRecording;
bool useCreatedOperations = input.UseCreatedOperation;
bool useCreatedCropfieldCharacteristic = input.UseCreatedCropfieldCharacteristic;
var cropYear = input.CropYear;
var fieldName = input.fieldName;
//bool storeSatelliteStatistics = input.StoreSatelliteStatisticsSingleImage;
@@ -98,63 +101,119 @@ namespace FarmmapsKPI
// Use already created cropfield or create new one, added a Data input, with field specific data for the KPI calculation
Item cropfieldItem;
//1 useCreatedCropfield = false -> get new
//1 useCreatedCropfield = false -> create new
//2 useCreatedCropfield = true && CropfieldItemCode = "" or absent -> read from settings json
//2 useCreatedCropfield = true && CropfieldItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
//3 useCreatedCropfield = true && CropfieldItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.CropfieldItemCode))
{
_logger.LogInformation("Creating cropfield, writting to settings file");
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
$"DataCropfield {input.OutputFileName}", cropYear, input.GeometryJson.ToString(Formatting.None), input.Data.ToString(Formatting.None));
$"{input.OutputFileName}", cropYear, input.GeometryJson.ToString(Formatting.None), input.DataCropfield.ToString(Formatting.None));
_settings.CropfieldItemCode = cropfieldItem.Code;
SaveSettings(settingsfile);
}
else if (string.IsNullOrEmpty(input.CropfieldItemCode))
{
_logger.LogInformation("CropfieldItemCode not in json input, reading from settings json");
_logger.LogInformation("reading CropfieldItemCode from settings file");
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
}
else
{
_logger.LogInformation("CropfieldItemCode not in json input, reading from settings json");
_logger.LogInformation("reading CropfieldItemCode from KPIinput.json");
cropfieldItem = await _farmmapsApiService.GetItemAsync(input.CropfieldItemCode);
}
//Get croprecordings
if (input.GetCropRecordings)
// Use already created croprecording or create new one, added a Data input, with field specific data for the KPI calculation
Item crprecItem;
//1 useCreatedCropRecording = false -> create new
//2 useCreatedCropRecording = true && CropRecordingItemCode = "" or absent -> read from settings json
//3 useCreatedCropRecording = true && CropRecordingItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
if (useCreatedCropRecording == false || string.IsNullOrEmpty(_settings.CropRecordingItemCode))
{
var crprecItem = input.CrprecItem;
_logger.LogInformation($"Trying to get crop recordings of croprecording: {crprecItem}");
var cropRec = await _farmmapsApiService.GetItemChildrenAsync(crprecItem, CROPREC_ITEMTYPE);
if (cropRec == null)
{
_logger.LogError("Something went wrong while obtaining the croprecordings");
return;
}
var cropRecPath = Path.Combine(downloadFolder, $"croprecordings_{crprecItem}.json");
_logger.LogInformation($"Found {cropRec.Count} crop recordings");
var count1 = 0;
await Task.Delay(500);
foreach (var item in cropRec)
{
Console.WriteLine($"Crop recording #{count1}: {item.Name}");
File.AppendAllText(cropRecPath, item.Data +Environment.NewLine);
count1++;
}
_logger.LogInformation($"Downloaded file {cropRecPath}");
_logger.LogInformation("RunCropRecordingTask ...");
crprecItem = await _generalService.RunCropRecordingTask(cropfieldItem);
_settings.CropRecordingItemCode = crprecItem.Code;
SaveSettings(settingsfile);
}
else if (string.IsNullOrEmpty(input.CropfieldItemCode))
{
_logger.LogInformation("reading CropRecordingItemCode from settings file");
crprecItem = await _farmmapsApiService.GetItemAsync(_settings.CropRecordingItemCode);
}
else
{
//todo set croprecordings from file
_logger.LogInformation("reading CropRecordingItemCode from KPIinput.json");
crprecItem = await _farmmapsApiService.GetItemAsync(input.CropRecordingItemCode);
}
// Use already created operation or create new one, added a Data input, with field specific data for the KPI calculation
Item crpOperationItem;
//1 useCreatedOperation = false -> create new
//2 useCreatedOperation = true && OperationItemCode = "" or absent -> read from settings json
//3 useCreatedOperation = true && OperationItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
if (useCreatedOperations == false || string.IsNullOrEmpty(_settings.OperationItemCode))
{
_logger.LogInformation("CreateOperationItemAsync ...");
crpOperationItem = await _generalService.CreateOperationItemAsync(crprecItem.Code,input.DataOperation.ToString(Formatting.None));
_settings.OperationItemCode = crpOperationItem.Code;
SaveSettings(settingsfile);
}
else if (string.IsNullOrEmpty(input.CropfieldItemCode))
{
_logger.LogInformation("reading OperationItemCode from settings file");
crpOperationItem = await _farmmapsApiService.GetItemAsync(_settings.OperationItemCode);
}
else
{
_logger.LogInformation("reading OperationItemCode from KPIinput.json");
crpOperationItem = await _farmmapsApiService.GetItemAsync(input.OperationItemCode);
}
// The cropfieldCharacteristicItem is used to enter crop yields
// So once we have added an operation for fertilizer application and a crop yield, then KPIapp can calculate
// Nutrient balance.
// Use already created cropfieldCharacteristicItem or create new one, added a Data input, with field specific data for the KPI calculation
Item cropfieldCharacteristicItem;
//1 useCreatedCropfieldCharacteristic = false -> create new
//2 useCreatedCropfieldCharacteristic = true && CropfieldCharacteristicItemCode = "" or absent -> read from settings json
//3 useCreatedCropfieldCharacteristic = true && CropfieldCharacteristicItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
if (useCreatedCropfieldCharacteristic == false)
{
_logger.LogInformation("CreateCropfieldCharacteristicItemAsync ...");
cropfieldCharacteristicItem = await _generalService.CreateCropfieldCharacteristicItemAsync(cropfieldItem.Code, input.DataCropfieldCharacteristic.ToString(Formatting.None));
_settings.CropfieldCharacteristicItemCode = cropfieldCharacteristicItem.Code;
SaveSettings(settingsfile);
}
else if (string.IsNullOrEmpty(input.CropfieldCharacteristicItemCode))
{
_logger.LogInformation("reading OperationItemCode from settings file");
cropfieldCharacteristicItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldCharacteristicItemCode);
}
else
{
_logger.LogInformation("reading CropfieldCharacteristicItemCode from KPIinput.json");
cropfieldCharacteristicItem = await _farmmapsApiService.GetItemAsync(input.CropfieldCharacteristicItemCode);
}
// Inspect the children. If all is well, cropfield will have one crprec and one edicrop.characteristic
// And the crprec will have 0-many operations as children
// And the Data of an operation will have specification of how much fertilizer was applied
// And crprec can have multiple operations
// Note existing cropfields and croprecordings keep existing properties you added in previous runs
// (unless you deleted them)
// So ech time you run with "UseCreatedCropfield": true & "UseCreatedCropRecording": false -> a new recording will be added to the existing cropfield
// So ech time you run with "UseCreatedCropfield": true & "useCreatedCropfieldCharacteristic": false -> a new CropfieldCharacteristic will be added to the existing cropfield
// So ech time you run with "UseCreatedCropRecording": true & "UseCreatedOperation": false -> a new operation will be added to the existing crop recording
var cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
var crprecChildren = await _farmmapsApiService.GetItemChildrenAsync(crprecItem.Code);
//Now get the KPIs for this cropfield, mounted with operations & cropyield
// Get KPI data for saving it in a file, here the generalsedrvice is called to get the KPI data
_logger.LogInformation($"Trying to get the cropfielditem: {cropfieldItem.Code}");
_logger.LogInformation($"GetKpiItemsForCropField({cropfieldItem.Code})");
var KPIItem = await _generalService.GetKpiItemsForCropField(cropfieldItem);
//Download KPI's into a json output file for this specific cropfield (with unique cropfieldItem.Code)
var KPIItemPath = Path.Combine(downloadFolder, $"KPIItems_{cropfieldItem.Code}.json");
_logger.LogInformation($"Found {KPIItem.Count} KPI items");
var count = 0;
@@ -167,23 +226,6 @@ namespace FarmmapsKPI
}
_logger.LogInformation($"Downloaded file {KPIItemPath}");
////////////
//_logger.LogInformation("Calculate KPI map for field");
//var KPIItem = await _generalService.GetKpiItemsForCropField(cropfieldItem);// hier dus verwijzen naar de KPI task
//if ((object)null == null)
//{
// _logger.LogError("Something went wrong while obtaining the KPI map");
// return;
// }
//_logger.LogInformation("Downloading KPI map");
//await _farmmapsApiService.DownloadItemAsync(KPIItem.Code,
// Path.Combine(downloadFolder, $"{input.OutputFileName}_KPI.zip"));
}
// Functions to save previously created cropfields