Added Taskmap sample code to potenAPI

This commit is contained in:
Riepma
2021-02-12 17:10:25 +01:00
parent a29b973268
commit c02b232837
6 changed files with 170 additions and 34 deletions

View File

@@ -38,7 +38,7 @@ namespace FarmmapsApi.Services
}
public async Task<Item> UploadDataAsync(UserRoot root, string itemType, string filePath, string itemName) {
var startUpload = DateTime.UtcNow;
var startUpload = DateTime.UtcNow.AddSeconds(-3);
var result = await _farmmapsApiService.UploadFile(filePath, root.Code,
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
@@ -74,6 +74,10 @@ namespace FarmmapsApi.Services
public async Task<Item> GeotiffToShape(Item tiffItem) {
var taskmapRequest = new TaskRequest { TaskType = TASKMAP_TASK };
taskmapRequest.attributes["cellWidth"] = "3";
taskmapRequest.attributes["cellHeight"] = "1";
taskmapRequest.attributes["angle"] = "0";
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(tiffItem.Code, taskmapRequest);
@@ -92,7 +96,7 @@ namespace FarmmapsApi.Services
//the taskmap is a child of the input tiff
var itemName = "Taskmap";
var taskMapItem = await FindChildItemAsync(tiffItem.Code,
var taskMapItem = await FindChildItemAsync(tiffItem.ParentCode,
SHAPE_PROCESSED_ITEMTYPE, itemName);
if (taskMapItem == null) {
_logger.LogError("Could not find the shape taskmap as a child item under the input");
@@ -102,6 +106,46 @@ namespace FarmmapsApi.Services
return taskMapItem;
}
// Create taskmap based on width, height and direction
public async Task<Item> CreateTaskmap(Item tiffItem, string cellWidth, string cellHeight, string startPoint, string endPoint = null, string angle = null)
{
var taskmapRequest = new TaskRequest { TaskType = TASKMAP_TASK };
taskmapRequest.attributes["inputCode"] = tiffItem.Code;
taskmapRequest.attributes["cellWidth"] = cellWidth; //metres
taskmapRequest.attributes["cellHeight"] = cellHeight; //metres
taskmapRequest.attributes["startPoint"] = startPoint; // Coordinates WGS84
if (angle == null) taskmapRequest.attributes["endPoint"] = endPoint; // Coordinates WGS84
if (endPoint == null) taskmapRequest.attributes["angle"] = angle; // degrees between 0.0 and 360.0
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(tiffItem.Code, taskmapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => {
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(tiffItem.Code, itemTaskCode);
_logger.LogInformation($"Waiting on conversion to Taskmap; status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished)
tokenSource.Cancel();
});
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(tiffItem.Code, itemTaskCode);
if (itemTask.State == ItemTaskState.Error)
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null;
}
//the taskmap is a child of the input tiff
var itemName = "Taskmap";
var taskMapItem = await FindChildItemAsync(tiffItem.ParentCode,
SHAPE_PROCESSED_ITEMTYPE, itemName);
if (taskMapItem == null)
{
_logger.LogError("Could not find the shape taskmap as a child item under the input");
return null;
}
return taskMapItem;
}
public async Task<ItemTaskStatus> RunAndWaitForTask(Item subjectItem, string taskIdentifier,
Action<TaskRequest> configureCallback = null, int retrySeconds = 3) {