Compare commits
3 Commits
594c1507c3
...
5f1073b80c
Author | SHA1 | Date | |
---|---|---|---|
5f1073b80c | |||
e8e31e077d | |||
07601a321f |
@ -3,11 +3,16 @@ namespace FarmmapsApiSamples
|
||||
public static class Constants
|
||||
{
|
||||
public const string USERINPUT_ITEMTYPE = "vnd.farmmaps.itemtype.user.input";
|
||||
public const string TEMPORAL_ITEMTYPE = "vnd.farmmaps.itemtype.temporal";
|
||||
public const string GEOTIFF_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff";
|
||||
public const string GEOTIFF_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff.processed";
|
||||
public const string CROPFIELD_ITEMTYPE = "vnd.farmmaps.itemtype.cropfield";
|
||||
public const string SHAPE_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.shape.processed";
|
||||
public const string SHAPE_ITEMTYPE = "vnd.farmmaps.itemtype.shape";
|
||||
|
||||
public const string VRANBS_TASK = "vnd.farmmaps.task.vranbs";
|
||||
public const string VRAHERBICIDE_TASK = "vnd.farmmaps.task.vraherbicide";
|
||||
public const string VRAHAULMKILLING_TASK = "vnd.farmmaps.task.vrahaulmkilling";
|
||||
public const string SATELLITE_TASK = "vnd.farmmaps.task.satellite";
|
||||
}
|
||||
}
|
@ -96,27 +96,37 @@ namespace FarmmapsApi.Services
|
||||
|
||||
public async Task<Item> ShapeToGeotiff(Item shapeItem)
|
||||
{
|
||||
var shapeToGeotiffRequest = new TaskRequest()
|
||||
{
|
||||
TaskType = "vnd.farmmaps.task.shapetogeotiff"
|
||||
};
|
||||
var taskCode = await _farmmapsApiService.QueueTaskAsync(shapeItem.Code, shapeToGeotiffRequest);
|
||||
|
||||
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
|
||||
{
|
||||
_logger.LogInformation("Checking shapetogeotiff task status");
|
||||
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(shapeItem.Code, taskCode);
|
||||
if (itemTaskStatus.IsFinished)
|
||||
tokenSource.Cancel();
|
||||
});
|
||||
|
||||
_logger.LogInformation("Data shape converted to geotiff");
|
||||
await RunAndWaitForTask(shapeItem, "vnd.farmmaps.task.shapetogeotiff");
|
||||
|
||||
// the parent of the shape item is now the tiff item
|
||||
shapeItem = await _farmmapsApiService.GetItemAsync(shapeItem.Code);
|
||||
return await _farmmapsApiService.GetItemAsync(shapeItem.ParentCode);
|
||||
}
|
||||
|
||||
public async Task<ItemTaskStatus> RunAndWaitForTask(Item subjectItem, string taskIdentifier,
|
||||
Action<TaskRequest> configureCallback = null, int retrySeconds = 3)
|
||||
{
|
||||
var taskRequest = new TaskRequest()
|
||||
{
|
||||
TaskType = taskIdentifier
|
||||
};
|
||||
configureCallback?.Invoke(taskRequest);
|
||||
|
||||
var taskCode = await _farmmapsApiService.QueueTaskAsync(subjectItem.Code, taskRequest);
|
||||
|
||||
await PollTask(TimeSpan.FromSeconds(retrySeconds), async (tokenSource) =>
|
||||
{
|
||||
_logger.LogInformation($"Checking {taskIdentifier} task status");
|
||||
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(subjectItem.Code, taskCode);
|
||||
if (itemTaskStatus.IsFinished)
|
||||
tokenSource.Cancel();
|
||||
});
|
||||
|
||||
_logger.LogInformation($"{taskIdentifier} finished");
|
||||
|
||||
return await _farmmapsApiService.GetTaskStatusAsync(subjectItem.Code, taskCode);
|
||||
}
|
||||
|
||||
public async Task<Item> FindChildItemAsync(string parentCode, string itemType, string containsName,
|
||||
Func<Item, bool> filter = null, int maxTries = 10)
|
||||
{
|
||||
|
@ -2,19 +2,27 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using FarmmapsApi;
|
||||
using FarmmapsApi.Models;
|
||||
using FarmmapsApi.Services;
|
||||
using FarmmapsHaulmkilling.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using static FarmmapsApiSamples.Constants;
|
||||
|
||||
namespace FarmmapsHaulmkilling
|
||||
{
|
||||
public class HaulmkillingApplication : IApplication
|
||||
{
|
||||
private const string DownloadFolder = "Downloads";
|
||||
private const string SettingsFile = "settings.json";
|
||||
|
||||
private readonly ILogger<HaulmkillingApplication> _logger;
|
||||
private readonly FarmmapsApiService _farmmapsApiService;
|
||||
private readonly HaulmkillingService _haulmkillingService;
|
||||
private readonly GeneralService _generalService;
|
||||
|
||||
private Settings _settings;
|
||||
|
||||
public HaulmkillingApplication(ILogger<HaulmkillingApplication> logger, FarmmapsApiService farmmapsApiService,
|
||||
GeneralService generalService, HaulmkillingService haulmkillingService)
|
||||
@ -29,6 +37,8 @@ namespace FarmmapsHaulmkilling
|
||||
{
|
||||
if (!Directory.Exists(DownloadFolder))
|
||||
Directory.CreateDirectory(DownloadFolder);
|
||||
|
||||
LoadSettings();
|
||||
|
||||
// !! this call is needed the first time an api is called with a fresh clientid and secret !!
|
||||
await _farmmapsApiService.GetCurrentUserCodeAsync();
|
||||
@ -55,15 +65,88 @@ namespace FarmmapsHaulmkilling
|
||||
return;
|
||||
}
|
||||
|
||||
// save cropfield item and check if already exists and created (this to not have to run satellite everytime)
|
||||
_logger.LogInformation("Creating cropfield");
|
||||
var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Haulmkilling", 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]]]}");
|
||||
Item cropfieldItem;
|
||||
if (string.IsNullOrEmpty(_settings.CropfieldItemCode))
|
||||
{
|
||||
_logger.LogInformation("Creating cropfield");
|
||||
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Haulmkilling", 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]]]}");
|
||||
_settings.CropfieldItemCode = cropfieldItem.Code;
|
||||
SaveSettings();
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Cropfield already exists trying to get");
|
||||
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
_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);
|
||||
}
|
||||
|
||||
// run satellite task on just added cropfield
|
||||
|
||||
|
||||
// find ndvi or wdvi satellite data geotiffs
|
||||
var temporalITem = await _generalService.FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE,
|
||||
"Cropfield VRA Haulmkilling, Satellite", item => item.SourceTask == SATELLITE_TASK &&
|
||||
taskStatus.Finished >= item.Created &&
|
||||
taskStatus.Finished <= item.Created.Value.AddHours(1));
|
||||
|
||||
if (temporalITem == null)
|
||||
{
|
||||
_logger.LogError($"Temporal item not found");
|
||||
return;
|
||||
}
|
||||
|
||||
var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalITem.Code);
|
||||
var firstSatelliteItem = satelliteTiffs.FirstOrDefault();
|
||||
|
||||
if (firstSatelliteItem == null)
|
||||
{
|
||||
_logger.LogError($"Satellite item not found");
|
||||
return;
|
||||
}
|
||||
|
||||
// create haulmkilling application map
|
||||
|
||||
// download application map
|
||||
}
|
||||
|
||||
private void LoadSettings()
|
||||
{
|
||||
if (File.Exists(SettingsFile))
|
||||
{
|
||||
var jsonText = File.ReadAllText(SettingsFile);
|
||||
_settings = JsonConvert.DeserializeObject<Settings>(jsonText);
|
||||
}
|
||||
else
|
||||
{
|
||||
_settings = new Settings();
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveSettings()
|
||||
{
|
||||
if (_settings == null)
|
||||
return;
|
||||
|
||||
var json = JsonConvert.SerializeObject(_settings);
|
||||
File.WriteAllText(SettingsFile, json);
|
||||
}
|
||||
}
|
||||
}
|
8
FarmmapsHaulmkilling/Models/Settings.cs
Normal file
8
FarmmapsHaulmkilling/Models/Settings.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace FarmmapsHaulmkilling.Models
|
||||
{
|
||||
public class Settings
|
||||
{
|
||||
public string CropfieldItemCode { get; set; }
|
||||
public string SatelliteTaskCode { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user