Eerdere aanpassingen NBS en zonering verwerkt

This commit is contained in:
Riepma 2020-12-11 14:02:01 +01:00
parent 2196036135
commit 02708ba01d
7 changed files with 429 additions and 215 deletions

View File

@ -147,6 +147,8 @@ namespace FarmmapsApi.Services
{ {
_logger.LogInformation($"Trying to get {containsName} data"); _logger.LogInformation($"Trying to get {containsName} data");
var uploadedFilesChildren = await _farmmapsApiService.GetItemChildrenAsync(parentCode, itemType); var uploadedFilesChildren = await _farmmapsApiService.GetItemChildrenAsync(parentCode, itemType);
Func<Item, bool> func = filter ?? (i => i.Name.ToLower().Contains(containsName.ToLower())); Func<Item, bool> func = filter ?? (i => i.Name.ToLower().Contains(containsName.ToLower()));
dataItem = uploadedFilesChildren.FirstOrDefault(func); dataItem = uploadedFilesChildren.FirstOrDefault(func);
if (dataItem != null || tries == maxTries) if (dataItem != null || tries == maxTries)
@ -268,5 +270,88 @@ namespace FarmmapsApi.Services
return shadowItem; return shadowItem;
} }
public async Task<string> RunSatelliteTask(Item cropfieldItem)
{
_logger.LogInformation("Gathering satellite information for cropfield, this might take a while!");
var taskmapRequest = new TaskRequest { TaskType = SATELLITE_TASK };
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
_logger.LogInformation($"Waiting on satellite data; status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished)
tokenSource.Cancel();
});
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTask.State == ItemTaskState.Error)
{
_logger.LogError($"Something went wrong when trying to process satellite data; {itemTask.Message}");
}
return itemTask.Code;
}
public async Task<Item> FindSatelliteItem(Item cropfieldItem, string satelliteTaskCode)
{
var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, satelliteTaskCode);
// find ndvi or wdvi satellite data geotiffs
var temporalItem = await FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE,
"Cropfield Satellite items", item => item.SourceTask == SATELLITE_TASK &&
taskStatus.Finished >= item.Created &&
taskStatus.Finished <= item.Created.Value.AddHours(1));
if (temporalItem == null)
{
_logger.LogError("Temporal item not found");
}
var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalItem.Code);
_logger.LogInformation("Available satellite images:");
var count = 0;
TimeSpan.FromSeconds(0.5);
foreach (var item in satelliteTiffs)
{
Console.WriteLine($"Satellite image #{count}: {item.DataDate}");
count++;
}
_logger.LogInformation("Enter satellite image number for NBS application");
int elment = Int32.Parse(Console.ReadLine());
var selectedSatelliteItem = satelliteTiffs[elment];
if (selectedSatelliteItem == null)
{
_logger.LogError("Satellite item not found");
}
return selectedSatelliteItem;
}
} }
} }

View File

@ -13,5 +13,8 @@ namespace FarmmapsNbs.Models
public string PotatoPurposeType { get; set; } public string PotatoPurposeType { get; set; }
public int TargetYield { get; set; } public int TargetYield { get; set; }
public JObject GeometryJson { get; set; } public JObject GeometryJson { get; set; }
public string InputLayerName { get; set; }
} }
} }

View File

