forked from FarmMaps/FarmMapsApiClient
		
	nbs application can now read multiple input from json file.
This commit is contained in:
		@@ -17,8 +17,6 @@ namespace FarmmapsApi.Services
 | 
				
			|||||||
        private readonly ILogger<GeneralService> _logger;
 | 
					        private readonly ILogger<GeneralService> _logger;
 | 
				
			||||||
        private readonly FarmmapsApiService _farmmapsApiService;
 | 
					        private readonly FarmmapsApiService _farmmapsApiService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private readonly string _downloadFolder = "Downloads";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        public GeneralService(ILogger<GeneralService> logger, FarmmapsApiService farmmapsApiService)
 | 
					        public GeneralService(ILogger<GeneralService> logger, FarmmapsApiService farmmapsApiService)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _logger = logger;
 | 
					            _logger = logger;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,6 +12,9 @@
 | 
				
			|||||||
      <None Update="Data\**\*">
 | 
					      <None Update="Data\**\*">
 | 
				
			||||||
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 | 
					        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 | 
				
			||||||
      </None>
 | 
					      </None>
 | 
				
			||||||
 | 
					      <None Update="NitrogenInput.json">
 | 
				
			||||||
 | 
					        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 | 
				
			||||||
 | 
					      </None>
 | 
				
			||||||
    </ItemGroup>
 | 
					    </ItemGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <ItemGroup>
 | 
					    <ItemGroup>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										16
									
								
								FarmmapsNbs/Models/NitrogenInput.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								FarmmapsNbs/Models/NitrogenInput.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using Newtonsoft.Json.Linq;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace FarmmapsNbs.Models
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class NitrogenInput
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public string IsariaZipFile { get; set; }
 | 
				
			||||||
 | 
					        public string OutputFileName { get; set; }
 | 
				
			||||||
 | 
					        public DateTime PlantingDate { get; set; }
 | 
				
			||||||
 | 
					        public DateTime MeasurementDate { get; set; }
 | 
				
			||||||
 | 
					        public string PotatoPurposeType { get; set; }
 | 
				
			||||||
 | 
					        public int TargetYield { get; set; }
 | 
				
			||||||
 | 
					        public JObject GeometryJson { get; set; }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,10 +1,14 @@
 | 
				
			|||||||
using System;
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
using System.IO;
 | 
					using System.IO;
 | 
				
			||||||
using System.Linq;
 | 
					using System.Linq;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
using FarmmapsApi;
 | 
					using FarmmapsApi;
 | 
				
			||||||
 | 
					using FarmmapsApi.Models;
 | 
				
			||||||
using FarmmapsApi.Services;
 | 
					using FarmmapsApi.Services;
 | 
				
			||||||
 | 
					using FarmmapsNbs.Models;
 | 
				
			||||||
using Microsoft.Extensions.Logging;
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
 | 
					using Newtonsoft.Json;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace FarmmapsNbs
 | 
					namespace FarmmapsNbs
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -28,17 +32,33 @@ namespace FarmmapsNbs
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        public async Task RunAsync()
 | 
					        public async Task RunAsync()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            try
 | 
					            var nitrogenInputJson = File.ReadAllText("NitrogenInput.json");
 | 
				
			||||||
            {
 | 
					            List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson);
 | 
				
			||||||
                // !! 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))
 | 
					            if (!Directory.Exists(DownloadFolder))
 | 
				
			||||||
                Directory.CreateDirectory(DownloadFolder);
 | 
					                Directory.CreateDirectory(DownloadFolder);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                var plantingDate = new DateTime(DateTime.UtcNow.Year, 2, 1);
 | 
					            // !! this call is needed the first time an api is called with a fresh clientid and secret !!
 | 
				
			||||||
                var measurementDate = plantingDate.AddMonths(5);
 | 
					            await _farmmapsApiService.GetCurrentUserCodeAsync();
 | 
				
			||||||
 | 
					            var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            foreach (var input in nitrogenInputs)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                try
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    await Process(roots, input);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                catch (Exception ex)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    _logger.LogError(ex.Message);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private async Task Process(List<UserRoot> roots, NitrogenInput input)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var plantingDate = input.PlantingDate;
 | 
				
			||||||
 | 
					            var measurementDate = input.MeasurementDate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
 | 
					            var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
 | 
				
			||||||
            if (uploadedRoot == null)
 | 
					            if (uploadedRoot == null)
 | 
				
			||||||
