2020-08-19 11:40:42 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using FarmmapsApi;
|
|
|
|
|
using FarmmapsApi.Models;
|
|
|
|
|
using FarmmapsApi.Services;
|
|
|
|
|
using FarmmapsPoten.Models;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using static FarmmapsApiSamples.Constants;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace FarmmapsVRApoten
|
|
|
|
|
{
|
2021-01-28 14:08:32 +00:00
|
|
|
|
public class PotenApplication : IApplication {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
private const string DownloadFolder = "Downloads";
|
|
|
|
|
|
|
|
|
|
private readonly ILogger<PotenApplication> _logger;
|
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
|
private readonly PotenService _potenService;
|
|
|
|
|
private readonly GeneralService _generalService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PotenApplication(ILogger<PotenApplication> logger, FarmmapsApiService farmmapsApiService,
|
2021-01-28 14:08:32 +00:00
|
|
|
|
GeneralService generalService, PotenService potenService) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_farmmapsApiService = farmmapsApiService;
|
|
|
|
|
_generalService = generalService;
|
|
|
|
|
_potenService = potenService;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-28 14:08:32 +00:00
|
|
|
|
public async Task RunAsync() {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
// read field data from separate json file
|
|
|
|
|
var VRAPotenInputJson = File.ReadAllText("PotenInput.json");
|
|
|
|
|
List<PotenInput> potenInputs = JsonConvert.DeserializeObject<List<PotenInput>>(VRAPotenInputJson);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(DownloadFolder))
|
|
|
|
|
Directory.CreateDirectory(DownloadFolder);
|
|
|
|
|
|
|
|
|
|
// !! this call is needed the first time an api is called with a fresh clientid and secret !!
|
|
|
|
|
await _farmmapsApiService.GetCurrentUserCodeAsync();
|
|
|
|
|
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
|
|
|
|
|
|
2021-01-28 14:08:32 +00:00
|
|
|
|
foreach (var input in potenInputs) {
|
|
|
|
|
try {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
await Process(roots, input);
|
2021-01-28 14:08:32 +00:00
|
|
|
|
} catch (Exception ex) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger.LogError(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-28 14:08:32 +00:00
|
|
|
|
private async Task Process(List<UserRoot> roots, PotenInput input) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
var meanDensity = input.MeanDensity;
|
|
|
|
|
var variation = input.Variation;
|
|
|
|
|
var fieldName = input.FieldName;
|
|
|
|
|
bool useShadow = input.UseShadow;
|
2021-03-01 15:31:31 +00:00
|
|
|
|
bool countPerArea = input.CountPerArea;
|
2021-04-14 18:53:26 +00:00
|
|
|
|
var inputLayerName = input.InputLayerName;
|
2020-08-19 11:40:42 +00:00
|
|
|
|
|
|
|
|
|
var myDrive = roots.SingleOrDefault(r => r.Name == "My drive");
|
2021-01-28 14:08:32 +00:00
|
|
|
|
if (myDrive == null) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
|
2021-01-28 14:08:32 +00:00
|
|
|
|
if (uploadedRoot == null) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Creating cropfield");
|
|
|
|
|
|
|
|
|
|
var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code,
|
|
|
|
|
$"VRA Poten cropfield {input.OutputFileName}", input.PlantingYear,
|
|
|
|
|
input.GeometryJson.ToString(Formatting.None));
|
|
|
|
|
|
2021-04-14 18:53:26 +00:00
|
|
|
|
_logger.LogInformation($"CropfielditemCode: {cropfieldItem.Code}");
|
|
|
|
|
|
2021-04-12 15:01:17 +00:00
|
|
|
|
//Downloading shadowMap for own interpretation
|
2021-01-28 14:08:32 +00:00
|
|
|
|
if (useShadow) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger.LogInformation("Calculate shadow map for field");
|
|
|
|
|
var shadowItem = await _generalService.RunShadowTask(cropfieldItem);
|
2021-01-28 14:08:32 +00:00
|
|
|
|
if (shadowItem == null) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger.LogError("Something went wrong while obtaining the shadow map");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading shadow map");
|
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(shadowItem.Code,
|
|
|
|
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.shadow.zip"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Looking for local data to use");
|
|
|
|
|
var localDataAvailable = input.File;
|
2021-01-28 14:08:32 +00:00
|
|
|
|
var geotiffItem = (Item)null;
|
2020-08-19 11:40:42 +00:00
|
|
|
|
|
|
|
|
|
|
2021-01-28 14:08:32 +00:00
|
|
|
|
if (String.IsNullOrEmpty(localDataAvailable)) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger.LogInformation("Could not find item for uploaded data, using BOFEK");
|
|
|
|
|
|
|
|
|
|
//Retreiving BOFEK
|
|
|
|
|
_logger.LogInformation("Get BOFEK for field");
|
|
|
|
|
var bofekItem = await _generalService.RunBofekTask(cropfieldItem);
|
2021-01-28 14:08:32 +00:00
|
|
|
|
if (bofekItem == null) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger.LogError("Something went wrong while obtaining the BOFEK data");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading Bofek map");
|
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(bofekItem.Code,
|
|
|
|
|
Path.Combine(DownloadFolder, $"{input.OutputFileName}.BOFEK.zip"));
|
2021-01-28 14:08:32 +00:00
|
|
|
|
} else if (input.File.Contains(".tif") || input.File.Contains(".geotiff")) {
|
2020-10-07 20:06:56 +00:00
|
|
|
|
_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) {
|
|
|
|
|
_logger.LogError("Could not find item for uploaded data");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-28 14:08:32 +00:00
|
|
|
|
} else {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
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,
|
2021-04-07 09:10:47 +00:00
|
|
|
|
Path.GetFileNameWithoutExtension(input.File), input.GeometryJson.ToString(Formatting.None));
|
2020-08-19 11:40:42 +00:00
|
|
|
|
|
2021-01-28 14:08:32 +00:00
|
|
|
|
if (shapeItem == null) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger.LogError("Something went wrong while searching for the shape file");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// transform shape to geotiff as VRA poten only supports tiff input item
|
|
|
|
|
_logger.LogInformation($"Converting shape to geotiff");
|
|
|
|
|
|
|
|
|
|
geotiffItem = await _generalService.ShapeToGeotiff(shapeItem);
|
2021-01-28 14:08:32 +00:00
|
|
|
|
if (geotiffItem == null) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
_logger.LogError("Something went wrong with shape to geotiff transformation");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading geotiff file");
|
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(geotiffItem.Code,
|
|
|
|
|
Path.Combine(DownloadFolder, $"VRApoten_inputGeotiff_{input.OutputFileName}.zip"));
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-12 16:10:25 +00:00
|
|
|
|
|
|
|
|
|
|
2020-08-19 11:40:42 +00:00
|
|
|
|
// create appliance map
|
|
|
|
|
_logger.LogInformation("Calculating application map");
|
|
|
|
|
|
|
|
|
|
// INPUT IS NEEDED as GEOTIFF
|
|
|
|
|
var applianceMapItem =
|
2021-04-14 18:53:26 +00:00
|
|
|
|
await _potenService.CalculateApplicationMapAsync(cropfieldItem, geotiffItem, meanDensity, variation, countPerArea, useShadow, inputLayerName);
|
2020-08-19 11:40:42 +00:00
|
|
|
|
|
2021-01-28 14:08:32 +00:00
|
|
|
|
if (applianceMapItem == null) {
|
2020-08-19 11:40:42 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading application map");
|
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code,
|
|
|
|
|
Path.Combine(DownloadFolder, $"VRApoten_appliancemap_{input.OutputFileName}.zip"));
|
|
|
|
|
|
|
|
|
|
string finalOutput = Path.Combine(DownloadFolder, $"VRApoten_appliancemap_{input.OutputFileName}.zip");
|
|
|
|
|
_logger.LogInformation(File.Exists(finalOutput)
|
|
|
|
|
? "Download application map completed."
|
|
|
|
|
: "Something went wrong while downloading.");
|
2021-04-12 12:22:24 +00:00
|
|
|
|
|
2021-02-12 16:10:25 +00:00
|
|
|
|
|
2021-04-07 08:38:42 +00:00
|
|
|
|
|
|
|
|
|
if(input.GenerateTaskmap) {
|
2021-02-12 16:10:25 +00:00
|
|
|
|
//GEOTIFF TO Taskmap
|
|
|
|
|
_logger.LogInformation($"Converting geotiff to taskmap");
|
2021-04-07 09:10:47 +00:00
|
|
|
|
|
2021-04-07 08:38:42 +00:00
|
|
|
|
var taskmap = (Item)null;
|
|
|
|
|
if (input.OutputType == "isoxml")
|
|
|
|
|
{
|
2021-04-12 15:01:17 +00:00
|
|
|
|
|
|
|
|
|
if (input.DdiCode == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("DDi not given. Using expected identifiers");
|
|
|
|
|
if (countPerArea == true) {input.DdiCode = input.DdiCode = "0011";}
|
|
|
|
|
else { input.DdiCode = "0016"; };
|
|
|
|
|
}
|
2021-04-12 12:22:24 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2021-04-07 08:38:42 +00:00
|
|
|
|
} else
|
|
|
|
|
{
|
2021-04-12 12:22:24 +00:00
|
|
|
|
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);
|
2021-04-07 08:38:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-07 09:10:47 +00:00
|
|
|
|
|
2021-02-12 16:10:25 +00:00
|
|
|
|
if (taskmap == null)
|
|
|
|
|
{
|
2021-02-16 08:46:50 +00:00
|
|
|
|
_logger.LogError("Something went wrong with geotiff to taskmap transformation");
|
2020-08-19 11:40:42 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading taskmap");
|
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(taskmap.Code,
|
|
|
|
|
Path.Combine(DownloadFolder, $"VRApoten_taskmap_{input.OutputFileName}.zip"));
|
2021-04-07 08:38:42 +00:00
|
|
|
|
}
|
2020-08-19 11:40:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|