forked from FarmMaps/FarmMapsApiClient
		
	zonering most basic flow
This commit is contained in:
		@@ -16,6 +16,7 @@ namespace FarmmapsApiSamples
 | 
				
			|||||||
        public const string VRAHERBICIDE_TASK = "vnd.farmmaps.task.vraherbicide";
 | 
					        public const string VRAHERBICIDE_TASK = "vnd.farmmaps.task.vraherbicide";
 | 
				
			||||||
        public const string VRAHAULMKILLING_TASK = "vnd.farmmaps.task.vrahaulmkilling";
 | 
					        public const string VRAHAULMKILLING_TASK = "vnd.farmmaps.task.vrahaulmkilling";
 | 
				
			||||||
        public const string VRAPLANTING_TASK = "vnd.farmmaps.task.vrapoten";
 | 
					        public const string VRAPLANTING_TASK = "vnd.farmmaps.task.vrapoten";
 | 
				
			||||||
 | 
					        public const string VRAZONERING_TASK = "vnd.farmmaps.task.vrazonering";
 | 
				
			||||||
        public const string SATELLITE_TASK = "vnd.farmmaps.task.satellite";
 | 
					        public const string SATELLITE_TASK = "vnd.farmmaps.task.satellite";
 | 
				
			||||||
        public const string TASKMAP_TASK = "vnd.farmmaps.task.taskmap";
 | 
					        public const string TASKMAP_TASK = "vnd.farmmaps.task.taskmap";
 | 
				
			||||||
        public const string WORKFLOW_TASK = "vnd.farmmaps.task.workflow";
 | 
					        public const string WORKFLOW_TASK = "vnd.farmmaps.task.workflow";
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										
											BIN
										
									
								
								FarmmapsZonering/Data/neo_ndvi-reglone-low-weed.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								FarmmapsZonering/Data/neo_ndvi-reglone-low-weed.tif
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										8
									
								
								FarmmapsZonering/Data/neo_pg.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								FarmmapsZonering/Data/neo_pg.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					"type": "FeatureCollection",
 | 
				
			||||||
 | 
					"name": "neo_pg",
 | 
				
			||||||
 | 
					"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
 | 
				
			||||||
 | 
					"features": [
 | 
				
			||||||
 | 
					{ "type": "Feature", "properties": { "id": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 5.670991253771027, 52.796788997702613 ], [ 5.671526456638633, 52.797291618546666 ], [ 5.671275936147413, 52.797422436717852 ], [ 5.671959173850738, 52.798269302728798 ], [ 5.670649634919365, 52.798778791408822 ], [ 5.671503682048522, 52.799591206957416 ], [ 5.675159003761311, 52.798193567415474 ], [ 5.673029579585948, 52.796024727480535 ], [ 5.670991253771027, 52.796788997702613 ] ] ] } }
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										
											BIN
										
									
								
								FarmmapsZonering/Data/neo_pg.zip
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								FarmmapsZonering/Data/neo_pg.zip
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							@@ -21,4 +21,8 @@
 | 
				
			|||||||
        <ProjectReference Include="..\FarmmapsApi\FarmmapsApi.csproj" />
 | 
					        <ProjectReference Include="..\FarmmapsApi\FarmmapsApi.csproj" />
 | 
				
			||||||
    </ItemGroup>
 | 
					    </ItemGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <ItemGroup>
 | 
				
			||||||
 | 
					      <Folder Include="Data" />
 | 
				
			||||||
 | 
					    </ItemGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
</Project>
 | 
					</Project>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										8
									
								
								FarmmapsZonering/Models/InputParameter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								FarmmapsZonering/Models/InputParameter.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					namespace FarmmapsHaulmkilling.Models
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class InputParameter
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public string ItemCode { get; set; }
 | 
				
			||||||
 | 
					        public string ItemLayer { get; set; }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										9
									
								
								FarmmapsZonering/Models/Output.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								FarmmapsZonering/Models/Output.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					namespace FarmmapsZonering.Models
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class Output
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public string Name { get; set; }
 | 
				
			||||||
 | 
					        public string Quantity { get; set; }
 | 
				
			||||||
 | 
					        public string Unit { get; set; }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -3,6 +3,5 @@ namespace FarmmapsHaulmkilling.Models
 | 
				
			|||||||
    public class Settings
 | 
					    public class Settings
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public string CropfieldItemCode { get; set; }
 | 
					        public string CropfieldItemCode { get; set; }
 | 
				
			||||||
        public string SatelliteTaskCode { get; set; }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,5 +1,12 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					using FarmmapsApi.Models;
 | 
				
			||||||
