forked from FarmMaps/FarmMapsApiClient
Compare commits
No commits in common. "a29b973268cb80d95627f1c2393d9783c1b513c6" and "693c82db2e54385bd7a0c4aeb2776314860fc38d" have entirely different histories.
a29b973268
...
693c82db2e
@ -18,12 +18,10 @@ 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";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,19 +12,23 @@ 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,
|
||||||
@ -37,7 +41,8 @@ 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}"));
|
||||||
@ -50,7 +55,8 @@ 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}"));
|
||||||
@ -60,10 +66,11 @@ 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
|
||||||
@ -72,12 +79,14 @@ 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)
|
||||||
@ -85,7 +94,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -94,7 +104,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -104,15 +115,18 @@ 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)
|
||||||
@ -125,24 +139,28 @@ 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;
|
||||||
}
|
}
|
||||||
@ -151,12 +169,14 @@ 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)
|
||||||
@ -164,7 +184,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -173,7 +194,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -181,12 +203,14 @@ 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)
|
||||||
@ -194,7 +218,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -203,7 +228,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -211,12 +237,14 @@ 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)
|
||||||
@ -224,7 +252,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -233,7 +262,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -242,14 +272,16 @@ 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)
|
||||||
@ -259,7 +291,8 @@ 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}");
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -268,7 +301,8 @@ namespace FarmmapsApi.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<Item> FindSatelliteItem(Item cropfieldItem, string satelliteTaskCode, string FieldName, bool StoreStatistics) {
|
public async Task<Item> FindSatelliteItem(Item cropfieldItem, string satelliteTaskCode)
|
||||||
|
{
|
||||||
|
|
||||||
var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, satelliteTaskCode);
|
var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, satelliteTaskCode);
|
||||||
|
|
||||||
@ -280,7 +314,8 @@ 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");
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -290,18 +325,19 @@ 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");
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -314,168 +350,8 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,8 +14,6 @@ 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; }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,5 @@
|
|||||||
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; }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -38,11 +38,16 @@ 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();
|
||||||
@ -62,16 +67,10 @@ 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 = true;
|
bool useCreatedCropfield = false;
|
||||||
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)
|
||||||
@ -87,6 +86,7 @@ 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(settingsfile);
|
SaveSettings();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -103,59 +103,40 @@ 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(settingsfile);
|
SaveSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Select a particular satellite item from satelliteTask
|
// Select a particular satellite item from satelliteTask
|
||||||
Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode, FieldName, StoreStatistics);
|
Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode);
|
||||||
|
|
||||||
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}_{inputType}_{SatelliteDate}.zip"));
|
Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}.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;
|
||||||
@ -166,18 +147,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) {
|
if (geotiffItem == null) {
|
||||||
_logger.LogError("Could not find item for uploaded data");
|
_logger.LogError("Could not find item for uploaded data");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// json/shape input
|
// json/shape input
|
||||||
else {
|
else {
|
||||||
@ -205,28 +187,18 @@ 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;
|
||||||
}
|
}
|
||||||
@ -243,7 +215,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -258,7 +231,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -270,13 +244,15 @@ 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;
|
||||||
}
|
}
|
||||||
@ -288,13 +264,12 @@ namespace FarmmapsNbs
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Functions to save previously created cropfields
|
// Functions to save previously created cropfields
|
||||||
private void LoadSettings(string file)
|
private void LoadSettings()
|
||||||
{
|
{
|
||||||
if (File.Exists(file))
|
if (File.Exists(SettingsFile))
|
||||||
{
|
{
|
||||||
var jsonText = File.ReadAllText(file);
|
var jsonText = File.ReadAllText(SettingsFile);
|
||||||
_settings = JsonConvert.DeserializeObject<Settings>(jsonText);
|
_settings = JsonConvert.DeserializeObject<Settings>(jsonText);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -303,22 +278,13 @@ namespace FarmmapsNbs
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SaveSettings(string file)
|
private void SaveSettings()
|
||||||
{
|
{
|
||||||
if (_settings == null)
|
if (_settings == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var json = JsonConvert.SerializeObject(_settings);
|
var json = JsonConvert.SerializeObject(_settings);
|
||||||
File.WriteAllText(file, json);
|
File.WriteAllText(SettingsFile, json);
|
||||||
}
|
}
|
||||||
private void SaveInfo(string file) {
|
|
||||||
if (_settings == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var json = JsonConvert.SerializeObject(_settings);
|
|
||||||
File.WriteAllText(file, json);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
@ -14,7 +14,9 @@ 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;
|
||||||
@ -24,14 +26,16 @@ 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);
|
||||||
@ -44,29 +48,36 @@ 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;
|
||||||
}
|
}
|
||||||
@ -78,10 +89,12 @@ 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;
|
||||||
}
|
}
|
||||||
@ -91,30 +104,21 @@ 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;
|
||||||
}
|
}
|
||||||
@ -122,7 +126,10 @@ 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,
|
||||||
@ -132,7 +139,10 @@ 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
|
||||||
@ -141,7 +151,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -150,7 +161,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -167,7 +179,8 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,7 +195,8 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,7 @@ 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; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
|
||||||
namespace FarmmapsZonering.Models
|
namespace FarmmapsZonering.Models
|
||||||
{
|
{
|
||||||
public class ZoneringInput
|
public class ZoneringInput
|
||||||
@ -16,9 +17,6 @@ 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; }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -52,10 +52,9 @@ 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");
|
||||||
|
@ -41,6 +41,9 @@ 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);
|
||||||
@ -73,16 +76,6 @@ 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)
|
||||||
{
|
{
|
||||||
@ -95,18 +88,12 @@ 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(settingsfile);
|
SaveSettings();
|
||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
@ -114,48 +101,15 @@ 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", $"{input.InputItemOne}"), Path.GetFileNameWithoutExtension($"{input.InputItemOne}"));
|
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;
|
||||||
@ -167,7 +121,6 @@ namespace FarmmapsZonering
|
|||||||
Quantity = input.CalculatedQuantity,
|
Quantity = input.CalculatedQuantity,
|
||||||
Unit = input.CalculatedUnit,
|
Unit = input.CalculatedUnit,
|
||||||
|
|
||||||
|
|
||||||
}, new InputParameter()
|
}, new InputParameter()
|
||||||
{
|
{
|
||||||
ItemCode = inputOneItem.Code,
|
ItemCode = inputOneItem.Code,
|
||||||
@ -175,8 +128,8 @@ namespace FarmmapsZonering
|
|||||||
},
|
},
|
||||||
new InputParameter()
|
new InputParameter()
|
||||||
{
|
{
|
||||||
ItemCode = inputOneItem.Code,
|
ItemCode = inputTwoItem.Code,
|
||||||
LayerName = inputOneItem.Data["layers"][0]["name"].ToString()
|
LayerName = inputTwoItem.Data["layers"][0]["name"].ToString()
|
||||||
});
|
});
|
||||||
|
|
||||||
_logger.LogInformation("Downloading output");
|
_logger.LogInformation("Downloading output");
|
||||||
@ -184,33 +137,29 @@ namespace FarmmapsZonering
|
|||||||
|
|
||||||
|
|
||||||
await _farmmapsApiService.DownloadItemAsync(outputItem.Code,
|
await _farmmapsApiService.DownloadItemAsync(outputItem.Code,
|
||||||
|
|
||||||
Path.Combine(DownloadFolder, $"{input.OutputFileName}.zip"));
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.zip"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Functions to save previously created cropfields
|
private void LoadSettings()
|
||||||
private void LoadSettings(string file) {
|
{
|
||||||
if (File.Exists(file)) {
|
if (File.Exists(SettingsFile))
|
||||||
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(string file) {
|
private void SaveSettings()
|
||||||
|
{
|
||||||
if (_settings == null)
|
if (_settings == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var json = JsonConvert.SerializeObject(_settings);
|
var json = JsonConvert.SerializeObject(_settings);
|
||||||
File.WriteAllText(file, json);
|
File.WriteAllText(SettingsFile, json);
|
||||||
}
|
|
||||||
private void SaveInfo(string file) {
|
|
||||||
if (_settings == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var json = JsonConvert.SerializeObject(_settings);
|
|
||||||
File.WriteAllText(file, json);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,71 +1,93 @@
|
|||||||
[
|
[
|
||||||
|
{
|
||||||
|
"InputItemOne": "data_9001.tif",
|
||||||
|
"InputItemTwo": "data_times_two_4326.tiff",
|
||||||
|
"Formula": "if [0] >= 1.28 then [1] else 0",
|
||||||
|
"CreatedLayerName": "Remove",
|
||||||
|
"CalculatedQuantity": "Nonsense",
|
||||||
|
"CalculatedUnit": "m&m",
|
||||||
|
|
||||||
//Formule kan meerdere inputs aan
|
"OutputFileName": "NonsenseRemove",
|
||||||
// Met blokhaken en een nummer specificeer je een input:
|
"CropFieldName": "Cropfield VRA Zonering",
|
||||||
//[0], [10]. etc...
|
"CreateNewCropfield": true,
|
||||||
// Aan de hand van de volgorde van input wordt momenteel bepaald welk nummer bij welk input hoort.
|
"CropYear": 2020,
|
||||||
//[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.
|
"geometryJson": {
|
||||||
//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]
|
"type": "Polygon",
|
||||||
//Functies: abs, min en max
|
"coordinates": [
|
||||||
//Constanten: pi en e
|
|
||||||
|
|
||||||
//if ([0] -[1])/ ([0] +[1]) < 0 then 0
|
[
|
||||||
//else if ([0] -[1])/ ([0] +[1]) > 1 then 1
|
[ 5.670991253771027, 52.796788997702613 ],
|
||||||
//else ([0] -[1]) / ([0] +[1])
|
[ 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 ]
|
||||||
|
|
||||||
//{
|
]
|
||||||
// "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": {
|
|
||||||
// "type": "Polygon",
|
|
||||||
// "coordinates": [
|
|
||||||
|
|
||||||
// [
|
|
||||||
// [ 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": "if [0] >= 1.28 then [1] else 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 ]
|
|
||||||
]
|
|
||||||
]
|
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
//{
|
||||||
|
// "InputItemOne": "sentinel2_L2A_B04.tiff",
|
||||||
|
// "InputItemTwo": "sentinel2_L2A_B08.tiff",
|
||||||
|
// //"InputItemOne": "data_9001.tif",
|
||||||
|
// //"InputItemTwo": "data_times_two_4326.tiff",
|
||||||
|
// "Formula": "([1]-[0])/([1]+[0])",
|
||||||
|
// "CreatedLayerName": "Biomassa",
|
||||||
|
// "CalculatedQuantity": "NDVI",
|
||||||
|
// "CalculatedUnit": "ndviValue",
|
||||||
|
|
||||||
|
// "OutputFileName": "FullField_NDVI",
|
||||||
|
// "CropFieldName": "FullField",
|
||||||
|
// "CreateNewCropfield": true,
|
||||||
|
// "CropYear": 2020,
|
||||||
|
// "geometryJson": {
|
||||||
|
// "type": "Polygon",
|
||||||
|
// "coordinates": [
|
||||||
|
|
||||||
|
// [
|
||||||
|
// [ 4.958640119816389, 52.80350324727047 ],
|
||||||
|
// [ 4.958730764622494, 52.793849575420225 ],
|
||||||
|
// [ 4.974049736854337, 52.793804253017171 ],
|
||||||
|
// [ 4.973959092048232, 52.803457924867416 ],
|
||||||
|
// [ 4.958640119816389, 52.80350324727047 ]
|
||||||
|
|
||||||
|
// ]
|
||||||
|
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
//},
|
||||||
|
|
||||||
|
//{
|
||||||
|
|
||||||
|
// "InputItemOne": "Points.json",
|
||||||
|
// "Formula": "[0]*100",
|
||||||
|
// "CreatedLayerName": "PointsX100",
|
||||||
|
// "CalculatedQuantity": "NDVI",
|
||||||
|
// "CalculatedUnit": "PointsX100",
|
||||||
|
|
||||||
|
// "OutputFileName": "PointsX100",
|
||||||
|
// "CropFieldName": "PointsField",
|
||||||
|
// "CreateNewCropfield": 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 ]
|
||||||
|
// ]
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user