@ -9,6 +9,7 @@ using FarmmapsApi.Services;
using FarmmapsNbs.Models; using FarmmapsNbs.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static FarmmapsApiSamples.Constants; using static FarmmapsApiSamples.Constants;
namespace FarmmapsNbs namespace FarmmapsNbs
@ -16,12 +17,15 @@ namespace FarmmapsNbs
public class NbsApplication : IApplication public class NbsApplication : IApplication
{ {
private const string DownloadFolder = "Downloads"; private const string DownloadFolder = "Downloads";
private const string SettingsFile = "settings.json";
private readonly ILogger<NbsApplication> _logger; private readonly ILogger<NbsApplication> _logger;
private readonly FarmmapsApiService _farmmapsApiService; private readonly FarmmapsApiService _farmmapsApiService;
private readonly NitrogenService _nitrogenService; private readonly NitrogenService _nitrogenService;
private readonly GeneralService _generalService; private readonly GeneralService _generalService;
private Settings _settings;
public NbsApplication(ILogger<NbsApplication> logger, FarmmapsApiService farmmapsApiService, public NbsApplication(ILogger<NbsApplication> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService, NitrogenService nitrogenService) GeneralService generalService, NitrogenService nitrogenService)
{ {
@ -40,6 +44,10 @@ namespace FarmmapsNbs
if (!Directory.Exists(DownloadFolder)) if (!Directory.Exists(DownloadFolder))
Directory.CreateDirectory(DownloadFolder); Directory.CreateDirectory(DownloadFolder);
LoadSettings();
// !! this call is needed the first time an api is called with a fresh clientid and secret !! // !! this call is needed the first time an api is called with a fresh clientid and secret !!
await _farmmapsApiService.GetCurrentUserCodeAsync(); await _farmmapsApiService.GetCurrentUserCodeAsync();
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync(); var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
@ -59,6 +67,8 @@ namespace FarmmapsNbs
private async Task Process(List<UserRoot> roots, NitrogenInput input) 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 plantingDate = input.PlantingDate;
var measurementDate = input.MeasurementDate; var measurementDate = input.MeasurementDate;
@ -75,24 +85,83 @@ namespace FarmmapsNbs
_logger.LogError("Could not find a needed root item"); _logger.LogError("Could not find a needed root item");
return; return;
} }
var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code, // Use already created cropfield or create new one
$"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None)); 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; var geotiffItem = (Item)null;
if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) { // No file input, use most recent satellite image
_logger.LogInformation("input = tiff data"); if (string.IsNullOrEmpty(input.File))
var dataPath = Path.Combine("Data", input.File); {
geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath, _logger.LogInformation("No specific data given, retrieving most recent satellite image");
Path.GetFileNameWithoutExtension(input.File));
if (geotiffItem == null) { // check if satellite task not yet done, do here and save taskcode
_logger.LogError("Could not find item for uploaded data"); if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode))
return; {
} var satelliteTaskCode = await _generalService.RunSatelliteTask(cropfieldItem);
_settings.SatelliteTaskCode = satelliteTaskCode;
SaveSettings();
} }
// Select a particular satellite item from satelliteTask
Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode);
// must be wdvi[1]
var inputType = (satalliteItem.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(satalliteItem.Code,
Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}.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")) {
_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 { else {
var isGeoJson = input.File.Contains("json"); var isGeoJson = input.File.Contains("json");
var dataPath = Path.Combine("Data", input.File); var dataPath = Path.Combine("Data", input.File);
@ -117,6 +186,12 @@ namespace FarmmapsNbs
Path.Combine(DownloadFolder, $"{input.OutputFileName}.input_geotiff.zip")); Path.Combine(DownloadFolder, $"{input.OutputFileName}.input_geotiff.zip"));
} }
_logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}"); _logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}");
var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem); var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem);
var targetNData = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate, var targetNData = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate,
@ -139,7 +214,7 @@ namespace FarmmapsNbs
_logger.LogInformation("Calculating uptake map"); _logger.LogInformation("Calculating uptake map");
var uptakeMapItem = var uptakeMapItem =
await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate, await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate,
measurementDate, input.InputVariable); measurementDate, input.InputVariable, input.InputLayerName);
if (uptakeMapItem == null) if (uptakeMapItem == null)
{ {
_logger.LogError("Something went wrong with creating the uptakeMap"); _logger.LogError("Something went wrong with creating the uptakeMap");
@ -154,8 +229,7 @@ namespace FarmmapsNbs
_logger.LogInformation("Calculating application map"); _logger.LogInformation("Calculating application map");
var applicationMapItem = var applicationMapItem =
await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate, await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate,
measurementDate, measurementDate, input.InputVariable, targetNData.TargetN, input.InputLayerName);
input.InputVariable, targetNData.TargetN);
if (applicationMapItem == null) if (applicationMapItem == null)
{ {
@ -189,5 +263,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);
}
} }
} }

