From 5da74c2db097d796cab7ccf49e156ea5b9248a3a Mon Sep 17 00:00:00 2001 From: Sijbrandij Date: Wed, 12 May 2021 13:05:17 +0200 Subject: [PATCH] small update on DataDownload and GenerelServices --- FarmmapsApi/Services/GeneralService.cs | 81 +++++++++++++++---- .../DataDownloadApplication.cs | 36 +++++---- FarmmapsDataDownload/DataDownloadInput.json | 51 ++++++------ .../FarmmapsDataDownload.csproj | 2 +- .../Models/DataDownloadInput.cs | 1 + 5 files changed, 114 insertions(+), 57 deletions(-) diff --git a/FarmmapsApi/Services/GeneralService.cs b/FarmmapsApi/Services/GeneralService.cs index e934f85..b6ee298 100644 --- a/FarmmapsApi/Services/GeneralService.cs +++ b/FarmmapsApi/Services/GeneralService.cs @@ -348,7 +348,7 @@ namespace FarmmapsApi.Services } - public async Task FindSatelliteItem(Item cropfieldItem, string satelliteTaskCode) { + public async Task FindSatelliteItem(Item cropfieldItem, string satelliteTaskCode, string FieldName = null, int selectedLayer = 0, bool allItemsStatistics = false, string DownloadFolder = null) { var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, satelliteTaskCode); @@ -366,30 +366,81 @@ namespace FarmmapsApi.Services } var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalItem.Code); + + if (allItemsStatistics == false) { + _logger.LogInformation("Available satellite images:"); + var count = 0; + TimeSpan.FromSeconds(0.5); + foreach (var item in satelliteTiffs) { - _logger.LogInformation("Available satellite images:"); - var count = 0; - TimeSpan.FromSeconds(0.5); - foreach (var item in satelliteTiffs) { + Console.WriteLine($"Satellite image #{count}: {item.DataDate}"); + count++; + } - Console.WriteLine($"Satellite image #{count}: {item.DataDate}"); - count++; + _logger.LogInformation("Enter satellite image number for NBS application"); + int element = Int32.Parse(Console.ReadLine()); + var selectedSatelliteItem = satelliteTiffs[element]; + + if (selectedSatelliteItem == null) { + _logger.LogError("Satellite item not found"); + + return selectedSatelliteItem; + } } - _logger.LogInformation("Enter satellite image number for NBS application"); - int element = Int32.Parse(Console.ReadLine()); - var selectedSatelliteItem = satelliteTiffs[element]; + if (allItemsStatistics == true ) { + var count = 0; + foreach (var item in satelliteTiffs) { + var satellitetBand = item.Data["layers"][selectedLayer]["name"]; + var SatelliteImageYear = (DateTime)item.DataDate; + var SatelliteYear = SatelliteImageYear.ToString("yyyy"); + var SatelliteFile = $"{DownloadFolder}/SatelliteDataStatistics_{SatelliteYear}_{FieldName}_{satellitetBand}.csv"; + var NewLineField = $"Field,Date,Mean,Min,Max,Standard deviation, ConfidenceInterval low, ConfidenceInterval high" + Environment.NewLine; + if (count == 0) { + File.AppendAllText(SatelliteFile, NewLineField); + var numbervandersat = satelliteTiffs.Count; + Console.WriteLine($"{numbervandersat} Satellite images found"); + } - if (selectedSatelliteItem == null) { + var SatelliteStatistics = item.Data["layers"][0]["renderer"]["band"]["statistics"]; + var SatelliteImageDate = (DateTime)item.DataDate; + var satelliteDate = SatelliteImageDate.ToString("yyyy-MM-dd"); + var NewLineDate = $"\"date\":{satelliteDate}" + Environment.NewLine; + if (SatelliteStatistics == null) { + Console.WriteLine($"{satelliteDate} no statistics found"); + //Console.WriteLine($"Available data: {item.Data}"); + } else { + File.AppendAllText(SatelliteFile, $"{FieldName},{satelliteDate},{SatelliteStatistics["mean"]},{SatelliteStatistics["min"]},{SatelliteStatistics["max"]},{SatelliteStatistics["stddev"]},{SatelliteStatistics["confidenceIntervalLow"]},{SatelliteStatistics["confidenceIntervalHigh"]}" + Environment.NewLine); + } + + if (true) { + // download the geotiff of needed inputtype + var selectedSatelliteItemDate = (DateTime)item.DataDate; + var SatelliteDate = selectedSatelliteItemDate.ToString("yyyyMMdd"); + _logger.LogInformation("Downloading geotiff file"); + await _farmmapsApiService.DownloadItemAsync(item.Code, + Path.Combine(DownloadFolder, $"satelliteGeotiff_{FieldName}_{satellitetBand}_{SatelliteDate}.zip")); + } + + count++; + } + } + + var selectedSatelliteItemTemp = satelliteTiffs[0]; + + if (selectedSatelliteItemTemp == null) { _logger.LogError("Satellite item not found"); + return selectedSatelliteItemTemp; } - return selectedSatelliteItem; - } + return selectedSatelliteItemTemp; - //VanDerSat - public async Task RunVanDerSatTask(Item cropfieldItem) { + } + + + //VanDerSat + public async Task RunVanDerSatTask(Item cropfieldItem) { _logger.LogInformation("Gathering VanDerSat information for cropfield, this might take a while!"); diff --git a/FarmmapsDataDownload/DataDownloadApplication.cs b/FarmmapsDataDownload/DataDownloadApplication.cs index 9aec1d2..6001596 100644 --- a/FarmmapsDataDownload/DataDownloadApplication.cs +++ b/FarmmapsDataDownload/DataDownloadApplication.cs @@ -67,6 +67,7 @@ namespace FarmmapsDataDownload var cropYear = input.CropYear; var fieldName = input.fieldName; bool storeSatelliteStatistics = input.StoreSatelliteStatistics; + bool storeSatelliteStatisticsCropYear = input.StoreSatelliteStatisticsCropYear; string settingsfile = $"Settings_{fieldName}.json"; LoadSettings(settingsfile); @@ -132,20 +133,23 @@ namespace FarmmapsDataDownload _settings.SatelliteTaskCode = satelliteTaskCode; SaveSettings(settingsfile); } - - // Select a particular satellite item from satelliteTask - Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode); + int selectedLayer= 2; if (input.SatelliteBand == "ndvi") selectedLayer = 0; if (input.SatelliteBand == "wdvi") selectedLayer = 1; if (input.SatelliteBand == "natural") selectedLayer = 2; + // Select a particular satellite item from satelliteTask + Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode, fieldName, selectedLayer, storeSatelliteStatisticsCropYear, DownloadFolder); + var satelliteBand = satalliteItem.Data["layers"][selectedLayer]["name"]; //Store satellite data to csv if (storeSatelliteStatistics == true && (selectedLayer == 0 || selectedLayer ==1)) { + + var satelliteStatistics = satalliteItem.Data["layers"][selectedLayer]["renderer"]["band"]["statistics"]; Console.WriteLine($"Satellite image date: {satalliteItem.DataDate}"); @@ -160,24 +164,24 @@ namespace FarmmapsDataDownload } } - } + var inputType = (satalliteItem.Data["layers"] as JArray)?[selectedLayer]["name"].ToString(); + if (string.IsNullOrEmpty(inputType)) { + _logger.LogError("Could not get the input type name from the satellite item"); + return; + } - var inputType = (satalliteItem.Data["layers"] as JArray)?[selectedLayer]["name"].ToString(); - if (string.IsNullOrEmpty(inputType)) - { - _logger.LogError("Could not get the input type name from the satellite item"); - return; + // download the geotiff of needed inputtype + var SatelliteImageDate = (DateTime)satalliteItem.DataDate; + var SatelliteDate = SatelliteImageDate.ToString("yyyyMMdd"); + _logger.LogInformation("Downloading geotiff file"); + await _farmmapsApiService.DownloadItemAsync(satalliteItem.Code, + Path.Combine(DownloadFolder, $"satelliteGeotiff_{input.OutputFileName}_{inputType}_{SatelliteDate}.zip")); } - - // download the geotiff of needed inputtype - var SatelliteImageDate = (DateTime)satalliteItem.DataDate; - var SatelliteDate = SatelliteImageDate.ToString("yyyyMMdd"); - _logger.LogInformation("Downloading geotiff file"); - await _farmmapsApiService.DownloadItemAsync(satalliteItem.Code, - Path.Combine(DownloadFolder, $"satelliteGeotiff_{input.OutputFileName}_{inputType}_{SatelliteDate}.zip")); } + + // Get vanDerSat data if (input.GetVanDerSatData) { diff --git a/FarmmapsDataDownload/DataDownloadInput.json b/FarmmapsDataDownload/DataDownloadInput.json index 15678ae..11d80ac 100644 --- a/FarmmapsDataDownload/DataDownloadInput.json +++ b/FarmmapsDataDownload/DataDownloadInput.json @@ -1,31 +1,32 @@ [ - { - "UseCreatedCropfield": true, - "outputFileName": "testSatData", - "fieldName": "test_satData", - "GetShadowData": true, - "GetSatelliteData": true, - "SatelliteBand": "natural", // "ndvi" or "wdvi" - "StoreSatelliteStatistics": true, + { + "UseCreatedCropfield": true, + "outputFileName": "testSatData2", + "fieldName": "test_satData2", + "GetShadowData": false, + "GetSatelliteData": true, + "SatelliteBand": "wdvi", // "natural", "ndvi" or "wdvi" + "StoreSatelliteStatistics": false, + "StoreSatelliteStatisticsCropYear": true, - "GetVanDerSatData": false, - "StoreVanDerSatStatistics": false, - "CropYear": "2020-01-01", - "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 ] - ] - ] - } + "GetVanDerSatData": false, + "StoreVanDerSatStatistics": false, + "CropYear": "2020-01-01", + "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 ] + ] + ] } + } ] \ No newline at end of file diff --git a/FarmmapsDataDownload/FarmmapsDataDownload.csproj b/FarmmapsDataDownload/FarmmapsDataDownload.csproj index f1a5343..7f0e2c6 100644 --- a/FarmmapsDataDownload/FarmmapsDataDownload.csproj +++ b/FarmmapsDataDownload/FarmmapsDataDownload.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + netcoreapp3.0 diff --git a/FarmmapsDataDownload/Models/DataDownloadInput.cs b/FarmmapsDataDownload/Models/DataDownloadInput.cs index 57cc6e8..348f95e 100644 --- a/FarmmapsDataDownload/Models/DataDownloadInput.cs +++ b/FarmmapsDataDownload/Models/DataDownloadInput.cs @@ -17,6 +17,7 @@ namespace FarmmapsDataDownload.Models public bool GetVanDerSatData { get; set; } public string SatelliteBand { get; set; } public bool StoreSatelliteStatistics { get; set; } + public bool StoreSatelliteStatisticsCropYear { get; set; } public bool StoreVanDerSatStatistics { get; set; } public bool GetShadowData { get; set; }