some adjustments to zoning, included vandersat among others

This commit is contained in:
Sijbrandij 2021-01-28 15:29:11 +01:00
commit c33a8c8a11
11 changed files with 472 additions and 242 deletions

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,27 +242,24 @@ 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)
tokenSource.Cancel(); tokenSource.Cancel();
}); });
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;
}
} }
} }

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)
@ -85,8 +86,7 @@ namespace FarmmapsNbs
_logger.LogError("Could not find a needed root item"); _logger.LogError("Could not find a needed root item");
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,20 +166,19 @@ 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");
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,
Path.GetFileNameWithoutExtension(input.File)); Path.GetFileNameWithoutExtension(input.File));
if (geotiffItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
}
if (geotiffItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
}
// json/shape input // json/shape input
else { else {
var isGeoJson = input.File.Contains("json"); var isGeoJson = input.File.Contains("json");
@ -187,22 +205,32 @@ 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;
} }
_logger.LogInformation($"TargetN: {targetNData.TargetN}"); _logger.LogInformation($"TargetN: {targetNData.TargetN}");
////Option to manually adjust the Target N, for test purposes only! ////Option to manually adjust the Target N, for test purposes only!
//targetNData.TargetN = 225; //targetNData.TargetN = 225;
@ -215,12 +243,11 @@ 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;
} }
_logger.LogInformation("Downloading uptake map"); _logger.LogInformation("Downloading uptake map");
await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code, await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code,
Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip")); Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip"));
@ -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

@ -4,7 +4,8 @@ namespace FarmmapsHaulmkilling.Models
{ {
public string CropfieldItemCode { get; set; } public string CropfieldItemCode { get; set; }
public string CropfieldName { get; set; } public string CropfieldName { get; set; }
public string SatelliteTaskCode { get; set; }
public string VanDerSatTaskCode { get; set; }
public string WatBalTaskCode { get; set; }
} }
} }

View File

