forked from FarmMaps/FarmMapsApiClient
Refactored some nitrogen methods.
This commit is contained in:
@@ -50,9 +50,10 @@ namespace FarmmapsApiSamples
|
||||
return;
|
||||
}
|
||||
|
||||
var cropfieldItem = await GetOrCreateCropfieldItemAsync(myDriveRoot.Code);
|
||||
|
||||
var dataPath = Path.Combine("Data", "Scan_1_20190605.zip");
|
||||
var cropfieldItem = await CreateCropfieldItemAsync(myDriveRoot.Code, "VRA NBS cropfield", 2020,
|
||||
@"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] }");
|
||||
|
||||
var dataPath = Path.Combine("Data", "Nbs", "Scan_1_20190605.zip");
|
||||
var isariaShapeItem = await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, "Scan_1_20190605");
|
||||
if (isariaShapeItem == null)
|
||||
{
|
||||
@@ -60,6 +61,7 @@ namespace FarmmapsApiSamples
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation($"Converting shape to geotiff");
|
||||
var isariaGeotiffItem = await _generalService.ShapeToGeotiff(isariaShapeItem);
|
||||
if (isariaGeotiffItem == null)
|
||||
{
|
||||
@@ -68,7 +70,8 @@ namespace FarmmapsApiSamples
|
||||
}
|
||||
|
||||
_logger.LogInformation($"Calculating targetN with targetYield: {60}");
|
||||
var targetN = await CalculateTargetN(cropfieldItem, plantingDate, measurementDate, 60);
|
||||
var targetNItem = await CreateTargetNItem(cropfieldItem);
|
||||
var targetN = await CalculateTargetN(cropfieldItem, targetNItem, plantingDate, measurementDate, "consumption", "irmi", 60);
|
||||
_logger.LogInformation($"TargetN: {targetN}");
|
||||
|
||||
_logger.LogInformation("Calculating uptake map");
|
||||
@@ -80,35 +83,39 @@ namespace FarmmapsApiSamples
|
||||
|
||||
_logger.LogInformation("Calculating appliance map");
|
||||
var applianceMapItem =
|
||||
await CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate, measurementDate, targetN);
|
||||
await CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate, measurementDate, "irmi", targetN);
|
||||
|
||||
_logger.LogInformation("Downloading appliance map");
|
||||
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(downloadFolder, $"{applianceMapItem.Name}.zip"));
|
||||
}
|
||||
|
||||
private async Task<Item> GetOrCreateCropfieldItemAsync(string parentItemCode)
|
||||
private async Task<Item> CreateCropfieldItemAsync(string parentItemCode, string name, int year, string fieldGeomJson)
|
||||
{
|
||||
var cropfieldItems = await
|
||||
_farmmapsApiService.GetItemChildrenAsync(parentItemCode, CROPFIELD_ITEMTYPE);
|
||||
|
||||
if (cropfieldItems.Count > 0)
|
||||
return cropfieldItems[0];
|
||||
|
||||
var currentYear = new DateTime(DateTime.UtcNow.Year, 1, 1);
|
||||
var currentYear = new DateTime(year, 1, 1);
|
||||
var cropfieldItemRequest = new ItemRequest()
|
||||
{
|
||||
ParentCode = parentItemCode,
|
||||
ItemType = CROPFIELD_ITEMTYPE,
|
||||
Name = "Cropfield for VRA",
|
||||
Name = name,
|
||||
DataDate = currentYear,
|
||||
DataEndDate = currentYear.AddYears(1).AddDays(-1),
|
||||
Data = JObject.Parse("{}"),
|
||||
Geometry = JObject.Parse(
|
||||
@"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] }")
|
||||
Geometry = JObject.Parse(fieldGeomJson)
|
||||
};
|
||||
|
||||
return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
|
||||
}
|
||||
|
||||
public async Task<Item> CreateTargetNItem(Item cropfieldItem)
|
||||
{
|
||||
var itemRequest = new ItemRequest()
|
||||
{
|
||||
ParentCode = cropfieldItem.ParentCode,
|
||||
ItemType = USERINPUT_ITEMTYPE,
|
||||
Name = "TargetN"
|
||||
};
|
||||
return await _farmmapsApiService.CreateItemAsync(itemRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates TargetN, makes the assumption the cropfield and user.input(targetn) item have the same parent
|
||||
@@ -118,31 +125,16 @@ namespace FarmmapsApiSamples
|
||||
/// <param name="plantingDate">The date the crop is planted</param>
|
||||
/// <param name="measurementDate">The date the measurements are taken</param>
|
||||
/// <returns>The TargetN</returns>
|
||||
public async Task<double> CalculateTargetN(Item cropfieldItem, DateTime plantingDate,
|
||||
DateTime measurementDate, int targetYield)
|
||||
public async Task<double> CalculateTargetN(Item cropfieldItem, Item targetNItem, DateTime plantingDate,
|
||||
DateTime measurementDate, string inputType, string purposeType, int targetYield)
|
||||
{
|
||||
var targetNItems = await
|
||||
_farmmapsApiService.GetItemChildrenAsync(cropfieldItem.ParentCode, USERINPUT_ITEMTYPE);
|
||||
|
||||
Item targetNItem;
|
||||
if (targetNItems.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("Creating targetN item");
|
||||
var itemRequest = CreateTargetNItemRequest(cropfieldItem.ParentCode);
|
||||
targetNItem = await _farmmapsApiService.CreateItemAsync(itemRequest);
|
||||
}
|
||||
else
|
||||
{
|
||||
targetNItem = targetNItems[0];
|
||||
}
|
||||
|
||||
var nbsTargetNRequest = new TaskRequest {TaskType = VRANBS_TASK};
|
||||
nbsTargetNRequest.attributes["operation"] = "targetn";
|
||||
nbsTargetNRequest.attributes["inputCode"] = targetNItem.Code;
|
||||
nbsTargetNRequest.attributes["plantingDate"] = plantingDate.ToString();
|
||||
nbsTargetNRequest.attributes["measurementDate"] = measurementDate.ToString();
|
||||
nbsTargetNRequest.attributes["inputType"] = "irmi";
|
||||
nbsTargetNRequest.attributes["purposeType"] = "consumption";
|
||||
nbsTargetNRequest.attributes["inputType"] = inputType;
|
||||
nbsTargetNRequest.attributes["purposeType"] = purposeType.ToLower();
|
||||
nbsTargetNRequest.attributes["targetYield"] = targetYield.ToString();
|
||||
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsTargetNRequest);
|
||||
|
||||
@@ -176,14 +168,14 @@ namespace FarmmapsApiSamples
|
||||
/// <param name="measurementDate">The date the measurements are taken</param>
|
||||
/// <returns></returns>
|
||||
public async Task<Item> CalculateUptakeMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,
|
||||
DateTime measurementDate)
|
||||
DateTime measurementDate, string inputType = "irmi")
|
||||
{
|
||||
var nbsUptakeMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
|
||||
nbsUptakeMapRequest.attributes["operation"] = "uptake";
|
||||
nbsUptakeMapRequest.attributes["inputCode"] = inputItem.Code;
|
||||
nbsUptakeMapRequest.attributes["plantingDate"] = plantingDate.ToString();
|
||||
nbsUptakeMapRequest.attributes["measurementDate"] = measurementDate.ToString();
|
||||
nbsUptakeMapRequest.attributes["inputType"] = "irmi";
|
||||
nbsUptakeMapRequest.attributes["inputType"] = inputType.ToLower();
|
||||
|
||||
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsUptakeMapRequest);
|
||||
|
||||
@@ -200,21 +192,19 @@ namespace FarmmapsApiSamples
|
||||
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
|
||||
return null;
|
||||
}
|
||||
|
||||
var geotiffItems = await
|
||||
_farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code, GEOTIFF_PROCESSED_ITEMTYPE);
|
||||
|
||||
// maybe poll here to, to wait for geotiff task process to be finished?
|
||||
|
||||
// how to uniquely know which item is really created!?
|
||||
var nitrogenItem = geotiffItems.SingleOrDefault(i => i.Name.Contains("uptake") && i.Updated >= itemTask.Finished);
|
||||
if (nitrogenItem == null)
|
||||
var itemName = "VRANbs uptake";
|
||||
var uptakeMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
|
||||
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
|
||||
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
|
||||
i.Name.ToLower().Contains(itemName.ToLower()));
|
||||
if (uptakeMapItem == null)
|
||||
{
|
||||
_logger.LogError("Could not find the uptake geotiff child item under cropfield");
|
||||
return null;
|
||||
}
|
||||
|
||||
return nitrogenItem;
|
||||
return uptakeMapItem;
|
||||
}
|
||||
|
||||
|
||||
@@ -225,10 +215,11 @@ namespace FarmmapsApiSamples
|
||||
/// <param name="inputItem">The farmmaps item containing the geotiff data</param>
|
||||
/// <param name="plantingDate">The date the crop is planted</param>
|
||||
/// <param name="measurementDate">The date the measurements are taken</param>
|
||||
/// <param name="inputType">The inputtype to use</param>
|
||||
/// <param name="targetN">The target nitrogen to use for the calculations</param>
|
||||
/// <returns></returns>
|
||||
public async Task<Item> CalculateApplicationMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,
|
||||
DateTime measurementDate, double targetN)
|
||||
DateTime measurementDate, string inputType = "irmi", double targetN = 60.0)
|
||||
{
|
||||
var nbsApplianceMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
|
||||
nbsApplianceMapRequest.attributes["operation"] = "application";
|
||||
@@ -236,7 +227,7 @@ namespace FarmmapsApiSamples
|
||||
nbsApplianceMapRequest.attributes["plantingDate"] = plantingDate.ToString();
|
||||
nbsApplianceMapRequest.attributes["measurementDate"] = measurementDate.ToString();
|
||||
nbsApplianceMapRequest.attributes["inputCode"] = inputItem.Code;
|
||||
nbsApplianceMapRequest.attributes["inputType"] = "irmi";
|
||||
nbsApplianceMapRequest.attributes["inputType"] = inputType.ToLower();
|
||||
nbsApplianceMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplianceMapRequest);
|
||||
@@ -254,14 +245,12 @@ namespace FarmmapsApiSamples
|
||||
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
|
||||
return null;
|
||||
}
|
||||
|
||||
var geotiffItems = await
|
||||
_farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code, GEOTIFF_PROCESSED_ITEMTYPE);
|
||||
|
||||
// maybe poll here too, to wait for geotiff task process to be finished?
|
||||
|
||||
// how to uniquely know which item is really created!?
|
||||
var applianceMapItem = geotiffItems.SingleOrDefault(i => i.Name.Contains("nitrogen") && i.Updated >= itemTask.Finished);
|
||||
var itemName = $"VRANbs application";
|
||||
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 application map geotiff child item under cropfield");
|
||||
@@ -270,15 +259,5 @@ namespace FarmmapsApiSamples
|
||||
|
||||
return applianceMapItem;
|
||||
}
|
||||
|
||||
private ItemRequest CreateTargetNItemRequest(string parentItemCode)
|
||||
{
|
||||
return new ItemRequest()
|
||||
{
|
||||
ParentCode = parentItemCode,
|
||||
ItemType = USERINPUT_ITEMTYPE,
|
||||
Name = "TargetN"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user