added project to only download some data
This commit is contained in:
235
FarmmapsDataDownload/DataDownloadApplication.cs
Normal file
235
FarmmapsDataDownload/DataDownloadApplication.cs
Normal file
@@ -0,0 +1,235 @@
|
||||
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 FarmmapsDataDownload.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using static FarmmapsApiSamples.Constants;
|
||||
|
||||
namespace FarmmapsDataDownload
|
||||
{
|
||||
public class DataDownloadApplication : IApplication
|
||||
{
|
||||
private const string DownloadFolder = "Downloads";
|
||||
private const string SettingsFile = "settings.json";
|
||||
|
||||
private readonly ILogger<DataDownloadApplication> _logger;
|
||||
private readonly FarmmapsApiService _farmmapsApiService;
|
||||
private readonly DataDownloadService _dataDownloadService;
|
||||
private readonly GeneralService _generalService;
|
||||
|
||||
private Settings _settings;
|
||||
|
||||
public DataDownloadApplication(ILogger<DataDownloadApplication> logger, FarmmapsApiService farmmapsApiService,
|
||||
GeneralService generalService, DataDownloadService dataDownloadService)
|
||||
{
|
||||
_logger = logger;
|
||||
_farmmapsApiService = farmmapsApiService;
|
||||
_generalService = generalService;
|
||||
_dataDownloadService = dataDownloadService;
|
||||
}
|
||||
|
||||
public async Task RunAsync()
|
||||
{
|
||||
var fieldsInputJson = File.ReadAllText("DataDownloadInput.json");
|
||||
List<DataDownloadInput> fieldsInputs = JsonConvert.DeserializeObject<List<DataDownloadInput>>(fieldsInputJson);
|
||||
|
||||
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();
|
||||
|
||||
foreach (var input in fieldsInputs)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Process(roots, input);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Process(List<UserRoot> roots, DataDownloadInput input)
|
||||
{
|
||||
// !!specify if you are using an already created cropfield:
|
||||
bool useCreatedCropfield = input.UseCreatedCropfield;
|
||||
var cropYear = input.CropYear;
|
||||
var fieldName = input.fieldName;
|
||||
bool storeSatelliteStatistics = input.StoreSatelliteStatistics;
|
||||
string settingsfile = $"Settings_{fieldName}.json";
|
||||
|
||||
LoadSettings(settingsfile);
|
||||
|
||||
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
|
||||
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)
|
||||
{
|
||||
_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))
|
||||
{
|
||||
_logger.LogInformation("Creating cropfield");
|
||||
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
|
||||
$"DataCropfield {input.OutputFileName}", cropYear.Year, input.GeometryJson.ToString(Formatting.None));
|
||||
_settings.CropfieldItemCode = cropfieldItem.Code;
|
||||
SaveSettings(settingsfile);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Cropfield already exists, trying to get it");
|
||||
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
|
||||
}
|
||||
|
||||
|
||||
// Get shadow data
|
||||
|
||||
if (input.GetShadowData)
|
||||
{
|
||||
_logger.LogInformation("Calculate shadow map for field");
|
||||
var shadowItem = await _generalService.RunShadowTask(cropfieldItem);
|
||||
if (shadowItem == null)
|
||||
{
|
||||
_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"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Get satellite data
|
||||
if (input.GetSatelliteData)
|
||||
{
|
||||
// check if satellite task not yet done, do here and save taskcode
|
||||
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.SatelliteTaskCode))
|
||||
{
|
||||
var satelliteTaskCode = await _generalService.RunSatelliteTask(cropfieldItem);
|
||||
_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;
|
||||
|
||||
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}");
|
||||
|
||||
var SatelliteStatsFile = $"{DownloadFolder}/SatelliteDataStatistics_{fieldName}_{input.SatelliteBand}_{satalliteItem.DataDate.Value:yyyy-MM-dd}.csv";
|
||||
using var w = new StreamWriter(SatelliteStatsFile);
|
||||
{
|
||||
foreach (var item in satelliteStatistics)
|
||||
{
|
||||
var line = string.Format("{0}", item);
|
||||
w.WriteLine(line);
|
||||
w.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
|
||||
// Get vanDerSat data
|
||||
if (input.GetVanDerSatData)
|
||||
{
|
||||
// check if satellite 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 satellite item from satelliteTask
|
||||
Item vanDerSatItem = await _generalService.FindVanDerSatItem(cropfieldItem, _settings.VanDerSatTaskCode, fieldName, input.StoreVanDerSatStatistics);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Functions to save previously created cropfields
|
||||
private void LoadSettings(string file)
|
||||
{
|
||||
if (File.Exists(file))
|
||||
{
|
||||
var jsonText = File.ReadAllText(file);
|
||||
_settings = JsonConvert.DeserializeObject<Settings>(jsonText);
|
||||
}
|
||||
else
|
||||
{
|
||||
_settings = new Settings();
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveSettings(string file)
|
||||
{
|
||||
if (_settings == null)
|
||||
return;
|
||||
|
||||
var json = JsonConvert.SerializeObject(_settings);
|
||||
File.WriteAllText(file, json);
|
||||
}
|
||||
private void SaveInfo(string file)
|
||||
{
|
||||
if (_settings == null)
|
||||
return;
|
||||
|
||||
var json = JsonConvert.SerializeObject(_settings);
|
||||
File.WriteAllText(file, json);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
31
FarmmapsDataDownload/DataDownloadInput.json
Normal file
31
FarmmapsDataDownload/DataDownloadInput.json
Normal file
@@ -0,0 +1,31 @@
|
||||
[
|
||||
{
|
||||
"UseCreatedCropfield": true,
|
||||
"outputFileName": "testSatData",
|
||||
"fieldName": "test_satData",
|
||||
"GetShadowData": true,
|
||||
"GetSatelliteData": true,
|
||||
"SatelliteBand": "natural", // "ndvi" or "wdvi"
|
||||
"StoreSatelliteStatistics": 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 ]
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
]
|
29
FarmmapsDataDownload/DataDownloadService.cs
Normal file
29
FarmmapsDataDownload/DataDownloadService.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using FarmmapsApi.Models;
|
||||
using FarmmapsApi.Services;
|
||||
using FarmmapsDataDownload.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static FarmmapsApi.Extensions;
|
||||
using static FarmmapsApiSamples.Constants;
|
||||
|
||||
namespace FarmmapsDataDownload
|
||||
{
|
||||
public class DataDownloadService
|
||||
{
|
||||
private readonly ILogger<DataDownloadService> _logger;
|
||||
private readonly FarmmapsApiService _farmmapsApiService;
|
||||
private readonly GeneralService _generalService;
|
||||
|
||||
public DataDownloadService(ILogger<DataDownloadService> logger, FarmmapsApiService farmmapsApiService,
|
||||
GeneralService generalService)
|
||||
{
|
||||
_logger = logger;
|
||||
_farmmapsApiService = farmmapsApiService;
|
||||
_generalService = generalService;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
27
FarmmapsDataDownload/FarmmapsDataDownload.csproj
Normal file
27
FarmmapsDataDownload/FarmmapsDataDownload.csproj
Normal file
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Data\**\*">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="DataDownloadInput.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="InputData-NBS.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FarmmapsApi\FarmmapsApi.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
26
FarmmapsDataDownload/Models/DataDownloadInput.cs
Normal file
26
FarmmapsDataDownload/Models/DataDownloadInput.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace FarmmapsDataDownload.Models
|
||||
{
|
||||
public class DataDownloadInput
|
||||
{
|
||||
public bool UseCreatedCropfield { get; set; }
|
||||
public string File { get; set; }
|
||||
public string InputVariable { get; set; }
|
||||
public string OutputFileName { get; set; }
|
||||
public DateTime CropYear { get; set; }
|
||||
public JObject GeometryJson { get; set; }
|
||||
public string InputLayerName { get; set; }
|
||||
public string fieldName { get; set; }
|
||||
public bool GetSatelliteData { get; set; }
|
||||
public bool GetVanDerSatData { get; set; }
|
||||
public string SatelliteBand { get; set; }
|
||||
public bool StoreSatelliteStatistics { get; set; }
|
||||
public bool StoreVanDerSatStatistics { get; set; }
|
||||
public bool GetShadowData { get; set; }
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
11
FarmmapsDataDownload/Models/Settings.cs
Normal file
11
FarmmapsDataDownload/Models/Settings.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace FarmmapsDataDownload
|
||||
{
|
||||
public class Settings
|
||||
{
|
||||
public string CropfieldItemCode { get; set; }
|
||||
public string SatelliteTaskCode { get; set; }
|
||||
public string VanDerSatTaskCode { get; set; }
|
||||
public string WatBalTaskCode { get; set; }
|
||||
|
||||
}
|
||||
}
|
9
FarmmapsDataDownload/Models/TargetNData.cs
Normal file
9
FarmmapsDataDownload/Models/TargetNData.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace FarmmapsDataDownload.Models
|
||||
{
|
||||
public class TargetNData
|
||||
{
|
||||
public double TSum { get; set; }
|
||||
public int TargetYield { get; set; }
|
||||
public double TargetN { get; set; }
|
||||
}
|
||||
}
|
21
FarmmapsDataDownload/Program.cs
Normal file
21
FarmmapsDataDownload/Program.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Threading.Tasks;
|
||||
using FarmmapsApi;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FarmmapsDataDownload
|
||||
{
|
||||
class Program : FarmmapsProgram<DataDownloadApplication>
|
||||
{
|
||||
private static async Task Main(string[] args)
|
||||
{
|
||||
await new Program().Start(args);
|
||||
}
|
||||
|
||||
protected override void Configure(IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddLogging()
|
||||
.AddTransient<DataDownloadService>();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user