Compare commits
	
		
			9 Commits
		
	
	
		
			5f1073b80c
			...
			a331437433
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| a331437433 | |||
| 276cce1808 | |||
| 17765f17b0 | |||
| 690807a632 | |||
| f16ad90f68 | |||
| e6244ff74b | |||
| 357bcaea60 | |||
| 7565c576c1 | |||
| 262bd641a8 | 
| @@ -3,11 +3,16 @@ namespace FarmmapsApiSamples | ||||
|     public static class Constants | ||||
|     { | ||||
|         public const string USERINPUT_ITEMTYPE = "vnd.farmmaps.itemtype.user.input"; | ||||
|         public const string TEMPORAL_ITEMTYPE = "vnd.farmmaps.itemtype.temporal"; | ||||
|         public const string GEOTIFF_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff"; | ||||
|         public const string GEOTIFF_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff.processed"; | ||||
|         public const string CROPFIELD_ITEMTYPE = "vnd.farmmaps.itemtype.cropfield"; | ||||
|         public const string SHAPE_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.shape.processed"; | ||||
|         public const string SHAPE_ITEMTYPE = "vnd.farmmaps.itemtype.shape"; | ||||
|          | ||||
|         public const string VRANBS_TASK = "vnd.farmmaps.task.vranbs"; | ||||
|         public const string VRAHERBICIDE_TASK = "vnd.farmmaps.task.vraherbicide"; | ||||
|         public const string VRAHAULMKILLING_TASK = "vnd.farmmaps.task.vrahaulmkilling"; | ||||
|         public const string SATELLITE_TASK = "vnd.farmmaps.task.satellite"; | ||||
|     } | ||||
| } | ||||
| @@ -96,27 +96,37 @@ namespace FarmmapsApi.Services | ||||
|  | ||||
|         public async Task<Item> ShapeToGeotiff(Item shapeItem) | ||||
|         { | ||||
|             var shapeToGeotiffRequest = new TaskRequest() | ||||
|             { | ||||
|                 TaskType = "vnd.farmmaps.task.shapetogeotiff" | ||||
|             }; | ||||
|             var taskCode = await _farmmapsApiService.QueueTaskAsync(shapeItem.Code, shapeToGeotiffRequest); | ||||
|  | ||||
|             await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) => | ||||
|             { | ||||
|                 _logger.LogInformation("Checking shapetogeotiff task status"); | ||||
|                 var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(shapeItem.Code, taskCode); | ||||
|                 if (itemTaskStatus.IsFinished) | ||||
|                     tokenSource.Cancel(); | ||||
|             }); | ||||
|  | ||||
|             _logger.LogInformation("Data shape converted to geotiff"); | ||||
|             await RunAndWaitForTask(shapeItem, "vnd.farmmaps.task.shapetogeotiff"); | ||||
|  | ||||
|             // the parent of the shape item is now the tiff item | ||||
|             shapeItem = await _farmmapsApiService.GetItemAsync(shapeItem.Code); | ||||
|             return await _farmmapsApiService.GetItemAsync(shapeItem.ParentCode); | ||||
|         } | ||||
|  | ||||
|         public async Task<ItemTaskStatus> RunAndWaitForTask(Item subjectItem, string taskIdentifier,  | ||||
|             Action<TaskRequest> configureCallback = null, int retrySeconds = 3) | ||||
|         { | ||||
|             var taskRequest = new TaskRequest() | ||||
|             { | ||||
|                 TaskType = taskIdentifier | ||||
|             }; | ||||
|             configureCallback?.Invoke(taskRequest); | ||||
|              | ||||
|             var taskCode = await _farmmapsApiService.QueueTaskAsync(subjectItem.Code, taskRequest); | ||||
|  | ||||
|             await PollTask(TimeSpan.FromSeconds(retrySeconds), async (tokenSource) => | ||||
|             { | ||||
|                 _logger.LogInformation($"Checking {taskIdentifier} task status"); | ||||
|                 var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(subjectItem.Code, taskCode); | ||||
|                 if (itemTaskStatus.IsFinished) | ||||
|                     tokenSource.Cancel(); | ||||
|             }); | ||||
|              | ||||
|             _logger.LogInformation($"{taskIdentifier} finished"); | ||||
|              | ||||
|             return await _farmmapsApiService.GetTaskStatusAsync(subjectItem.Code, taskCode); | ||||
|         } | ||||
|  | ||||
|         public async Task<Item> FindChildItemAsync(string parentCode, string itemType, string containsName, | ||||
|             Func<Item, bool> filter = null, int maxTries = 10) | ||||
|         { | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
|  | ||||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||||
| #  | ||||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FarmmapsNbs", "FarmmapsNbs\FarmmapsNbs.csproj", "{E08EF7E9-F09E-42D8-825C-164E458C78F4}" | ||||
| EndProject | ||||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FarmmapsApi", "FarmmapsApi\FarmmapsApi.csproj", "{1FA9E50B-F45E-4534-953A-37C783D03C74}" | ||||
| @@ -12,6 +13,8 @@ EndProjectSection | ||||
| EndProject | ||||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FarmmapsHerbicide", "FarmmapsHerbicide\FarmmapsHerbicide.csproj", "{731A88CD-9DC4-4969-86F2-2315830A6998}" | ||||
| EndProject | ||||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FarmmapsHaulmkilling", "FarmmapsHaulmkilling\FarmmapsHaulmkilling.csproj", "{DFA89D0B-5400-4374-B824-8367B76B4B6E}" | ||||
| EndProject | ||||
| Global | ||||
| 	GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
| 		Debug|Any CPU = Debug|Any CPU | ||||
| @@ -30,5 +33,9 @@ Global | ||||
| 		{731A88CD-9DC4-4969-86F2-2315830A6998}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||||
| 		{731A88CD-9DC4-4969-86F2-2315830A6998}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||||
| 		{731A88CD-9DC4-4969-86F2-2315830A6998}.Release|Any CPU.Build.0 = Release|Any CPU | ||||
| 		{DFA89D0B-5400-4374-B824-8367B76B4B6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||||
| 		{DFA89D0B-5400-4374-B824-8367B76B4B6E}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||||
| 		{DFA89D0B-5400-4374-B824-8367B76B4B6E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||||
| 		{DFA89D0B-5400-4374-B824-8367B76B4B6E}.Release|Any CPU.Build.0 = Release|Any CPU | ||||
| 	EndGlobalSection | ||||
| EndGlobal | ||||
|   | ||||
							
								
								
									
										20
									
								
								FarmmapsHaulmkilling/FarmmapsHaulmkilling.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								FarmmapsHaulmkilling/FarmmapsHaulmkilling.csproj
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| <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> | ||||
							
								
								
									
										152
									
								
								FarmmapsHaulmkilling/HaulmkillingApplication.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										152
									
								
								FarmmapsHaulmkilling/HaulmkillingApplication.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,152 @@ | ||||
| using System.IO; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| using FarmmapsApi; | ||||
| using FarmmapsApi.Models; | ||||
| using FarmmapsApi.Services; | ||||
| using FarmmapsHaulmkilling.Models; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| using static FarmmapsApiSamples.Constants; | ||||
|  | ||||
| namespace FarmmapsHaulmkilling | ||||
| { | ||||
|     public class HaulmkillingApplication : IApplication | ||||
|     { | ||||
|         private const string DownloadFolder = "Downloads"; | ||||
|         private const string SettingsFile = "settings.json"; | ||||
|  | ||||
|         private readonly ILogger<HaulmkillingApplication> _logger; | ||||
|         private readonly FarmmapsApiService _farmmapsApiService; | ||||
|         private readonly HaulmkillingService _haulmkillingService; | ||||
|         private readonly GeneralService _generalService; | ||||
|  | ||||
|         private Settings _settings; | ||||
|          | ||||
|         public HaulmkillingApplication(ILogger<HaulmkillingApplication> logger, FarmmapsApiService farmmapsApiService, | ||||
|             GeneralService generalService, HaulmkillingService haulmkillingService) | ||||
|         { | ||||
|             _logger = logger; | ||||
|             _farmmapsApiService = farmmapsApiService; | ||||
|             _generalService = generalService; | ||||
|             _haulmkillingService = haulmkillingService; | ||||
|         } | ||||
|          | ||||
|         public async Task RunAsync() | ||||
|         { | ||||
|             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(); | ||||
|  | ||||
|             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; | ||||
|             } | ||||
|  | ||||
|             var agents = await _haulmkillingService.GetHaulmkillingAgents(); | ||||
|             if (agents == null) | ||||
|             { | ||||
|                 _logger.LogError("No valid agents found for haulmkilling"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             Item cropfieldItem; | ||||
|             if (string.IsNullOrEmpty(_settings.CropfieldItemCode)) | ||||
|             { | ||||
|                 _logger.LogInformation("Creating cropfield"); | ||||
|                 cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Haulmkilling", 2020,  | ||||
|                     @"{""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]]]}"); | ||||
|                 _settings.CropfieldItemCode = cropfieldItem.Code; | ||||
|                 SaveSettings(); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 _logger.LogInformation("Cropfield already exists trying to get"); | ||||
|                 cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode); | ||||
|             } | ||||
|  | ||||
|             ItemTaskStatus taskStatus; | ||||
|             if (!string.IsNullOrEmpty(_settings.SatelliteTaskCode)) | ||||
|             { | ||||
|                 _logger.LogInformation("Gathering satellite information for cropfield, this might take a while!"); | ||||
|                 taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, SATELLITE_TASK, null, 20); | ||||
|  | ||||
|                 if (taskStatus.State == ItemTaskState.Error) | ||||
|                 { | ||||
|                     _logger.LogError($"Something went wrong when trying to process satellite data; {taskStatus.Message}"); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 _settings.SatelliteTaskCode = taskStatus.Code; | ||||
|                 SaveSettings(); | ||||
|  | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 taskStatus = await _farmmapsApiService.GetTaskStatusAsync(_settings.CropfieldItemCode, _settings.SatelliteTaskCode); | ||||
|             } | ||||
|  | ||||
|             // find ndvi or wdvi satellite data geotiffs | ||||
|             var temporalITem = await _generalService.FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE, | ||||
|                 "Cropfield VRA Haulmkilling, Satellite", item => item.SourceTask == SATELLITE_TASK &&  | ||||
|                                                                  taskStatus.Finished >= item.Created &&  | ||||
|                                                                  taskStatus.Finished <= item.Created.Value.AddHours(1)); | ||||
|              | ||||
|             if (temporalITem == null) | ||||
|             { | ||||
|                 _logger.LogError($"Temporal item not found"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalITem.Code); | ||||
|             var firstSatelliteItem = satelliteTiffs.FirstOrDefault(); | ||||
|  | ||||
|             if (firstSatelliteItem == null) | ||||
|             { | ||||
|                 _logger.LogError($"Satellite item not found"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             // create haulmkilling application map | ||||
|  | ||||
|             // download application map | ||||
|         } | ||||
|  | ||||
|         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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										36
									
								
								FarmmapsHaulmkilling/HaulmkillingService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								FarmmapsHaulmkilling/HaulmkillingService.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| using FarmmapsApi.Services; | ||||
| using FarmmapsHaulmkilling.Models; | ||||
| using Microsoft.Extensions.Logging; | ||||
|  | ||||
| namespace FarmmapsHaulmkilling | ||||
| { | ||||
|     public class HaulmkillingService | ||||
|     { | ||||
|         private readonly ILogger<HaulmkillingService> _logger; | ||||
|         private readonly FarmmapsApiService _farmmapsApiService; | ||||
|         private readonly GeneralService _generalService; | ||||
|  | ||||
|         public HaulmkillingService(ILogger<HaulmkillingService> logger, FarmmapsApiService farmmapsApiService, | ||||
|             GeneralService generalService) | ||||
|         { | ||||
|             _logger = logger; | ||||
|             _farmmapsApiService = farmmapsApiService; | ||||
|             _generalService = generalService; | ||||
|         } | ||||
|          | ||||
|         public async Task<List<HaulmkillingAgent>> GetHaulmkillingAgents() | ||||
|         { | ||||
|             var itemType = "vnd.farmmaps.package.vra.haulmkilling"; | ||||
|             var vraHerbicideDataItems = await _farmmapsApiService.GetItemsAsync(string.Empty, itemType); | ||||
|  | ||||
|             var item = vraHerbicideDataItems.FirstOrDefault(); | ||||
|             if (item == null) | ||||
|                 return null; | ||||
|  | ||||
|             return item.Data.ContainsKey("agents") ? item.Data["agents"].ToObject<List<HaulmkillingAgent>>() : null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										30
									
								
								FarmmapsHaulmkilling/Models/HaulmkillingAgent.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								FarmmapsHaulmkilling/Models/HaulmkillingAgent.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
|  | ||||
| namespace FarmmapsHaulmkilling.Models | ||||
| { | ||||
|     public class HaulmkillingAgent | ||||
|     { | ||||
|         public string Name { get; set; } | ||||
|         public List<string> SupportedOptions { get; set; } | ||||
|         public Dictionary<string, List<HaulmkillingAgentValue>> Values { get; set; } | ||||
|  | ||||
|         public HaulmkillingAgentValue? GetAgentValue(string inputName, string optionKey) | ||||
|         { | ||||
|             if (!Values.ContainsKey(inputName)) | ||||
|                 return null; | ||||
|  | ||||
|             var agentDataValues = Values[inputName]; | ||||
|             return agentDataValues.FirstOrDefault(v => v.Option.Equals(optionKey)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public struct HaulmkillingAgentValue | ||||
|     { | ||||
|         public string Option { get; set; } | ||||
|         public float Min { get; set; } | ||||
|         public float Max { get; set; } | ||||
|         public float FMul { get; set; } | ||||
|         public float FExp { get; set; } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										8
									
								
								FarmmapsHaulmkilling/Models/Settings.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								FarmmapsHaulmkilling/Models/Settings.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| namespace FarmmapsHaulmkilling.Models | ||||
| { | ||||
|     public class Settings | ||||
|     { | ||||
|         public string CropfieldItemCode { get; set; } | ||||
|         public string SatelliteTaskCode { get; set; } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										23
									
								
								FarmmapsHaulmkilling/Program.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								FarmmapsHaulmkilling/Program.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| using System.Threading.Tasks; | ||||
| using FarmmapsApi; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Microsoft.Extensions.Logging; | ||||
|  | ||||
| namespace FarmmapsHaulmkilling | ||||
| { | ||||
|     class Program : FarmmapsProgram<HaulmkillingApplication> | ||||
|     { | ||||
|         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<HaulmkillingService>(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										10
									
								
								FarmmapsHaulmkilling/appsettings.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								FarmmapsHaulmkilling/appsettings.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| { | ||||
|   "Authority": "https://accounts.farmmaps.awtest.nl/", | ||||
|   "Endpoint": "http://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"] | ||||
| } | ||||
| @@ -5,7 +5,8 @@ namespace FarmmapsNbs.Models | ||||
| { | ||||
|     public class NitrogenInput | ||||
|     { | ||||
|         public string IsariaZipFile { get; set; } | ||||
|         public string ZippedShapeFile { get; set; } | ||||
|         public string InputVariable { get; set; } | ||||
|         public string OutputFileName { get; set; } | ||||
|         public DateTime PlantingDate { get; set; } | ||||
|         public DateTime MeasurementDate { get; set; } | ||||
|   | ||||
| @@ -77,20 +77,20 @@ namespace FarmmapsNbs | ||||
|             var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code, | ||||
|                 $"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None)); | ||||
|  | ||||
|             var dataPath = Path.Combine("Data", input.IsariaZipFile); | ||||
|             var isariaShapeItem = | ||||
|                 await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, Path.GetFileNameWithoutExtension(input.IsariaZipFile)); | ||||
|             if (isariaShapeItem == null) | ||||
|             var dataPath = Path.Combine("Data", input.ZippedShapeFile); | ||||
|             var shapeItem = | ||||
|                 await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, Path.GetFileNameWithoutExtension(input.ZippedShapeFile)); | ||||
|             if (shapeItem == null) | ||||
|             { | ||||
|                 _logger.LogError("Could not find isaria shape item"); | ||||
|                 _logger.LogError("Could not find shape item"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             _logger.LogInformation($"Converting shape to geotiff"); | ||||
|             var isariaGeotiffItem = await _generalService.ShapeToGeotiff(isariaShapeItem); | ||||
|             if (isariaGeotiffItem == null) | ||||
|             var geotiffItem = await _generalService.ShapeToGeotiff(shapeItem); | ||||
|             if (geotiffItem == null) | ||||
|             { | ||||
|                 _logger.LogError("Something went wrong with isaria shape to geotiff transformation"); | ||||
|                 _logger.LogError("Something went wrong with shape to geotiff transformation"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
| @@ -112,21 +112,21 @@ namespace FarmmapsNbs | ||||
|  | ||||
|             _logger.LogInformation("Calculating uptake map"); | ||||
|             var uptakeMapItem = | ||||
|                 await _nitrogenService.CalculateUptakeMap(cropfieldItem, isariaGeotiffItem, plantingDate, | ||||
|                     measurementDate); | ||||
|                 await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate, | ||||
|                     measurementDate, input.InputVariable); | ||||
|  | ||||
|             _logger.LogInformation("Downloading uptake map"); | ||||
|             await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code, | ||||
|                 Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip")); | ||||
|  | ||||
|             _logger.LogInformation("Calculating appliance map"); | ||||
|             var applianceMapItem = | ||||
|                 await _nitrogenService.CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate, | ||||
|             _logger.LogInformation("Calculating application map"); | ||||
|             var applicationMapItem = | ||||
|                 await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate, | ||||
|                     measurementDate, | ||||
|                     "irmi", targetNData.TargetN); | ||||
|                     input.InputVariable, targetNData.TargetN); | ||||
|  | ||||
|             _logger.LogInformation("Downloading appliance map"); | ||||
|             await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, | ||||
|             _logger.LogInformation("Downloading application map"); | ||||
|             await _farmmapsApiService.DownloadItemAsync(applicationMapItem.Code, | ||||
|                 Path.Combine(DownloadFolder, $"{input.OutputFileName}.application.zip")); | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| [ | ||||
|   { | ||||
|     "isariaZipFile": "Scan_1_20190605.zip", | ||||
|     "zippedShapeFile": "Scan_1_20190605.zip", | ||||
|     "inputVariable":  "irmi", | ||||
|     "outputFileName": "vranbs1", | ||||
|     "plantingDate": "2019-04-18", | ||||
|     "measurementDate": "2019-06-05", | ||||
| @@ -9,30 +10,117 @@ | ||||
|     "geometryJson": { "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 ] ] ] } | ||||
|   }, | ||||
|   { | ||||
|     "isariaZipFile": "Scan_1_20190605.zip", | ||||
|     "zippedShapeFile": "Scan_1_20190605.zip", | ||||
|     "inputVariable": "irmi", | ||||
|     "outputFileName": "vranbs2", | ||||
|     "plantingDate": "2019-04-18", | ||||
|     "measurementDate": "2019-06-05", | ||||
|     "potatoPurposeType": "starch", | ||||
|     "targetYield": 45, | ||||
|     "geometryJson": { "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 ] ] ] } | ||||
|     "geometryJson": { | ||||
|       "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 ] | ||||
|         ] | ||||
|       ] | ||||
|     } | ||||
|   }, | ||||
|   { | ||||
|     "isariaZipFile": "Scan_1_20190605.zip", | ||||
|     "zippedShapeFile": "Scan_1_20190605.zip", | ||||
|     "inputVariable": "irmi", | ||||
|     "outputFileName": "vranbs3", | ||||
|     "plantingDate": "2019-04-18", | ||||
|     "measurementDate": "2019-06-20", | ||||
|     "potatoPurposeType": "starch", | ||||
|     "targetYield": 45, | ||||
|     "geometryJson": { "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 ] ] ] } | ||||
|     "geometryJson": { | ||||
|       "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 ] | ||||
|         ] | ||||
|       ] | ||||
|     } | ||||
|   }, | ||||
|   { | ||||
|     "isariaZipFile": "Scan_1_20190605.zip", | ||||
|     "zippedShapeFile": "Scan_1_20190605.zip", | ||||
|     "inputVariable": "irmi", | ||||
|     "outputFileName": "vranbs4", | ||||
|     "plantingDate": "2019-04-18", | ||||
|     "measurementDate": "2019-07-03", | ||||
|     "potatoPurposeType": "starch", | ||||
|     "targetYield": 45, | ||||
|     "geometryJson": { "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 ] ] ] } | ||||
|     "geometryJson": { | ||||
|       "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 ] | ||||
|         ] | ||||
|       ] | ||||
|     } | ||||
|   } | ||||
| ] | ||||
| @@ -84,7 +84,7 @@ namespace FarmmapsNbs | ||||
|         /// <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") | ||||
|             DateTime measurementDate, string inputType) | ||||
|         { | ||||
|             var nbsUptakeMapRequest = new TaskRequest {TaskType = VRANBS_TASK}; | ||||
|             nbsUptakeMapRequest.attributes["operation"] = "uptake"; | ||||
| @@ -125,7 +125,7 @@ namespace FarmmapsNbs | ||||
|  | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates the nitrogen appliance map based on given input data | ||||
|         /// Creates the nitrogen application 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> | ||||
| @@ -135,18 +135,18 @@ namespace FarmmapsNbs | ||||
|         /// <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) | ||||
|             DateTime measurementDate, string inputType, double targetN) | ||||
|         { | ||||
|             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); | ||||
|             var nbsApplicationMapRequest = new TaskRequest {TaskType = VRANBS_TASK}; | ||||
|             nbsApplicationMapRequest.attributes["operation"] = "application"; | ||||
|             nbsApplicationMapRequest.attributes["inputCode"] = inputItem.Code; | ||||
|             nbsApplicationMapRequest.attributes["plantingDate"] = plantingDate.ToString(); | ||||
|             nbsApplicationMapRequest.attributes["measurementDate"] = measurementDate.ToString(); | ||||
|             nbsApplicationMapRequest.attributes["inputCode"] = inputItem.Code; | ||||
|             nbsApplicationMapRequest.attributes["inputType"] = inputType.ToLower(); | ||||
|             nbsApplicationMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture); | ||||
|              | ||||
|             string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplianceMapRequest); | ||||
|             string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplicationMapRequest); | ||||
|              | ||||
|             await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => | ||||
|             { | ||||
| @@ -163,17 +163,17 @@ namespace FarmmapsNbs | ||||
|             } | ||||
|  | ||||
|             var itemName = $"VRANbs application"; | ||||
|             var applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code, | ||||
|             var applicationMapItem = 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 (applicationMapItem == null) | ||||
|             { | ||||
|                 _logger.LogError("Could not find the application map geotiff child item under cropfield"); | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             return applianceMapItem; | ||||
|             return applicationMapItem; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user