2021-04-12 12:22:24 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using FarmmapsApi;
|
|
|
|
using FarmmapsApi.Models;
|
|
|
|
using FarmmapsApi.Services;
|
|
|
|
using FarmmapsNbs.Models;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using static FarmmapsApiSamples.Constants;
|
|
|
|
|
|
|
|
namespace FarmmapsNbs
|
|
|
|
{
|
|
|
|
public class NbsApplication : IApplication
|
|
|
|
{
|
|
|
|
private const string DownloadFolder = "Downloads";
|
|
|
|
private const string SettingsFile = "settings.json";
|
|
|
|
|
|
|
|
private readonly ILogger<NbsApplication> _logger;
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
private readonly NitrogenService _nitrogenService;
|
|
|
|
private readonly GeneralService _generalService;
|
|
|
|
|
|
|
|
private Settings _settings;
|
|
|
|
|
|
|
|
public NbsApplication(ILogger<NbsApplication> logger, FarmmapsApiService farmmapsApiService,
|
|
|
|
GeneralService generalService, NitrogenService nitrogenService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_farmmapsApiService = farmmapsApiService;
|
|
|
|
_generalService = generalService;
|
|
|
|
_nitrogenService = nitrogenService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task RunAsync()
|
|
|
|
{
|
2021-04-12 15:01:17 +00:00
|
|
|
var nitrogenInputJson = File.ReadAllText("NitrogenInput.json");
|
2021-04-12 12:22:24 +00:00
|
|
|
List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson);
|
2021-04-12 15:01:17 +00:00
|
|
|
|
2021-04-12 12:22:24 +00:00
|
|
|
if (!Directory.Exists(DownloadFolder))
|
|
|
|
Directory.CreateDirectory(DownloadFolder);
|
|
|
|
|
|
|
|
// !! 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();
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-04-12 15:01:17 +00:00
|
|
|
|
2021-04-12 12:22:24 +00:00
|
|
|
// !!specify if you are using an already created cropfield:
|
2021-04-12 15:01:17 +00:00
|
|
|
bool useCreatedCropfield = input.UseCreatedCropfield;
|
2021-04-12 12:22:24 +00:00
|
|
|
var plantingDate = input.PlantingDate;
|
|
|
|
var FieldName = input.fieldName;
|
|
|
|
bool StoreStatistics = input.storeSatelliteStatistics;
|
|
|
|
var measurementDate = input.MeasurementDate;
|
|
|
|
string settingsfile = $"Settings_{FieldName}.json";
|
|
|
|
|
|
|
|
LoadSettings(settingsfile);
|
|
|
|
|
|
|
|
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
|
|
|
|
if (uploadedRoot == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
|
|
|
|
if (myDriveRoot == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
return;
|
|
|
|
}
|
2021-04-12 15:01:17 +00:00
|
|
|
|
2021-04-12 12:22:24 +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;
|
|
|
|
SaveSettings(settingsfile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Cropfield already exists, trying to get it");
|
|
|
|
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
var geotiffItem = (Item)null;
|
|
|
|
|
2021-04-12 15:01:17 +00:00
|
|
|
// If no input file is specified, use most recent satellite image
|
|
|
|
if (string.IsNullOrEmpty(input.File))
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_logger.LogInformation("No specific data given, retrieving most recent satellite image");
|
|
|
|
|
|
|
|
// check if satellite task not yet done, do here and save taskcode
|
2021-04-12 15:01:17 +00:00
|
|
|
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode))
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
var satelliteTaskCode = await _generalService.RunSatelliteTask(cropfieldItem);
|
|
|
|
_settings.SatelliteTaskCode = satelliteTaskCode;
|
|
|
|
SaveSettings(settingsfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Select a particular satellite item from satelliteTask
|
2021-04-13 08:54:04 +00:00
|
|
|
Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode);
|
2021-04-12 12:22:24 +00:00
|
|
|
|
|
|
|
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
|
2021-04-12 15:01:17 +00:00
|
|
|
if (StoreStatistics == true)
|
|
|
|
{
|
2021-04-13 08:54:04 +00:00
|
|
|
|
2021-04-21 07:03:25 +00:00
|
|
|
var SatelliteStatsFile = $"{DownloadFolder}/SatelliteDataStatistics_{FieldName}_{satalliteItem.DataDate.Value:yyyy-MM-dd}.csv";
|
2021-04-13 08:54:04 +00:00
|
|
|
using var w = new StreamWriter(SatelliteStatsFile);
|
2021-04-12 15:01:17 +00:00
|
|
|
{
|
2021-04-13 08:54:04 +00:00
|
|
|
foreach (var item in satelliteStatistics)
|
2021-04-12 15:01:17 +00:00
|
|
|
{
|
2021-04-13 08:54:04 +00:00
|
|
|
var line = string.Format("{0}", item);
|
|
|
|
w.WriteLine(line);
|
|
|
|
w.Flush();
|
2021-04-12 12:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 08:54:04 +00:00
|
|
|
|
2021-04-12 12:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// must be wdvi[1]
|
|
|
|
var inputType = (satalliteItem.Data["layers"] as JArray)?[1]["name"].ToString();
|
2021-04-12 15:01:17 +00:00
|
|
|
if (string.IsNullOrEmpty(inputType))
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_logger.LogError("Could not get the input type name from the satellite item");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// download the geotiff
|
|
|
|
var SatelliteImageDate = (DateTime)satalliteItem.DataDate;
|
|
|
|
var SatelliteDate = SatelliteImageDate.ToString("yyyyMMdd");
|
|
|
|
_logger.LogInformation("Downloading geotiff file");
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(satalliteItem.Code,
|
|
|
|
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:
|
2021-04-12 15:01:17 +00:00
|
|
|
else if (input.File.Contains(".tif") || input.File.Contains(".geotiff"))
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_logger.LogInformation("input = tiff data");
|
|
|
|
var dataPath = Path.Combine("Data", input.File);
|
|
|
|
geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath,
|
|
|
|
Path.GetFileNameWithoutExtension(input.File));
|
|
|
|
|
2021-04-12 15:01:17 +00:00
|
|
|
if (geotiffItem == null)
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_logger.LogError("Could not find item for uploaded data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// json/shape input
|
2021-04-12 15:01:17 +00:00
|
|
|
else
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
var isGeoJson = input.File.Contains("json");
|
|
|
|
var dataPath = Path.Combine("Data", input.File);
|
|
|
|
var shapeItem = isGeoJson ?
|
2021-04-12 15:01:17 +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));
|
2021-04-12 12:22:24 +00:00
|
|
|
|
2021-04-12 15:01:17 +00:00
|
|
|
if (shapeItem == null)
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_logger.LogError("Could not find item for uploaded data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation($"Converting shape to geotiff");
|
|
|
|
geotiffItem = await _generalService.ShapeToGeotiff(shapeItem);
|
2021-04-12 15:01:17 +00:00
|
|
|
if (geotiffItem == null)
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}");
|
|
|
|
var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem);
|
|
|
|
var targetNData = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate,
|
|
|
|
measurementDate, input.PotatoPurposeType, input.TargetYield);
|
|
|
|
|
2021-04-12 15:01:17 +00:00
|
|
|
if (targetNData == null)
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_logger.LogError("Something went wrong with TargetN calculation");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_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");
|
2021-04-12 15:01:17 +00:00
|
|
|
|
2021-04-12 12:22:24 +00:00
|
|
|
await File.WriteAllTextAsync(targetNDataPath, JsonConvert.SerializeObject(targetNData, Formatting.Indented));
|
|
|
|
|
|
|
|
_logger.LogInformation("Calculating uptake map");
|
|
|
|
var uptakeMapItem =
|
|
|
|
await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate,
|
|
|
|
measurementDate, input.InputVariable, input.InputLayerName);
|
2021-04-12 15:01:17 +00:00
|
|
|
if (uptakeMapItem == null)
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_logger.LogError("Something went wrong with creating the uptakeMap");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading uptake map");
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code,
|
|
|
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip"));
|
2021-04-12 15:01:17 +00:00
|
|
|
_logger.LogInformation("UptakeMap downloaded");
|
2021-04-12 12:22:24 +00:00
|
|
|
|
|
|
|
_logger.LogInformation("Calculating application map");
|
2021-04-12 15:01:17 +00:00
|
|
|
var applianceMapItem =
|
2021-04-12 12:22:24 +00:00
|
|
|
await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate,
|
|
|
|
measurementDate, input.InputVariable, targetNData.TargetN, input.InputLayerName);
|
|
|
|
|
2021-04-12 15:01:17 +00:00
|
|
|
if (applianceMapItem == null)
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_logger.LogError("Something went wrong with creating the applicationMap");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_logger.LogInformation("Downloading application map");
|
2021-04-12 15:01:17 +00:00
|
|
|
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code,
|
2021-04-12 12:22:24 +00:00
|
|
|
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
|
2021-04-12 15:01:17 +00:00
|
|
|
var tiffItem = applianceMapItem;
|
2021-04-12 12:22:24 +00:00
|
|
|
|
2021-04-12 15:01:17 +00:00
|
|
|
if (tiffItem == null)
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
_logger.LogError("Could not find item for uploaded data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//ApplicationMap (GEOTIFF) To Taskmap
|
2021-04-12 15:01:17 +00:00
|
|
|
if (input.GenerateTaskmap)
|
2021-04-12 12:22:24 +00:00
|
|
|
{
|
2021-04-12 15:01:17 +00:00
|
|
|
//GEOTIFF TO Taskmap
|
|
|
|
_logger.LogInformation($"Converting geotiff to taskmap");
|
|
|
|
|
|
|
|
var taskmap = (Item)null;
|
|
|
|
if (input.OutputType == "isoxml")
|
|
|
|
{
|
|
|
|
|
|
|
|
if (input.DdiCode == null)
|
|
|
|
{
|
|
|
|
_logger.LogInformation("DDi not given. Using expected identifiers");
|
|
|
|
input.DdiCode = input.DdiCode = "0006";
|
|
|
|
|
|
|
|
}
|
|
|
|
taskmap = await _generalService.CreateTaskmap(cropfieldItem: cropfieldItem, tiffItem: applianceMapItem, outputType: input.OutputType, cellWidth: input.CellWidth,
|
|
|
|
cellHeight: input.CellHeight, startPoint: input.StartPoint.ToString(Formatting.None), ddiCode: input.DdiCode, centered: input.Centered,
|
|
|
|
endPoint: input.EndPoint.ToString(Formatting.None), angle: input.Angle, precision: input.Precision,
|
|
|
|
cropTypeName: null, costumerName: null, ProductGroupName: null, productName: null, resolution: null, unitScale: null, maximumClasses: input.MaximumClasses);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
taskmap = await _generalService.CreateTaskmap(cropfieldItem: cropfieldItem, tiffItem: applianceMapItem, outputType: input.OutputType, cellWidth: input.CellWidth,
|
|
|
|
cellHeight: input.CellHeight, startPoint: input.StartPoint.ToString(Formatting.None), centered: input.Centered,
|
|
|
|
endPoint: input.EndPoint.ToString(Formatting.None), angle: input.Angle, precision: input.Precision, maximumClasses: input.MaximumClasses);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (taskmap == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Something went wrong with geotiff to taskmap transformation");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading taskmap");
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(taskmap.Code,
|
|
|
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.taskmap.zip"));
|
2021-04-12 12:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions to save previously created cropfields
|
|
|
|
private void LoadSettings(string file)
|
|
|
|
{
|
|
|
|
if (File.Exists(file))
|
|
|
|
{
|
|
|
|
var jsonText = File.ReadAllText(file);
|
|
|
|
_settings = JsonConvert.DeserializeObject<Settings>(jsonText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_settings = new Settings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SaveSettings(string file)
|
|
|
|
{
|
|
|
|
if (_settings == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var json = JsonConvert.SerializeObject(_settings);
|
|
|
|
File.WriteAllText(file, json);
|
|
|
|
}
|
2021-04-12 15:01:17 +00:00
|
|
|
private void SaveInfo(string file)
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
if (_settings == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var json = JsonConvert.SerializeObject(_settings);
|
|
|
|
File.WriteAllText(file, json);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-12 15:01:17 +00:00
|
|
|
}
|
|
|
|
}
|