added vandersat data to sample code
This commit is contained in:
		| @@ -14,6 +14,8 @@ namespace FarmmapsNbs.Models | ||||
|         public int TargetYield { get; set; } | ||||
|         public JObject GeometryJson { get; set; } | ||||
|         public string InputLayerName { get; set; } | ||||
|         public string fieldName{ get; set; } | ||||
|         public bool storeSatelliteStatistics { get; set; } | ||||
|  | ||||
|  | ||||
|     } | ||||
|   | ||||
| @@ -2,5 +2,8 @@ | ||||
|     public class Settings { | ||||
|         public string CropfieldItemCode { get; set; } | ||||
|         public string SatelliteTaskCode { get; set; } | ||||
|         public string VanDerSatTaskCode { get; set; } | ||||
|         public string WatBalTaskCode { get; set; } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -38,16 +38,11 @@ namespace FarmmapsNbs | ||||
|         public async Task RunAsync() | ||||
|         { | ||||
|             var nitrogenInputJson = File.ReadAllText("NitrogenInput.json"); | ||||
|             //var nitrogenInputJson = File.ReadAllText("fivefieldsinput.json"); | ||||
|             List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson); | ||||
|              | ||||
|             if (!Directory.Exists(DownloadFolder)) | ||||
|                 Directory.CreateDirectory(DownloadFolder); | ||||
|  | ||||
|              | ||||
|              | ||||
|             LoadSettings(); | ||||
|  | ||||
|             // !! 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(); | ||||
| @@ -67,10 +62,16 @@ namespace FarmmapsNbs | ||||
|  | ||||
|         private async Task Process(List<UserRoot> roots, NitrogenInput input) | ||||
|         { | ||||
|              | ||||
|             // !!specify if you are using an already created cropfield: | ||||
|             bool useCreatedCropfield = false; | ||||
|             bool useCreatedCropfield = true; | ||||
|             var plantingDate = input.PlantingDate; | ||||
|             var FieldName = input.fieldName; | ||||
|             bool StoreStatistics = input.storeSatelliteStatistics; | ||||
|             var measurementDate = input.MeasurementDate; | ||||
|             string settingsfile = $"Settings_{FieldName}.json"; | ||||
|  | ||||
|             LoadSettings(settingsfile); | ||||
|  | ||||
|             var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded"); | ||||
|             if (uploadedRoot == null) | ||||
| @@ -85,8 +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)) | ||||
| @@ -95,7 +95,7 @@ namespace FarmmapsNbs | ||||
|                 cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code, | ||||
|                     $"VRA NBS cropfield {input.OutputFileName}", plantingDate.Year, input.GeometryJson.ToString(Formatting.None)); | ||||
|                 _settings.CropfieldItemCode = cropfieldItem.Code; | ||||
|                 SaveSettings(); | ||||
|                 SaveSettings(settingsfile); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
| @@ -103,40 +103,59 @@ namespace FarmmapsNbs | ||||
|                 cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode); | ||||
|             } | ||||
|  | ||||
|  | ||||
|  | ||||
|             var geotiffItem = (Item)null; | ||||
|  | ||||
|             // No file input, 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(); | ||||
|                     SaveSettings(settingsfile); | ||||
|                 } | ||||
|  | ||||
|  | ||||
|                 // Select a particular satellite item from satelliteTask | ||||
|                 Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode); | ||||
|                 Item satalliteItem = await _generalService.FindSatelliteItem(cropfieldItem, _settings.SatelliteTaskCode, FieldName, StoreStatistics); | ||||
|  | ||||
|                 var satelliteBand = satalliteItem.Data["layers"][0]["name"]; | ||||
|                 var satelliteStatistics = satalliteItem.Data["layers"][0]["renderer"]["band"]["statistics"]; | ||||
|                 Console.WriteLine($"Satellite image date: {satalliteItem.DataDate}"); | ||||
|                 //Console.WriteLine($"Satellite image statistics for band {satelliteBand}: {satelliteStatistics}"); | ||||
|  | ||||
|                 //Store data to csv  | ||||
|                 if (StoreStatistics==true) { | ||||
|                     var SatelliteFile = $"C:\\Akkerweb\\DataSatellite_{FieldName}.csv"; | ||||
|                     var NewLineField = $"\"Field\":{FieldName}" + Environment.NewLine; | ||||
|                     var NewLineDate = $"\"date\":{satalliteItem.DataDate}" + Environment.NewLine; | ||||
|                     var i = 0; | ||||
|                     foreach (var item in satelliteStatistics) { | ||||
|                         //var NewLines2; | ||||
|                         if (i == 0) { | ||||
|                             File.AppendAllText(SatelliteFile, NewLineDate); | ||||
|                             i++; | ||||
|                         } | ||||
|                         File.AppendAllText(SatelliteFile, $"{item}" + Environment.NewLine); | ||||
|  | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|  | ||||
|                 // 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; | ||||
|                 } | ||||
|  | ||||
|                 // download the geotiff | ||||
|                 var SatelliteImageDate = (DateTime)satalliteItem.DataDate; | ||||
|                 var SatelliteDate = SatelliteImageDate.ToString("yyyyMMdd"); | ||||
|                 _logger.LogInformation("Downloading geotiff file"); | ||||
|                 await _farmmapsApiService.DownloadItemAsync(satalliteItem.Code, | ||||
|                     Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}.zip")); | ||||
|                     Path.Combine(DownloadFolder, $"nbs_inputSatelliteGeotiff_{input.OutputFileName}_{inputType}_{SatelliteDate}.zip")); | ||||
|  | ||||
|                 // overwrite measurement date by date of satellite item | ||||
|                 measurementDate = satalliteItem.DataDate.Value; | ||||
| @@ -147,20 +166,19 @@ namespace FarmmapsNbs | ||||
|             } | ||||
|  | ||||
|  | ||||
|         | ||||
|             // (geo)tiff input: | ||||
|             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)); | ||||
|                 _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; | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|                     if (geotiffItem == null) { | ||||
|                         _logger.LogError("Could not find item for uploaded data"); | ||||
|                         return; | ||||
|                     }  | ||||
|                     } | ||||
|                  | ||||
|             // json/shape input | ||||
|             else { | ||||
|                 var isGeoJson = input.File.Contains("json"); | ||||
| @@ -187,22 +205,32 @@ namespace FarmmapsNbs | ||||
|             } | ||||
|  | ||||
|  | ||||
|              | ||||
|  | ||||
|             //// check if vandersat task not yet done, do here and save taskcode | ||||
|             //if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.VanDerSatTaskCode)) { | ||||
|             //    var VanDerSatTaskCode = await _generalService.RunVanDerSatTask(cropfieldItem); | ||||
|             //    _settings.VanDerSatTaskCode = VanDerSatTaskCode; | ||||
|             //    SaveSettings(settingsfile); | ||||
|             //} | ||||
|  | ||||
|             //// Select a particular image item from VanDerSat | ||||
|             //Item VanDerSatItem = await _generalService.FindVanDerSatItem(cropfieldItem, _settings.VanDerSatTaskCode, FieldName, StoreStatistics); | ||||
|  | ||||
|  | ||||
|             //// download the geotiff | ||||
|             //_logger.LogInformation("Downloading geotiff file"); | ||||
|             //await _farmmapsApiService.DownloadItemAsync(VanDerSatItem.Code, | ||||
|             //    Path.Combine(DownloadFolder, $"nbs_VanDerSatGeotiff_{input.OutputFileName}.zip")); | ||||
|  | ||||
|             _logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}"); | ||||
|             var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem); | ||||
|             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; | ||||
| @@ -215,12 +243,11 @@ namespace FarmmapsNbs | ||||
|             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; | ||||
|             } | ||||
|              | ||||
|  | ||||
|             _logger.LogInformation("Downloading uptake map"); | ||||
|             await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code, | ||||
|                 Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip")); | ||||
| @@ -231,8 +258,7 @@ namespace FarmmapsNbs | ||||
|                 await _nitrogenService.CalculateApplicationMap(cropfieldItem, geotiffItem, plantingDate, | ||||
|                     measurementDate, input.InputVariable, targetNData.TargetN, input.InputLayerName); | ||||
|  | ||||
|             if (applicationMapItem == null) | ||||
|             { | ||||
|             if (applicationMapItem == null) { | ||||
|                 _logger.LogError("Something went wrong with creating the applicationMap"); | ||||
|                 return; | ||||
|             } | ||||
| @@ -244,15 +270,13 @@ namespace FarmmapsNbs | ||||
|             //transforming tiff to shape | ||||
|             var tiffItem = applicationMapItem; | ||||
|  | ||||
|             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) | ||||
|             { | ||||
|             if (taskmap == null) { | ||||
|                 _logger.LogError("Something went wrong with geotiff to shape transformation"); | ||||
|                 return; | ||||
|             } | ||||
| @@ -264,12 +288,13 @@ namespace FarmmapsNbs | ||||
|  | ||||
|         } | ||||
|  | ||||
|  | ||||
|         // Functions to save previously created cropfields | ||||
|         private void LoadSettings() | ||||
|         private void LoadSettings(string file) | ||||
|         { | ||||
|             if (File.Exists(SettingsFile)) | ||||
|             if (File.Exists(file)) | ||||
|             { | ||||
|                 var jsonText = File.ReadAllText(SettingsFile); | ||||
|                 var jsonText = File.ReadAllText(file); | ||||
|                 _settings = JsonConvert.DeserializeObject<Settings>(jsonText); | ||||
|             } | ||||
|             else | ||||
| @@ -278,13 +303,22 @@ namespace FarmmapsNbs | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private void SaveSettings() | ||||
|         private void SaveSettings(string file) | ||||
|         { | ||||
|             if (_settings == null) | ||||
|                 return; | ||||
|  | ||||
|             var json = JsonConvert.SerializeObject(_settings); | ||||
|             File.WriteAllText(SettingsFile, json); | ||||
|             File.WriteAllText(file, json); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|             private void SaveInfo(string file) { | ||||
|             if (_settings == null) | ||||
|                 return; | ||||
|  | ||||
|             var json = JsonConvert.SerializeObject(_settings); | ||||
|             File.WriteAllText(file, json); | ||||
|  | ||||
|         } | ||||
|  | ||||
|       } | ||||
|     } | ||||
		Reference in New Issue
	
	Block a user