Skip to content

Commit

Permalink
Merge pull request ADAPT#220 from knelson-farmbeltnorth/develop
Browse files Browse the repository at this point in the history
Adding example prescription export.   Fixing null reference exception…
  • Loading branch information
strhea authored Sep 5, 2024
2 parents 5bc78f4 + ac1c84c commit 7640b89
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Examples/Examples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\ISOv4Plugin\ISOv4Plugin.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AgGatewayADAPTFramework" Version="3.1.0" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
2 changes: 2 additions & 0 deletions Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//Currently one 1 example
RasterPrescriptions.ExportExamplePrescription();
81 changes: 81 additions & 0 deletions Examples/RasterPrescriptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//While ISO1173-10 (and this plugin) supports vector prescriptions, industry adoption is limited.
//Raster prescriptions are commonly used.

using AgGateway.ADAPT.ApplicationDataModel;
using AgGateway.ADAPT.ApplicationDataModel.Common;
using AgGateway.ADAPT.ApplicationDataModel.Products;
using AgGateway.ADAPT.ApplicationDataModel.Prescriptions;
using AgGateway.ADAPT.ApplicationDataModel.Representations;
using AgGateway.ADAPT.ApplicationDataModel.ADM;
using AgGateway.ADAPT.ApplicationDataModel.Shapes;
using AgGateway.ADAPT.ApplicationDataModel.Documents;

class RasterPrescriptions
{
public static void ExportExamplePrescription()
{
var adm = new ApplicationDataModel
{
Catalog = new Catalog
{
Products = new List<Product>
{
new GenericProduct { Description = "Product 1", Category = CategoryEnum.Fertilizer, ProductType = ProductTypeEnum.Fertilizer, Form = ProductFormEnum.Solid, Status = ProductStatusEnum.Active },
new GenericProduct { Description = "Product 2", Category = CategoryEnum.Fungicide, ProductType = ProductTypeEnum.Chemical, Form = ProductFormEnum.Liquid, Status = ProductStatusEnum.Active },
new GenericProduct { Description = "Product 3", Category = CategoryEnum.Herbicide, ProductType = ProductTypeEnum.Chemical, Form = ProductFormEnum.Liquid, Status = ProductStatusEnum.Active },
new GenericProduct { Description = "Product 4", Category = CategoryEnum.Insecticide, ProductType = ProductTypeEnum.Chemical, Form = ProductFormEnum.Liquid, Status = ProductStatusEnum.Active }
},
},
Documents = new Documents()
};

var rx = new RasterGridPrescription
{
CellHeight = new NumericRepresentationValue { Value = new NumericValue(new UnitOfMeasure(), 0.001) }, //In ISO1173-10, cell size is listed in degrees of lat/lon
CellWidth = new NumericRepresentationValue { Value = new NumericValue(new UnitOfMeasure(), 0.002) },
ColumnCount = 10,
RowCount = 5,
OperationType = OperationTypeEnum.SowingAndPlanting,
Origin = new Point { X = -87.60, Y = 41.88 }, //SW Corner
RxProductLookups = adm.Catalog.Products.Select(x => new RxProductLookup { ProductId = x.Id.ReferenceId, UnitOfMeasure = new UnitOfMeasure { } }).ToList(),
ProductIds = adm.Catalog.Products.Select(x => x.Id.ReferenceId).ToList(),
Rates = new List<RxCellLookup>(),
Description = "RasterRx"
};

for (int h = 0; h < rx.RowCount; h++) //Rates in ADAPT/ISOXML Raster prescriptions are in a simple list, starting at the SW cell and going row-by-row left to right
{
for (int w = 0; w < rx.ColumnCount; w++)
{
var rates = rx.RxProductLookups.Select((x, i) => new RxRate
{
Rate = h * rx.ColumnCount + w + i * 100, //Dummy rates for the example for illustrative purposes
RxProductLookupId = x.Id.ReferenceId
}).ToList();
rx.Rates.Add(new RxCellLookup { RxRates = rates });
}
}

adm.Catalog.Prescriptions = new List<Prescription> { rx };

//The plugin is currently only recognizes Prescriptions mapped through Work Items and Work Item Operations
//See TaskDataMapper.cs at "Tasks" export
var workItemOperation = new WorkItemOperation
{
PrescriptionId = rx.Id.ReferenceId
};
adm.Documents.WorkItemOperations = new List<WorkItemOperation> { workItemOperation };

var workItem = new WorkItem
{
WorkItemOperationIds = new List<int>() { workItemOperation.Id.ReferenceId }
};
adm.Documents.WorkItems = new List<WorkItem> { workItem };


var plugin = new AgGateway.ADAPT.ISOv4Plugin.Plugin();
var properties = new AgGateway.ADAPT.ApplicationDataModel.ADM.Properties();
properties.SetProperty("GridType", "2"); //ISOXML has 2 formats of grid prescriptions. 2 is the default if this property is omitted.
plugin.Export(adm, System.IO.Directory.GetCurrentDirectory(), properties); //Exports to location of the Examples binary
}
}
6 changes: 6 additions & 0 deletions ISOv4Plugin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ISOv4Plugin", "ISOv4Plugin\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "TestUtilities\TestUtilities.csproj", "{98DBD0C7-5677-4B64-8DDA-BDA1E3B3DFCB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{22ECC922-F80E-4142-9835-455B02A70B63}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{98DBD0C7-5677-4B64-8DDA-BDA1E3B3DFCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98DBD0C7-5677-4B64-8DDA-BDA1E3B3DFCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98DBD0C7-5677-4B64-8DDA-BDA1E3B3DFCB}.Release|Any CPU.Build.0 = Release|Any CPU
{22ECC922-F80E-4142-9835-455B02A70B63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{22ECC922-F80E-4142-9835-455B02A70B63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22ECC922-F80E-4142-9835-455B02A70B63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{22ECC922-F80E-4142-9835-455B02A70B63}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion ISOv4Plugin/Mappers/PrescriptionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private ISOTreatmentZone ExportTreatmentZonesForType2(ISOTask task, RasterGridPr
outOfFieldTreatmentZone.ProcessDataVariables.Add(oofPDV);
}

NumericRepresentation defaultRepresentation = productLookup?.LossOfGpsRate.Representation; //We can reuse the loss of gps representation here if it exists
NumericRepresentation defaultRepresentation = productLookup?.LossOfGpsRate?.Representation; //We can reuse the loss of gps representation here if it exists
if (defaultRepresentation == null)
{
//Determine the representation based on the unit of the product to be applied
Expand Down

0 comments on commit 7640b89

Please sign in to comment.