2020-12-11 13:02:01 +00:00
|
|
|
using System;
|
2020-11-19 09:38:17 +00:00
|
|
|
using System.Collections.Generic;
|
2020-10-15 13:14:29 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using FarmmapsApi;
|
|
|
|
using FarmmapsApi.Models;
|
|
|
|
using FarmmapsApi.Services;
|
|
|
|
using FarmmapsHaulmkilling.Models;
|
2020-11-12 14:49:17 +00:00
|
|
|
using FarmmapsZonering.Models;
|
2020-10-15 13:14:29 +00:00
|
|
|
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<ZoneringApplication> _logger;
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
private readonly GeneralService _generalService;
|
|
|
|
private readonly ZoneringService _zoneringService;
|
|
|
|
|
|
|
|
private Settings _settings;
|
|
|
|
|
|
|
|
public ZoneringApplication(ILogger<ZoneringApplication> 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);
|
|
|
|
|
2020-12-11 13:02:01 +00:00
|
|
|
// Load settings from previous cropfield
|
2020-10-15 13:14:29 +00:00
|
|
|
LoadSettings();
|
2020-12-11 13:02:01 +00:00
|
|
|
|
|
|
|
// Read input data from separate file
|
|
|
|
var zoneringInputJson = File.ReadAllText("ZoneringInput.json");
|
|
|
|
List<ZoneringInput> zoneringInputs = JsonConvert.DeserializeObject<List<ZoneringInput>>(zoneringInputJson);
|
2020-10-15 13:14:29 +00:00
|
|
|
|
|
|
|
// !! 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();
|
2020-11-17 15:37:13 +00:00
|
|
|
|
2020-11-12 14:49:17 +00:00
|
|
|
|
2020-12-11 13:02:01 +00:00
|
|
|
foreach (var input in zoneringInputs)
|
|
|
|
{
|
|
|
|
try
|
2020-11-17 15:37:13 +00:00
|
|
|
{
|
2020-12-11 13:02:01 +00:00
|
|
|
await ZoningAsync(roots, input);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
2020-11-17 15:37:13 +00:00
|
|
|
{
|
2020-12-11 13:02:01 +00:00
|
|
|
_logger.LogError(ex.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 13:14:29 +00:00
|
|
|
}
|
2020-11-19 09:38:17 +00:00
|
|
|
|
2020-12-11 13:02:01 +00:00
|
|
|
private async Task ZoningAsync(List<UserRoot> roots, ZoneringInput input)
|
2020-11-19 09:38:17 +00:00
|
|
|
{
|
|
|
|
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;
|
2020-12-18 09:22:23 +00:00
|
|
|
if (string.IsNullOrEmpty(_settings.CropfieldItemCode) || input.CreateNewCropfield == true)
|
2020-11-19 09:38:17 +00:00
|
|
|
{
|
|
|
|
_logger.LogInformation("Creating cropfield");
|
2020-12-11 13:02:01 +00:00
|
|
|
|
2020-12-18 09:22:23 +00:00
|
|
|
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, input.CropFieldName, input.CropYear,
|
|
|
|
input.GeometryJson.ToString(Formatting.None));
|
|
|
|
|
|
|
|
_settings.CropfieldName = cropfieldItem.Name;
|
2020-11-19 09:38:17 +00:00
|
|
|
_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,
|
2020-12-18 09:22:23 +00:00
|
|
|
Path.Combine("Data", $"{input.InputItemOne}"),Path.GetFileNameWithoutExtension($"{input.InputItemOne}"));
|
2020-11-19 09:38:17 +00:00
|
|
|
if (inputOneItem == null) {
|
|
|
|
_logger.LogError("Could not find item for uploaded data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:22:23 +00:00
|
|
|
var inputTwoItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE,
|
|
|
|
Path.Combine("Data", $"{input.InputItemTwo}"), Path.GetFileNameWithoutExtension($"{input.InputItemTwo}"));
|
2020-11-19 09:38:17 +00:00
|
|
|
if (inputTwoItem == null) {
|
|
|
|
_logger.LogError("Could not find item for uploaded data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:22:23 +00:00
|
|
|
var outputItem = await _zoneringService.CreateApplicationMapAsync(cropfieldItem, input.Formula, new Output()
|
2020-11-19 09:38:17 +00:00
|
|
|
{
|
2020-12-18 09:22:23 +00:00
|
|
|
Name = input.LayerName,
|
|
|
|
Quantity = input.CalculatedQuantity,
|
|
|
|
Unit = input.CalculatedUnit,
|
|
|
|
|
2020-11-19 09:38:17 +00:00
|
|
|
}, new InputParameter()
|
|
|
|
{
|
|
|
|
ItemCode = inputOneItem.Code,
|
|
|
|
LayerName = inputOneItem.Data["layers"][0]["name"].ToString()
|
|
|
|
},
|
|
|
|
new InputParameter()
|
|
|
|
{
|
|
|
|
ItemCode = inputTwoItem.Code,
|
|
|
|
LayerName = inputTwoItem.Data["layers"][0]["name"].ToString()
|
|
|
|
});
|
|
|
|
|
2020-12-18 09:22:23 +00:00
|
|
|
_logger.LogInformation("Downloading output");
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-19 09:38:17 +00:00
|
|
|
await _farmmapsApiService.DownloadItemAsync(outputItem.Code,
|
2020-12-18 09:22:23 +00:00
|
|
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.zip"));
|
2020-11-19 09:38:17 +00:00
|
|
|
}
|
2020-10-15 13:14:29 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|