Added option to use satellite data in NBS
This commit is contained in:
@@ -9,6 +9,7 @@ using FarmmapsApi.Services;
|
||||
using FarmmapsNbs.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using static FarmmapsApiSamples.Constants;
|
||||
|
||||
namespace FarmmapsNbs
|
||||
@@ -16,12 +17,15 @@ 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)
|
||||
{
|
||||
@@ -40,6 +44,10 @@ namespace FarmmapsNbs
|
||||
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();
|
||||
@@ -59,6 +67,8 @@ namespace FarmmapsNbs
|
||||
|
||||
private async Task Process(List<UserRoot> roots, NitrogenInput input)
|
||||
{
|
||||
// specify if you are using an already created cropfield:
|
||||
bool useCreatedCropfield = false;
|
||||
var plantingDate = input.PlantingDate;
|
||||
var measurementDate = input.MeasurementDate;
|
||||
|
||||
@@ -75,24 +85,79 @@ namespace FarmmapsNbs
|
||||
_logger.LogError("Could not find a needed root item");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
|
||||
$"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None));
|
||||
// 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();
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Cropfield already exists, trying to get it");
|
||||
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
|
||||
}
|
||||
|
||||
|
||||
|
||||
var geotiffItem = (Item)null;
|
||||
|
||||
if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
|
||||
_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));
|
||||
// No file input, use most recent satellite image
|
||||
if (string.IsNullOrEmpty(input.File))
|
||||
{
|
||||
_logger.LogInformation("No specific data given, retrieving most recent satellite image");
|
||||
|
||||
if (geotiffItem == null) {
|
||||
_logger.LogError("Could not find item for uploaded data");
|
||||
return;
|
||||
}
|
||||
// check if satellite task not yet done, do here and save taskcode
|
||||
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode))
|
||||
{
|
||||
var satelliteTaskCode = await _generalService.RunSatelliteTask(cropfieldItem);
|
||||
_settings.SatelliteTaskCode = satelliteTaskCode;
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Find most recent satellite item
|
||||
Item recentSatalliteItem = await _generalService.FindSatelliteItems(cropfieldItem, _settings.SatelliteTaskCode);
|
||||
|
||||
|
||||
// must be ndvi[0] or wdvi[1]
|
||||
var inputType = (recentSatalliteItem.Data["layers"] as JArray)?[1]["name"].ToString();
|
||||
if (string.IsNullOrEmpty(inputType))
|
||||
{
|
||||
_logger.LogError("Could not get the input type name from the satellite item");
|
||||
return;
|
||||
}
|
||||
|
||||
// download the geotiff
|
||||
_logger.LogInformation("Downloading geotiff file");
|
||||
await _farmmapsApiService.DownloadItemAsync(recentSatalliteItem.Code,
|
||||
Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}.zip"));
|
||||
|
||||
geotiffItem = recentSatalliteItem;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// (geo)tiff input:
|
||||
else if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
|
||||
_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));
|
||||
|
||||
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);
|
||||
@@ -117,6 +182,12 @@ namespace FarmmapsNbs
|
||||
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,
|
||||
@@ -139,7 +210,7 @@ namespace FarmmapsNbs
|
||||
_logger.LogInformation("Calculating uptake map");
|
||||
var uptakeMapItem =
|
||||
await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate,
|
||||
measurementDate, input.InputVariable);
|
||||
measurementDate, input.InputVariable, input.InputLayerName);
|
||||
if (uptakeMapItem == null)
|
||||
{
|
||||
_logger.LogError("Something went wrong with creating the uptakeMap");
|
||||
@@ -189,5 +260,28 @@ namespace FarmmapsNbs
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Functions to save previously created cropfields
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user