Added Taskmap sample code to potenAPI

master
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) {

View File

@ -12,9 +12,6 @@
<None Update="Data\**\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="PotenInput.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>

View File

@ -5,14 +5,23 @@ namespace FarmmapsPoten.Models
{
public class PotenInput
{
public bool UseShadow { get; set; }
public string File { get; set; }
public string OutputFileName { get; set; }
public string FieldName { get; set; }
public int PlantingYear { get; set; }
public string MeanDensity { get; set; }
public string Variation { get; set; }
public bool UseShadow { get; set; }
public bool ConvertToCountPerArea { get; set; }
public float Rijbreedte_m { get; set; }
public JObject GeometryJson { get; set; }
public bool GenerateTaskmap { get; set; }
public string CellWidth { get; set; }
public string CellHeight { get; set; }
public JObject StartPoint { get; set; }
public JObject EndPoint { get; set; }
public string Angle { get; set; }
}
}

View File

@ -58,6 +58,8 @@ namespace FarmmapsVRApoten
var variation = input.Variation;
var fieldName = input.FieldName;
bool useShadow = input.UseShadow;
bool convertToCountPerArea = input.ConvertToCountPerArea;
float rijBreedte_m = input.Rijbreedte_m;
var myDrive = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDrive == null) {
@ -94,7 +96,8 @@ namespace FarmmapsVRApoten
//Calculating AHN map
_logger.LogInformation("retreiving AHN map for field");
var AHNItem = await _generalService.RunAhnTask(cropfieldItem);
if (AHNItem == null) {
if (AHNItem == null)
{
_logger.LogError("Something went wrong while obtaining the AHN map");
return;
}
@ -160,6 +163,8 @@ namespace FarmmapsVRApoten
Path.Combine(DownloadFolder, $"VRApoten_inputGeotiff_{input.OutputFileName}.zip"));
}
// create appliance map
_logger.LogInformation("Calculating application map");
@ -180,13 +185,35 @@ namespace FarmmapsVRApoten
? "Download application map completed."
: "Something went wrong while downloading.");
_logger.LogInformation($"Converting geotiff to shape");
var taskmap = await _generalService.GeotiffToShape(applianceMapItem);
if (taskmap == null) {
// if convertToCountPerArea == True, than recalculate pootafstand in cm to # of poters/m2 from the geotiffItem with the use of the zoneringsTask
if (convertToCountPerArea)
{
applianceMapItem =
await _potenService.ConvertToCountPerAreaTroughZonering(cropfieldItem, applianceMapItem, input.Rijbreedte_m);
}
//GEOTIFF TO Taskmap
_logger.LogInformation($"Converting geotiff to taskmap");
var taskmap = await _generalService.CreateTaskmap(applianceMapItem, input.CellWidth, input.CellHeight, input.StartPoint.ToString(Formatting.None),
input.EndPoint.ToString(Formatting.None), input.Angle);
if (taskmap == null)
{
_logger.LogError("Something went wrong with geotiff to shape transformation");
return;
}
////GEOTIFF TO SHAPE
//_logger.LogInformation($"Converting geotiff to shape");
//var geotiffToShapeItem= await _generalService.GeotiffToShape(applianceMapItem);
//if (taskmap == null) {
// _logger.LogError("Something went wrong with geotiff to shape transformation");
// return;
//}
_logger.LogInformation("Downloading taskmap");
await _farmmapsApiService.DownloadItemAsync(taskmap.Code,
Path.Combine(DownloadFolder, $"VRApoten_taskmap_{input.OutputFileName}.zip"));

View File

@ -1,24 +1,41 @@
[
{
"File": "PlantingSampleDataLutum.zip",
//"File": "Lutum_SampleDataPlanting.zip",
"OutputFileName": "vraPoten_SampleData",
"FieldName": "lutum",
"PlantingYear": 2020,
"MeanDensity": "30",
"Variation": "20",
"UseShadow": false,
"geometryJson": {
"type": "Polygon",
"coordinates": [
[
[ 5.66886041703652044, 52.52929999060298627 ],
[ 5.6716230923214912, 52.52946316399909676 ],
[ 5.67185376229668581, 52.5280565894154563 ],
[ 5.66903207841337231, 52.52790646510525363 ],
[ 5.66886041703652044, 52.52929999060298627 ]
]
]
{
"File": "PlantingSampleDataLutum.zip",
//"File": "Lutum_SampleDataPlanting.zip",
"OutputFileName": "20210212_vraPoten_SampleData_TASKMAP_ENDPOINT",
"FieldName": "lutum",
"PlantingYear": 2020,
"MeanDensity": "30",
"Variation": "20",
"UseShadow": false,
"ConvertToCountPerArea": false,
"Rijbreedte_m": 0.75,
"geometryJson": {
"type": "Polygon",
"coordinates": [
[
[ 5.66886041703652044, 52.52929999060298627 ],
[ 5.6716230923214912, 52.52946316399909676 ],
[ 5.67185376229668581, 52.5280565894154563 ],
[ 5.66903207841337231, 52.52790646510525363 ],
[ 5.66886041703652044, 52.52929999060298627 ]
]
]
},
"GenerateTaskmap": true,
"CellWidth": "3",
"CellHeight": "10",
"StartPoint": {
"type": "Point",
"coordinates": [ 5.66886041703652044, 52.52929999060298627 ]
},
"EndPoint": {
"type": "Point",
"coordinates": [ 5.6716230923214912, 52.52946316399909676 ]
} // if no angle
//"Angle": "317.0" // if no endpoint
}
}
]

View File

@ -82,10 +82,52 @@ namespace FarmmapsVRApoten
return applianceMapItem;
}
}
}
// Extra task making use of the zonering task to convert the planting distance in cm to number of seeds per m2
public async Task<Item> ConvertToCountPerAreaTroughZonering(Item cropfieldItem, Item geotiffItem, float rijBreedte_m)
{
var zoneringTaskRequest = new TaskRequest() { TaskType = VRAZONERING_TASK };
zoneringTaskRequest.attributes["formula"] = $"((100/[0])/{rijBreedte_m.ToString()})";
zoneringTaskRequest.attributes["output"] = "{\"Name\":\"CountPerAreaConversion\",\"Quantity\":\"CountPerArea\",\"Unit\":\"#/m2\"}";
zoneringTaskRequest.attributes["inputs"] = $"{{\"ItemCode\":{geotiffItem.Code},\"LayerName\":null\"}}";
var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, zoneringTaskRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
_logger.LogInformation($"Waiting on convertion to Count per area through zoneringTast; Status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished)
tokenSource.Cancel();
});
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
if (itemTask.State == ItemTaskState.Error)
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null;
}
var itemName = $"VRAZonering";
var applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
//i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
// i.Name.ToLower().Contains(itemName.ToLower()));
i =>
i.Name.ToLower().Contains(itemName.ToLower()));
if (applianceMapItem == null)
{
_logger.LogError("Could not find the converted to count per area geotiff child item under cropfield");
return null;
}
return applianceMapItem;
}
}
}