Update appsettings, included the three environments test, acceptance and production. + update the NBS sample code to the newest flow (backwards compatible #10

Merged
mark merged 1 commits from NBS_update into master 2022-02-11 12:31:21 +00:00
5 changed files with 8973 additions and 229 deletions

File diff suppressed because it is too large Load Diff

View File

@ -12,10 +12,8 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static FarmmapsApiSamples.Constants;
namespace FarmmapsNbs
{
public class NbsApplication : IApplication
{
namespace FarmmapsNbs {
public class NbsApplication : IApplication {
private const string DownloadFolder = "Downloads";
private const string SettingsFile = "settings.json";
@ -27,16 +25,14 @@ namespace FarmmapsNbs
private Settings _settings;
public NbsApplication(ILogger<NbsApplication> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService, NitrogenService nitrogenService)
{
GeneralService generalService, NitrogenService nitrogenService) {
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_generalService = generalService;
_nitrogenService = nitrogenService;
}
public async Task RunAsync()
{
public async Task RunAsync() {
var nitrogenInputJson = File.ReadAllText("NitrogenInput.json");
List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson);
@ -47,21 +43,16 @@ namespace FarmmapsNbs
await _farmmapsApiService.GetCurrentUserCodeAsync();
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
foreach (var input in nitrogenInputs)
{
try
{
foreach (var input in nitrogenInputs) {
try {
await Process(roots, input);
}
catch (Exception ex)
{
} catch (Exception ex) {
_logger.LogError(ex.Message);
}
}
}
private async Task Process(List<UserRoot> roots, NitrogenInput input)
{
private async Task Process(List<UserRoot> roots, NitrogenInput input) {
// !!specify if you are using an already created cropfield:
bool useCreatedCropfield = input.UseCreatedCropfield;
@ -74,45 +65,39 @@ namespace FarmmapsNbs
LoadSettings(settingsfile);
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot == null)
{
if (uploadedRoot == null) {
_logger.LogError("Could not find a needed root item");
return;
}
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDriveRoot == null)
{
if (myDriveRoot == null) {
_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))
{
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.CropfieldItemCode)) {
_logger.LogInformation("Creating cropfield");
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
$"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None));
_settings.CropfieldItemCode = cropfieldItem.Code;
SaveSettings(settingsfile);
}
else
{
} else {
_logger.LogInformation("Cropfield already exists, trying to get it");
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
}
var inputItem = (Item)null;
var geotiffItem = (Item)null;
// If no input file is specified, use most recent satellite image
if (string.IsNullOrEmpty(input.File))
{
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);
@ -120,96 +105,87 @@ namespace FarmmapsNbs
// Select a particular satellite item from satelliteTask
Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode);
Item satelliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode);
var satelliteBand = satalliteItem.Data["layers"][0]["name"];
var satelliteStatistics = satalliteItem.Data["layers"][0]["renderer"]["band"]["statistics"];
Console.WriteLine($"Satellite image date: {satalliteItem.DataDate}");
var satelliteBand = satelliteItem.Data["layers"][0]["name"];
var satelliteStatistics = satelliteItem.Data["layers"][0]["renderer"]["band"]["statistics"];
Console.WriteLine($"Satellite image date: {satelliteItem.DataDate}");
//Console.WriteLine($"Satellite image statistics for band {satelliteBand}: {satelliteStatistics}");
//Store data to csv
if (StoreStatistics == true)
{
if (StoreStatistics == true) {
var SatelliteStatsFile = $"{DownloadFolder}/SatelliteDataStatistics_{FieldName}_{satalliteItem.DataDate.Value:yyyy-MM-dd}.csv";
var SatelliteStatsFile = $"{DownloadFolder}/SatelliteDataStatistics_{FieldName}_{satelliteItem.DataDate.Value:yyyy-MM-dd}.csv";
using var w = new StreamWriter(SatelliteStatsFile);
{
foreach (var item in satelliteStatistics)
{
foreach (var item in satelliteStatistics) {
var line = string.Format("{0}", item);
w.WriteLine(line);
w.Flush();
}
}
}
// must be wdvi[1]
var inputType = (satalliteItem.Data["layers"] as JArray)?[1]["name"].ToString();
if (string.IsNullOrEmpty(inputType))
{
var inputType = (satelliteItem.Data["layers"] as JArray)?[1]["name"].ToString();
if (string.IsNullOrEmpty(inputType)) {
_logger.LogError("Could not get the input type name from the satellite item");
return;
}
// download the geotiff
var SatelliteImageDate = (DateTime)satalliteItem.DataDate;
var SatelliteImageDate = (DateTime)satelliteItem.DataDate;
var SatelliteDate = SatelliteImageDate.ToString("yyyyMMdd");
_logger.LogInformation("Downloading geotiff file");
await _farmmapsApiService.DownloadItemAsync(satalliteItem.Code,
await _farmmapsApiService.DownloadItemAsync(satelliteItem.Code,
Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}_{inputType}_{SatelliteDate}.zip"));
// overwrite measurement date by date of satellite item
measurementDate = satalliteItem.DataDate.Value;
geotiffItem = satalliteItem;
measurementDate = satelliteItem.DataDate.Value;
inputItem = satelliteItem;
}
// (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,
inputItem = 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), 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;
}
inputItem = shapeItem;
////convert shape to geotiff, not necessary anymore. However can still be done
//_logger.LogInformation($"Converting shape to geotiff");
//geotiffItem = await _generalService.ShapeToGeotiff(shapeItem, 1, input.InputLayerName);
//if (geotiffItem == null)
//{
// _logger.LogError("Something went wrong with shape to geotiff transformation");
// return;
//}
_logger.LogInformation($"Converting shape to geotiff");
geotiffItem = await _generalService.ShapeToGeotiff(shapeItem, 1, input.InputLayerName);
if (geotiffItem == null)
{
_logger.LogError("Something went wrong with shape to geotiff transformation");
return;
}
_logger.LogInformation("Downloading geotiff file");
await _farmmapsApiService.DownloadItemAsync(geotiffItem.Code,
Path.Combine(DownloadFolder, $"{input.OutputFileName}.input_geotiff.zip"));
//_logger.LogInformation("Downloading geotiff file");
//await _farmmapsApiService.DownloadItemAsync(geotiffItem.Code,
// Path.Combine(DownloadFolder, $"{input.OutputFileName}.input_geotiff.zip"));
//inputItem = geotiffItem;
////End shape to geotiff transformation
}
@ -218,16 +194,12 @@ 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;
}
_logger.LogInformation($"TargetN: {targetNData.TargetN}");
////Option to manually adjust the Target N, for test purposes only!
//targetNData.TargetN = 225;
//_logger.LogInformation($"TargetN adjusted: {targetNData.TargetN}");
var targetNDataPath = Path.Combine(DownloadFolder, $"{input.OutputFileName}.targetn.json");
@ -235,10 +207,9 @@ namespace FarmmapsNbs
_logger.LogInformation("Calculating uptake map");
var uptakeMapItem =
await _nitrogenService.CalculateUptakeMap(cropfieldItem, geotiffItem, plantingDate,
await _nitrogenService.CalculateUptakeMap(cropfieldItem, inputItem, plantingDate,
measurementDate, input.InputVariable, input.InputLayerName);
if (uptakeMapItem == null)
{
if (uptakeMapItem == null) {
_logger.LogError("Something went wrong with creating the uptakeMap");
return;
}
@ -250,11 +221,10 @@ namespace FarmmapsNbs
_logger.LogInformation("Calculating application map");
var applianceMapItem =
await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate,
await _nitrogenService.CalculateApplicationMap(cropfieldItem, inputItem, plantingDate,
measurementDate, input.InputVariable, targetNData.TargetN, input.InputLayerName);
if (applianceMapItem == null)
{
if (applianceMapItem == null) {
_logger.LogError("Something went wrong with creating the applicationMap");
return;
}
@ -266,24 +236,20 @@ namespace FarmmapsNbs
//transforming tiff to shape
var tiffItem = applianceMapItem;
if (tiffItem == null)
{
if (tiffItem == null) {
_logger.LogError("Could not find item for uploaded data");
return;
}
//ApplicationMap (GEOTIFF) To Taskmap
if (input.GenerateTaskmap)
{
if (input.GenerateTaskmap) {
//GEOTIFF TO Taskmap
_logger.LogInformation($"Converting geotiff to taskmap");
var taskmap = (Item)null;
if (input.OutputType == "isoxml")
{
if (input.OutputType == "isoxml") {
if (input.DdiCode == null)
{
if (input.DdiCode == null) {
_logger.LogInformation("DDi not given. Using expected identifiers");
input.DdiCode = input.DdiCode = "0006";
@ -293,9 +259,7 @@ namespace FarmmapsNbs
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
{
} else {
taskmap = await _generalService.CreateTaskmap(cropfieldItem: cropfieldItem, tiffItem: applianceMapItem, outputType: input.OutputType, cellWidth: input.CellWidth,
cellHeight: input.CellHeight, startPoint: input.StartPoint.ToString(Formatting.None), inputLayerName: input.InputLayerName, centered: input.Centered,
endPoint: input.EndPoint.ToString(Formatting.None), angle: input.Angle, precision: input.Precision, maximumClasses: input.MaximumClasses);
@ -303,8 +267,7 @@ namespace FarmmapsNbs
if (taskmap == null)
{
if (taskmap == null) {
_logger.LogError("Something went wrong with geotiff to taskmap transformation");
return;
}
@ -320,29 +283,23 @@ namespace FarmmapsNbs
}
// Functions to save previously created cropfields
private void LoadSettings(string file)
{
if (File.Exists(file))
{
private void LoadSettings(string file) {
if (File.Exists(file)) {
var jsonText = File.ReadAllText(file);
_settings = JsonConvert.DeserializeObject<Settings>(jsonText);
}
else
{
} else {
_settings = new Settings();
}
}
private void SaveSettings(string file)
{
private void SaveSettings(string file) {
if (_settings == null)
return;
var json = JsonConvert.SerializeObject(_settings);
File.WriteAllText(file, json);
}
private void SaveInfo(string file)
{
private void SaveInfo(string file) {
if (_settings == null)
return;
@ -352,4 +309,4 @@ namespace FarmmapsNbs
}
}
}
}

View File

@ -1,111 +1,59 @@
[
//{
// "UseCreatedCropfield": true,
// "file": "Scan_1_20190605.json",
// "inputVariable": "irmi",
// "inputLayerName": "",
// "outputFileName": "vranbs1",
// "fieldName": "testJson_irmi",
// "storeSatelliteStatistics": true,
// "plantingDate": "2019-04-18",
// "measurementDate": "2019-06-05",
// "potatoPurposeType": "consumption",
// "targetYield": 45,
// "geometryJson": {
// "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 ]
// ]
// ]
// },
// "GenerateTaskmap": true,
// "OutputType": "shape", // "shape" or "isoxml" if isoxml also add ddiCode
// "Precision": "2",
// "MaximumClasses": "4",
// "DdiCode": "0006",
// "CellWidth": "3",
// "CellHeight": "10",
// "Centered": "true",
// "StartPoint": {
// "type": "Point",
// "coordinates": [ 3.409341600000000, 50.638991900000001 ]
// },
// "EndPoint": { // if no angle
// "type": "Point",
// "coordinates": [ 3.411897000000000, 50.638981500000000 ]
// }
// //"Angle": "317.0" // if no endpoint
//},
{
"UseCreatedCropfield": false,
"file": "", // keep emptpy to use satellite image
"inputVariable": "wdvi",
"InputLayerName": "wdvi",
"outputFileName": "rtest1",
"fieldName": "test_satelliteWDVI",
"storeSatelliteStatistics": true,
"plantingDate": "2020-05-01",
"measurementDate": "2020-06-14",
"potatoPurposeType": "consumption",
"targetYield": 60,
"geometryJson": {
"type": "Polygon",
"coordinates": [
[
[ 4.960707146896585, 52.800583669708487 ],
[ 4.960645975538824, 52.800470217610922 ],
[ 4.962140695752897, 52.799177147194797 ],
[ 4.967523821195745, 52.801502400041208 ],
[ 4.966336768950911, 52.802543735879809 ],
[ 4.961711880764330, 52.801009996856429 ],
[ 4.960707146896585, 52.800583669708487 ]
]
]
},
"GenerateTaskmap": true,
"OutputType": "shape", // "shape" or "isoxml" if isoxml also add ddiCode
"Precision": "2",
"MaximumClasses": "4",
"DdiCode": "0006",
"CellWidth": "3",
"CellHeight": "10",
"Centered": "true",
"StartPoint": {
"type": "Point",
"coordinates": [ 4.960707146896585, 52.800583669708487 ]
},
"EndPoint": { // if no angle
"type": "Point",
"coordinates": [ 4.961711880764330, 52.801009996856429 ]
}
//"Angle": "317.0" // if no endpoint
}
{
"UseCreatedCropfield": false,
"file": "Scan_1_20190605_sample.json",
"inputVariable": "irmi",
"inputLayerName": "irmi",
"outputFileName": "vranbs_sample",
"fieldName": "sampleJson_irmi",
"storeSatelliteStatistics": true,
"plantingDate": "2019-04-18",
"measurementDate": "2019-06-05",
"potatoPurposeType": "consumption",
"targetYield": 45,
"geometryJson": {
"type": "Polygon",
"coordinates": [
[
[ 5.63217247936348731, 51.96438066944019596 ],
[ 5.6321047379532132, 51.96445630006819982 ],
[ 5.63206782435562925, 51.96450152306518078 ],
[ 5.63189463337882312, 51.96471369141583807 ],
[ 5.63125445938590019, 51.96545230717740793 ],
[ 5.63038475519850046, 51.96645153536461237 ],
[ 5.6302190722686376, 51.96664178312045124 ],
[ 5.63010295472675626, 51.96676711160860407 ],
[ 5.63204023224759265, 51.96725135278578023 ],
[ 5.63281523134503814, 51.96638856753320823 ],
[ 5.63281527471734655, 51.96638851620966904 ],
[ 5.63366926331355611, 51.96542171716968284 ],
[ 5.63397637358524683, 51.96498159324188748 ],
[ 5.6341135965418534, 51.96474743038492505 ],
[ 5.63219031197733688, 51.96438405049333653 ],
[ 5.63217541872332195, 51.96438123226843686 ],
[ 5.63217247936348731, 51.96438066944019596 ]
]
]
},
"GenerateTaskmap": false
}
// "GenerateTaskmap": true,
// "OutputType": "shape", // "shape" or "isoxml" if isoxml also add ddiCode
// "Precision": "0",
// "MaximumClasses": "4",
// "DdiCode": "0006",
// "CellWidth": "27",
// "CellHeight": "10",
// "Centered": "false",
// "StartPoint": {
// "type": "Point",
// "coordinates": [ 5.6322, 51.9644 ]
// },
// "EndPoint": { // if no angle
// "type": "Point",
// "coordinates": [ 5.6341, 51.9647 ]
// }
// //"Angle": "317.0" // if no endpoint
//}
]

View File

@ -1,13 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard1.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json" />
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="appsettings.secrets.json" />
<Content Include="appsettings.secrets.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
</Project>

View File

@ -1,8 +1,25 @@
{
"Authority": "https://accounts.test.farmmaps.eu/",
"Endpoint": "https://test.farmmaps.eu/",
"BasePath": "api/v1",
"DiscoveryEndpointUrl": "https://accounts.test.farmmaps.eu/.well-known/openid-configuration",
//There are three farmmaps environments, uncomment the environemnt you want to use with this sample client
////test environment
//"Authority": "https://accounts.test.farmmaps.eu/",
//"Endpoint": "https://test.farmmaps.eu/",
//"BasePath": "api/v1",
//"DiscoveryEndpointUrl": "https://accounts.test.farmmaps.eu/.well-known/openid-configuration",
////acceptance environment
//"Authority": "https://accounts.acc.farmmaps.eu/",
//"Endpoint": "https://acc.farmmaps.eu/",
//"BasePath": "api/v1",
//"DiscoveryEndpointUrl": "https://accounts.acc.farmmaps.eu/.well-known/openid-configuration",
//production environment
"authority": "https://accounts.farmmaps.eu/",
"endpoint": "https://farmmaps.eu/",
"basepath": "api/v1",
"discoveryendpointurl": "https://accounts.farmmaps.eu/.well-known/openid-configuration",
//overige info
"RedirectUri": "http://example.nl/api",
"Scopes": ["api"]
"Scopes": [ "api" ]
}