refactored program
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								FarmmapsNbs/Data/Herbicide/Lutum.tiff
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								FarmmapsNbs/Data/Herbicide/Lutum.tiff
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								FarmmapsNbs/Data/Herbicide/vd_born_lutum.zip
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								FarmmapsNbs/Data/Herbicide/vd_born_lutum.zip
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								FarmmapsNbs/Data/Herbicide/vd_born_om.zip
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								FarmmapsNbs/Data/Herbicide/vd_born_om.zip
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								FarmmapsNbs/Data/Nbs/Scan_1_20190605.zip
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								FarmmapsNbs/Data/Nbs/Scan_1_20190605.zip
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										21
									
								
								FarmmapsNbs/FarmmapsNbs.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								FarmmapsNbs/FarmmapsNbs.csproj
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
|  | ||||
|     <PropertyGroup> | ||||
|         <OutputType>Exe</OutputType> | ||||
|         <TargetFramework>netcoreapp3.0</TargetFramework> | ||||
|     </PropertyGroup> | ||||
|  | ||||
|     <ItemGroup> | ||||
|       <None Update="appsettings.json"> | ||||
|         <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||||
|       </None> | ||||
|       <None Update="Data\**\*"> | ||||
|         <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||||
|       </None> | ||||
|     </ItemGroup> | ||||
|  | ||||
|     <ItemGroup> | ||||
|       <ProjectReference Include="..\FarmmapsApi\FarmmapsApi.csproj" /> | ||||
|     </ItemGroup> | ||||
|  | ||||
| </Project> | ||||
							
								
								
									
										250
									
								
								FarmmapsNbs/HerbicideService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										250
									
								
								FarmmapsNbs/HerbicideService.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,250 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.IO; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| using FarmmapsApi.Models; | ||||
