Veranderingen jits, fedde en pepijn. #9

Merged
mark merged 46 commits from jits/FarmMapsApiClient_WURtest:master into master 2021-06-10 08:25:25 +00:00
11 changed files with 431 additions and 214 deletions
Showing only changes of commit bb14c4e4b0 - Show all commits

View File

@ -18,10 +18,12 @@ namespace FarmmapsApiSamples
public const string VRAPLANTING_TASK = "vnd.farmmaps.task.vrapoten"; public const string VRAPLANTING_TASK = "vnd.farmmaps.task.vrapoten";
public const string VRAZONERING_TASK = "vnd.farmmaps.task.vrazonering"; public const string VRAZONERING_TASK = "vnd.farmmaps.task.vrazonering";
public const string SATELLITE_TASK = "vnd.farmmaps.task.satellite"; public const string SATELLITE_TASK = "vnd.farmmaps.task.satellite";
public const string VANDERSAT_TASK = "vnd.farmmaps.task.vandersat";
public const string TASKMAP_TASK = "vnd.farmmaps.task.taskmap"; public const string TASKMAP_TASK = "vnd.farmmaps.task.taskmap";
public const string WORKFLOW_TASK = "vnd.farmmaps.task.workflow"; public const string WORKFLOW_TASK = "vnd.farmmaps.task.workflow";
public const string BOFEK_TASK = "vnd.farmmaps.task.bofek"; public const string BOFEK_TASK = "vnd.farmmaps.task.bofek";
public const string SHADOW_TASK = "vnd.farmmaps.task.shadow"; public const string SHADOW_TASK = "vnd.farmmaps.task.shadow";
public const string AHN_TASK = "vnd.farmmaps.task.ahn"; public const string AHN_TASK = "vnd.farmmaps.task.ahn";
public const string WATBAL_TASK = "vnd.farmmaps.task.watbal";
} }
} }

View File