@@ -54,12 +74,12 @@ namespace FarmmapsNbs
 | 
				
			|||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code, "VRA NBS cropfield", 2020,
 | 
					            var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
 | 
				
			||||||
                    @"{ ""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 ] ] ] }");
 | 
					                $"VRA NBS cropfield {input.OutputFileName}", DateTime.Now.Year, input.GeometryJson.ToString(Formatting.None));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                var dataPath = Path.Combine("Data", "Scan_1_20190605.zip");
 | 
					            var dataPath = Path.Combine("Data", input.IsariaZipFile);
 | 
				
			||||||
            var isariaShapeItem =
 | 
					            var isariaShapeItem =
 | 
				
			||||||
                    await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, "Scan_1_20190605");
 | 
					                await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, Path.GetFileNameWithoutExtension(input.IsariaZipFile));
 | 
				
			||||||
            if (isariaShapeItem == null)
 | 
					            if (isariaShapeItem == null)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                _logger.LogError("Could not find isaria shape item");
 | 
					                _logger.LogError("Could not find isaria shape item");
 | 
				
			||||||
@@ -74,15 +94,16 @@ namespace FarmmapsNbs
 | 
				
			|||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                _logger.LogInformation($"Calculating targetN with targetYield: {60}");
 | 
					            _logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}");
 | 
				
			||||||
            var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem);
 | 
					            var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem);
 | 
				
			||||||
                var targetN = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate, measurementDate,
 | 
					            var targetN = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate,
 | 
				
			||||||
                    "consumption", "irmi", 60);
 | 
					                measurementDate, input.PotatoPurposeType, "irmi", input.TargetYield);
 | 
				
			||||||
            _logger.LogInformation($"TargetN: {targetN}");
 | 
					            _logger.LogInformation($"TargetN: {targetN}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _logger.LogInformation("Calculating uptake map");
 | 
					            _logger.LogInformation("Calculating uptake map");
 | 
				
			||||||
            var uptakeMapItem =
 | 
					            var uptakeMapItem =
 | 
				
			||||||
                    await _nitrogenService.CalculateUptakeMap(cropfieldItem, isariaGeotiffItem, plantingDate, measurementDate);
 | 
					                await _nitrogenService.CalculateUptakeMap(cropfieldItem, isariaGeotiffItem, plantingDate,
 | 
				
			||||||
 | 
					                    measurementDate);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _logger.LogInformation("Downloading uptake map");
 | 
					            _logger.LogInformation("Downloading uptake map");
 | 
				
			||||||
            await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code,
 | 
					            await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code,
 | 
				
			||||||
@@ -90,17 +111,13 @@ namespace FarmmapsNbs
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            _logger.LogInformation("Calculating appliance map");
 | 
					            _logger.LogInformation("Calculating appliance map");
 | 
				
			||||||
            var applianceMapItem =
 | 
					            var applianceMapItem =
 | 
				
			||||||
                    await _nitrogenService.CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate, measurementDate,
 | 
					                await _nitrogenService.CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate,
 | 
				
			||||||
 | 
					                    measurementDate,
 | 
				
			||||||
                    "irmi", targetN);
 | 
					                    "irmi", targetN);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _logger.LogInformation("Downloading appliance map");
 | 
					            _logger.LogInformation("Downloading appliance map");
 | 
				
			||||||
            await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code,
 | 
					            await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code,
 | 
				
			||||||
                Path.Combine(DownloadFolder, $"{applianceMapItem.Name}.zip"));
 | 
					                Path.Combine(DownloadFolder, $"{applianceMapItem.Name}.zip"));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
            catch (Exception ex)
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                _logger.LogError(ex.Message);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										11
									
								
								FarmmapsNbs/NitrogenInput.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								FarmmapsNbs/NitrogenInput.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					[
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    "isariaZipFile": "Scan_1_20190605.zip",
 | 
				
			||||||
 | 
					    "outputFileName": "vranbs1",
 | 
				
			||||||
 | 
					    "plantingDate": "2019-04-18",
 | 
				
			||||||
 | 
					    "measurementDate": "2019-06-05",
 | 
				
			||||||
 | 
					    "potatoPurposeType": "consumption",
 | 
				
			||||||
 | 
					    "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 ] ] ] }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
		Reference in New Issue
	
	Block a user