| using FarmmapsApi.Services; | ||||
| using FarmmapsApiSamples.Models; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using Newtonsoft.Json; | ||||
| using Newtonsoft.Json.Linq; | ||||
| using static FarmmapsApi.Extensions; | ||||
| using static FarmmapsApiSamples.Constants; | ||||
|  | ||||
| namespace FarmmapsApiSamples | ||||
| { | ||||
|     public class HerbicideService | ||||
|     { | ||||
|         private readonly ILogger<HerbicideService> _logger; | ||||
|         private readonly FarmmapsApiService _farmmapsApiService; | ||||
|         private readonly GeneralService _generalService; | ||||
|  | ||||
|         private HerbicideAgent liberatorTarweAgent; | ||||
|         private readonly string _downloadFolder = "Downloads"; | ||||
|  | ||||
|         public HerbicideService(ILogger<HerbicideService> logger, FarmmapsApiService farmmapsApiService, | ||||
|             GeneralService generalService) | ||||
|         { | ||||
|             _logger = logger; | ||||
|             _farmmapsApiService = farmmapsApiService; | ||||
|             _generalService = generalService; | ||||
|  | ||||
|             liberatorTarweAgent = new HerbicideAgent() | ||||
|             { | ||||
|                 Middel = "Liberator", | ||||
|                 Grondsoort = "Klei", | ||||
|                 ExtraProductType = "OS", | ||||
|                 ActieveStofGehalte = "diflu100+flufenacet400", | ||||
|                 MinDosis = 0.33f, | ||||
|                 MaxDosis = 0.6f, | ||||
|                 A = 0.007f, | ||||
|                 B = 0.25f, | ||||
|                 C = 1.05f, | ||||
|                 D = 97f, | ||||
|                 E = 3f, | ||||
|                 P = 1f, | ||||
|                 Gewas = "Tarwe", | ||||
|                 Opmerking = "Org. stof arme grond", | ||||
|                 TankMix = string.Empty | ||||
|             }; | ||||
|         } | ||||
|  | ||||
|         public async Task TestFlow(List<UserRoot> roots) | ||||
|         { | ||||
|             if (!Directory.Exists(_downloadFolder)) | ||||
|                 Directory.CreateDirectory(_downloadFolder); | ||||
|                  | ||||
| //            await SingleLutumTiffFlow(roots); | ||||
|             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 CreateCropfieldSingleLutumItemAsync(myDrive.Code); | ||||
|              | ||||
|             _logger.LogInformation("Uploading data"); | ||||
|             var filePath = Path.Combine("Data", "Herbicide", "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 CalculateApplianceMapAsync(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 CreateCropfieldVanDenBorneAsync(myDrive.Code); | ||||
|              | ||||
|             // upload data | ||||
|             _logger.LogInformation("Uploading lutum data"); | ||||
|             var inputItem = await _generalService.UploadZipWithShapeAsync(uploadedRoot,  | ||||
|                 Path.Combine("Data", "Herbicide", "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", "Herbicide", "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 | ||||
|             _logger.LogInformation("Calculating appliance map"); | ||||
|             var applianceMapItem = await CalculateApplianceMapAsync(cropfieldItem, liberatorTarweAgent, tiffInputItem, tiffInputExtraItem); | ||||
|             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<Item> CreateCropfieldSingleLutumItemAsync(string parentItemCode) | ||||
|         { | ||||
|             var currentYear = new DateTime(DateTime.UtcNow.Year, 1, 1); | ||||
|             var cropfieldItemRequest = new ItemRequest() | ||||
|             { | ||||
|                 ParentCode = parentItemCode, | ||||
|                 ItemType = CROPFIELD_ITEMTYPE, | ||||
|                 Name = "Cropfield VRA Herbicide LUTUM", | ||||
|                 DataDate = currentYear, | ||||
|                 DataEndDate = currentYear.AddMonths(3), | ||||
|                 Data = JObject.Parse("{}"), | ||||
|                 Geometry = JObject.Parse( | ||||
|                     @"{""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]]]}") | ||||
|             }; | ||||
|  | ||||
|             return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest); | ||||
|         } | ||||
|          | ||||
|         private async Task<Item> CreateCropfieldVanDenBorneAsync(string parentItemCode) | ||||
|         { | ||||
|             var currentYear = new DateTime(DateTime.UtcNow.Year, 1, 1); | ||||
|             var cropfieldItemRequest = new ItemRequest() | ||||
|             { | ||||
|                 ParentCode = parentItemCode, | ||||
|                 ItemType = CROPFIELD_ITEMTYPE, | ||||
|                 Name = "Cropfield VRA Herbicide van den borne", | ||||
|                 DataDate = currentYear, | ||||
|                 DataEndDate = currentYear.AddMonths(3), | ||||
|                 Data = JObject.Parse("{}"), | ||||
|                 Geometry = JObject.Parse( | ||||
|                     @"{ ""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 ] ] ] }") | ||||
|             }; | ||||
|  | ||||
|             return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest); | ||||
|         } | ||||
|  | ||||
|         private async Task<Item> CalculateApplianceMapAsync(Item cropfieldItem, HerbicideAgent agent, params Item[] inputItem) | ||||
|         { | ||||
|             if (inputItem.Length == 0) | ||||
|                 return null; | ||||
|              | ||||
|             var taskRequest = new TaskRequest() | ||||
|             { | ||||
|                 TaskType = "vnd.farmmaps.task.vraherbicide" | ||||
|             }; | ||||
|             taskRequest.attributes["inputCode"] = inputItem[0].Code; | ||||
|             taskRequest.attributes["agent"] = JsonConvert.SerializeObject(agent); | ||||
|  | ||||
|             if (inputItem.Length > 1) | ||||
|                 taskRequest.attributes["extraInputCode"] = inputItem[1].Code; | ||||
|  | ||||
|             var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskRequest); | ||||
|             await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) => | ||||
|             { | ||||
|                 _logger.LogInformation("Checking vraherbicide task status"); | ||||
|                 var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode); | ||||
|                 if (itemTaskStatus.IsFinished) | ||||
|                     tokenSource.Cancel(); | ||||
|             }); | ||||
|              | ||||
|             var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode); | ||||
|             if (itemTask.State == ItemTaskState.Error) | ||||
|             { | ||||
|                 _logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             var itemName = $"VRAHerbicide {agent.Middel}"; | ||||
|             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) | ||||
|             { | ||||
|                 _logger.LogError("Could not find the VRAHerbicide geotiff child item under cropfield"); | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             return applianceMapItem; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										22
									
								
								FarmmapsNbs/Models/HerbicideAgent.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								FarmmapsNbs/Models/HerbicideAgent.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| namespace FarmmapsApiSamples.Models | ||||
| { | ||||
|     public class HerbicideAgent | ||||
|     { | ||||
|         public string ProductType { get; set; } | ||||
|         public string Middel { get; set; } | ||||
|         public string Grondsoort { get; set; } | ||||
|         public string ActieveStofGehalte{ get; set; } | ||||
|         public string Gewas { get; set; } | ||||
|         public string Opmerking { get; set; } | ||||
|         public string TankMix { get; set; } | ||||
|         public string ExtraProductType { get; set; } | ||||
|         public float MinDosis { get; set; } | ||||
|         public float MaxDosis { get; set; } | ||||
|         public float A { get; set; } | ||||
|         public float B { get; set; } | ||||
|         public float C { get; set; } | ||||
|         public float D { get; set; } | ||||
|         public float E { get; set; } | ||||
|         public float P { get; set; } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										107
									
								
								FarmmapsNbs/NbsApplication.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										107
									
								
								FarmmapsNbs/NbsApplication.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,107 @@ | ||||
| using System; | ||||
| using System.IO; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| using FarmmapsApi; | ||||
| using FarmmapsApi.Services; | ||||
| using Microsoft.Extensions.Logging; | ||||
|  | ||||
| namespace FarmmapsApiSamples | ||||
| { | ||||
|     public class NbsApplication : IApplication | ||||
|     { | ||||
|         private const string DownloadFolder = "Downloads"; | ||||
|  | ||||
|         private readonly ILogger<NbsApplication> _logger; | ||||
|         private readonly FarmmapsApiService _farmmapsApiService; | ||||
|         private readonly FarmmapsEventHub _farmmapsEventHub; | ||||
|         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() | ||||
|         { | ||||
|             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(); | ||||
|  | ||||
|                 if (!Directory.Exists(DownloadFolder)) | ||||
|                     Directory.CreateDirectory(DownloadFolder); | ||||
|  | ||||
|                 var plantingDate = new DateTime(DateTime.UtcNow.Year, 2, 1); | ||||
|                 var measurementDate = plantingDate.AddMonths(5); | ||||
|  | ||||
|                 var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded"); | ||||
|                 if (uploadedRoot == null) | ||||
|                 { | ||||
|                     _logger.LogError("Could not find a needed root item"); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive"); | ||||
|                 if (myDriveRoot == null) | ||||
|                 { | ||||
|                     _logger.LogError("Could not find a needed root item"); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var cropfieldItem = await _generalService.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", "Nbs", "Scan_1_20190605.zip"); | ||||
|                 var isariaShapeItem = | ||||
|                     await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, "Scan_1_20190605"); | ||||
|                 if (isariaShapeItem == null) | ||||
|                 { | ||||
|                     _logger.LogError("Could not find isaria shape item"); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 _logger.LogInformation($"Converting shape to geotiff"); | ||||
|                 var isariaGeotiffItem = await _generalService.ShapeToGeotiff(isariaShapeItem); | ||||
|                 if (isariaGeotiffItem == null) | ||||
|                 { | ||||
|                     _logger.LogError("Something went wrong with isaria shape to geotiff transformation"); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 _logger.LogInformation($"Calculating targetN with targetYield: {60}"); | ||||
|                 var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem); | ||||
|                 var targetN = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate, measurementDate, | ||||
|                     "consumption", "irmi", 60); | ||||
|                 _logger.LogInformation($"TargetN: {targetN}"); | ||||
|  | ||||
|                 _logger.LogInformation("Calculating uptake map"); | ||||
|                 var uptakeMapItem = | ||||
|                     await _nitrogenService.CalculateUptakeMap(cropfieldItem, isariaGeotiffItem, plantingDate, measurementDate); | ||||
|  | ||||
|                 _logger.LogInformation("Downloading uptake map"); | ||||
|                 await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code, | ||||
|                     Path.Combine(DownloadFolder, $"{uptakeMapItem.Name}.zip")); | ||||
|  | ||||
|                 _logger.LogInformation("Calculating appliance map"); | ||||
|                 var applianceMapItem = | ||||
|                     await _nitrogenService.CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate, measurementDate, | ||||
|                         "irmi", targetN); | ||||
|  | ||||
|                 _logger.LogInformation("Downloading appliance map"); | ||||
|                 await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, | ||||
|                     Path.Combine(DownloadFolder, $"{applianceMapItem.Name}.zip")); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 _logger.LogError(ex.Message); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										184
									
								
								FarmmapsNbs/NitrogenService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										184
									
								
								FarmmapsNbs/NitrogenService.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,184 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Globalization; | ||||
| using System.IO; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| using FarmmapsApi.Models; | ||||
| using FarmmapsApi.Services; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using Newtonsoft.Json.Linq; | ||||
| using static FarmmapsApi.Extensions; | ||||
| using static FarmmapsApiSamples.Constants; | ||||
|  | ||||
| namespace FarmmapsApiSamples | ||||
| { | ||||
|     public class NitrogenService | ||||
|     { | ||||
|         private readonly ILogger<NitrogenService> _logger; | ||||
|         private readonly FarmmapsApiService _farmmapsApiService; | ||||
|         private readonly GeneralService _generalService; | ||||
|  | ||||
|         public NitrogenService(ILogger<NitrogenService> logger, FarmmapsApiService farmmapsApiService, | ||||
|             GeneralService generalService) | ||||
|         { | ||||
|             _logger = logger; | ||||
|             _farmmapsApiService = farmmapsApiService; | ||||
|             _generalService = generalService; | ||||
|         } | ||||
|  | ||||
|         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> | ||||
|         /// Calculates TargetN, makes the assumption the cropfield and user.input(targetn) item have the same parent | ||||
|         /// </summary> | ||||
|         /// <param name="cropfieldItem">The cropfield to base the calculations on</param> | ||||
|         /// <param name="targetYield">The target yield input for the TargetN calculation</param> | ||||
|         /// <param name="plantingDate">The date the crop is planted</param> | ||||
|         /// <param name="measurementDate">The date the measurements are taken</param> | ||||
|         /// <returns>The TargetN</returns> | ||||
|         public async Task<double> CalculateTargetN(Item cropfieldItem, Item targetNItem, DateTime plantingDate,  | ||||
|             DateTime measurementDate, string inputType, string purposeType, int targetYield) | ||||
|         { | ||||
|             var nbsTargetNRequest = new TaskRequest {TaskType = VRANBS_TASK}; | ||||
|             nbsTargetNRequest.attributes["operation"] = "targetn"; | ||||
|             nbsTargetNRequest.attributes["inputCode"] = targetNItem.Code; | ||||
|             nbsTargetNRequest.attributes["plantingDate"] = plantingDate.ToString(); | ||||
|             nbsTargetNRequest.attributes["measurementDate"] = measurementDate.ToString(); | ||||
|             nbsTargetNRequest.attributes["inputType"] = inputType; | ||||
|             nbsTargetNRequest.attributes["purposeType"] = purposeType.ToLower(); | ||||
|             nbsTargetNRequest.attributes["targetYield"] = targetYield.ToString(); | ||||
|             string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsTargetNRequest); | ||||
|              | ||||
|             await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) => | ||||
|             { | ||||
|                 var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); | ||||
|                 if (itemTaskStatus.IsFinished) | ||||
|                     tokenSource.Cancel(); | ||||
|             }); | ||||
|  | ||||
|             var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); | ||||
|             if(itemTask.State == ItemTaskState.Error) | ||||
|             { | ||||
|                 _logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); | ||||
|                 return 0; | ||||
|             } | ||||
|              | ||||
|             var item = await _farmmapsApiService.GetItemAsync(targetNItem.Code); | ||||
|             if (item.Data.ContainsKey("TargetN")) | ||||
|                 return item.Data.Value<double>("TargetN"); | ||||
|  | ||||
|             return 0; | ||||
|         } | ||||
|          | ||||
|         /// <summary> | ||||
|         /// Calculates the uptake map based on the given inputs | ||||
|         /// </summary> | ||||
|         /// <param name="cropfieldItem">The cropfield to base the calculations on</param> | ||||
|         /// <param name="inputItem"></param> | ||||
|         /// <param name="plantingDate">The date the crop is planted</param> | ||||
|         /// <param name="measurementDate">The date the measurements are taken</param> | ||||
|         /// <returns></returns> | ||||
|         public async Task<Item> CalculateUptakeMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,  | ||||
|             DateTime measurementDate, string inputType = "irmi") | ||||
|         { | ||||
|             var nbsUptakeMapRequest = new TaskRequest {TaskType = VRANBS_TASK}; | ||||
|             nbsUptakeMapRequest.attributes["operation"] = "uptake"; | ||||
|             nbsUptakeMapRequest.attributes["inputCode"] = inputItem.Code; | ||||
|             nbsUptakeMapRequest.attributes["plantingDate"] = plantingDate.ToString(); | ||||
|             nbsUptakeMapRequest.attributes["measurementDate"] = measurementDate.ToString(); | ||||
|             nbsUptakeMapRequest.attributes["inputType"] = inputType.ToLower(); | ||||
|  | ||||
|             string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsUptakeMapRequest); | ||||
|              | ||||
|             await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => | ||||
|             { | ||||
|                 var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); | ||||
|                 if (itemTaskStatus.IsFinished) | ||||
|                     tokenSource.Cancel(); | ||||
|             }); | ||||
|              | ||||
|             var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); | ||||
|             if(itemTask.State == ItemTaskState.Error) | ||||
|             { | ||||
|                 _logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             var itemName = "VRANbs uptake"; | ||||
|             var uptakeMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code, | ||||
|                 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"); | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             return uptakeMapItem; | ||||
|         } | ||||
|  | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates the nitrogen appliance map based on given input data | ||||
|         /// </summary> | ||||
|         /// <param name="cropfieldItem">The cropfield to base the calculations on</param> | ||||
|         /// <param name="inputItem">The farmmaps item containing the geotiff data</param> | ||||
|         /// <param name="plantingDate">The date the crop is planted</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> | ||||
|         /// <returns></returns> | ||||
|         public async Task<Item> CalculateApplicationMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,  | ||||
|             DateTime measurementDate, string inputType = "irmi", double targetN = 60.0) | ||||
|         { | ||||
|             var nbsApplianceMapRequest = new TaskRequest {TaskType = VRANBS_TASK}; | ||||
|             nbsApplianceMapRequest.attributes["operation"] = "application"; | ||||
|             nbsApplianceMapRequest.attributes["inputCode"] = inputItem.Code; | ||||
|             nbsApplianceMapRequest.attributes["plantingDate"] = plantingDate.ToString(); | ||||
|             nbsApplianceMapRequest.attributes["measurementDate"] = measurementDate.ToString(); | ||||
|             nbsApplianceMapRequest.attributes["inputCode"] = inputItem.Code; | ||||
|             nbsApplianceMapRequest.attributes["inputType"] = inputType.ToLower(); | ||||
|             nbsApplianceMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture); | ||||
|              | ||||
|             string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplianceMapRequest); | ||||
|              | ||||
|             await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => | ||||
|             { | ||||
|                 var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); | ||||
|                 if (itemTaskStatus.IsFinished) | ||||
|                     tokenSource.Cancel(); | ||||
|             }); | ||||
|              | ||||
|             var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); | ||||
|             if(itemTask.State == ItemTaskState.Error) | ||||
|             { | ||||
|                 _logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             var itemName = $"VRANbs application"; | ||||
|             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) | ||||
|             { | ||||
|                 _logger.LogError("Could not find the application map geotiff child item under cropfield"); | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             return applianceMapItem; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										23
									
								
								FarmmapsNbs/Program.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								FarmmapsNbs/Program.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| using System.Threading.Tasks; | ||||
| using FarmmapsApi; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Microsoft.Extensions.Logging; | ||||
|  | ||||
| namespace FarmmapsApiSamples | ||||
| { | ||||
|     class Program : FarmmapsProgram<NbsApplication> | ||||
|     { | ||||
|         private static async Task Main(string[] args) | ||||
|         { | ||||
|             await new Program().Start(args); | ||||
|         } | ||||
|  | ||||
|         protected override void Configure(IServiceCollection serviceCollection) | ||||
|         { | ||||
|             serviceCollection.AddLogging(opts => opts | ||||
|                     .AddConsole() | ||||
|                     .AddFilter("System.Net.Http", LogLevel.Warning)) | ||||
|                 .AddTransient<NitrogenService>(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										10
									
								
								FarmmapsNbs/appsettings.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								FarmmapsNbs/appsettings.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| { | ||||
|   "Authority": "https://accounts.farmmaps.awtest.nl/", | ||||
|   "Endpoint": "https://farmmaps.awtest.nl/", | ||||
|   "BasePath": "api/v1", | ||||
|   "DiscoveryEndpointUrl": "https://accounts.farmmaps.awtest.nl/.well-known/openid-configuration", | ||||
|   "RedirectUri": "http://example.nl/api", | ||||
|   "ClientId": "", | ||||
|   "ClientSecret": "", | ||||
|   "Scopes": ["api"] | ||||
| } | ||||
		Reference in New Issue
	
	Block a user