@ -12,23 +12,19 @@ using static FarmmapsApiSamples.Constants;
namespace FarmmapsApi.Services namespace FarmmapsApi.Services
{ {
public class GeneralService public class GeneralService {
{
private readonly ILogger<GeneralService> _logger; private readonly ILogger<GeneralService> _logger;
private readonly FarmmapsApiService _farmmapsApiService; private readonly FarmmapsApiService _farmmapsApiService;
public GeneralService(ILogger<GeneralService> logger, FarmmapsApiService farmmapsApiService) public GeneralService(ILogger<GeneralService> logger, FarmmapsApiService farmmapsApiService) {
{
_logger = logger; _logger = logger;
_farmmapsApiService = farmmapsApiService; _farmmapsApiService = farmmapsApiService;
} }
public async Task<Item> CreateCropfieldItemAsync(string parentItemCode, string name, int year, public async Task<Item> CreateCropfieldItemAsync(string parentItemCode, string name, int year,
string fieldGeomJson, string data = "{}") string fieldGeomJson, string data = "{}") {
{
var currentYear = new DateTime(year, 1, 1); var currentYear = new DateTime(year, 1, 1);
var cropfieldItemRequest = new ItemRequest() var cropfieldItemRequest = new ItemRequest() {
{
ParentCode = parentItemCode, ParentCode = parentItemCode,
ItemType = CROPFIELD_ITEMTYPE, ItemType = CROPFIELD_ITEMTYPE,
Name = name, Name = name,
@ -41,8 +37,7 @@ namespace FarmmapsApi.Services
return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest); return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
} }
public async Task<Item> UploadDataAsync(UserRoot root, string itemType, string filePath, string itemName) public async Task<Item> UploadDataAsync(UserRoot root, string itemType, string filePath, string itemName) {
{
var startUpload = DateTime.UtcNow; var startUpload = DateTime.UtcNow;
var result = await _farmmapsApiService.UploadFile(filePath, root.Code, var result = await _farmmapsApiService.UploadFile(filePath, root.Code,
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}")); progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
@ -55,8 +50,7 @@ namespace FarmmapsApi.Services
i.Name.ToLower().Contains(itemName.ToLower())); i.Name.ToLower().Contains(itemName.ToLower()));
} }
public async Task<Item> UploadZipWithShapeAsync(UserRoot root, string filePath, string itemName) public async Task<Item> UploadZipWithShapeAsync(UserRoot root, string filePath, string itemName) {
{
var startUpload = DateTime.UtcNow; var startUpload = DateTime.UtcNow;
var result = await _farmmapsApiService.UploadFile(filePath, root.Code, var result = await _farmmapsApiService.UploadFile(filePath, root.Code,
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}")); progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
@ -66,11 +60,10 @@ namespace FarmmapsApi.Services
return await FindChildItemAsync(root.Code, SHAPE_PROCESSED_ITEMTYPE, itemName, return await FindChildItemAsync(root.Code, SHAPE_PROCESSED_ITEMTYPE, itemName,
i => i.Created >= startUpload && i => i.Created >= startUpload &&
i.Name.ToLower().Contains(itemName.ToLower()));; i.Name.ToLower().Contains(itemName.ToLower())); ;
} }
public async Task<Item> ShapeToGeotiff(Item shapeItem) public async Task<Item> ShapeToGeotiff(Item shapeItem) {
{
await RunAndWaitForTask(shapeItem, "vnd.farmmaps.task.shapetogeotiff"); await RunAndWaitForTask(shapeItem, "vnd.farmmaps.task.shapetogeotiff");
// the parent of the shape item is now the tiff item // the parent of the shape item is now the tiff item
@ -79,14 +72,12 @@ namespace FarmmapsApi.Services
} }
public async Task<Item> GeotiffToShape(Item tiffItem) public async Task<Item> GeotiffToShape(Item tiffItem) {
{ var taskmapRequest = new TaskRequest { TaskType = TASKMAP_TASK };
var taskmapRequest = new TaskRequest {TaskType = TASKMAP_TASK};
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(tiffItem.Code, taskmapRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(tiffItem.Code, taskmapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => {
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(tiffItem.Code, itemTaskCode); var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(tiffItem.Code, itemTaskCode);
_logger.LogInformation($"Waiting on converting geotiff to shape; status: {itemTaskStatus.State}"); _logger.LogInformation($"Waiting on converting geotiff to shape; status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished) if (itemTaskStatus.IsFinished)
@ -94,8 +85,7 @@ namespace FarmmapsApi.Services
}); });
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(tiffItem.Code, itemTaskCode); var itemTask = await _farmmapsApiService.GetTaskStatusAsync(tiffItem.Code, itemTaskCode);
if (itemTask.State == ItemTaskState.Error) if (itemTask.State == ItemTaskState.Error) {
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); _logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null; return null;
} }
@ -104,8 +94,7 @@ namespace FarmmapsApi.Services
var itemName = "Taskmap"; var itemName = "Taskmap";
var taskMapItem = await FindChildItemAsync(tiffItem.Code, var taskMapItem = await FindChildItemAsync(tiffItem.Code,
SHAPE_PROCESSED_ITEMTYPE, itemName); SHAPE_PROCESSED_ITEMTYPE, itemName);
if (taskMapItem == null) if (taskMapItem == null) {
{
_logger.LogError("Could not find the shape taskmap as a child item under the input"); _logger.LogError("Could not find the shape taskmap as a child item under the input");
return null; return null;
} }
@ -115,18 +104,15 @@ namespace FarmmapsApi.Services
public async Task<ItemTaskStatus> RunAndWaitForTask(Item subjectItem, string taskIdentifier, public async Task<ItemTaskStatus> RunAndWaitForTask(Item subjectItem, string taskIdentifier,
Action<TaskRequest> configureCallback = null, int retrySeconds = 3) Action<TaskRequest> configureCallback = null, int retrySeconds = 3) {
{ var taskRequest = new TaskRequest() {
var taskRequest = new TaskRequest()
{
TaskType = taskIdentifier TaskType = taskIdentifier
}; };
configureCallback?.Invoke(taskRequest); configureCallback?.Invoke(taskRequest);
var taskCode = await _farmmapsApiService.QueueTaskAsync(subjectItem.Code, taskRequest); var taskCode = await _farmmapsApiService.QueueTaskAsync(subjectItem.Code, taskRequest);
await PollTask(TimeSpan.FromSeconds(retrySeconds), async (tokenSource) => await PollTask(TimeSpan.FromSeconds(retrySeconds), async (tokenSource) => {
{
_logger.LogInformation($"Checking {taskIdentifier} task status"); _logger.LogInformation($"Checking {taskIdentifier} task status");
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(subjectItem.Code, taskCode); var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(subjectItem.Code, taskCode);
if (itemTaskStatus.IsFinished) if (itemTaskStatus.IsFinished)
@ -139,28 +125,24 @@ namespace FarmmapsApi.Services
} }
public async Task<Item> FindChildItemAsync(string parentCode, string itemType, string containsName, public async Task<Item> FindChildItemAsync(string parentCode, string itemType, string containsName,
Func<Item, bool> filter = null, int maxTries = 10) Func<Item, bool> filter = null, int maxTries = 10) {
{
Item dataItem = null; Item dataItem = null;
int tries = 0; int tries = 0;
await PollTask(TimeSpan.FromSeconds(3), async source => await PollTask(TimeSpan.FromSeconds(3), async source => {
{
_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) {
{
source.Cancel(); source.Cancel();
} }
tries++; tries++;
}); });
if (dataItem == null) if (dataItem == null) {
{
_logger.LogError("dataItem not found"); _logger.LogError("dataItem not found");
return null; return null;
} }
@ -169,14 +151,12 @@ namespace FarmmapsApi.Services
return dataItem; return dataItem;
} }
public async Task<Item> RunBofekTask(Item cropfieldItem) public async Task<Item> RunBofekTask(Item cropfieldItem) {
{ var taskmapRequest = new TaskRequest { TaskType = BOFEK_TASK };
var taskmapRequest = new TaskRequest {TaskType = BOFEK_TASK};
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => {
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
_logger.LogInformation($"Waiting on retreiving BOFEK data; status: {itemTaskStatus.State}"); _logger.LogInformation($"Waiting on retreiving BOFEK data; status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished) if (itemTaskStatus.IsFinished)
@ -184,8 +164,7 @@ namespace FarmmapsApi.Services
}); });
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTask.State == ItemTaskState.Error) if (itemTask.State == ItemTaskState.Error) {
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); _logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null; return null;
} }
@ -194,8 +173,7 @@ namespace FarmmapsApi.Services
var itemName = "bofek"; var itemName = "bofek";
var bofekItem = await FindChildItemAsync(cropfieldItem.Code, var bofekItem = await FindChildItemAsync(cropfieldItem.Code,
SHAPE_PROCESSED_ITEMTYPE, itemName); SHAPE_PROCESSED_ITEMTYPE, itemName);
if (bofekItem == null) if (bofekItem == null) {
{
_logger.LogError("Could not find the BOFEK data as a child item under the cropfield"); _logger.LogError("Could not find the BOFEK data as a child item under the cropfield");
return null; return null;
} }
@ -203,14 +181,12 @@ namespace FarmmapsApi.Services
return bofekItem; return bofekItem;
} }
public async Task<Item> RunAhnTask(Item cropfieldItem) public async Task<Item> RunAhnTask(Item cropfieldItem) {
{ var taskmapRequest = new TaskRequest { TaskType = AHN_TASK };
var taskmapRequest = new TaskRequest {TaskType = AHN_TASK};
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => {
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
_logger.LogInformation($"Waiting on retreiving AHN data; status: {itemTaskStatus.State}"); _logger.LogInformation($"Waiting on retreiving AHN data; status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished) if (itemTaskStatus.IsFinished)
@ -218,8 +194,7 @@ namespace FarmmapsApi.Services
}); });
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTask.State == ItemTaskState.Error) if (itemTask.State == ItemTaskState.Error) {
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); _logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null; return null;
} }
@ -228,8 +203,7 @@ namespace FarmmapsApi.Services
var itemName = "ahn"; var itemName = "ahn";
var ahnItem = await FindChildItemAsync(cropfieldItem.Code, var ahnItem = await FindChildItemAsync(cropfieldItem.Code,
GEOTIFF_PROCESSED_ITEMTYPE, itemName); GEOTIFF_PROCESSED_ITEMTYPE, itemName);
if (ahnItem == null) if (ahnItem == null) {
{
_logger.LogError("Could not find the AHN data as a child item under the cropfield"); _logger.LogError("Could not find the AHN data as a child item under the cropfield");
return null; return null;
} }
@ -237,14 +211,12 @@ namespace FarmmapsApi.Services
return ahnItem; return ahnItem;
} }
public async Task<Item> RunShadowTask(Item cropfieldItem) public async Task<Item> RunShadowTask(Item cropfieldItem) {
{ var taskmapRequest = new TaskRequest { TaskType = SHADOW_TASK };
var taskmapRequest = new TaskRequest {TaskType = SHADOW_TASK};
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => {
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
_logger.LogInformation($"Waiting on calculation shadow data; status: {itemTaskStatus.State}"); _logger.LogInformation($"Waiting on calculation shadow data; status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished) if (itemTaskStatus.IsFinished)
@ -252,8 +224,7 @@ namespace FarmmapsApi.Services
}); });
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTask.State == ItemTaskState.Error) if (itemTask.State == ItemTaskState.Error) {
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); _logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null; return null;
} }
@ -262,8 +233,7 @@ namespace FarmmapsApi.Services
var itemName = "shadow"; var itemName = "shadow";
var shadowItem = await FindChildItemAsync(cropfieldItem.Code, var shadowItem = await FindChildItemAsync(cropfieldItem.Code,
GEOTIFF_PROCESSED_ITEMTYPE, itemName); GEOTIFF_PROCESSED_ITEMTYPE, itemName);
if (shadowItem == null) if (shadowItem == null) {
{
_logger.LogError("Could not find the shadow data as a child item under the cropfield"); _logger.LogError("Could not find the shadow data as a child item under the cropfield");
return null; return null;
} }
@ -272,16 +242,14 @@ namespace FarmmapsApi.Services
} }
public async Task<string> RunSatelliteTask(Item cropfieldItem) public async Task<string> RunSatelliteTask(Item cropfieldItem) {
{
_logger.LogInformation("Gathering satellite information for cropfield, this might take a while!"); _logger.LogInformation("Gathering satellite information for cropfield, this might take a while!");
var taskmapRequest = new TaskRequest { TaskType = SATELLITE_TASK }; var taskmapRequest = new TaskRequest { TaskType = SATELLITE_TASK };
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest); string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => {
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
_logger.LogInformation($"Waiting on satellite data; status: {itemTaskStatus.State}"); _logger.LogInformation($"Waiting on satellite data; status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished) if (itemTaskStatus.IsFinished)
@ -291,8 +259,7 @@ namespace FarmmapsApi.Services
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTask.State == ItemTaskState.Error) if (itemTask.State == ItemTaskState.Error) {
{
_logger.LogError($"Something went wrong when trying to process satellite data; {itemTask.Message}"); _logger.LogError($"Something went wrong when trying to process satellite data; {itemTask.Message}");
} }
@ -301,8 +268,7 @@ namespace FarmmapsApi.Services
} }
public async Task<Item> FindSatelliteItem(Item cropfieldItem, string satelliteTaskCode) public async Task<Item> FindSatelliteItem(Item cropfieldItem, string satelliteTaskCode, string FieldName, bool StoreStatistics) {
{
var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, satelliteTaskCode); var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, satelliteTaskCode);
@ -314,8 +280,7 @@ namespace FarmmapsApi.Services
taskStatus.Finished <= item.Created.Value.AddHours(1)); taskStatus.Finished <= item.Created.Value.AddHours(1));
if (temporalItem == null) if (temporalItem == null) {
{
_logger.LogError("Temporal item not found"); _logger.LogError("Temporal item not found");
} }
@ -325,19 +290,18 @@ namespace FarmmapsApi.Services
_logger.LogInformation("Available satellite images:"); _logger.LogInformation("Available satellite images:");
var count = 0; var count = 0;
TimeSpan.FromSeconds(0.5); TimeSpan.FromSeconds(0.5);
foreach (var item in satelliteTiffs) foreach (var item in satelliteTiffs) {
{
Console.WriteLine($"Satellite image #{count}: {item.DataDate}"); Console.WriteLine($"Satellite image #{count}: {item.DataDate}");
count++; count++;
} }
_logger.LogInformation("Enter satellite image number for NBS application"); _logger.LogInformation("Enter satellite image number for NBS application");
int elment = Int32.Parse(Console.ReadLine()); int elment = Int32.Parse(Console.ReadLine());
var selectedSatelliteItem = satelliteTiffs[elment]; var selectedSatelliteItem = satelliteTiffs[elment];
if (selectedSatelliteItem == null) if (selectedSatelliteItem == null) {
{
_logger.LogError("Satellite item not found"); _logger.LogError("Satellite item not found");
} }
@ -350,8 +314,168 @@ namespace FarmmapsApi.Services
} }
//VanDerSat
public async Task<string> RunVanDerSatTask(Item cropfieldItem) {
_logger.LogInformation("Gathering VanDerSat information for cropfield, this might take a while!");
var taskmapRequest = new TaskRequest { TaskType = VANDERSAT_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 VanDerSat 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 VanDerSat data; {itemTask.Message}");
}
return itemTask.Code;
}
public async Task<string> RunWatBalTask(Item cropfieldItem) {
_logger.LogInformation("Gathering WatBal information for cropfield, this might take a while!");
var taskmapRequest = new TaskRequest { TaskType = WATBAL_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 VanDerSat 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 WatBal data; {itemTask.Message}");
}
return itemTask.Code;
}
public async Task<Item> FindVanDerSatItem(Item cropfieldItem, string VanDerSatTaskCode, string FieldName, bool StoreStatistics) {
var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, VanDerSatTaskCode);
// find VanDerSat data temporal
var temporalItem = await FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE, "Van der Sat");
if (temporalItem == null) {
_logger.LogError("Temporal item not found");
}
var VanDerSatiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalItem.Code);
_logger.LogInformation("Available VanDerSat images:");
var count = 0;
TimeSpan.FromSeconds(0.5);
foreach (var item in VanDerSatiffs) {
//Console.WriteLine($"Van der Sat image #{count}: {item.DataDate}");
//if (count == 0 ) {
// Console.WriteLine($"vandersat image #{count}: {item.Data}");
//}
if (StoreStatistics == true) {
var VanDerSatBand = item.Data["layers"][0]["name"];
var VanderSatFile = $"C:\\Akkerweb\\{FieldName}_{VanDerSatBand}.csv";
var NewLineField = $"Field,Date,Mean,Min,Max,Standard deviation, ConfidenceInterval low, ConfidenceInterval high" + Environment.NewLine;
if (count == 0) {
File.AppendAllText(VanderSatFile, NewLineField);
var numbervandersat = VanDerSatiffs.Count;
Console.WriteLine($"{numbervandersat} Van der Sat images found");
}
var VanderSatStatistics = item.Data["layers"][0]["renderer"]["band"]["statistics"];
var VanDerSatImageDate = (DateTime)item.DataDate;
var VanderSatDate = VanDerSatImageDate.ToString("yyyy-MM-dd");
var NewLineDate = $"\"date\":{VanderSatDate}" + Environment.NewLine;
if (VanderSatStatistics == null) {
Console.WriteLine($"{VanderSatDate} no statistics found");
//Console.WriteLine($"Available data: {item.Data}");
} else {
File.AppendAllText(VanderSatFile, $"{FieldName},{VanderSatDate},{VanderSatStatistics["mean"]},{VanderSatStatistics["min"]},{VanderSatStatistics["max"]},{VanderSatStatistics["stddev"]},{VanderSatStatistics["confidenceIntervalLow"]},{VanderSatStatistics["confidenceIntervalHigh"]}" + Environment.NewLine);
}
}
count++;
}
//_logger.LogInformation("Enter VanDerSat image number");
//int element = Int32.Parse(Console.ReadLine());
int element = 0;
var selectedVanDerSatItem = VanDerSatiffs[element];
if (selectedVanDerSatItem == null) {
_logger.LogError("VanDerSat item not found");
}
return selectedVanDerSatItem;
}
public async Task<Item> FindWatBalItem(Item cropfieldItem, string WatBalTaskCode, string FieldName, bool StoreStatistics) {
var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, WatBalTaskCode);
// find WatBal data temporal
var temporalItem = await FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE, "WatBal");//, item => item.SourceTask == VANDERSAT_TASK &&
// taskStatus.Finished >= item.Created &&
// taskStatus.Finished <= item.Created.Value.AddHours(1));
if (temporalItem == null) {
_logger.LogError("Temporal item not found");
}
var WatBalData = await _farmmapsApiService.GetItemChildrenAsync(temporalItem.Code);
_logger.LogInformation("Available WatBal Data:");
var count = 0;
TimeSpan.FromSeconds(0.5);
foreach (var item in WatBalData) {
Console.WriteLine($"WatBal data #{count}: {item.DataDate}");
if (count == 0) {
Console.WriteLine($"WatBalData #{count}: {item.Data}");
}
count++;
}
int element = 0;
var selectedWatBalItem = WatBalData[element];
if (selectedWatBalItem == null) {
_logger.LogError("WatBal item not found");
}
return selectedWatBalItem;
Review

