forked from FarmMaps/FarmMapsApiClient
updated poten&nbs, both work properly again
This commit is contained in:
@@ -37,9 +37,9 @@ namespace FarmmapsNbs
|
||||
|
||||
public async Task RunAsync()
|
||||
{
|
||||
var nitrogenInputJson = File.ReadAllText("InputData-NBS.json"); //NitrogenInput.json
|
||||
var nitrogenInputJson = File.ReadAllText("NitrogenInput.json");
|
||||
List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson);
|
||||
|
||||
|
||||
if (!Directory.Exists(DownloadFolder))
|
||||
Directory.CreateDirectory(DownloadFolder);
|
||||
|
||||
@@ -62,9 +62,9 @@ namespace FarmmapsNbs
|
||||
|
||||
private async Task Process(List<UserRoot> roots, NitrogenInput input)
|
||||
{
|
||||
|
||||
|
||||
// !!specify if you are using an already created cropfield:
|
||||
bool useCreatedCropfield = input. UseCreatedCropfield;
|
||||
bool useCreatedCropfield = input.UseCreatedCropfield;
|
||||
var plantingDate = input.PlantingDate;
|
||||
var FieldName = input.fieldName;
|
||||
bool StoreStatistics = input.storeSatelliteStatistics;
|
||||
@@ -86,7 +86,7 @@ namespace FarmmapsNbs
|
||||
_logger.LogError("Could not find a needed root item");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Use already created cropfield or create new one
|
||||
Item cropfieldItem;
|
||||
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.CropfieldItemCode))
|
||||
@@ -105,12 +105,14 @@ namespace FarmmapsNbs
|
||||
|
||||
var geotiffItem = (Item)null;
|
||||
|
||||
// No file input, use most recent satellite image
|
||||
if (string.IsNullOrEmpty(input.File)) {
|
||||
// If no input file is specified, use most recent satellite image
|
||||
if (string.IsNullOrEmpty(input.File))
|
||||
{
|
||||
_logger.LogInformation("No specific data given, retrieving most recent satellite image");
|
||||
|
||||
// check if satellite task not yet done, do here and save taskcode
|
||||
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode)) {
|
||||
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode))
|
||||
{
|
||||
var satelliteTaskCode = await _generalService.RunSatelliteTask(cropfieldItem);
|
||||
_settings.SatelliteTaskCode = satelliteTaskCode;
|
||||
SaveSettings(settingsfile);
|
||||
@@ -126,14 +128,17 @@ namespace FarmmapsNbs
|
||||
//Console.WriteLine($"Satellite image statistics for band {satelliteBand}: {satelliteStatistics}");
|
||||
|
||||
//Store data to csv
|
||||
if (StoreStatistics==true) {
|
||||
var SatelliteFile = $"C:\\Akkerweb\\DataSatellite_{FieldName}.csv";
|
||||
if (StoreStatistics == true)
|
||||
{
|
||||
var SatelliteFile = $"\\Downloads\\DataSatellite_{FieldName}.csv";
|
||||
var NewLineField = $"\"Field\":{FieldName}" + Environment.NewLine;
|
||||
var NewLineDate = $"\"date\":{satalliteItem.DataDate}" + Environment.NewLine;
|
||||
var i = 0;
|
||||
foreach (var item in satelliteStatistics) {
|
||||
foreach (var item in satelliteStatistics)
|
||||
{
|
||||
//var NewLines2;
|
||||
if (i == 0) {
|
||||
if (i == 0)
|
||||
{
|
||||
File.AppendAllText(SatelliteFile, NewLineDate);
|
||||
i++;
|
||||
}
|
||||
@@ -145,7 +150,8 @@ namespace FarmmapsNbs
|
||||
|
||||
// must be wdvi[1]
|
||||
var inputType = (satalliteItem.Data["layers"] as JArray)?[1]["name"].ToString();
|
||||
if (string.IsNullOrEmpty(inputType)) {
|
||||
if (string.IsNullOrEmpty(inputType))
|
||||
{
|
||||
_logger.LogError("Could not get the input type name from the satellite item");
|
||||
return;
|
||||
}
|
||||
@@ -167,34 +173,39 @@ namespace FarmmapsNbs
|
||||
|
||||
|
||||
// (geo)tiff input:
|
||||
else if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
|
||||
else if (input.File.Contains(".tif") || input.File.Contains(".geotiff"))
|
||||
{
|
||||
_logger.LogInformation("input = tiff data");
|
||||
var dataPath = Path.Combine("Data", input.File);
|
||||
geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath,
|
||||
Path.GetFileNameWithoutExtension(input.File));
|
||||
|
||||
if (geotiffItem == null) {
|
||||
if (geotiffItem == null)
|
||||
{
|
||||
_logger.LogError("Could not find item for uploaded data");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// json/shape input
|
||||
else {
|
||||
else
|
||||
{
|
||||
var isGeoJson = input.File.Contains("json");
|
||||
var dataPath = Path.Combine("Data", input.File);
|
||||
var shapeItem = isGeoJson ?
|
||||
await _generalService.UploadDataAsync(uploadedRoot, SHAPE_PROCESSED_ITEMTYPE, dataPath, Path.GetFileNameWithoutExtension(input.File)) :
|
||||
await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, Path.GetFileNameWithoutExtension(input.File));
|
||||
await _generalService.UploadDataAsync(uploadedRoot, SHAPE_PROCESSED_ITEMTYPE, dataPath, Path.GetFileNameWithoutExtension(input.File), input.GeometryJson.ToString(Formatting.None)) :
|
||||
await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, Path.GetFileNameWithoutExtension(input.File), input.GeometryJson.ToString(Formatting.None));
|
||||
|
||||
if (shapeItem == null) {
|
||||
if (shapeItem == null)
|
||||
{
|
||||
_logger.LogError("Could not find item for uploaded data");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation($"Converting shape to geotiff");
|
||||
geotiffItem = await _generalService.ShapeToGeotiff(shapeItem);
|
||||
if (geotiffItem == null) {
|
||||
if (geotiffItem == null)
|
||||
{
|
||||
_logger.LogError("Something went wrong with shape to geotiff transformation");
|
||||
return;
|
||||
}
|
||||
@@ -226,7 +237,8 @@ namespace FarmmapsNbs
|
||||
var targetNData = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate,
|
||||
measurementDate, input.PotatoPurposeType, input.TargetYield);
|
||||
|
||||
if (targetNData == null) {
|
||||
if (targetNData == null)
|
||||
{
|
||||
_logger.LogError("Something went wrong with TargetN calculation");
|
||||
return;
|
||||
}
|
||||
@@ -237,14 +249,15 @@ namespace FarmmapsNbs
|
||||
//_logger.LogInformation($"TargetN adjusted: {targetNData.TargetN}");
|
||||
|
||||
var targetNDataPath = Path.Combine(DownloadFolder, $"{input.OutputFileName}.targetn.json");
|
||||
|
||||
|
||||
await File.WriteAllTextAsync(targetNDataPath, JsonConvert.SerializeObject(targetNData, Formatting.Indented));
|
||||
|
||||
_logger.LogInformation("Calculating uptake map");
|
||||
var uptakeMapItem =
|
||||
await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate,
|
||||
measurementDate, input.InputVariable, input.InputLayerName);
|
||||
if (uptakeMapItem == null) {
|
||||
if (uptakeMapItem == null)
|
||||
{
|
||||
_logger.LogError("Something went wrong with creating the uptakeMap");
|
||||
return;
|
||||
}
|
||||
@@ -252,57 +265,79 @@ namespace FarmmapsNbs
|
||||
_logger.LogInformation("Downloading uptake map");
|
||||
await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code,
|
||||
Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip"));
|
||||
_logger.LogInformation("UptakeMap downloaded to {0}", Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip"));
|
||||
_logger.LogInformation("UptakeMap downloaded");
|
||||
|
||||
_logger.LogInformation("Calculating application map");
|
||||
var applicationMapItem =
|
||||
var applianceMapItem =
|
||||
await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate,
|
||||
measurementDate, input.InputVariable, targetNData.TargetN, input.InputLayerName);
|
||||
|
||||
if (applicationMapItem == null) {
|
||||
if (applianceMapItem == null)
|
||||
{
|
||||
_logger.LogError("Something went wrong with creating the applicationMap");
|
||||
return;
|
||||
}
|
||||
_logger.LogInformation("Downloading application map");
|
||||
await _farmmapsApiService.DownloadItemAsync(applicationMapItem.Code,
|
||||
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code,
|
||||
Path.Combine(DownloadFolder, $"{input.OutputFileName}.application.zip"));
|
||||
_logger.LogInformation("Application map can be found in {0}", Path.Combine(DownloadFolder, $"{input.OutputFileName}.application.zip"));
|
||||
|
||||
//transforming tiff to shape
|
||||
var tiffItem = applicationMapItem;
|
||||
var tiffItem = applianceMapItem;
|
||||
|
||||
if (tiffItem == null) {
|
||||
if (tiffItem == null)
|
||||
{
|
||||
_logger.LogError("Could not find item for uploaded data");
|
||||
return;
|
||||
}
|
||||
|
||||
//_logger.LogInformation($"Converting geotiff to shape");
|
||||
//var taskmap = await _generalService.GeotiffToShape(tiffItem);
|
||||
//if (taskmap == null) {
|
||||
// _logger.LogError("Something went wrong with geotiff to shape transformation");
|
||||
// return;
|
||||
//}
|
||||
|
||||
//ApplicationMap (GEOTIFF) To Taskmap
|
||||
_logger.LogInformation($"Converting geotiff to taskmap");
|
||||
var taskmap = await _generalService.CreateTaskmap(cropfieldItem, tiffItem, input.CellWidth, input.CellHeight, input.StartPoint.ToString(Formatting.None),
|
||||
input.Centered.ToString(), input.EndPoint.ToString(Formatting.None), input.Angle);
|
||||
|
||||
if (taskmap == null)
|
||||
if (input.GenerateTaskmap)
|
||||
{
|
||||
_logger.LogError("Something went wrong with geotiff to shape transformation");
|
||||
return;
|
||||
//GEOTIFF TO Taskmap
|
||||
_logger.LogInformation($"Converting geotiff to taskmap");
|
||||
|
||||
var taskmap = (Item)null;
|
||||
if (input.OutputType == "isoxml")
|
||||
{
|
||||
|
||||
if (input.DdiCode == null)
|
||||
{
|
||||
_logger.LogInformation("DDi not given. Using expected identifiers");
|
||||
input.DdiCode = input.DdiCode = "0006";
|
||||
|
||||
}
|
||||
taskmap = await _generalService.CreateTaskmap(cropfieldItem: cropfieldItem, tiffItem: applianceMapItem, outputType: input.OutputType, cellWidth: input.CellWidth,
|
||||
cellHeight: input.CellHeight, startPoint: input.StartPoint.ToString(Formatting.None), ddiCode: input.DdiCode, centered: input.Centered,
|
||||
endPoint: input.EndPoint.ToString(Formatting.None), angle: input.Angle, precision: input.Precision,
|
||||
cropTypeName: null, costumerName: null, ProductGroupName: null, productName: null, resolution: null, unitScale: null, maximumClasses: input.MaximumClasses);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
taskmap = await _generalService.CreateTaskmap(cropfieldItem: cropfieldItem, tiffItem: applianceMapItem, outputType: input.OutputType, cellWidth: input.CellWidth,
|
||||
cellHeight: input.CellHeight, startPoint: input.StartPoint.ToString(Formatting.None), centered: input.Centered,
|
||||
endPoint: input.EndPoint.ToString(Formatting.None), angle: input.Angle, precision: input.Precision, maximumClasses: input.MaximumClasses);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (taskmap == null)
|
||||
{
|
||||
_logger.LogError("Something went wrong with geotiff to taskmap transformation");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Downloading taskmap");
|
||||
await _farmmapsApiService.DownloadItemAsync(taskmap.Code,
|
||||
Path.Combine(DownloadFolder, $"{input.OutputFileName}.taskmap.zip"));
|
||||
}
|
||||
|
||||
|
||||
_logger.LogInformation("Downloading taskmap");
|
||||
await _farmmapsApiService.DownloadItemAsync(taskmap.Code,
|
||||
Path.Combine(DownloadFolder, $"{input.OutputFileName}.taskmap.zip"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Functions to save previously created cropfields
|
||||
private void LoadSettings(string file)
|
||||
{
|
||||
@@ -325,7 +360,8 @@ namespace FarmmapsNbs
|
||||
var json = JsonConvert.SerializeObject(_settings);
|
||||
File.WriteAllText(file, json);
|
||||
}
|
||||
private void SaveInfo(string file) {
|
||||
private void SaveInfo(string file)
|
||||
{
|
||||
if (_settings == null)
|
||||
return;
|
||||
|
||||
@@ -334,5 +370,5 @@ namespace FarmmapsNbs
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user