Refactored some nitrogen methods.

This commit is contained in:
Mark van der Wal 2020-04-08 19:28:30 +02:00
parent b6e65d2881
commit 6dea82b973
10 changed files with 96 additions and 134 deletions

View File

@ -1,52 +0,0 @@
using System;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
using Microsoft.Extensions.Logging;
namespace FarmmapsApiSamples
{
public class Application : IApp
{
private readonly ILogger<Application> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly FarmmapsEventHub _farmmapsEventHub;
private readonly NitrogenService _nitrogenService;
private readonly HerbicideService _herbicideService;
public Application(ILogger<Application> logger, FarmmapsApiService farmmapsApiService,
FarmmapsEventHub farmmapsEventHub, NitrogenService nitrogenService,
HerbicideService herbicideService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_farmmapsEventHub = farmmapsEventHub;
_nitrogenService = nitrogenService;
_herbicideService = herbicideService;
_farmmapsEventHub.EventCallback += OnEvent;
}
private void OnEvent(EventMessage @event)
{
// _logger.LogInformation(@event.EventType);
}
public async Task RunAsync()
{
try
{
// !! 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();
await _nitrogenService.TestFlow(roots);
// await _herbicideService.TestFlow(roots);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
}
}
}

View File

@ -6,7 +6,6 @@ using System.Threading.Tasks;
using FarmmapsApi.Models; using FarmmapsApi.Models;
using FarmmapsApi.Services; using FarmmapsApi.Services;
using FarmmapsApiSamples.Models; using FarmmapsApiSamples.Models;
using Google.Apis.Upload;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -80,7 +79,8 @@ namespace FarmmapsApiSamples
var cropfieldItem = await CreateCropfieldSingleLutumItemAsync(myDrive.Code); var cropfieldItem = await CreateCropfieldSingleLutumItemAsync(myDrive.Code);
_logger.LogInformation("Uploading data"); _logger.LogInformation("Uploading data");
var inputItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, Path.Combine("Data", "Lutum.tiff"), "Lutum.tiff"); var filePath = Path.Combine("Data", "Herbicide", "Lutum.tiff");
var inputItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, filePath, "Lutum.tiff");
if(inputItem == null) if(inputItem == null)
{ {
_logger.LogError($"Failed to upload data"); _logger.LogError($"Failed to upload data");
@ -120,7 +120,8 @@ namespace FarmmapsApiSamples
// upload data // upload data
_logger.LogInformation("Uploading lutum data"); _logger.LogInformation("Uploading lutum data");
var inputItem = await _generalService.UploadZipWithShapeAsync(uploadedRoot, Path.Combine("Data", "vd_born_lutum.zip"), "Lutum"); var inputItem = await _generalService.UploadZipWithShapeAsync(uploadedRoot,
Path.Combine("Data", "Herbicide", "vd_born_lutum.zip"), "Lutum");
if(inputItem == null) if(inputItem == null)
{ {
_logger.LogError($"Failed to upload lutum data"); _logger.LogError($"Failed to upload lutum data");
@ -128,7 +129,8 @@ namespace FarmmapsApiSamples
} }
_logger.LogInformation("Uploading organic matter data"); _logger.LogInformation("Uploading organic matter data");
var inputExtraItem = await _generalService.UploadZipWithShapeAsync(uploadedRoot, Path.Combine("Data", "vd_born_om.zip"), "OS"); var inputExtraItem = await _generalService.UploadZipWithShapeAsync(uploadedRoot,
Path.Combine("Data", "Herbicide", "vd_born_om.zip"), "OS");
if(inputExtraItem == null) if(inputExtraItem == null)
{ {
_logger.LogError($"Failed to upload organic matter data"); _logger.LogError($"Failed to upload organic matter data");

View File

@ -1,9 +0,0 @@
using System.Threading.Tasks;
namespace FarmmapsApiSamples
{
public interface IApp
{
Task RunAsync();
}
}

View File

@ -50,9 +50,10 @@ namespace FarmmapsApiSamples
return; return;
} }
var cropfieldItem = await GetOrCreateCropfieldItemAsync(myDriveRoot.Code); var cropfieldItem = await CreateCropfieldItemAsync(myDriveRoot.Code, "VRA NBS cropfield", 2020,
@"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] }");
var dataPath = Path.Combine("Data", "Scan_1_20190605.zip");
var dataPath = Path.Combine("Data", "Nbs", "Scan_1_20190605.zip");
var isariaShapeItem = await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, "Scan_1_20190605"); var isariaShapeItem = await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, "Scan_1_20190605");
if (isariaShapeItem == null) if (isariaShapeItem == null)
{ {
@ -60,6 +61,7 @@ namespace FarmmapsApiSamples
return; return;
} }
_logger.LogInformation($"Converting shape to geotiff");
var isariaGeotiffItem = await _generalService.ShapeToGeotiff(isariaShapeItem); var isariaGeotiffItem = await _generalService.ShapeToGeotiff(isariaShapeItem);
if (isariaGeotiffItem == null) if (isariaGeotiffItem == null)
{ {
@ -68,7 +70,8 @@ namespace FarmmapsApiSamples
} }
_logger.LogInformation($"Calculating targetN with targetYield: {60}"); _logger.LogInformation($"Calculating targetN with targetYield: {60}");
var targetN = await CalculateTargetN(cropfieldItem, plantingDate, measurementDate, 60); var targetNItem = await CreateTargetNItem(cropfieldItem);
var targetN = await CalculateTargetN(cropfieldItem, targetNItem, plantingDate, measurementDate, "consumption", "irmi", 60);
_logger.LogInformation($"TargetN: {targetN}"); _logger.LogInformation($"TargetN: {targetN}");
_logger.LogInformation("Calculating uptake map"); _logger.LogInformation("Calculating uptake map");
@ -80,35 +83,39 @@ namespace FarmmapsApiSamples
_logger.LogInformation("Calculating appliance map"); _logger.LogInformation("Calculating appliance map");
var applianceMapItem = var applianceMapItem =
await CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate, measurementDate, targetN); await CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate, measurementDate, "irmi", targetN);
_logger.LogInformation("Downloading appliance map"); _logger.LogInformation("Downloading appliance map");
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(downloadFolder, $"{applianceMapItem.Name}.zip")); await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(downloadFolder, $"{applianceMapItem.Name}.zip"));
} }
private async Task<Item> GetOrCreateCropfieldItemAsync(string parentItemCode) private async Task<Item> CreateCropfieldItemAsync(string parentItemCode, string name, int year, string fieldGeomJson)
{ {
var cropfieldItems = await var currentYear = new DateTime(year, 1, 1);
_farmmapsApiService.GetItemChildrenAsync(parentItemCode, CROPFIELD_ITEMTYPE);
if (cropfieldItems.Count > 0)
return cropfieldItems[0];
var currentYear = new DateTime(DateTime.UtcNow.Year, 1, 1);
var cropfieldItemRequest = new ItemRequest() var cropfieldItemRequest = new ItemRequest()
{ {
ParentCode = parentItemCode, ParentCode = parentItemCode,
ItemType = CROPFIELD_ITEMTYPE, ItemType = CROPFIELD_ITEMTYPE,
Name = "Cropfield for VRA", Name = name,
DataDate = currentYear, DataDate = currentYear,
DataEndDate = currentYear.AddYears(1).AddDays(-1), DataEndDate = currentYear.AddYears(1).AddDays(-1),
Data = JObject.Parse("{}"), Data = JObject.Parse("{}"),
Geometry = JObject.Parse( Geometry = JObject.Parse(fieldGeomJson)
@"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] }")
}; };
return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest); return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
} }
public async Task<Item> CreateTargetNItem(Item cropfieldItem)
{
var itemRequest = new ItemRequest()
{
ParentCode = cropfieldItem.ParentCode,
ItemType = USERINPUT_ITEMTYPE,
Name = "TargetN"
};
return await _farmmapsApiService.CreateItemAsync(itemRequest);
}
/// <summary> /// <summary>
/// Calculates TargetN, makes the assumption the cropfield and user.input(targetn) item have the same parent /// Calculates TargetN, makes the assumption the cropfield and user.input(targetn) item have the same parent
@ -118,31 +125,16 @@ namespace FarmmapsApiSamples
/// <param name="plantingDate">The date the crop is planted</param> /// <param name="plantingDate">The date the crop is planted</param>
/// <param name="measurementDate">The date the measurements are taken</param> /// <param name="measurementDate">The date the measurements are taken</param>
/// <returns>The TargetN</returns> /// <returns>The TargetN</returns>
public async Task<double> CalculateTargetN(Item cropfieldItem, DateTime plantingDate, public async Task<double> CalculateTargetN(Item cropfieldItem, Item targetNItem, DateTime plantingDate,
DateTime measurementDate, int targetYield) DateTime measurementDate, string inputType, string purposeType, int targetYield)
{ {
var targetNItems = await
_farmmapsApiService.GetItemChildrenAsync(cropfieldItem.ParentCode, USERINPUT_ITEMTYPE);
Item targetNItem;
if (targetNItems.Count == 0)
{
_logger.LogInformation("Creating targetN item");
var itemRequest = CreateTargetNItemRequest(cropfieldItem.ParentCode);
targetNItem = await _farmmapsApiService.CreateItemAsync(itemRequest);
}
else
{
targetNItem = targetNItems[0];
}
var nbsTargetNRequest = new TaskRequest {TaskType = VRANBS_TASK}; var nbsTargetNRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsTargetNRequest.attributes["operation"] = "targetn"; nbsTargetNRequest.attributes["operation"] = "targetn";
nbsTargetNRequest.attributes["inputCode"] = targetNItem.Code; nbsTargetNRequest.attributes["inputCode"] = targetNItem.Code;
nbsTargetNRequest.attributes["plantingDate"] = plantingDate.ToString(); nbsTargetNRequest.attributes["plantingDate"] = plantingDate.ToString();
nbsTargetNRequest.attributes["measurementDate"] = measurementDate.ToString(); nbsTargetNRequest.attributes["measurementDate"] = measurementDate.ToString();
nbsTargetNRequest.attributes["inputType"] = "irmi"; nbsTargetNRequest.attributes["inputType"] = inputType;
nbsTargetNRequest.attributes["purposeType"] = "consumption"; nbsTargetNRequest.attributes["purposeType"] = purposeType.ToLower();
nbsTargetNRequest.attributes["targetYield"] = targetYield.ToString(); nbsTargetNRequest.attributes["targetYield"] = targetYield.ToString();
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsTargetNRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsTargetNRequest);
@ -176,14 +168,14 @@ namespace FarmmapsApiSamples
/// <param name="measurementDate">The date the measurements are taken</param> /// <param name="measurementDate">The date the measurements are taken</param>
/// <returns></returns> /// <returns></returns>
public async Task<Item> CalculateUptakeMap(Item cropfieldItem, Item inputItem, DateTime plantingDate, public async Task<Item> CalculateUptakeMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,
DateTime measurementDate) DateTime measurementDate, string inputType = "irmi")
{ {
var nbsUptakeMapRequest = new TaskRequest {TaskType = VRANBS_TASK}; var nbsUptakeMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsUptakeMapRequest.attributes["operation"] = "uptake"; nbsUptakeMapRequest.attributes["operation"] = "uptake";
nbsUptakeMapRequest.attributes["inputCode"] = inputItem.Code; nbsUptakeMapRequest.attributes["inputCode"] = inputItem.Code;
nbsUptakeMapRequest.attributes["plantingDate"] = plantingDate.ToString(); nbsUptakeMapRequest.attributes["plantingDate"] = plantingDate.ToString();
nbsUptakeMapRequest.attributes["measurementDate"] = measurementDate.ToString(); nbsUptakeMapRequest.attributes["measurementDate"] = measurementDate.ToString();
nbsUptakeMapRequest.attributes["inputType"] = "irmi"; nbsUptakeMapRequest.attributes["inputType"] = inputType.ToLower();
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsUptakeMapRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsUptakeMapRequest);
@ -200,21 +192,19 @@ namespace FarmmapsApiSamples
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); _logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null; return null;
} }
var geotiffItems = await
_farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code, GEOTIFF_PROCESSED_ITEMTYPE);
// maybe poll here to, to wait for geotiff task process to be finished?
// how to uniquely know which item is really created!? var itemName = "VRANbs uptake";
var nitrogenItem = geotiffItems.SingleOrDefault(i => i.Name.Contains("uptake") && i.Updated >= itemTask.Finished); var uptakeMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
if (nitrogenItem == null) GEOTIFF_PROCESSED_ITEMTYPE, itemName,
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
i.Name.ToLower().Contains(itemName.ToLower()));
if (uptakeMapItem == null)
{ {
_logger.LogError("Could not find the uptake geotiff child item under cropfield"); _logger.LogError("Could not find the uptake geotiff child item under cropfield");
return null; return null;
} }
return nitrogenItem; return uptakeMapItem;
} }
@ -225,10 +215,11 @@ namespace FarmmapsApiSamples
/// <param name="inputItem">The farmmaps item containing the geotiff data</param> /// <param name="inputItem">The farmmaps item containing the geotiff data</param>
/// <param name="plantingDate">The date the crop is planted</param> /// <param name="plantingDate">The date the crop is planted</param>
/// <param name="measurementDate">The date the measurements are taken</param> /// <param name="measurementDate">The date the measurements are taken</param>
/// <param name="inputType">The inputtype to use</param>
/// <param name="targetN">The target nitrogen to use for the calculations</param> /// <param name="targetN">The target nitrogen to use for the calculations</param>
/// <returns></returns> /// <returns></returns>
public async Task<Item> CalculateApplicationMap(Item cropfieldItem, Item inputItem, DateTime plantingDate, public async Task<Item> CalculateApplicationMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,
DateTime measurementDate, double targetN) DateTime measurementDate, string inputType = "irmi", double targetN = 60.0)
{ {
var nbsApplianceMapRequest = new TaskRequest {TaskType = VRANBS_TASK}; var nbsApplianceMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsApplianceMapRequest.attributes["operation"] = "application"; nbsApplianceMapRequest.attributes["operation"] = "application";
@ -236,7 +227,7 @@ namespace FarmmapsApiSamples
nbsApplianceMapRequest.attributes["plantingDate"] = plantingDate.ToString(); nbsApplianceMapRequest.attributes["plantingDate"] = plantingDate.ToString();
nbsApplianceMapRequest.attributes["measurementDate"] = measurementDate.ToString(); nbsApplianceMapRequest.attributes["measurementDate"] = measurementDate.ToString();
nbsApplianceMapRequest.attributes["inputCode"] = inputItem.Code; nbsApplianceMapRequest.attributes["inputCode"] = inputItem.Code;
nbsApplianceMapRequest.attributes["inputType"] = "irmi"; nbsApplianceMapRequest.attributes["inputType"] = inputType.ToLower();
nbsApplianceMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture); nbsApplianceMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture);
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplianceMapRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplianceMapRequest);
@ -254,14 +245,12 @@ namespace FarmmapsApiSamples
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); _logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null; return null;
} }
var geotiffItems = await
_farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code, GEOTIFF_PROCESSED_ITEMTYPE);
// maybe poll here too, to wait for geotiff task process to be finished?
// how to uniquely know which item is really created!? var itemName = $"VRANbs application";
var applianceMapItem = geotiffItems.SingleOrDefault(i => i.Name.Contains("nitrogen") && i.Updated >= itemTask.Finished); var applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
i.Name.ToLower().Contains(itemName.ToLower()));
if (applianceMapItem == null) if (applianceMapItem == null)
{ {
_logger.LogError("Could not find the application map geotiff child item under cropfield"); _logger.LogError("Could not find the application map geotiff child item under cropfield");
@ -270,15 +259,5 @@ namespace FarmmapsApiSamples
return applianceMapItem; return applianceMapItem;
} }
private ItemRequest CreateTargetNItemRequest(string parentItemCode)
{
return new ItemRequest()
{
ParentCode = parentItemCode,
ItemType = USERINPUT_ITEMTYPE,
Name = "TargetN"
};
}
} }
} }