using FarmmapsApi.Services;
 | 
					using FarmmapsApi.Services;
 | 
				
			||||||
 | 
					using FarmmapsHaulmkilling.Models;
 | 
				
			||||||
 | 
					using FarmmapsZonering.Models;
 | 
				
			||||||
using Microsoft.Extensions.Logging;
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
 | 
					using static FarmmapsApi.Extensions;
 | 
				
			||||||
 | 
					using static FarmmapsApiSamples.Constants;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace FarmmapsZonering.Services
 | 
					namespace FarmmapsZonering.Services
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -16,5 +23,40 @@ namespace FarmmapsZonering.Services
 | 
				
			|||||||
            _farmmapsApiService = farmmapsApiService;
 | 
					            _farmmapsApiService = farmmapsApiService;
 | 
				
			||||||
            _generalService = generalService;
 | 
					            _generalService = generalService;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public async Task<Item> CreateApplicationMapAsync(Item cropfieldItem, string formula, Output output, params InputParameter[] inputItemCodes)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var zoneringTaskRequest = new TaskRequest() {TaskType = VRAZONERING_TASK};
 | 
				
			||||||
 | 
					            var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, zoneringTaskRequest);
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
 | 
				
			||||||
 | 
					                _logger.LogInformation($"Waiting on calculation of application map; Status: {itemTaskStatus.State}");
 | 
				
			||||||
 | 
					                if (itemTaskStatus.IsFinished)
 | 
				
			||||||
 | 
					                    tokenSource.Cancel();
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
 | 
				
			||||||
 | 
					            if (itemTask.State == ItemTaskState.Error)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                _logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
 | 
				
			||||||
 | 
					                return null;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            var itemName = $"VRAZonering";
 | 
				
			||||||
 | 
					            var applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
 | 
				
			||||||
 | 
					                GEOTIFF_PROCESSED_ITEMTYPE, itemName,
 | 
				
			||||||
 | 
					                i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
 | 
				
			||||||
 | 
					                     i.Name.ToLower().Contains(itemName.ToLower()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (applianceMapItem == null)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                _logger.LogError("Could not find the VRAZonering geotiff child item under cropfield");
 | 
				
			||||||
 | 
					                return null;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return applianceMapItem;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -5,6 +5,7 @@ using FarmmapsApi;
 | 
				
			|||||||
using FarmmapsApi.Models;
 | 
					using FarmmapsApi.Models;
 | 
				
			||||||
using FarmmapsApi.Services;
 | 
					using FarmmapsApi.Services;
 | 
				
			||||||
using FarmmapsHaulmkilling.Models;
 | 
					using FarmmapsHaulmkilling.Models;
 | 
				
			||||||
 | 
					using FarmmapsZonering.Models;
 | 
				
			||||||
using FarmmapsZonering.Services;
 | 
					using FarmmapsZonering.Services;
 | 
				
			||||||
using Microsoft.Extensions.Logging;
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
using Newtonsoft.Json;
 | 
					using Newtonsoft.Json;
 | 
				
			||||||
@@ -63,7 +64,7 @@ namespace FarmmapsZonering
 | 
				
			|||||||
            {
 | 
					            {
 | 
				
			||||||
                _logger.LogInformation("Creating cropfield");
 | 
					                _logger.LogInformation("Creating cropfield");
 | 
				
			||||||
                cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Zonering", 2020, 
 | 
					                cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Zonering", 2020, 
 | 
				
			||||||
                    @"{""type"":""Polygon"",""coordinates"":[[[4.617786844284247,52.22533706956424],[4.618642601314543,52.225938364585989],[4.6192153806397,52.22563988897754],[4.619192414656403,52.2256242822442],[4.620306732153958,52.225031745661528],[4.620542019225217,52.22519855319158],[4.621157509147853,52.22487436515405],[4.623387917230182,52.22367660757213],[4.624563444939009,52.22304740241544],[4.624562779355982,52.223046635247019],[4.624534908813479,52.22302596787506],[4.627873021330343,52.221240670658399],[4.627504935938338,52.220104419135129],[4.627324878706837,52.22020569669098],[4.627320696113512,52.22020660117888],[4.626707169518044,52.22053923770041],[4.624700376420229,52.221619047547488],[4.623471571183885,52.22227447969577],[4.623471511010673,52.22227500174403],[4.623468838689317,52.22228052566992],[4.617786844284247,52.22533706956424]]]}");
 | 
					                    @"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 5.670991253771027, 52.796788997702613 ], [ 5.671526456638633, 52.797291618546666 ], [ 5.671275936147413, 52.797422436717852 ], [ 5.671959173850738, 52.798269302728798 ], [ 5.670649634919365, 52.798778791408822 ], [ 5.671503682048522, 52.799591206957416 ], [ 5.675159003761311, 52.798193567415474 ], [ 5.673029579585948, 52.796024727480535 ], [ 5.670991253771027, 52.796788997702613 ] ] ] }");
 | 
				
			||||||
                _settings.CropfieldItemCode = cropfieldItem.Code;
 | 
					                _settings.CropfieldItemCode = cropfieldItem.Code;
 | 
				
			||||||
                SaveSettings();
 | 
					                SaveSettings();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -73,26 +74,30 @@ namespace FarmmapsZonering
 | 
				
			|||||||
                cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
 | 
					                cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            ItemTaskStatus taskStatus;
 | 
					            var inputFileName = "neo_ndvi-reglone-low-weed.tif";
 | 
				
			||||||
            if (string.IsNullOrEmpty(_settings.SatelliteTaskCode))
 | 
					            var dataPath = Path.Combine("Data", inputFileName);
 | 
				
			||||||
            {
 | 
					            var geotiffItem = await _generalService.UploadDataAsync(uploadedRoot, GEOTIFF_PROCESSED_ITEMTYPE, dataPath,
 | 
				
			||||||
                _logger.LogInformation("Gathering satellite information for cropfield, this might take a while!");
 | 
					                Path.GetFileNameWithoutExtension(inputFileName));
 | 
				
			||||||
                taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, SATELLITE_TASK, null, 20);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (taskStatus.State == ItemTaskState.Error)
 | 
					            if (geotiffItem == null) {
 | 
				
			||||||
                {
 | 
					                _logger.LogError("Could not find item for uploaded data");
 | 
				
			||||||
                    _logger.LogError($"Something went wrong when trying to process satellite data; {taskStatus.Message}");
 | 
					 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            } 
 | 
					            } 
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
                _settings.SatelliteTaskCode = taskStatus.Code;
 | 
					            var outputItem = await _zoneringService.CreateApplicationMapAsync(cropfieldItem, "[0] * 2", new Output()
 | 
				
			||||||
                SaveSettings();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            else
 | 
					 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                taskStatus = await _farmmapsApiService.GetTaskStatusAsync(_settings.CropfieldItemCode, _settings.SatelliteTaskCode);
 | 
					                Name = "times_2_zonering",
 | 
				
			||||||
            }
 | 
					                Unit = "n/kg",
 | 
				
			||||||
 | 
					                Quantity = "Nitrogen"
 | 
				
			||||||
 | 
					            }, new InputParameter()
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                ItemCode = geotiffItem.Code,
 | 
				
			||||||
 | 
					                ItemLayer = geotiffItem.Data["layers"][0]["name"].ToString()
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            _logger.LogInformation("Downloading output");
 | 
				
			||||||
 | 
					            await _farmmapsApiService.DownloadItemAsync(outputItem.Code,
 | 
				
			||||||
 | 
					                Path.Combine(DownloadFolder, $"times_2_zonering.zip"));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private void LoadSettings()
 | 
					        private void LoadSettings()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user