Dit hoeft geen async methode te zijn (want niet async).

Dit hoeft geen async methode te zijn (want niet async).
}
} }
} }

View File

@ -14,6 +14,8 @@ namespace FarmmapsNbs.Models
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; } public string InputLayerName { get; set; }
public string fieldName{ get; set; }
public bool storeSatelliteStatistics { get; set; }
} }

View File

@ -2,5 +2,8 @@
public class Settings { public class Settings {
public string CropfieldItemCode { get; set; } public string CropfieldItemCode { get; set; }
public string SatelliteTaskCode { get; set; } public string SatelliteTaskCode { get; set; }
public string VanDerSatTaskCode { get; set; }
public string WatBalTaskCode { get; set; }
} }
} }

View File

@ -38,16 +38,11 @@ namespace FarmmapsNbs
public async Task RunAsync() public async Task RunAsync()
{ {
var nitrogenInputJson = File.ReadAllText("NitrogenInput.json"); var nitrogenInputJson = File.ReadAllText("NitrogenInput.json");
//var nitrogenInputJson = File.ReadAllText("fivefieldsinput.json");
List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson); List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson);
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();
@ -67,10 +62,16 @@ 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: // !!specify if you are using an already created cropfield:
bool useCreatedCropfield = false; bool useCreatedCropfield = true;
var plantingDate = input.PlantingDate; var plantingDate = input.PlantingDate;
var FieldName = input.fieldName;
bool StoreStatistics = input.storeSatelliteStatistics;
var measurementDate = input.MeasurementDate; var measurementDate = input.MeasurementDate;
string settingsfile = $"Settings_{FieldName}.json";
LoadSettings(settingsfile);
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded"); var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot == null) if (uploadedRoot == null)
@ -86,7 +87,6 @@ namespace FarmmapsNbs
return; return;
} }
// Use already created cropfield or create new one // Use already created cropfield or create new one
Item cropfieldItem; Item cropfieldItem;
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.CropfieldItemCode)) if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.CropfieldItemCode))
@ -95,7 +95,7 @@ namespace FarmmapsNbs
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code, cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
$"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None)); $"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None));
_settings.CropfieldItemCode = cropfieldItem.Code; _settings.CropfieldItemCode = cropfieldItem.Code;
SaveSettings(); SaveSettings(settingsfile);
} }
else else
{ {
@ -103,40 +103,59 @@ namespace FarmmapsNbs
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode); cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
} }
var geotiffItem = (Item)null; var geotiffItem = (Item)null;
// No file input, use most recent satellite image // No file input, use most recent satellite image
if (string.IsNullOrEmpty(input.File)) if (string.IsNullOrEmpty(input.File)) {
{
_logger.LogInformation("No specific data given, retrieving most recent satellite image"); _logger.LogInformation("No specific data given, retrieving most recent satellite image");
// check if satellite task not yet done, do here and save taskcode // check if satellite task not yet done, do here and save taskcode
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode)) if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode)) {
{
var satelliteTaskCode = await _generalService.RunSatelliteTask(cropfieldItem); var satelliteTaskCode = await _generalService.RunSatelliteTask(cropfieldItem);
_settings.SatelliteTaskCode = satelliteTaskCode; _settings.SatelliteTaskCode = satelliteTaskCode;
SaveSettings(); SaveSettings(settingsfile);
} }
// Select a particular satellite item from satelliteTask // Select a particular satellite item from satelliteTask
Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode); 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] // must be wdvi[1]
var inputType = (satalliteItem.Data["layers"] as JArray)?[1]["name"].ToString(); var inputType = (satalliteItem.Data["layers"] as JArray)?[1]["name"].ToString();
if (string.IsNullOrEmpty(inputType)) if (string.IsNullOrEmpty(inputType)) {
{
_logger.LogError("Could not get the input type name from the satellite item"); _logger.LogError("Could not get the input type name from the satellite item");
return; return;
} }
// download the geotiff // download the geotiff
var SatelliteImageDate = (DateTime)satalliteItem.DataDate;
var SatelliteDate = SatelliteImageDate.ToString("yyyyMMdd");
_logger.LogInformation("Downloading geotiff file"); _logger.LogInformation("Downloading geotiff file");
await _farmmapsApiService.DownloadItemAsync(satalliteItem.Code, await _farmmapsApiService.DownloadItemAsync(satalliteItem.Code,
Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}.zip")); Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}_{inputType}_{SatelliteDate}.zip"));
// overwrite measurement date by date of satellite item // overwrite measurement date by date of satellite item
measurementDate = satalliteItem.DataDate.Value; measurementDate = satalliteItem.DataDate.Value;
@ -147,7 +166,6 @@ namespace FarmmapsNbs
} }
// (geo)tiff input: // (geo)tiff input:
else if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) { else if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
_logger.LogInformation("input = tiff data"); _logger.LogInformation("input = tiff data");
@ -187,18 +205,28 @@ namespace FarmmapsNbs
} }
//// 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);
//}
//// Select a particular image item from VanDerSat
//Item VanDerSatItem = await _generalService.FindVanDerSatItem(cropfieldItem, _settings.VanDerSatTaskCode, FieldName, StoreStatistics);
//// 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}"); _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,
measurementDate, input.PotatoPurposeType, input.TargetYield); measurementDate, input.PotatoPurposeType, input.TargetYield);
if (targetNData == null) if (targetNData == null) {
{
_logger.LogError("Something went wrong with TargetN calculation"); _logger.LogError("Something went wrong with TargetN calculation");
return; return;
} }
@ -215,8 +243,7 @@ namespace FarmmapsNbs
var uptakeMapItem = var uptakeMapItem =
await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate, await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate,
measurementDate, input.InputVariable, input.InputLayerName); 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");
return; return;
} }
@ -231,8 +258,7 @@ namespace FarmmapsNbs
await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate, await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate,
measurementDate, input.InputVariable, targetNData.TargetN, input.InputLayerName); measurementDate, input.InputVariable, targetNData.TargetN, input.InputLayerName);
if (applicationMapItem == null) if (applicationMapItem == null) {
{
_logger.LogError("Something went wrong with creating the applicationMap"); _logger.LogError("Something went wrong with creating the applicationMap");
return; return;
} }
@ -244,15 +270,13 @@ namespace FarmmapsNbs
//transforming tiff to shape //transforming tiff to shape
var tiffItem = applicationMapItem; var tiffItem = applicationMapItem;
if (tiffItem == null) if (tiffItem == null) {
{
_logger.LogError("Could not find item for uploaded data"); _logger.LogError("Could not find item for uploaded data");
return; return;
} }
_logger.LogInformation($"Converting geotiff to shape"); _logger.LogInformation($"Converting geotiff to shape");
var taskmap = await _generalService.GeotiffToShape(tiffItem); var taskmap = await _generalService.GeotiffToShape(tiffItem);
if (taskmap == null) if (taskmap == null) {
{
_logger.LogError("Something went wrong with geotiff to shape transformation"); _logger.LogError("Something went wrong with geotiff to shape transformation");
return; return;
} }
@ -264,12 +288,13 @@ namespace FarmmapsNbs
} }
// Functions to save previously created cropfields // Functions to save previously created cropfields
private void LoadSettings() private void LoadSettings(string file)
{ {
if (File.Exists(SettingsFile)) if (File.Exists(file))
{ {
var jsonText = File.ReadAllText(SettingsFile); var jsonText = File.ReadAllText(file);
_settings = JsonConvert.DeserializeObject<Settings>(jsonText); _settings = JsonConvert.DeserializeObject<Settings>(jsonText);
} }
else else
@ -278,13 +303,22 @@ namespace FarmmapsNbs
} }
} }
private void SaveSettings() private void SaveSettings(string file)
{ {
if (_settings == null) if (_settings == null)
return; return;
var json = JsonConvert.SerializeObject(_settings); var json = JsonConvert.SerializeObject(_settings);
File.WriteAllText(SettingsFile, json); File.WriteAllText(file, json);
}
private void SaveInfo(string file) {
if (_settings == null)
return;
var json = JsonConvert.SerializeObject(_settings);
File.WriteAllText(file, json);
}
} }
} }
}