View File

@ -1,126 +1,195 @@
[ [
{ {
"file": "Scan_1_20190605.json", "file": "", // keep emptpy to use satellite image
"inputVariable": "irmi", "inputVariable": "wdvi",
"outputFileName": "vranbs1", "InputLayerName": "wdvi",
"plantingDate": "2019-04-18", "outputFileName": "rtest1",
"measurementDate": "2019-06-05", "plantingDate": "2020-05-01",
"potatoPurposeType": "consumption", "measurementDate": "2020-06-14",
"targetYield": 45, "potatoPurposeType": "consumption",
"geometryJson": { "type": "Polygon", "coordinates": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] } "targetYield": 60,
}, "geometryJson": {
{ "type": "Polygon",
"file": "Scan_1_20190605.zip", "coordinates": [
"inputVariable": "irmi", [
"outputFileName": "vranbs2", [ 4.960707146896585, 52.800583669708487 ],
"plantingDate": "2019-04-18", [ 4.960645975538824, 52.800470217610922 ],
"measurementDate": "2019-06-05", [ 4.962140695752897, 52.799177147194797 ],
"potatoPurposeType": "starch", [ 4.967523821195745, 52.801502400041208 ],
"targetYield": 45, [ 4.966336768950911, 52.802543735879809 ],
"geometryJson": { [ 4.961711880764330, 52.801009996856429 ],
"type": "Polygon", [ 4.960707146896585, 52.800583669708487 ]
"coordinates": [ ]
[ ]
[ 3.40843828875524, 50.638966444680605 ], }
[ 3.408953272886064, 50.639197789621612 ],
[ 3.409242951459603, 50.639469958681836 ],
[ 3.409328782148028, 50.639612846807708 ],
[ 3.409457528180712, 50.639789755314411 ],
[ 3.409639918393741, 50.640014292074966 ],
[ 3.409833037442765, 50.640211611372706 ],
[ 3.410069071836049, 50.640395321698435 ],
[ 3.410380208081761, 50.640572227259661 ],
[ 3.410605513638958, 50.640715112034222 ],
[ 3.411925160474145, 50.641177783561204 ],
[ 3.411935889310142, 50.640728720085136 ],
[ 3.412590348309737, 50.63948356709389 ],
[ 3.413244807309242, 50.638224772339846 ],
[ 3.413400375432099, 50.637901562841307 ],
[ 3.413539850300779, 50.637449065809889 ],
[ 3.413475477284437, 50.637418445552932 ],
[ 3.40999396998362, 50.637449065810451 ],
[ 3.409940325803365, 50.638102293212661 ],
[ 3.409575545377398, 50.638483338338325 ],
[ 3.409060561246574, 50.638707881340494 ],
[ 3.40843828875524, 50.638966444680605 ]
]
]
} }
}, //,
{ //{
"file": "Scan_1_20190605.zip", // "file": "", // keep emptpy to use satellite image
"inputVariable": "irmi", // "inputVariable": "wdvi",
"outputFileName": "vranbs3", // "InputLayerName": "wdvi",
"plantingDate": "2019-04-18", // "outputFileName": "mtest1",
"measurementDate": "2019-06-20", // "plantingDate": "2020-04-01",
"potatoPurposeType": "starch", // "measurementDate": "2020-06-24",
"targetYield": 45, // "potatoPurposeType": "consumption",
"geometryJson": { // "targetYield": 60,
"type": "Polygon", // "geometryJson": {
"coordinates": [ // "type": "Polygon",
[ // "coordinates": [
[ 3.40843828875524, 50.638966444680605 ], // [
[ 3.408953272886064, 50.639197789621612 ], // [ 3.575458869234128, 51.308707885669762 ],
[ 3.409242951459603, 50.639469958681836 ], // [ 3.575508957999423, 51.30878478562019 ],
[ 3.409328782148028, 50.639612846807708 ], // [ 3.576188404403633, 51.309372997559777 ],
[ 3.409457528180712, 50.639789755314411 ], // [ 3.576188872410267, 51.309374219701091 ],
[ 3.409639918393741, 50.640014292074966 ], // [ 3.576210290749152, 51.309430091473608 ],
[ 3.409833037442765, 50.640211611372706 ], // [ 3.57621266537704, 51.309483685674898 ],
[ 3.410069071836049, 50.640395321698435 ], // [ 3.576213477455834, 51.309502027981374 ],
[ 3.410380208081761, 50.640572227259661 ], // [ 3.577543136860447, 51.310682367527122 ],
[ 3.410605513638958, 50.640715112034222 ], // [ 3.57796629328866, 51.31104321405175 ],
[ 3.411925160474145, 50.641177783561204 ], // [ 3.578442479292087, 51.311449273042747 ],
[ 3.411935889310142, 50.640728720085136 ], // [ 3.57866702353106, 51.311636072786726 ],
[ 3.412590348309737, 50.63948356709389 ], // [ 3.57880446997978, 51.31157117599529 ],
[ 3.413244807309242, 50.638224772339846 ], // [ 3.579155910205885, 51.311863542729718 ],
[ 3.413400375432099, 50.637901562841307 ], // [ 3.579175814007489, 51.311875435159394 ],
[ 3.413539850300779, 50.637449065809889 ], // [ 3.579293885246395, 51.311936532835396 ],
[ 3.413475477284437, 50.637418445552932 ], // [ 3.579413896180069, 51.311998649478575 ],
[ 3.40999396998362, 50.637449065810451 ], // [ 3.579514543462617, 51.312041110734917 ],
[ 3.409940325803365, 50.638102293212661 ], // [ 3.579611760655688, 51.312082118352606 ],
[ 3.409575545377398, 50.638483338338325 ], // [ 3.579635115371588, 51.312093949652223 ],
[ 3.409060561246574, 50.638707881340494 ], // [ 3.579793143414486, 51.312189437140432 ],
[ 3.40843828875524, 50.638966444680605 ] // [ 3.579966991648108, 51.312286148850511 ],
] // [ 3.580079704980967, 51.312332458349751 ],
] // [ 3.580203717638148, 51.312336471368539 ],
} // [ 3.580307101018293, 51.312330239539847 ],
}, // [ 3.580383836270609, 51.312317097185243 ],
{ // [ 3.580505207977176, 51.312279163554869 ],
"file": "Scan_1_20190605.zip", // [ 3.580610387713855, 51.312233723091026 ],
"inputVariable": "irmi", // [ 3.5806309754483, 51.312226093729677 ],
"outputFileName": "vranbs4", // [ 3.580638516049738, 51.312223727082049 ],
"plantingDate": "2019-04-18", // [ 3.58075536599681, 51.312186990706344 ],
"measurementDate": "2019-07-03", // [ 3.580787745633303, 51.312176820129551 ],
"potatoPurposeType": "starch", // [ 3.580829682241423, 51.312167665428959 ],
"targetYield": 45, // [ 3.58086828456562, 51.312162614898625 ],
"geometryJson": { // [ 3.580980493636721, 51.312147935609723 ],
"type": "Polygon", // [ 3.581014352632766, 51.312145662592656 ],
"coordinates": [ // [ 3.581028980583245, 51.312145592849248 ],
[ // [ 3.581119189823368, 51.312145103215911 ],
[ 3.40843828875524, 50.638966444680605 ], // [ 3.581195330198145, 51.312144693075908 ],
[ 3.408953272886064, 50.639197789621612 ], // [ 3.581243537809229, 51.312148741603245 ],
[ 3.409242951459603, 50.639469958681836 ], // [ 3.58132480221972, 51.312163110548383 ],
[ 3.409328782148028, 50.639612846807708 ], // [ 3.581426517039001, 51.312181089466016 ],
[ 3.409457528180712, 50.639789755314411 ], // [ 3.581448095953263, 51.312184910295024 ],
[ 3.409639918393741, 50.640014292074966 ], // [ 3.581474337475052, 51.312191038736145 ],
[ 3.409833037442765, 50.640211611372706 ], // [ 3.581709405982819, 51.312260068944951 ],
[ 3.410069071836049, 50.640395321698435 ], // [ 3.581727319558337, 51.312266163697757 ],
[ 3.410380208081761, 50.640572227259661 ], // [ 3.581753583356718, 51.312276407881612 ],
[ 3.410605513638958, 50.640715112034222 ], // [ 3.581841655772683, 51.312310743468075 ],
[ 3.411925160474145, 50.641177783561204 ], // [ 3.581878851795624, 51.312325234405343 ],
[ 3.411935889310142, 50.640728720085136 ], // [ 3.581905889860924, 51.312338260798654 ],
[ 3.412590348309737, 50.63948356709389 ], // [ 3.581906254501594, 51.312338472113815 ],
[ 3.413244807309242, 50.638224772339846 ], // [ 3.582048236499295, 51.312422439022804 ],
[ 3.413400375432099, 50.637901562841307 ], // [ 3.58189849928915, 51.312481944577037 ],
[ 3.413539850300779, 50.637449065809889 ], // [ 3.583044298383354, 51.312095780444281 ],
[ 3.413475477284437, 50.637418445552932 ], // [ 3.582984006671231, 51.312017283925279 ],
[ 3.40999396998362, 50.637449065810451 ], // [ 3.582743535862999, 51.311699343434064 ],
[ 3.409940325803365, 50.638102293212661 ], // [ 3.582628599916243, 51.311582190756774 ],
[ 3.409575545377398, 50.638483338338325 ], // [ 3.581446834435509, 51.310511259982569 ],
[ 3.409060561246574, 50.638707881340494 ], // [ 3.580621864908701, 51.309767270412806 ],
[ 3.40843828875524, 50.638966444680605 ] // [ 3.579610575760466, 51.308860440593946 ],
] // [ 3.579112608916012, 51.308394999612226 ],
] // [ 3.578688808506157, 51.307968441218165 ],
} // [ 3.578394256007207, 51.307644098092617 ],
} // [ 3.578355980318371, 51.307607614964702 ],
// [ 3.578217977585775, 51.307654179547846 ],
// [ 3.577480636332469, 51.307921035607997 ],
// [ 3.575695560441006, 51.308577167973212 ],
// [ 3.575668643609169, 51.30855384769157 ],
// [ 3.575666204524265, 51.308551734020703 ],
// [ 3.575506397192348, 51.308609906947261 ],
// [ 3.575459139533024, 51.308653178431456 ],
// [ 3.575458869234128, 51.308707885669762 ]
// ]
// ]
// }
//}
//,
//{
// "file": "Scan_1_20190605.zip",
// "inputVariable": "irmi",
// "InputLayerName": "IRMI",
// "outputFileName": "vranbs3",
// "plantingDate": "2019-04-18",
// "measurementDate": "2019-06-20",
// "potatoPurposeType": "starch",
// "targetYield": 45,
// "geometryJson": {
// "type": "Polygon",
// "coordinates": [
// [
// [ 3.40843828875524, 50.638966444680605 ],
// [ 3.408953272886064, 50.639197789621612 ],
// [ 3.409242951459603, 50.639469958681836 ],
// [ 3.409328782148028, 50.639612846807708 ],
// [ 3.409457528180712, 50.639789755314411 ],
// [ 3.409639918393741, 50.640014292074966 ],
// [ 3.409833037442765, 50.640211611372706 ],
// [ 3.410069071836049, 50.640395321698435 ],
// [ 3.410380208081761, 50.640572227259661 ],
// [ 3.410605513638958, 50.640715112034222 ],
// [ 3.411925160474145, 50.641177783561204 ],
// [ 3.411935889310142, 50.640728720085136 ],
// [ 3.412590348309737, 50.63948356709389 ],
// [ 3.413244807309242, 50.638224772339846 ],
// [ 3.413400375432099, 50.637901562841307 ],
// [ 3.413539850300779, 50.637449065809889 ],
// [ 3.413475477284437, 50.637418445552932 ],
// [ 3.40999396998362, 50.637449065810451 ],
// [ 3.409940325803365, 50.638102293212661 ],
// [ 3.409575545377398, 50.638483338338325 ],
// [ 3.409060561246574, 50.638707881340494 ],
// [ 3.40843828875524, 50.638966444680605 ]
// ]
// ]
// }
//}
//,
//{
// "file": "Scan_1_20190605.zip",
// "inputVariable": "irmi",
// "InputLayerName": "",
// "outputFileName": "vranbs4",
// "plantingDate": "2019-04-18",
// "measurementDate": "2019-07-03",
// "potatoPurposeType": "starch",
// "targetYield": 45,
// "geometryJson": {
// "type": "Polygon",
// "coordinates": [
// [
// [ 3.40843828875524, 50.638966444680605 ],
// [ 3.408953272886064, 50.639197789621612 ],
// [ 3.409242951459603, 50.639469958681836 ],
// [ 3.409328782148028, 50.639612846807708 ],
// [ 3.409457528180712, 50.639789755314411 ],
// [ 3.409639918393741, 50.640014292074966 ],
// [ 3.409833037442765, 50.640211611372706 ],
// [ 3.410069071836049, 50.640395321698435 ],
// [ 3.410380208081761, 50.640572227259661 ],
// [ 3.410605513638958, 50.640715112034222 ],
// [ 3.411925160474145, 50.641177783561204 ],
// [ 3.411935889310142, 50.640728720085136 ],
// [ 3.412590348309737, 50.63948356709389 ],
// [ 3.413244807309242, 50.638224772339846 ],
// [ 3.413400375432099, 50.637901562841307 ],
// [ 3.413539850300779, 50.637449065809889 ],
// [ 3.413475477284437, 50.637418445552932 ],
// [ 3.40999396998362, 50.637449065810451 ],
// [ 3.409940325803365, 50.638102293212661 ],
// [ 3.409575545377398, 50.638483338338325 ],
// [ 3.409060561246574, 50.638707881340494 ],
// [ 3.40843828875524, 50.638966444680605 ]
// ]
// ]
// }
//}
] ]

