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

@@ -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;
}
}
}