Projects FarmmapsBulkSatDownload and FarmmapsDataDownload fully updated and tested
Added some extra Tasks to /FarmmapsApi/Services/GeneralService.cs Added class SatelliteStatistics.cs to /FarmmapsApi/Models
This commit is contained in:
		| @@ -6,6 +6,7 @@ using System.Threading.Tasks; | ||||
| using FarmmapsApi.Models; | ||||
| using Google.Apis.Upload; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using Newtonsoft.Json; | ||||
| using Newtonsoft.Json.Linq; | ||||
| using static FarmmapsApi.Extensions; | ||||
| using static FarmmapsApiSamples.Constants; | ||||
| @@ -348,12 +349,10 @@ namespace FarmmapsApi.Services | ||||
|         } | ||||
|  | ||||
|  | ||||
|         public async Task<Item> FindSatelliteItem(Item cropfieldItem, string satelliteTaskCode, string FieldName = null, int selectedLayer = 0, bool allItemsStatistics = false, string DownloadFolder = null) { | ||||
|         public async Task<Item> FindSatelliteItem(Item cropfieldItem, string satelliteTaskCode) { | ||||
|  | ||||
|             var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, satelliteTaskCode); | ||||
|  | ||||
|  | ||||
|             // find ndvi or wdvi satellite data geotiffs | ||||
|             var temporalItem = await FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE, | ||||
|                 "Cropfield Satellite items", item => item.SourceTask == SATELLITE_TASK && | ||||
|                                                                  taskStatus.Finished >= item.Created && | ||||
| @@ -367,101 +366,149 @@ 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++; | ||||
|                 } | ||||
|  | ||||
|                 _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; | ||||
|                 } | ||||
|                 Console.WriteLine($"Satellite image #{count}: {item.DataDate}"); | ||||
|                 count++; | ||||
|             } | ||||
|  | ||||
|             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"); | ||||
|                         } | ||||
|             _logger.LogInformation("Enter satellite image number for NBS application"); | ||||
|             int element = Int32.Parse(Console.ReadLine()); | ||||
|             var selectedSatelliteItem = satelliteTiffs[element]; | ||||
|  | ||||
|                         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) { | ||||
|             if (selectedSatelliteItem == null) | ||||
|             { | ||||
|                 _logger.LogError("Satellite item not found"); | ||||
|  | ||||
|                 return selectedSatelliteItemTemp; | ||||
|             } | ||||
|  | ||||
|             return selectedSatelliteItemTemp; | ||||
|  | ||||
|                  | ||||
|                 return selectedSatelliteItem; | ||||
|         } | ||||
|          | ||||
|  | ||||
|         public async Task<List<Item>> FindSatelliteItemsAll(Item cropfieldItem, string satelliteTaskCode) | ||||
|         public async Task<List<Item>> FindSatelliteItems(Item cropfieldItem, string satelliteTaskCode) | ||||
|         { | ||||
|  | ||||
|             var taskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, satelliteTaskCode); | ||||
|  | ||||
|  | ||||
|             // find ndvi or wdvi satellite data geotiffs | ||||
|             var temporalItem = await FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE, | ||||
|                 "Cropfield Satellite items", item => item.SourceTask == SATELLITE_TASK && | ||||
|                                                                  taskStatus.Finished >= item.Created && | ||||
|                                                                  taskStatus.Finished <= item.Created.Value.AddHours(1)); | ||||
|  | ||||
|  | ||||
|             if (temporalItem == null) | ||||
|             if (taskStatus.State == ItemTaskState.Error) | ||||
|             { | ||||
|                 _logger.LogError("Temporal item not found"); | ||||
|                 _logger.LogWarning(taskStatus.Message); | ||||
|                 return null; | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 // find satellite data geotiffs | ||||
|                 var temporalItem = await FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE, | ||||
|                     "Cropfield Satellite items", item => item.SourceTask == SATELLITE_TASK && | ||||
|                     taskStatus.Finished >= item.Created && | ||||
|                     taskStatus.Finished <= item.Created.Value.AddHours(1)); | ||||
|  | ||||
|                 if (temporalItem == null) | ||||
|                 { | ||||
|                     _logger.LogWarning("Temporal item not found"); | ||||
|                     return null; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalItem.Code); | ||||
|                     return satelliteTiffs; | ||||
|                 } | ||||
|  | ||||
|             } | ||||
|  | ||||
|             var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalItem.Code); | ||||
|  | ||||
|             return satelliteTiffs; | ||||
|         } | ||||
|         public async Task<string> DownloadSatelliteStats(List<Item> satelliteTiffs, string fieldName = null, List<string> satelliteBands = null, string downloadFolder = null) | ||||
|         { | ||||
|  | ||||
|             string satelliteDataStatisticsFile = Path.Combine(downloadFolder, $"satelliteStats_{fieldName}.csv"); | ||||
|             File.Delete(satelliteDataStatisticsFile); // Delete the SatelliteFile file if exists | ||||
|             List<int> selectedLayers = new List<int>(); | ||||
|             foreach (string satelliteBand in satelliteBands) | ||||
|             { | ||||
|                 if (satelliteBand == "ndvi") selectedLayers.Add(0); | ||||
|                 if (satelliteBand == "wdvi") selectedLayers.Add(1); | ||||
|             }; | ||||
|             string headerLineStats = $"FieldName,satelliteDate,satelliteBand,max,min,mean,mode,median,stddev,minPlus,curtosis,maxMinus,skewness,variance,populationCount,variationCoefficient,confidenceIntervalLow, confidenceIntervalHigh,confidenceIntervalErrorMargin" + Environment.NewLine; | ||||
|             File.AppendAllText(satelliteDataStatisticsFile, headerLineStats); | ||||
|             foreach (var item in satelliteTiffs) | ||||
|                 foreach (int selectedLayer in selectedLayers) | ||||
|                 { | ||||
|                     { | ||||
|                         var satelliteBand = item.Data["layers"][selectedLayer]["name"]; | ||||
|                         var satelliteImageDate = (DateTime)item.DataDate; | ||||
|                         var satelliteStatisticsJtoken = item.Data["layers"][selectedLayer]["renderer"]["band"]["statistics"]; | ||||
|                         if (satelliteStatisticsJtoken == null) | ||||
|                         { | ||||
|                             _logger.LogWarning($"{satelliteImageDate.ToString("yyyy-MM-dd")} no statistics found");  | ||||
|                             //Console.WriteLine($"Available data: {item.Data}"); | ||||
|                         } | ||||
|                         else | ||||
|                         { | ||||
|                             SatelliteStatistics satelliteStatistics = satelliteStatisticsJtoken.ToObject<SatelliteStatistics>(); | ||||
|                             satelliteStatistics.fieldName = fieldName; | ||||
|                             satelliteStatistics.satelliteDate = satelliteImageDate; | ||||
|                             satelliteStatistics.satelliteBand = satelliteBand.ToString(); | ||||
|                             File.AppendAllText(satelliteDataStatisticsFile, $"" + | ||||
|                                 $"{satelliteStatistics.fieldName}," + | ||||
|                                 $"{satelliteStatistics.satelliteDate.ToString("yyyy-MM-dd")}," + | ||||
|                                 $"{satelliteStatistics.satelliteBand}," + | ||||
|                                 $"{satelliteStatistics.max}," + | ||||
|                                 $"{satelliteStatistics.min}," + | ||||
|                                 $"{satelliteStatistics.mean}," + | ||||
|                                 $"{satelliteStatistics.mode}," + | ||||
|                                 $"{satelliteStatistics.median}," + | ||||
|                                 $"{satelliteStatistics.stddev}," + | ||||
|                                 $"{satelliteStatistics.minPlus}," + | ||||
|                                 $"{satelliteStatistics.curtosis}," + | ||||
|                                 $"{satelliteStatistics.maxMinus}," + | ||||
|                                 $"{satelliteStatistics.skewness}," + | ||||
|                                 $"{satelliteStatistics.variance}," + | ||||
|                                 $"{satelliteStatistics.populationCount}," + | ||||
|                                 $"{satelliteStatistics.variationCoefficient}," + | ||||
|                                 $"{satelliteStatistics.confidenceIntervalLow}," + | ||||
|                                 $"{satelliteStatistics.confidenceIntervalHigh}," + | ||||
|                                 $"{satelliteStatistics.confidenceIntervalErrorMargin}" + | ||||
|                                 Environment.NewLine); | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|             return satelliteDataStatisticsFile; | ||||
|         } | ||||
|         public async Task<List<SatelliteStatistics>> ListSatelliteStatistics(Item satelliteTiff, List<string> satelliteBands = null, string fieldName = null) | ||||
|         { | ||||
|             SatelliteStatistics satelliteStatistics; | ||||
|             List<SatelliteStatistics> listSatelliteStatistics = new List<SatelliteStatistics>(); | ||||
|             List<int> selectedLayers = new List<int>(); | ||||
|             foreach (string satelliteBand in satelliteBands) | ||||
|             { | ||||
|                 if (satelliteBand == "ndvi") selectedLayers.Add(0); | ||||
|                 if (satelliteBand == "wdvi") selectedLayers.Add(1); | ||||
|             }; | ||||
|  | ||||
|             foreach (int selectedLayer in selectedLayers) | ||||
|             { | ||||
|                 var satelliteBand = satelliteTiff.Data["layers"][selectedLayer]["name"]; | ||||
|                 var satelliteImageDate = (DateTime)satelliteTiff.DataDate; | ||||
|                 var satelliteStatisticsJtoken = satelliteTiff.Data["layers"][selectedLayer]["renderer"]["band"]["statistics"]; | ||||
|                 if (satelliteStatisticsJtoken == null) | ||||
|                 { | ||||
|                     _logger.LogWarning($"{satelliteImageDate.ToString("yyyy-MM-dd")} no statistics found"); | ||||
|                     //Console.WriteLine($"Available data: {item.Data}"); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     satelliteStatistics = satelliteStatisticsJtoken.ToObject<SatelliteStatistics>(); | ||||
|                     satelliteStatistics.fieldName = fieldName.ToString(); | ||||
|                     satelliteStatistics.satelliteDate = satelliteImageDate; | ||||
|                     satelliteStatistics.satelliteBand = satelliteBand.ToString(); | ||||
|                     listSatelliteStatistics.Add(satelliteStatistics); | ||||
|                 } | ||||
|             } | ||||
|                  | ||||
|             return listSatelliteStatistics; | ||||
|         } | ||||
|  | ||||
|         //VanDerSat | ||||
|         public async Task<string> RunVanDerSatTask(Item cropfieldItem) { | ||||
|             _logger.LogInformation("Gathering VanDerSat information for cropfield, this might take a while!"); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user