using System.IO; using System.Linq; using System.Threading.Tasks; using FarmmapsApi; using FarmmapsApi.Models; using FarmmapsApi.Services; using FarmmapsHaulmkilling.Models; using FarmmapsZonering.Models; using FarmmapsZonering.Services; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using static FarmmapsApiSamples.Constants; namespace FarmmapsZonering { public class ZoneringApplication : IApplication { private const string DownloadFolder = "Downloads"; private const string SettingsFile = "settings.json"; private readonly ILogger _logger; private readonly FarmmapsApiService _farmmapsApiService; private readonly GeneralService _generalService; private readonly ZoneringService _zoneringService; private Settings _settings; public ZoneringApplication(ILogger logger, FarmmapsApiService farmmapsApiService, GeneralService generalService, ZoneringService zoneringService) { _logger = logger; _farmmapsApiService = farmmapsApiService; _generalService = generalService; _zoneringService = zoneringService; } 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; } 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 inputFileName = "neo_ndvi-reglone-low-weed.tif"; var dataPath = Path.Combine("Data", inputFileName); var geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath, Path.GetFileNameWithoutExtension(inputFileName)); if (geotiffItem == null) { _logger.LogError("Could not find item for uploaded data"); return; } var outputItem = await _zoneringService.CreateApplicationMapAsync(cropfieldItem, "[0] * 2", new Output() { Name = "times_2_zonering", Unit = "n/kg", Quantity = "Nitrogen" }, new InputParameter() { ItemCode = geotiffItem.Code, ItemLayer = geotiffItem.Data["layers"][0]["name"].ToString() }); _logger.LogInformation("Downloading output"); await _farmmapsApiService.DownloadItemAsync(outputItem.Code, Path.Combine(DownloadFolder, $"times_2_zonering.zip")); } private void LoadSettings() { if (File.Exists(SettingsFile)) { var jsonText = File.ReadAllText(SettingsFile); _settings = JsonConvert.DeserializeObject(jsonText); } else { _settings = new Settings(); } } private void SaveSettings() { if (_settings == null) return; var json = JsonConvert.SerializeObject(_settings); File.WriteAllText(SettingsFile, json); } } }