FarmMapsApiClient_KB34_MAST/FarmmapsNbs/NbsApplication.cs

336 lines
15 KiB
C#
Raw Normal View History

2020-04-08 18:23:22 +00:00
using System;
using System.Collections.Generic;
2020-04-08 18:23:22 +00:00
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FarmmapsApi;
using FarmmapsApi.Models;
2020-04-08 18:23:22 +00:00
using FarmmapsApi.Services;
using FarmmapsNbs.Models;
2020-04-08 18:23:22 +00:00
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2020-07-06 11:12:44 +00:00
using static FarmmapsApiSamples.Constants;
2020-04-08 18:23:22 +00:00
2020-04-08 18:39:38 +00:00
namespace FarmmapsNbs
2020-04-08 18:23:22 +00:00
{
public class NbsApplication : IApplication
{
private const string DownloadFolder = "Downloads";
private const string SettingsFile = "settings.json";
2020-04-08 18:23:22 +00:00
private readonly ILogger<NbsApplication> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly NitrogenService _nitrogenService;
private readonly GeneralService _generalService;
private Settings _settings;
2020-04-08 18:23:22 +00:00
public NbsApplication(ILogger<NbsApplication> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService, NitrogenService nitrogenService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_generalService = generalService;
_nitrogenService = nitrogenService;
}
public async Task RunAsync()
{
var nitrogenInputJson = File.ReadAllText("InputData-NBS.json"); //NitrogenInput.json
List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson);
if (!Directory.Exists(DownloadFolder))
Directory.CreateDirectory(DownloadFolder);
2020-04-08 18:23:22 +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-04-08 18:23:22 +00:00
foreach (var input in nitrogenInputs)
{
try
2020-04-08 18:23:22 +00:00
{
await Process(roots, input);
2020-04-08 18:23:22 +00:00
}
catch (Exception ex)
2020-04-08 18:23:22 +00:00
{
_logger.LogError(ex.Message);
2020-04-08 18:23:22 +00:00
}
}
}
2020-04-08 18:23:22 +00:00
private async Task Process(List<UserRoot> roots, NitrogenInput input)
{
2021-01-28 14:08:32 +00:00
// !!specify if you are using an already created cropfield:
bool useCreatedCropfield = input. UseCreatedCropfield;
var plantingDate = input.PlantingDate;
2021-01-28 14:08:32 +00:00
var FieldName = input.fieldName;
bool StoreStatistics = input.storeSatelliteStatistics;
var measurementDate = input.MeasurementDate;
2021-01-28 14:08:32 +00:00
string settingsfile = $"Settings_{FieldName}.json";
LoadSettings(settingsfile);
2020-04-08 18:23:22 +00:00
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot == null)
{
_logger.LogError("Could not find a needed root item");
return;
}
2020-04-08 18:23:22 +00:00
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDriveRoot == null)
{
_logger.LogError("Could not find a needed root item");
return;
}
2021-01-28 14:08:32 +00:00
// Use already created cropfield or create new one
Item cropfieldItem;
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.CropfieldItemCode))
{
_logger.LogInformation("Creating cropfield");
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
$"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None));
_settings.CropfieldItemCode = cropfieldItem.Code;
2021-01-28 14:08:32 +00:00
SaveSettings(settingsfile);
}
else
{
_logger.LogInformation("Cropfield already exists, trying to get it");
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
}
var geotiffItem = (Item)null;
// No file input, use most recent satellite image
2021-01-28 14:08:32 +00:00
if (string.IsNullOrEmpty(input.File)) {
_logger.LogInformation("No specific data given, retrieving most recent satellite image");
// check if satellite task not yet done, do here and save taskcode
2021-01-28 14:08:32 +00:00
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode)) {
var satelliteTaskCode = await _generalService.RunSatelliteTask(cropfieldItem);
_settings.SatelliteTaskCode = satelliteTaskCode;
2021-01-28 14:08:32 +00:00
SaveSettings(settingsfile);
}
// Select a particular satellite item from satelliteTask
2021-01-28 14:08:32 +00:00
Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode, FieldName, StoreStatistics);
var satelliteBand = satalliteItem.Data["layers"][0]["name"];
var satelliteStatistics = satalliteItem.Data["layers"][0]["renderer"]["band"]["statistics"];
Console.WriteLine($"Satellite image date: {satalliteItem.DataDate}");
//Console.WriteLine($"Satellite image statistics for band {satelliteBand}: {satelliteStatistics}");
//Store data to csv
if (StoreStatistics==true) {
var SatelliteFile = $"C:\\Akkerweb\\DataSatellite_{FieldName}.csv";
var NewLineField = $"\"Field\":{FieldName}" + Environment.NewLine;
var NewLineDate = $"\"date\":{satalliteItem.DataDate}" + Environment.NewLine;
var i = 0;
foreach (var item in satelliteStatistics) {
//var NewLines2;
if (i == 0) {
File.AppendAllText(SatelliteFile, NewLineDate);
i++;
}
File.AppendAllText(SatelliteFile, $"{item}" + Environment.NewLine);
}
}
// must be wdvi[1]
var inputType = (satalliteItem.Data["layers"] as JArray)?[1]["name"].ToString();
2021-01-28 14:08:32 +00:00
if (string.IsNullOrEmpty(inputType)) {
_logger.LogError("Could not get the input type name from the satellite item");
return;
}
// download the geotiff
2021-01-28 14:08:32 +00:00
var SatelliteImageDate = (DateTime)satalliteItem.DataDate;
var SatelliteDate = SatelliteImageDate.ToString("yyyyMMdd");
_logger.LogInformation("Downloading geotiff file");
await _farmmapsApiService.DownloadItemAsync(satalliteItem.Code,
2021-01-28 14:08:32 +00:00
Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}_{inputType}_{SatelliteDate}.zip"));
// overwrite measurement date by date of satellite item
measurementDate = satalliteItem.DataDate.Value;
geotiffItem = satalliteItem;
}
// (geo)tiff input:
else if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
2021-01-28 14:08:32 +00:00
_logger.LogInformation("input = tiff data");
var dataPath = Path.Combine("Data", input.File);
geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath,
2021-04-02 14:15:44 +00:00
Path.GetFileNameWithoutExtension(input.File), input.GeometryJson.ToString(Formatting.None));
2021-01-28 14:08:32 +00:00
if (geotiffItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
}
// json/shape input
else {
var isGeoJson = input.File.Contains("json");
var dataPath = Path.Combine("Data", input.File);
var shapeItem = isGeoJson ?
2021-04-02 14:15:44 +00:00
await _generalService.UploadDataAsync(uploadedRoot, SHAPE_PROCESSED_ITEMTYPE, dataPath, Path.GetFileNameWithoutExtension(input.File), input.GeometryJson.ToString(Formatting.None)) :
await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, Path.GetFileNameWithoutExtension(input.File), input.GeometryJson.ToString(Formatting.None));
if (shapeItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
_logger.LogInformation($"Converting shape to geotiff");
geotiffItem = await _generalService.ShapeToGeotiff(shapeItem);
if (geotiffItem == null) {
_logger.LogError("Something went wrong with shape to geotiff transformation");
return;
}
_logger.LogInformation("Downloading geotiff file");
await _farmmapsApiService.DownloadItemAsync(geotiffItem.Code,
Path.Combine(DownloadFolder, $"{input.OutputFileName}.input_geotiff.zip"));
}
2021-01-28 14:08:32 +00:00
//// check if vandersat task not yet done, do here and save taskcode
//if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.VanDerSatTaskCode)) {
// var VanDerSatTaskCode = await _generalService.RunVanDerSatTask(cropfieldItem);
// _settings.VanDerSatTaskCode = VanDerSatTaskCode;
// SaveSettings(settingsfile);
//}
2021-01-28 14:08:32 +00:00
//// Select a particular image item from VanDerSat
//Item VanDerSatItem = await _generalService.FindVanDerSatItem(cropfieldItem, _settings.VanDerSatTaskCode, FieldName, StoreStatistics);
2021-01-28 14:08:32 +00:00
//// download the geotiff
//_logger.LogInformation("Downloading geotiff file");
//await _farmmapsApiService.DownloadItemAsync(VanDerSatItem.Code,
// Path.Combine(DownloadFolder, $"nbs_VanDerSatGeotiff_{input.OutputFileName}.zip"));
_logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}");
var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem);
var targetNData = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate,
2020-04-10 11:12:15 +00:00
measurementDate, input.PotatoPurposeType, input.TargetYield);
2021-01-28 14:08:32 +00:00
if (targetNData == null) {
_logger.LogError("Something went wrong with TargetN calculation");
return;
}
2021-01-28 14:08:32 +00:00
_logger.LogInformation($"TargetN: {targetNData.TargetN}");
////Option to manually adjust the Target N, for test purposes only!
//targetNData.TargetN = 225;
//_logger.LogInformation($"TargetN adjusted: {targetNData.TargetN}");
var targetNDataPath = Path.Combine(DownloadFolder, $"{input.OutputFileName}.targetn.json");
await File.WriteAllTextAsync(targetNDataPath, JsonConvert.SerializeObject(targetNData, Formatting.Indented));
_logger.LogInformation("Calculating uptake map");
var uptakeMapItem =
2020-04-28 14:35:14 +00:00
await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate,
measurementDate, input.InputVariable, input.InputLayerName);
2021-01-28 14:08:32 +00:00
if (uptakeMapItem == null) {
2020-07-07 19:45:39 +00:00
_logger.LogError("Something went wrong with creating the uptakeMap");
return;
}
2021-01-28 14:08:32 +00:00
_logger.LogInformation("Downloading uptake map");
await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code,
Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip"));
_logger.LogInformation("UptakeMap downloaded to {0}", Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip"));
2020-04-28 14:35:14 +00:00
_logger.LogInformation("Calculating application map");
var applicationMapItem =
2020-04-28 14:35:14 +00:00
await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate,
measurementDate, input.InputVariable, targetNData.TargetN, input.InputLayerName);
2021-01-28 14:08:32 +00:00
if (applicationMapItem == null) {
2020-07-07 19:45:39 +00:00
_logger.LogError("Something went wrong with creating the applicationMap");
return;
}
2020-04-28 14:35:14 +00:00
_logger.LogInformation("Downloading application map");
await _farmmapsApiService.DownloadItemAsync(applicationMapItem.Code,
Path.Combine(DownloadFolder, $"{input.OutputFileName}.application.zip"));
_logger.LogInformation("Application map can be found in {0}", Path.Combine(DownloadFolder, $"{input.OutputFileName}.application.zip"));
//transforming tiff to shape
var tiffItem = applicationMapItem;
2021-01-28 14:08:32 +00:00
if (tiffItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
//_logger.LogInformation($"Converting geotiff to shape");
//var taskmap = await _generalService.GeotiffToShape(tiffItem);
//if (taskmap == null) {
// _logger.LogError("Something went wrong with geotiff to shape transformation");
// return;
//}
//ApplicationMap (GEOTIFF) To Taskmap
_logger.LogInformation($"Converting geotiff to taskmap");
var taskmap = await _generalService.CreateTaskmap(cropfieldItem, tiffItem, input.CellWidth, input.CellHeight, input.StartPoint.ToString(Formatting.None),
input.EndPoint.ToString(Formatting.None), input.Angle);
if (taskmap == null)
{
_logger.LogError("Something went wrong with geotiff to shape transformation");
return;
}
_logger.LogInformation("Downloading taskmap");
await _farmmapsApiService.DownloadItemAsync(taskmap.Code,
Path.Combine(DownloadFolder, $"{input.OutputFileName}.taskmap.zip"));
2020-04-08 18:23:22 +00:00
}
2021-01-28 14:08:32 +00:00
// Functions to save previously created cropfields
2021-01-28 14:08:32 +00:00
private void LoadSettings(string file)
{
2021-01-28 14:08:32 +00:00
if (File.Exists(file))
{
2021-01-28 14:08:32 +00:00
var jsonText = File.ReadAllText(file);
_settings = JsonConvert.DeserializeObject<Settings>(jsonText);
}
else
{
_settings = new Settings();
}
}
2021-01-28 14:08:32 +00:00
private void SaveSettings(string file)
{
if (_settings == null)
return;
var json = JsonConvert.SerializeObject(_settings);
2021-01-28 14:08:32 +00:00
File.WriteAllText(file, json);
}
private void SaveInfo(string file) {
if (_settings == null)
return;
var json = JsonConvert.SerializeObject(_settings);
File.WriteAllText(file, json);
}
2021-01-28 14:08:32 +00:00
}
}