Fixed getting isaria data. Fixed cropfield creation.

This commit is contained in:
Mark van der Wal 2020-03-26 09:43:15 +01:00
parent 691be2185e
commit c54d0632ee
4 changed files with 96 additions and 43 deletions

View File

@ -4,8 +4,10 @@ namespace FarmmapsApiSamples
{ {
public const string USERINPUT_ITEMTYPE = "vnd.farmmaps.itemtype.user.input"; public const string USERINPUT_ITEMTYPE = "vnd.farmmaps.itemtype.user.input";
public const string GEOTIFF_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff"; 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 CROPFIELD_ITEMTYPE = "vnd.farmmaps.itemtype.cropfield";
public const string SHAPE_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.shape.processed"; 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 VRANBS_TASK = "vnd.farmmaps.task.vranbs";
} }
} }

View File

@ -39,11 +39,14 @@ namespace FarmmapsApiSamples
try try
{ {
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync(); var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
// upload data to Uploaded
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded"); var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot != null) var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
if (uploadedRoot == null || myDriveRoot == null)
{ {
_logger.LogError("Could not find a needed root item");
return;
}
var dataPath = Path.Combine("Data", "Scan_1_20190605.zip"); var dataPath = Path.Combine("Data", "Scan_1_20190605.zip");
var result = await _farmmapsApiService.UploadFile(dataPath, uploadedRoot.Code, var result = await _farmmapsApiService.UploadFile(dataPath, uploadedRoot.Code,
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}")); progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
@ -54,34 +57,21 @@ namespace FarmmapsApiSamples
return; return;
} }
// find file we need to use, we use delay as hack until events work correctly var isariaGeotiffItem = await _nitrogenService.ProcessIsaria(uploadedRoot.Code,"Scan_1_20190605.zip");
// await Task.Delay(10000);
// var isariaDataFilter = JObject.Parse(@""); // create and calculate target N
// var uploadedFilesChildren = await var cropfieldItem = await GetOrCreateCropfieldItemAsync(myDriveRoot.Code);
// _farmmapsApiService.GetItemChildrenAsync(uploadedRoot.Code, SHAPE_PROCESSED_ITEMTYPE);
// need to transform shape data to geotiff
// var shapeToGeotiffRequest = new TaskRequest()
// {
// TaskType = "vnd.farmmaps.task.shapetogeotiff"
// };
// var taskCode = await _farmmapsApiService.QueueTaskAsync(, shapeToGeotiffRequest);
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDriveRoot != null)
{
var cropfieldItem = await GetOrCreateCropfieldItem(myDriveRoot.Code);
_logger.LogInformation($"Calculating targetN with targetYield: {60}"); _logger.LogInformation($"Calculating targetN with targetYield: {60}");
var targetN = await _nitrogenService.CalculateTargetN(cropfieldItem, 60); var targetN = await _nitrogenService.CalculateTargetN(cropfieldItem, 60);
_logger.LogInformation($"TargetN: {targetN}"); _logger.LogInformation($"TargetN: {targetN}");
_logger.LogInformation("Calculating nitrogen map"); _logger.LogInformation("Calculating nitrogen map");
// var nitrogenMapItem = CalculateNitrogenMap(cropfieldItem,, targetN); var nitrogenMapItem =
} await _nitrogenService.CalculateNitrogenMap(cropfieldItem, isariaGeotiffItem, targetN);
}
_logger.LogInformation("Downloading nitrogen map");
await _farmmapsApiService.DownloadItemAsync(nitrogenMapItem.Code, $"{nitrogenMapItem.Name}.zip");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -89,7 +79,7 @@ namespace FarmmapsApiSamples
} }
} }
private async Task<Item> GetOrCreateCropfieldItem(string parentItemCode) private async Task<Item> GetOrCreateCropfieldItemAsync(string parentItemCode)
{ {
var cropfieldItems = await var cropfieldItems = await
_farmmapsApiService.GetItemChildrenAsync(parentItemCode, CROPFIELD_ITEMTYPE); _farmmapsApiService.GetItemChildrenAsync(parentItemCode, CROPFIELD_ITEMTYPE);
@ -101,7 +91,7 @@ namespace FarmmapsApiSamples
var cropfieldItemRequest = new ItemRequest() var cropfieldItemRequest = new ItemRequest()
{ {
ParentCode = parentItemCode, ParentCode = parentItemCode,
ItemType = "vnd.farmmaps.itemtype.cropfield", ItemType = CROPFIELD_ITEMTYPE,
Name = "Cropfield for VRA", Name = "Cropfield for VRA",
DataDate = currentYear, DataDate = currentYear,
DataEndDate = currentYear.AddYears(1), DataEndDate = currentYear.AddYears(1),

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using FarmmapsApi.Models; using FarmmapsApi.Models;
@ -21,6 +23,63 @@ namespace FarmmapsApiSamples
_farmmapsApiService = farmmapsApiService; _farmmapsApiService = farmmapsApiService;
} }
public async Task<Item> ProcessIsaria(string parentCode, string zipName)
{
// get processed isaria data (this can end in an infinite loop...)
// need better way of getting new items processed in a context...
Item isariaShapeItem = null;
await PollTask(TimeSpan.FromSeconds(3), async source =>
{
_logger.LogInformation("Trying to get isaria data");
var uploadedFilesChildren = await _farmmapsApiService.GetItemChildrenAsync(parentCode);
var zipItems = uploadedFilesChildren.Where(i => i.Name == zipName);
var isariaTasks = new List<Task<List<Item>>>();
foreach (var zipItem in zipItems)
{
isariaTasks.Add(_farmmapsApiService.GetItemChildrenAsync(zipItem.Code,
SHAPE_PROCESSED_ITEMTYPE));
}
List<Item>[] items = await Task.WhenAll(isariaTasks);
if (items.Length > 0)
{
var nameWithoutExtension = Path.GetFileNameWithoutExtension(zipName);
var flatItems = items.SelectMany(i => i).Where(i => i.Name.Contains(nameWithoutExtension)).ToList();
if (flatItems.Count > 0)
{
isariaShapeItem = flatItems.OrderByDescending(i => i.Created).First();
source.Cancel();
}
}
});
_logger.LogInformation("Found isaria data");
_logger.LogInformation("Converting shape to geotiff");
// need to transform shape data to geotiff
var shapeToGeotiffRequest = new TaskRequest()
{
TaskType = "vnd.farmmaps.task.shapetogeotiff"
};
var taskCode = await _farmmapsApiService.QueueTaskAsync(isariaShapeItem.Code, shapeToGeotiffRequest);
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
{
_logger.LogInformation("Checking shapetogeotiff task status");
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(isariaShapeItem.Code, taskCode);
if (itemTaskStatus.State != ItemTaskState.Processing && itemTaskStatus.State != ItemTaskState.Scheduled)
tokenSource.Cancel();
});
_logger.LogInformation("Isaria shape converted to geotiff");
// the parent of the shape item is now the tiff isaria item
isariaShapeItem = await _farmmapsApiService.GetItemAsync(isariaShapeItem.Code);
return await _farmmapsApiService.GetItemAsync(isariaShapeItem.ParentCode); ;
}
/// <summary> /// <summary>
/// Calculates TargetN, makes the assumption the cropfield and user.input(targetn) item have the same parent /// Calculates TargetN, makes the assumption the cropfield and user.input(targetn) item have the same parent
/// </summary> /// </summary>

View File

@ -1,3 +1,5 @@
##### NOT PRODUCTION READY CODE, JUST AN EXAMPLE
Put your clientId and clientSecret in the appsettings.json. Put your clientId and clientSecret in the appsettings.json.
* Needs upload API. * Needs upload API.