@ -1,7 +1,6 @@
using System; using System;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
namespace FarmmapsZonering.Models namespace FarmmapsZonering.Models
{ {
public class ZoneringInput public class ZoneringInput
@ -17,6 +16,9 @@ namespace FarmmapsZonering.Models
public bool CreateNewCropfield { get; set; } public bool CreateNewCropfield { get; set; }
public int CropYear { get; set; } public int CropYear { 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 = input.GetVanDerSat;
bool StoreVanDerSatStatistics = input.storeVanDerSatStatistics;
var FieldName = input.CropFieldName;
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)
{ {
@ -88,28 +95,67 @@ namespace FarmmapsZonering
{ {
_logger.LogInformation("Creating cropfield"); _logger.LogInformation("Creating cropfield");
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, input.CropFieldName, input.CropYear, cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, input.CropFieldName, input.CropYear,
input.GeometryJson.ToString(Formatting.None)); input.GeometryJson.ToString(Formatting.None));
_settings.CropfieldName = cropfieldItem.Name; _settings.CropfieldName = cropfieldItem.Name;
_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(settingsfile);
}
else else
{ {
_logger.LogInformation("Cropfield already exists trying to get"); _logger.LogInformation("Cropfield already exists trying to get");
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode); cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
} }
var inputOneItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, if (GetWatBal==true) {
Path.Combine("Data", $"{input.InputItemOne}"),Path.GetFileNameWithoutExtension($"{input.InputItemOne}")); ////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,
Path.Combine("Data", $"{input.InputItemOne}"), Path.GetFileNameWithoutExtension($"{input.InputItemOne}"));
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;
} }
var inputTwoItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, var inputTwoItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE,
Path.Combine("Data", $"{input.InputItemTwo}"), Path.GetFileNameWithoutExtension($"{input.InputItemTwo}")); Path.Combine("Data", $"{input.InputItemTwo}"), Path.GetFileNameWithoutExtension($"{input.InputItemTwo}"));
if (inputTwoItem == null) { if (inputTwoItem == null) {
_logger.LogError("Could not find item for uploaded data"); _logger.LogError("Could not find item for uploaded data");
return; return;
@ -120,7 +166,8 @@ namespace FarmmapsZonering
Name = input.LayerName, Name = input.LayerName,
Quantity = input.CalculatedQuantity, Quantity = input.CalculatedQuantity,
Unit = input.CalculatedUnit, Unit = input.CalculatedUnit,
}, new InputParameter() }, new InputParameter()
{ {
ItemCode = inputOneItem.Code, ItemCode = inputOneItem.Code,
@ -128,8 +175,8 @@ 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");
@ -137,29 +184,33 @@ namespace FarmmapsZonering
await _farmmapsApiService.DownloadItemAsync(outputItem.Code, await _farmmapsApiService.DownloadItemAsync(outputItem.Code,
Path.Combine(DownloadFolder, $"{input.OutputFileName}.zip")); Path.Combine(DownloadFolder, $"{input.OutputFileName}.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

@ -1,47 +1,71 @@
[ [
//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
"InputItemOne": "20201106_Sentinel2_L2A_B04.tiff", //else if ([0] -[1])/ ([0] +[1]) > 1 then 1
"InputItemTwo": "20201106_Sentinel2_L2A_B08.tiff", //else ([0] -[1]) / ([0] +[1])
"Formula": "([1]-[0])/([1]+[0])",
"CreatedLayerName": "Biomassa",
"CalculatedQuantity": "NDVI",
"CalculatedUnit": "ndviValue",
"OutputFileName": "FullField_NDVI",
"CropFieldName": "FullField",
"CreateNewCropfield": false,
"CropYear": 2020,
"geometryJson": {
"type": "Polygon",
"coordinates": [
[
[ 4.9593709, 52.8014339 ],
[ 4.9675488, 52.7943149 ],
[ 4.9735195, 52.7968665 ],
[ 4.9667833, 52.8030414 ],
[ 4.9593709, 52.8014339 ]
]
]
}
}
//{ //{
// "File": "Points.json", // "InputItemOne": "20201106_Sentinel2_L2A_B04.tiff",
// "InputItemTwo": "20201106_Sentinel2_L2A_B08.tiff",
// "Formula": "([1]-[0])/([1]+[0])",
// "CreatedLayerName": "Biomassa",
// "CalculatedQuantity": "NDVI",
// "CalculatedUnit": "ndviValue",
// "OutputFileName": "FullField_NDVI",
// "CropFieldName": "FullField",
// "CreateNewCropfield": false,
// "CropYear": 2020,
// "geometryJson": { // "geometryJson": {
// "type": "Polygon", // "type": "Polygon",
// "coordinates": [ // "coordinates": [
// [
// [ 5.66886041703652044, 52.52929999060298627 ],
// [ 5.6716230923214912, 52.52946316399909676 ],
// [ 5.67185376229668581, 52.5280565894154563 ],
// [ 5.66903207841337231, 52.52790646510525363 ],
// [ 5.66886041703652044, 52.52929999060298627 ]
// ]
// ]
// }
//}
// [
// [ 4.9593709, 52.8014339 ],
// [ 4.9675488, 52.7943149 ],
// [ 4.9735195, 52.7968665 ],
// [ 4.9667833, 52.8030414 ],
// [ 4.9593709, 52.8014339 ]
// ]
// ]
// },
{
"InputItemOne": "data_9001.tif",
"InputItemTwo": "data_times_two_4326.tiff",
"Formula": "([1]-[0])/([1]+[0])",
"CreatedLayerName": "Biomassa",
"CalculatedQuantity": "NDVI",
"CalculatedUnit": "ndviValue",
"OutputFileName": "Zoning",
"CropFieldName": "Data_whole",
"UseShadow": false,
"GetWatBal": false,
"GetVanDerSat": true,
"storeVanDerSatStatistics": true,
"CropYear": 2020,
"geometryJson": {
"type": "Polygon",
"coordinates": [
[
[ 5.66886041703652044, 52.52929999060298627 ],
[ 5.6716230923214912, 52.52946316399909676 ],
[ 5.67185376229668581, 52.5280565894154563 ],
[ 5.66903207841337231, 52.52790646510525363 ],
[ 5.66886041703652044, 52.52929999060298627 ]
]
]
}
}
] ]