Merge branch 'feature/zonering'

This commit is contained in:
Mark van der Wal 2020-11-17 17:00:01 +01:00
commit 8c104f5eee
12 changed files with 111 additions and 21 deletions

View File

@ -16,6 +16,7 @@ namespace FarmmapsApiSamples
public const string VRAHERBICIDE_TASK = "vnd.farmmaps.task.vraherbicide";
public const string VRAHAULMKILLING_TASK = "vnd.farmmaps.task.vrahaulmkilling";
public const string VRAPLANTING_TASK = "vnd.farmmaps.task.vrapoten";
public const string VRAZONERING_TASK = "vnd.farmmaps.task.vrazonering";
public const string SATELLITE_TASK = "vnd.farmmaps.task.satellite";
public const string TASKMAP_TASK = "vnd.farmmaps.task.taskmap";
public const string WORKFLOW_TASK = "vnd.farmmaps.task.workflow";

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,8 @@
{
"type": "FeatureCollection",
"name": "neo_pg",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1 }, "geometry": { "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 ] ] ] } }
]
}

Binary file not shown.

View File

@ -21,4 +21,8 @@
<ProjectReference Include="..\FarmmapsApi\FarmmapsApi.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Data" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,8 @@
namespace FarmmapsHaulmkilling.Models
{
public class InputParameter
{
public string ItemCode { get; set; }
public string LayerName { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace FarmmapsZonering.Models
{
public class Output
{
public string Name { get; set; }
public string Quantity { get; set; }
public string Unit { get; set; }
}
}

View File

@ -3,6 +3,5 @@ namespace FarmmapsHaulmkilling.Models
public class Settings
{
public string CropfieldItemCode { get; set; }
public string SatelliteTaskCode { get; set; }
}
}

View File

@ -1,5 +1,13 @@
using System;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
using FarmmapsHaulmkilling.Models;
using FarmmapsZonering.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using static FarmmapsApi.Extensions;
using static FarmmapsApiSamples.Constants;
namespace FarmmapsZonering.Services
{
@ -16,5 +24,44 @@ namespace FarmmapsZonering.Services
_farmmapsApiService = farmmapsApiService;
_generalService = generalService;
}
public async Task<Item> CreateApplicationMapAsync(Item cropfieldItem, string formula, Output output, params InputParameter[] inputItemCodes)
{
var zoneringTaskRequest = new TaskRequest() {TaskType = VRAZONERING_TASK};
zoneringTaskRequest.attributes["formula"] = formula;
zoneringTaskRequest.attributes["output"] = JsonConvert.SerializeObject(output);
zoneringTaskRequest.attributes["inputs"] = JsonConvert.SerializeObject(inputItemCodes);
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 calculation of application map; 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()));
if (applianceMapItem == null)
{
_logger.LogError("Could not find the VRAZonering geotiff child item under cropfield");
return null;
}
return applianceMapItem;
}
}
}

View File

@ -5,6 +5,7 @@ using FarmmapsApi;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
using FarmmapsHaulmkilling.Models;
using FarmmapsZonering.Models;
using FarmmapsZonering.Services;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
@ -63,7 +64,7 @@ namespace FarmmapsZonering
{
_logger.LogInformation("Creating cropfield");
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Zonering", 2020,
@"{""type"":""Polygon"",""coordinates"":[[[4.617786844284247,52.22533706956424],[4.618642601314543,52.225938364585989],[4.6192153806397,52.22563988897754],[4.619192414656403,52.2256242822442],[4.620306732153958,52.225031745661528],[4.620542019225217,52.22519855319158],[4.621157509147853,52.22487436515405],[4.623387917230182,52.22367660757213],[4.624563444939009,52.22304740241544],[4.624562779355982,52.223046635247019],[4.624534908813479,52.22302596787506],[4.627873021330343,52.221240670658399],[4.627504935938338,52.220104419135129],[4.627324878706837,52.22020569669098],[4.627320696113512,52.22020660117888],[4.626707169518044,52.22053923770041],[4.624700376420229,52.221619047547488],[4.623471571183885,52.22227447969577],[4.623471511010673,52.22227500174403],[4.623468838689317,52.22228052566992],[4.617786844284247,52.22533706956424]]]}");
@"{ ""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();
}
@ -72,27 +73,40 @@ namespace FarmmapsZonering
_logger.LogInformation("Cropfield already exists trying to get");
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
}
var inputOneItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE,
Path.Combine("Data", "data_9001.tif"),"data_9001");
if (inputOneItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
var inputTwoItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE,
Path.Combine("Data", "data_times_two_4326.tiff"), "data_times_two_4326");
if (inputTwoItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
ItemTaskStatus taskStatus;
if (string.IsNullOrEmpty(_settings.SatelliteTaskCode))
{
_logger.LogInformation("Gathering satellite information for cropfield, this might take a while!");
taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, SATELLITE_TASK, null, 20);
if (taskStatus.State == ItemTaskState.Error)
var outputItem = await _zoneringService.CreateApplicationMapAsync(cropfieldItem, "[0] + [1]", new Output()
{
_logger.LogError($"Something went wrong when trying to process satellite data; {taskStatus.Message}");
return;
}
_settings.SatelliteTaskCode = taskStatus.Code;
SaveSettings();
}
else
{
taskStatus = await _farmmapsApiService.GetTaskStatusAsync(_settings.CropfieldItemCode, _settings.SatelliteTaskCode);
}
Name = "add",
Unit = "n/kg",
Quantity = "Nitrogen"
}, new InputParameter()
{
ItemCode = inputOneItem.Code,
LayerName = inputOneItem.Data["layers"][0]["name"].ToString()
},
new InputParameter()
{
ItemCode = inputTwoItem.Code,
LayerName = inputTwoItem.Data["layers"][0]["name"].ToString()
});
_logger.LogInformation("Downloading output");
await _farmmapsApiService.DownloadItemAsync(outputItem.Code,
Path.Combine(DownloadFolder, $"times_2_zonering.zip"));
}
private void LoadSettings()