From 331acf63536c9b46ee9261014c97d04608efb94d Mon Sep 17 00:00:00 2001 From: Mark van der Wal Date: Thu, 19 Nov 2020 10:38:17 +0100 Subject: [PATCH] Added "zoning" if statement calculation sample --- FarmmapsZonering/ZoneringApplication.cs | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/FarmmapsZonering/ZoneringApplication.cs b/FarmmapsZonering/ZoneringApplication.cs index 5522490..375b7f9 100644 --- a/FarmmapsZonering/ZoneringApplication.cs +++ b/FarmmapsZonering/ZoneringApplication.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -45,6 +46,12 @@ namespace FarmmapsZonering await _farmmapsApiService.GetCurrentUserCodeAsync(); var roots = await _farmmapsApiService.GetCurrentUserRootsAsync(); + // await SimpleAdditionAsync(roots); + await ZoningAsync(roots); + } + + private async Task SimpleAdditionAsync(List roots) + { var myDrive = roots.SingleOrDefault(r => r.Name == "My drive"); if (myDrive == null) { @@ -108,6 +115,72 @@ namespace FarmmapsZonering await _farmmapsApiService.DownloadItemAsync(outputItem.Code, Path.Combine(DownloadFolder, $"times_2_zonering.zip")); } + + private async Task ZoningAsync(List 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; + } + + Item cropfieldItem; + if (string.IsNullOrEmpty(_settings.CropfieldItemCode)) + { + _logger.LogInformation("Creating cropfield"); + cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Zonering", 2020, + @"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 5.670991253771027, 52.796788997702613 ], [ 5.671526456638633, 52.797291618546666 ], [ 5.671275936147413, 52.797422436717852 ], [ 5.671959173850738, 52.798269302728798 ], [ 5.670649634919365, 52.798778791408822 ], [ 5.671503682048522, 52.799591206957416 ], [ 5.675159003761311, 52.798193567415474 ], [ 5.673029579585948, 52.796024727480535 ], [ 5.670991253771027, 52.796788997702613 ] ] ] }"); + _settings.CropfieldItemCode = cropfieldItem.Code; + SaveSettings(); + } + else + { + _logger.LogInformation("Cropfield already exists trying to get"); + cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode); + } + + var inputOneItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, + Path.Combine("Data", "data_9001.tif"),"data_9001"); + if (inputOneItem == null) { + _logger.LogError("Could not find item for uploaded data"); + return; + } + + var inputTwoItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, + Path.Combine("Data", "data_times_two_4326.tiff"), "data_times_two_4326"); + if (inputTwoItem == null) { + _logger.LogError("Could not find item for uploaded data"); + return; + } + + var outputItem = await _zoneringService.CreateApplicationMapAsync(cropfieldItem, "if [0] >= 1.28 then [1] else 0", new Output() + { + Name = "Remove", + Unit = "m&m", + Quantity = "Nonsense" + }, new InputParameter() + { + ItemCode = inputOneItem.Code, + LayerName = inputOneItem.Data["layers"][0]["name"].ToString() + }, + new InputParameter() + { + ItemCode = inputTwoItem.Code, + LayerName = inputTwoItem.Data["layers"][0]["name"].ToString() + }); + + _logger.LogInformation("Downloading output"); + await _farmmapsApiService.DownloadItemAsync(outputItem.Code, + Path.Combine(DownloadFolder, $"NonsenseRemove.zip")); + } private void LoadSettings() {