View File

@ -14,9 +14,7 @@ using static FarmmapsApiSamples.Constants;
namespace FarmmapsVRApoten namespace FarmmapsVRApoten
{ {
public class PotenApplication : IApplication public class PotenApplication : IApplication {
{
private const string DownloadFolder = "Downloads"; private const string DownloadFolder = "Downloads";
private readonly ILogger<PotenApplication> _logger; private readonly ILogger<PotenApplication> _logger;
@ -26,16 +24,14 @@ namespace FarmmapsVRApoten
public PotenApplication(ILogger<PotenApplication> logger, FarmmapsApiService farmmapsApiService, public PotenApplication(ILogger<PotenApplication> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService, PotenService potenService) GeneralService generalService, PotenService potenService) {
{
_logger = logger; _logger = logger;
_farmmapsApiService = farmmapsApiService; _farmmapsApiService = farmmapsApiService;
_generalService = generalService; _generalService = generalService;
_potenService = potenService; _potenService = potenService;
} }
public async Task RunAsync() public async Task RunAsync() {
{
// read field data from separate json file // read field data from separate json file
var VRAPotenInputJson = File.ReadAllText("PotenInput.json"); var VRAPotenInputJson = File.ReadAllText("PotenInput.json");
List<PotenInput> potenInputs = JsonConvert.DeserializeObject<List<PotenInput>>(VRAPotenInputJson); List<PotenInput> potenInputs = JsonConvert.DeserializeObject<List<PotenInput>>(VRAPotenInputJson);
@ -48,36 +44,29 @@ namespace FarmmapsVRApoten
await _farmmapsApiService.GetCurrentUserCodeAsync(); await _farmmapsApiService.GetCurrentUserCodeAsync();
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync(); var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
foreach (var input in potenInputs) foreach (var input in potenInputs) {
{ try {
try
{
await Process(roots, input); await Process(roots, input);
} } catch (Exception ex) {
catch (Exception ex)
{
_logger.LogError(ex.Message); _logger.LogError(ex.Message);
} }
} }
} }
private async Task Process(List<UserRoot> roots, PotenInput input) private async Task Process(List<UserRoot> roots, PotenInput input) {
{
var meanDensity = input.MeanDensity; var meanDensity = input.MeanDensity;
var variation = input.Variation; var variation = input.Variation;
var fieldName = input.FieldName; var fieldName = input.FieldName;
bool useShadow = input.UseShadow; bool useShadow = input.UseShadow;
var myDrive = roots.SingleOrDefault(r => r.Name == "My drive"); var myDrive = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDrive == null) if (myDrive == null) {
{
_logger.LogError("Could not find a needed root item"); _logger.LogError("Could not find a needed root item");
return; return;
} }
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded"); var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot == null) if (uploadedRoot == null) {
{
_logger.LogError("Could not find a needed root item"); _logger.LogError("Could not find a needed root item");
return; return;
} }
@ -89,12 +78,10 @@ namespace FarmmapsVRApoten
input.GeometryJson.ToString(Formatting.None)); input.GeometryJson.ToString(Formatting.None));
//Calculating shadow map //Calculating shadow map
if (useShadow) if (useShadow) {
{
_logger.LogInformation("Calculate shadow map for field"); _logger.LogInformation("Calculate shadow map for field");
var shadowItem = await _generalService.RunShadowTask(cropfieldItem); var shadowItem = await _generalService.RunShadowTask(cropfieldItem);
if (shadowItem == null) if (shadowItem == null) {
{
_logger.LogError("Something went wrong while obtaining the shadow map"); _logger.LogError("Something went wrong while obtaining the shadow map");
return; return;
} }
@ -104,21 +91,30 @@ namespace FarmmapsVRApoten
Path.Combine(DownloadFolder, $"{input.OutputFileName}.shadow.zip")); Path.Combine(DownloadFolder, $"{input.OutputFileName}.shadow.zip"));
} }
//Calculating AHN map
_logger.LogInformation("retreiving AHN map for field");
var AHNItem = await _generalService.RunAhnTask(cropfieldItem);
if (AHNItem == null) {
_logger.LogError("Something went wrong while obtaining the AHN map");
return;
}
_logger.LogInformation("Downloading AHN map");
await _farmmapsApiService.DownloadItemAsync(AHNItem.Code,
Path.Combine(DownloadFolder, $"{input.OutputFileName}_AHN.zip"));
_logger.LogInformation("Looking for local data to use"); _logger.LogInformation("Looking for local data to use");
var localDataAvailable = input.File; var localDataAvailable = input.File;
var geotiffItem = (Item) null; var geotiffItem = (Item)null;
if (String.IsNullOrEmpty(localDataAvailable)) if (String.IsNullOrEmpty(localDataAvailable)) {
{
_logger.LogInformation("Could not find item for uploaded data, using BOFEK"); _logger.LogInformation("Could not find item for uploaded data, using BOFEK");
//Retreiving BOFEK //Retreiving BOFEK
_logger.LogInformation("Get BOFEK for field"); _logger.LogInformation("Get BOFEK for field");
var bofekItem = await _generalService.RunBofekTask(cropfieldItem); var bofekItem = await _generalService.RunBofekTask(cropfieldItem);
if (bofekItem == null) if (bofekItem == null) {
{
_logger.LogError("Something went wrong while obtaining the BOFEK data"); _logger.LogError("Something went wrong while obtaining the BOFEK data");
return; return;
} }
@ -126,10 +122,7 @@ namespace FarmmapsVRApoten
_logger.LogInformation("Downloading Bofek map"); _logger.LogInformation("Downloading Bofek map");
await _farmmapsApiService.DownloadItemAsync(bofekItem.Code, await _farmmapsApiService.DownloadItemAsync(bofekItem.Code,
Path.Combine(DownloadFolder, $"{input.OutputFileName}.BOFEK.zip")); Path.Combine(DownloadFolder, $"{input.OutputFileName}.BOFEK.zip"));
} } else if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
else if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
_logger.LogInformation("input = tiff data"); _logger.LogInformation("input = tiff data");
var dataPath = Path.Combine("Data", input.File); var dataPath = Path.Combine("Data", input.File);
geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath, geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath,
@ -139,10 +132,7 @@ namespace FarmmapsVRApoten
_logger.LogError("Could not find item for uploaded data"); _logger.LogError("Could not find item for uploaded data");
return; return;
} }
} } 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);
var shapeItem = isGeoJson var shapeItem = isGeoJson
@ -151,8 +141,7 @@ namespace FarmmapsVRApoten
: await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, : await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath,
Path.GetFileNameWithoutExtension(input.File)); Path.GetFileNameWithoutExtension(input.File));
if (shapeItem == null) if (shapeItem == null) {
{
_logger.LogError("Something went wrong while searching for the shape file"); _logger.LogError("Something went wrong while searching for the shape file");
return; return;
} }
@ -161,8 +150,7 @@ namespace FarmmapsVRApoten
_logger.LogInformation($"Converting shape to geotiff"); _logger.LogInformation($"Converting shape to geotiff");
geotiffItem = await _generalService.ShapeToGeotiff(shapeItem); geotiffItem = await _generalService.ShapeToGeotiff(shapeItem);
if (geotiffItem == null) if (geotiffItem == null) {
{
_logger.LogError("Something went wrong with shape to geotiff transformation"); _logger.LogError("Something went wrong with shape to geotiff transformation");
return; return;
} }
@ -179,8 +167,7 @@ namespace FarmmapsVRApoten
var applianceMapItem = var applianceMapItem =
await _potenService.CalculateApplicationMapAsync(cropfieldItem, geotiffItem, meanDensity, variation); await _potenService.CalculateApplicationMapAsync(cropfieldItem, geotiffItem, meanDensity, variation);
if (applianceMapItem == null) if (applianceMapItem == null) {
{
return; return;
} }
@ -195,8 +182,7 @@ namespace FarmmapsVRApoten
_logger.LogInformation($"Converting geotiff to shape"); _logger.LogInformation($"Converting geotiff to shape");
var taskmap = await _generalService.GeotiffToShape(applianceMapItem); var taskmap = await _generalService.GeotiffToShape(applianceMapItem);
if (taskmap == null) if (taskmap == null) {
{
_logger.LogError("Something went wrong with geotiff to shape transformation"); _logger.LogError("Something went wrong with geotiff to shape transformation");
return; return;
} }