View File

@ -11,6 +11,12 @@ namespace FarmmapsApiSamples
{ {
class Program class Program
{ {
private readonly ILogger<Program> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly FarmmapsEventHub _farmmapsEventHub;
private readonly NitrogenService _nitrogenService;
private readonly HerbicideService _herbicideService;
private static async Task Main(string[] args) private static async Task Main(string[] args)
{ {
IConfiguration config = new ConfigurationBuilder() IConfiguration config = new ConfigurationBuilder()
@ -26,12 +32,47 @@ namespace FarmmapsApiSamples
.AddFarmmapsServices(configuration) .AddFarmmapsServices(configuration)
.AddTransient<NitrogenService>() .AddTransient<NitrogenService>()
.AddTransient<HerbicideService>() .AddTransient<HerbicideService>()
.AddSingleton<IApp, Application>() .AddSingleton<Program>()
.BuildServiceProvider(); .BuildServiceProvider();
await serviceProvider.GetService<FarmmapsApiService>().AuthenticateAsync(); await serviceProvider.GetService<FarmmapsApiService>().AuthenticateAsync();
// await serviceProvider.GetService<FarmmapsEventHub>().StartEventHub(); // await serviceProvider.GetService<FarmmapsEventHub>().StartEventHub();
await serviceProvider.GetService<IApp>().RunAsync(); await serviceProvider.GetService<Program>().RunAsync();
}
public Program(ILogger<Program> logger, FarmmapsApiService farmmapsApiService,
FarmmapsEventHub farmmapsEventHub, NitrogenService nitrogenService,
HerbicideService herbicideService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_farmmapsEventHub = farmmapsEventHub;
_nitrogenService = nitrogenService;
_herbicideService = herbicideService;
_farmmapsEventHub.EventCallback += OnEvent;
}
private void OnEvent(EventMessage @event)
{
// _logger.LogInformation(@event.EventType);
}
public async Task RunAsync()
{
try
{
// !! 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();
await _nitrogenService.TestFlow(roots);
// await _herbicideService.TestFlow(roots);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
} }
} }
} }

View File

@ -2,5 +2,6 @@
Put your clientId and clientSecret in the appsettings.json. Put your clientId and clientSecret in the appsettings.json.
* Needs upload API. * Isn't 100% complete.
* Needs testing of all public api methods. * Needs proper testing of all public api methods.
* Needs documentation of all public api methods.