View File

@ -74,7 +74,7 @@ namespace FarmmapsNbs
var item = await _farmmapsApiService.GetItemAsync(targetNItem.Code); var item = await _farmmapsApiService.GetItemAsync(targetNItem.Code);
return item.Data.ToObject<TargetNData>(); return item.Data.ToObject<TargetNData>();
} }
/// <summary> /// <summary>
/// Calculates the uptake map based on the given inputs /// Calculates the uptake map based on the given inputs
/// </summary> /// </summary>
@ -82,9 +82,11 @@ namespace FarmmapsNbs
/// <param name="inputItem"></param> /// <param name="inputItem"></param>
/// <param name="plantingDate">The date the crop is planted</param> /// <param name="plantingDate">The date the crop is planted</param>
/// <param name="measurementDate">The date the measurements are taken</param> /// <param name="measurementDate">The date the measurements are taken</param>
/// <param name="inputType">Data type, could be yara, ci, irmi or wdvi</param>
/// <param name="inputLayerName">Column name in which the sensor value is stored</param>
/// <returns></returns> /// <returns></returns>
public async Task<Item> CalculateUptakeMap(Item cropfieldItem, Item inputItem, DateTime plantingDate, public async Task<Item> CalculateUptakeMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,
DateTime measurementDate, string inputType) DateTime measurementDate, string inputType, string inputLayerName)
{ {
var nbsUptakeMapRequest = new TaskRequest {TaskType = VRANBS_TASK}; var nbsUptakeMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsUptakeMapRequest.attributes["operation"] = "uptake"; nbsUptakeMapRequest.attributes["operation"] = "uptake";
@ -92,10 +94,9 @@ namespace FarmmapsNbs
nbsUptakeMapRequest.attributes["plantingDate"] = plantingDate.ToString("o"); nbsUptakeMapRequest.attributes["plantingDate"] = plantingDate.ToString("o");
nbsUptakeMapRequest.attributes["measurementDate"] = measurementDate.ToString("o"); nbsUptakeMapRequest.attributes["measurementDate"] = measurementDate.ToString("o");
nbsUptakeMapRequest.attributes["inputType"] = inputType.ToLower(); nbsUptakeMapRequest.attributes["inputType"] = inputType.ToLower();
nbsUptakeMapRequest.attributes["inputLayerName"] = "IRMI"; //toevoeging FS. Kolom IRMI hernoemd als IMI. Deze wordt niet automatisch herkend. En moet dus gespecificeerd worden. if (!(string.IsNullOrEmpty(inputLayerName))) nbsUptakeMapRequest.attributes["inputLayerName"] = inputLayerName;
//toevoeging FS. Kolom IRMI hernoemd als IMI. Deze wordt niet automatisch herkend. En moet dus gespecificeerd worden.
//var layers = inputItem.Data["layers"]; //toevoeging FS, check welke data lagen worden omgezet
//_logger.LogInformation($"DataLayers: {layers}"); //toevoeging FS check welke data lagen worden omgezet
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsUptakeMapRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsUptakeMapRequest);
@ -116,9 +117,10 @@ namespace FarmmapsNbs
var itemName = "VRANbs uptake"; var itemName = "VRANbs uptake";
var uptakeMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code, var uptakeMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
GEOTIFF_PROCESSED_ITEMTYPE, itemName, GEOTIFF_PROCESSED_ITEMTYPE, itemName,
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) && i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
i.Name.ToLower().Contains(itemName.ToLower())); i.Name.ToLower().Contains(itemName.ToLower()));
if (uptakeMapItem == null) if (uptakeMapItem == null)
{ {
_logger.LogError("Could not find the uptake geotiff child item under cropfield"); _logger.LogError("Could not find the uptake geotiff child item under cropfield");
@ -136,11 +138,11 @@ namespace FarmmapsNbs
/// <param name="inputItem">The farmmaps item containing the geotiff data</param> /// <param name="inputItem">The farmmaps item containing the geotiff data</param>
/// <param name="plantingDate">The date the crop is planted</param> /// <param name="plantingDate">The date the crop is planted</param>
/// <param name="measurementDate">The date the measurements are taken</param> /// <param name="measurementDate">The date the measurements are taken</param>
/// <param name="inputType">The inputtype to use</param> /// <param name="inputType">The inputtype to use, could be yara, ci, irmi or wdvi</param>
/// <param name="targetN">The target nitrogen to use for the calculations</param> /// <param name="targetN">The target nitrogen to use for the calculations</param>
/// <returns></returns> /// <returns></returns>
public async Task<Item> CalculateApplicationMap(Item cropfieldItem, Item inputItem, DateTime plantingDate, public async Task<Item> CalculateApplicationMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,
DateTime measurementDate, string inputType, double targetN) DateTime measurementDate, string inputType, double targetN, string inputLayerName)
{ {
var nbsApplicationMapRequest = new TaskRequest {TaskType = VRANBS_TASK}; var nbsApplicationMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsApplicationMapRequest.attributes["operation"] = "application"; nbsApplicationMapRequest.attributes["operation"] = "application";
@ -150,7 +152,8 @@ namespace FarmmapsNbs
nbsApplicationMapRequest.attributes["inputCode"] = inputItem.Code; nbsApplicationMapRequest.attributes["inputCode"] = inputItem.Code;
nbsApplicationMapRequest.attributes["inputType"] = inputType.ToLower(); nbsApplicationMapRequest.attributes["inputType"] = inputType.ToLower();
nbsApplicationMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture); nbsApplicationMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture);
if (!(string.IsNullOrEmpty(inputLayerName))) nbsApplicationMapRequest.attributes["inputLayerName"] = inputLayerName;
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplicationMapRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplicationMapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>

View File

@ -1,4 +1,4 @@
namespace FarmmapsHaulmkilling.Models namespace FarmmapsZonering.Models
{ {
public class InputParameter public class InputParameter
{ {

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -40,83 +41,33 @@ namespace FarmmapsZonering
if (!Directory.Exists(DownloadFolder)) if (!Directory.Exists(DownloadFolder))
Directory.CreateDirectory(DownloadFolder); Directory.CreateDirectory(DownloadFolder);
// Load settings from previous cropfield
LoadSettings(); LoadSettings();
// Read input data from separate file
var zoneringInputJson = File.ReadAllText("ZoneringInput.json");
List<ZoneringInput> zoneringInputs = JsonConvert.DeserializeObject<List<ZoneringInput>>(zoneringInputJson);
// !! this call is needed the first time an api is called with a fresh clientid and secret !! // !! this call is needed the first time an api is called with a fresh clientid and secret !!
await _farmmapsApiService.GetCurrentUserCodeAsync(); await _farmmapsApiService.GetCurrentUserCodeAsync();
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync(); var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
// await SimpleAdditionAsync(roots);
await ZoningAsync(roots);
}
private async Task SimpleAdditionAsync(List<UserRoot> roots)
{
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 inputOneItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE,
Path.Combine("Data", "data_9001.tif"),"data_9001");
if (inputOneItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
var inputTwoItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, foreach (var input in zoneringInputs)
Path.Combine("Data", "data_times_two_4326.tiff"), "data_times_two_4326"); {
if (inputTwoItem == null) { try
_logger.LogError("Could not find item for uploaded data"); {
return; await ZoningAsync(roots, input);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
} }
var outputItem = await _zoneringService.CreateApplicationMapAsync(cropfieldItem, "[0] + [1]", new Output()
{
Name = "add",
Unit = "n/kg",
Quantity = "Nitrogen"
}, new InputParameter()
{
ItemCode = inputOneItem.Code,
LayerName = inputOneItem.Data["layers"][0]["name"].ToString()
},
new InputParameter()
{
ItemCode = inputTwoItem.Code,
LayerName = inputTwoItem.Data["layers"][0]["name"].ToString()
});
_logger.LogInformation("Downloading output");
await _farmmapsApiService.DownloadItemAsync(outputItem.Code,
Path.Combine(DownloadFolder, $"times_2_zonering.zip"));
} }
private async Task ZoningAsync(List<UserRoot> roots) private async Task ZoningAsync(List<UserRoot> roots, ZoneringInput input)
{ {
var myDrive = roots.SingleOrDefault(r => r.Name == "My drive"); var myDrive = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDrive == null) if (myDrive == null)
@ -136,6 +87,12 @@ namespace FarmmapsZonering
if (string.IsNullOrEmpty(_settings.CropfieldItemCode)) if (string.IsNullOrEmpty(_settings.CropfieldItemCode))
{ {
_logger.LogInformation("Creating cropfield"); _logger.LogInformation("Creating cropfield");
//var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, input.CropFieldName, input.cropYear
//$"VRA Poten cropfield {input.OutputFileName}",
//input.GeometryJson.ToString(Formatting.None));
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Zonering", 2020, 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 ] ] ] }"); @"{ ""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; _settings.CropfieldItemCode = cropfieldItem.Code;