View File

@ -3,5 +3,8 @@ namespace FarmmapsHaulmkilling.Models
public class Settings public class Settings
{ {
public string CropfieldItemCode { get; set; } public string CropfieldItemCode { get; set; }
public string SatelliteTaskCode { get; set; }
public string VanDerSatTaskCode { get; set; }
public string WatBalTaskCode { get; set; }
} }
} }

View File

@ -4,11 +4,13 @@ using Newtonsoft.Json.Linq;
namespace FarmmapsZonering { namespace FarmmapsZonering {
public class ZoneringInput { public class ZoneringInput {
public bool UseShadow { get; set; } public bool UseShadow { get; set; }
public string File { get; set; } public string File { get; set; }
public string OutputFileName { get; set; } public string OutputFileName { get; set; }
public string FieldName { get; set; } public string fieldName { get; set; }
public JObject GeometryJson { get; set; } public JObject GeometryJson { get; set; }
public bool GetWatBal { get; set; }
public bool GetVanDerSat { get; set; }
public bool storeVanDerSatStatistics { get; set; }
} }
} }

View File

@ -52,9 +52,10 @@ namespace FarmmapsZonering.Services
var itemName = $"VRAZonering"; var itemName = $"VRAZonering";
var applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code, var applianceMapItem = 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 =>
i.Name.ToLower().Contains(itemName.ToLower())); i.Name.ToLower().Contains(itemName.ToLower()));
if (applianceMapItem == null) if (applianceMapItem == null)
{ {
_logger.LogError("Could not find the VRAZonering geotiff child item under cropfield"); _logger.LogError("Could not find the VRAZonering geotiff child item under cropfield");

View File

@ -41,9 +41,6 @@ namespace FarmmapsZonering
if (!Directory.Exists(DownloadFolder)) if (!Directory.Exists(DownloadFolder))
Directory.CreateDirectory(DownloadFolder); Directory.CreateDirectory(DownloadFolder);
// Load settings from previous cropfield
LoadSettings();
// Read input data from separate file // Read input data from separate file
var zoneringInputJson = File.ReadAllText("ZoneringInput.json"); var zoneringInputJson = File.ReadAllText("ZoneringInput.json");
List<ZoneringInput> zoneringInputs = JsonConvert.DeserializeObject<List<ZoneringInput>>(zoneringInputJson); List<ZoneringInput> zoneringInputs = JsonConvert.DeserializeObject<List<ZoneringInput>>(zoneringInputJson);
@ -76,6 +73,16 @@ namespace FarmmapsZonering
return; return;
} }
bool useCreatedCropfield = true;
bool GetWatBal = input.GetWatBal;
bool getVanDerSat = true;// input.GetVanDerSat;
bool StoreVanDerSatStatistics = true;// input.storeVanDerSatStatistics;
var FieldName = input.fieldName;
string settingsfile = $"Settings_{FieldName}.json";
// Load settings from previous cropfield
LoadSettings(settingsfile);
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded"); var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot == null) if (uploadedRoot == null)
{ {
@ -91,12 +98,15 @@ namespace FarmmapsZonering
//var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, input.CropFieldName, input.cropYear //var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, input.CropFieldName, input.cropYear
//$"VRA Poten cropfield {input.OutputFileName}", //$"VRA Poten cropfield {input.OutputFileName}",
//input.GeometryJson.ToString(Formatting.None)); //input.GeometryJson.ToString(Formatting.None));
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Perceel mestsilo", 2020,
@"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 6.48270347, 53.25953201 ], [ 6.48257178, 53.25858122 ], [ 6.48781397, 53.25797859 ], [ 6.48793320, 53.25928036 ], [ 6.48270347, 53.25953201 ] ] ] }");
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; _settings.CropfieldItemCode = cropfieldItem.Code;
SaveSettings(); SaveSettings(settingsfile);
//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 else
{ {
@ -104,8 +114,38 @@ namespace FarmmapsZonering
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode); cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
} }
if (GetWatBal==true) {
////Run watbal
//if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.WatBalTaskCode)) {
// var WatBalTaskCode = await _generalService.RunWatBalTask(cropfieldItem);
// _settings.WatBalTaskCode = WatBalTaskCode;
// SaveSettings(settingsfile);
//}
//// Get watbal data
//Item WatBalItem = await _generalService.FindWatBalItem(cropfieldItem, _settings.WatBalTaskCode, FieldName, StoreStatistics);
}
if (getVanDerSat==true) {
// 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);
}
// Select a particular image item from VanDerSat
Item VanDerSatItem = await _generalService.FindVanDerSatItem(cropfieldItem, _settings.VanDerSatTaskCode, FieldName, StoreVanDerSatStatistics);
// download the geotiff
_logger.LogInformation("Downloading geotiff file");
await _farmmapsApiService.DownloadItemAsync(VanDerSatItem.Code,
Path.Combine(DownloadFolder, $"nbs_VanDerSatGeotiff_{input.OutputFileName}.zip"));
}
var inputOneItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, var inputOneItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE,
Path.Combine("Data", "data_9001.tif"),"data_9001"); Path.Combine("Data", "data_9001.tif"), "data_9001");
if (inputOneItem == null) { if (inputOneItem == null) {
_logger.LogError("Could not find item for uploaded data"); _logger.LogError("Could not find item for uploaded data");
return; return;
@ -118,11 +158,25 @@ namespace FarmmapsZonering
return; return;
} }
var outputItem = await _zoneringService.CreateApplicationMapAsync(cropfieldItem, "if [0] >= 1.28 then [1] else 0", new Output() //Formule kan meerdere inputs aan
// Met blokhaken en een nummer specificeer je een input:
//[0], [10]. etc...
// Aan de hand van de volgorde van input wordt momenteel bepaald welk nummer bij welk input hoort.
//[0] is dus de eerst tiff opgegeven.Als het goed is maakt het nummer nu niks uit, dus als er 2 inputs zijn en in de formule staan[0] en[10] dan zal die nog steeds werken.
//De volgorde van de tiff kun je verslepen, bij de formule inputs kun je de input van het ene[0] slot naar het andere slepen[10]
//Functies: abs, min en max
//Constanten: pi en e
//if ([0] -[1])/ ([0] +[1]) < 0 then 0
//else if ([0] -[1])/ ([0] +[1]) > 1 then 1
//else ([0] -[1]) / ([0] +[1])
var outputItem = await _zoneringService.CreateApplicationMapAsync(cropfieldItem, "[0] >= 1.28 then [1] else 0", new Output()
{ {
Name = "Remove", Name = "Test",
Unit = "m&m", Unit = "index",
Quantity = "Nonsense" Quantity = "kg"
}, new InputParameter() }, new InputParameter()
{ {
ItemCode = inputOneItem.Code, ItemCode = inputOneItem.Code,
@ -130,35 +184,38 @@ namespace FarmmapsZonering
}, },
new InputParameter() new InputParameter()
{ {
ItemCode = inputTwoItem.Code, ItemCode = inputOneItem.Code,
LayerName = inputTwoItem.Data["layers"][0]["name"].ToString() LayerName = inputOneItem.Data["layers"][0]["name"].ToString()
}); });
_logger.LogInformation("Downloading output"); _logger.LogInformation("Downloading output");
await _farmmapsApiService.DownloadItemAsync(outputItem.Code, await _farmmapsApiService.DownloadItemAsync(outputItem.Code,
Path.Combine(DownloadFolder, $"NonsenseRemove.zip")); Path.Combine(DownloadFolder, $"Test.zip"));
} }
private void LoadSettings() // Functions to save previously created cropfields
{ private void LoadSettings(string file) {
if (File.Exists(SettingsFile)) if (File.Exists(file)) {
{ var jsonText = File.ReadAllText(file);
var jsonText = File.ReadAllText(SettingsFile);
_settings = JsonConvert.DeserializeObject<Settings>(jsonText); _settings = JsonConvert.DeserializeObject<Settings>(jsonText);
} } else {
else
{
_settings = new Settings(); _settings = new Settings();
} }
} }
private void SaveSettings() private void SaveSettings(string file) {
{
if (_settings == null) if (_settings == null)
return; return;
var json = JsonConvert.SerializeObject(_settings); var json = JsonConvert.SerializeObject(_settings);
File.WriteAllText(SettingsFile, json); File.WriteAllText(file, json);
}
private void SaveInfo(string file) {
if (_settings == null)
return;
var json = JsonConvert.SerializeObject(_settings);
File.WriteAllText(file, json);
} }
} }
} }

View File

@ -4,6 +4,9 @@
"OutputFileName": "Zoning", "OutputFileName": "Zoning",
"FieldName": "Data_whole", "FieldName": "Data_whole",
"UseShadow": false, "UseShadow": false,
"GetWatBal": true,
"GetVanDerSat": true,
"storeVanDerSatStatistics": true,
"geometryJson": { "geometryJson": {
"type": "Polygon", "type": "Polygon",
"coordinates": [ "coordinates": [