Example #1
0
        public ISOGrid Export(RasterGridPrescription adaptRx, ISOTreatmentZone type2DefaultTreatmentZone = null, List <byte> type1TreatmentZoneCodes = null)
        {
            ISOGrid grid = new ISOGrid();

            if (type1TreatmentZoneCodes != null)
            {
                grid.Filename = WriteType1GridFile(type1TreatmentZoneCodes);
                grid.GridType = 1;
            }
            else if (type2DefaultTreatmentZone != null)
            {
                grid.Filename          = WriteType2GridFile(adaptRx, type2DefaultTreatmentZone);
                grid.GridType          = 2;
                grid.TreatmentZoneCode = type2DefaultTreatmentZone.TreatmentZoneCode;
            }

            FileInfo info = new FileInfo(Path.Combine(this.TaskDataPath, Path.ChangeExtension(grid.Filename, "bin")));

            grid.Filelength = (uint)info.Length;

            grid.GridMinimumNorthPosition = Convert.ToDecimal(GetOriginY(adaptRx));
            grid.GridMinimumEastPosition  = Convert.ToDecimal(GetOriginX(adaptRx));
            grid.GridCellNorthSize        = GetCellHeight(adaptRx);
            grid.GridCellEastSize         = GetCellWidth(adaptRx);
            grid.GridMaximumColumn        = (uint)adaptRx.ColumnCount;
            grid.GridMaximumRow           = (uint)adaptRx.RowCount;
            return(grid);
        }
Example #2
0
        private List <RxCellLookup> ImportRatesFromTreatmentZones(GridDescriptor gridDescriptor, IEnumerable <ISOTreatmentZone> treatmentZones, List <int> productIds, RasterGridPrescription prescription)
        {
            var rates = new List <RxCellLookup>();

            foreach (var treatmentZoneCode in gridDescriptor.TreatmentZoneCodes)
            {
                ISOTreatmentZone treatmentZone = treatmentZones.FirstOrDefault(t => t.TreatmentZoneCode == treatmentZoneCode);
                if (treatmentZone == null)
                {
                    return(null);
                }

                var lookup = new RxCellLookup {
                    RxRates = new List <RxRate>()
                };
                foreach (ISOProcessDataVariable pdv in treatmentZone.ProcessDataVariables)
                {
                    if (!string.IsNullOrEmpty(pdv.ProductIdRef))
                    {
                        int?productID = TaskDataMapper.InstanceIDMap.GetADAPTID(pdv.ProductIdRef);
                        if (productID.HasValue)
                        {
                            lookup.RxRates.Add(PrescriptionMapper.ImportAndConvertRate(productID.Value, pdv, prescription));
                        }
                    }
                }

                rates.Add(lookup);
            }

            return(rates);
        }
Example #3
0
        private void ImportRates(ISOTask task, GridDescriptor gridDescriptor, RasterGridPrescription prescription)
        {
            if (task.PositionLostTreatmentZone != null)
            {
                prescription.LossOfGpsRate = ImportTreatmentZoneAsNumericRepValue(task.PositionLostTreatmentZone);
            }
            if (task.OutOfFieldTreatmentZone != null)
            {
                prescription.OutOfFieldRate = ImportTreatmentZoneAsNumericRepValue(task.OutOfFieldTreatmentZone);
            }

            if (gridDescriptor.TreatmentZoneCodes != null)
            {
                //Grid Type 1
                ISOTreatmentZone treatmentZone = task.TreatmentZones.FirstOrDefault();
                if (treatmentZone == null)
                {
                    return;
                }

                prescription.Rates = ImportRatesFromTreatmentZones(gridDescriptor, task.TreatmentZones, prescription.ProductIds, prescription);
            }
            else if (gridDescriptor.ProductRates != null)
            {
                //Grid Type 2
                var treatmentZoneTemplate = task.DefaultTreatmentZone;
                if (treatmentZoneTemplate == null)
                {
                    return;
                }

                prescription.Rates = ImportRatesFromProducts(gridDescriptor, prescription.ProductIds, prescription);
            }
        }
Example #4
0
        private GridDescriptor LoadGridDescriptor(ISOTask task, string dataPath)
        {
            if (task.Grid == null)
            {
                return(null);
            }

            GridDescriptor descriptor = new GridDescriptor();

            if (!descriptor.LoadGridDefinition(task.Grid))
            {
                return(null);
            }

            ISOTreatmentZone treatmentZone = null;

            if (task.Grid.GridType == 2)
            {
                treatmentZone = task.TreatmentZones.SingleOrDefault(tz => tz.TreatmentZoneCode == task.Grid.TreatmentZoneCode);
                if (treatmentZone == null)
                {
                    return(null);
                }
            }
            if (!descriptor.LoadRates(dataPath, task.Grid, treatmentZone))
            {
                return(null);
            }

            return(descriptor);
        }
Example #5
0
        private NumericRepresentationValue ImportTreatmentZoneAsNumericRepValue(ISOTreatmentZone treatmentZone)
        {
            if (treatmentZone.ProcessDataVariables == null || treatmentZone.ProcessDataVariables.Count == 0)
            {
                return(null);
            }

            return(treatmentZone.ProcessDataVariables.First().AsNumericRepresentationValue(RepresentationMapper, ISOTaskData)); //In this situation, there should be only one PDV
        }
        //Adds a treatment zone for a new rate combination
        private ISOTreatmentZone GetNewType1TreatmentZone(RxCellLookup cellLookup, byte counter, Prescription rx)
        {
            ISOTreatmentZone treatmentZone = new ISOTreatmentZone()
            {
                TreatmentZoneCode = counter, TreatmentZoneDesignator = $"TreatmentZone {counter.ToString()}"
            };

            foreach (RxRate rate in cellLookup.RxRates)
            {
                treatmentZone.ProcessDataVariables.Add(ExportProcessDataVariable(rate, rx));
            }
            return(treatmentZone);
        }
 private void ExportRasterPrescription(ISOTask task, RasterGridPrescription rx, int gridType)
 {
     if (gridType == 1)
     {
         List <byte> treatmentZoneCodes = ExportTreatmentZonesForType1(task, rx);
         task.Grid = _gridMapper.Export(rx, null, treatmentZoneCodes);
     }
     else
     {
         ISOTreatmentZone defaultTreatmentZone = ExportTreatmentZonesForType2(task, rx);
         task.Grid = _gridMapper.Export(rx, defaultTreatmentZone);
     }
 }
Example #8
0
        private string WriteType2GridFile(RasterGridPrescription prescription, ISOTreatmentZone treatmentZone)
        {
            var gridFileName = GenerateId(5);
            Dictionary <string, ISOUnit> unitsByDDI = new Dictionary <string, ISOUnit>();
            Dictionary <int, Tuple <ISOProcessDataVariable, string> > treatmentZoneDataByLookupId = new Dictionary <int, Tuple <ISOProcessDataVariable, string> >();

            using (var binaryWriter = CreateWriter(Path.ChangeExtension(gridFileName, ".bin")))
            {
                byte[] previousBytes = BitConverter.GetBytes(0);
                foreach (var rxCellLookup in prescription.Rates)
                {
                    if (rxCellLookup.RxRates == null || !rxCellLookup.RxRates.Any())
                    {
                        //If there is null or no rate, write the previous rate (or 0 if we have not yet entered a valid rate)
                        binaryWriter.Write(previousBytes, 0, previousBytes.Length);
                    }
                    else
                    {
                        foreach (var rxRate in rxCellLookup.RxRates)
                        {
                            if (!treatmentZoneDataByLookupId.ContainsKey(rxRate.RxProductLookupId))
                            {
                                var    lookup                       = prescription.RxProductLookups.First(x => x.Id.ReferenceId == rxRate.RxProductLookupId);
                                string isoPDTDesignator             = TaskDataMapper.InstanceIDMap.GetISOID(lookup.ProductId.Value);
                                ISOProcessDataVariable dataVariable = treatmentZone.ProcessDataVariables.First(i => i.ProductIdRef == isoPDTDesignator);
                                treatmentZoneDataByLookupId.Add(rxRate.RxProductLookupId, new Tuple <ISOProcessDataVariable, string>(dataVariable, lookup.UnitOfMeasure.Code));
                            }
                            ISOProcessDataVariable pdv = treatmentZoneDataByLookupId[rxRate.RxProductLookupId].Item1;
                            string srcUnitCode         = treatmentZoneDataByLookupId[rxRate.RxProductLookupId].Item2;

                            ISOUnit unit;
                            if (!unitsByDDI.ContainsKey(pdv.ProcessDataDDI))
                            {
                                unit = UnitFactory.Instance.GetUnitByDDI(pdv.ProcessDataDDI.AsInt32DDI());
                                unitsByDDI.Add(pdv.ProcessDataDDI, unit);
                            }
                            unit = unitsByDDI[pdv.ProcessDataDDI];

                            previousBytes = BitConverter.GetBytes((int)Math.Round(unit.ConvertToIsoUnit(rxRate.Rate, srcUnitCode), 0));
                            binaryWriter.Write(previousBytes, 0, previousBytes.Length);
                        }
                    }
                }
            }

            return(gridFileName);
        }
Example #9
0
        private NumericRepresentationValue ImportTreatmentZoneAsNumericRepValue(ISOTreatmentZone treatmentZone, RxProductLookup productLookup)
        {
            if (treatmentZone.ProcessDataVariables == null || treatmentZone.ProcessDataVariables.Count == 0)
            {
                return(null);
            }

            if (productLookup == null)                                                                                              //This clause supports the obsolete placement of default rates on the prescription
            {
                return(treatmentZone.ProcessDataVariables.First().AsNumericRepresentationValue(RepresentationMapper, ISOTaskData)); //In this situation, there should be only one PDV
            }
            else
            {
                ISOProcessDataVariable productPDV = treatmentZone.ProcessDataVariables.FirstOrDefault(pdv => productLookup.ProductId == TaskDataMapper.InstanceIDMap.GetADAPTID(pdv.ProductIdRef));
                return(productPDV?.AsNumericRepresentationValue(RepresentationMapper, ISOTaskData));
            }
        }
        private void ExportManualPresciption(ISOTask task, ManualPrescription rx)
        {
            ISOTreatmentZone tzn = new ISOTreatmentZone();

            tzn.TreatmentZoneDesignator   = "Default Treatment Zone";
            tzn.TreatmentZoneCode         = 1;
            task.DefaultTreatmentZoneCode = tzn.TreatmentZoneCode;

            foreach (ProductUse productUse in rx.ProductUses)
            {
                var    isoUnit             = DetermineIsoUnit(rx.RxProductLookups.First(p => p.ProductId == productUse.ProductId).UnitOfMeasure);
                string productIDRef        = TaskDataMapper.InstanceIDMap.GetISOID(productUse.ProductId);
                ISOProcessDataVariable pdv = ExportProcessDataVariable(productUse.Rate, productIDRef, isoUnit);
                tzn.ProcessDataVariables.Add(pdv);
            }

            task.TreatmentZones.Add(tzn);
        }
        private void ExportVectorPrescription(ISOTask task, VectorPrescription rx)
        {
            byte i = 0;

            foreach (RxShapeLookup shapeLookup in rx.RxShapeLookups)
            {
                ISOTreatmentZone tzn = new ISOTreatmentZone();
                tzn.TreatmentZoneCode = i++;
                foreach (RxRate rxRate in shapeLookup.Rates)
                {
                    tzn.ProcessDataVariables.Add(ExportProcessDataVariable(rxRate, rx));
                }

                PolygonMapper polygonMapper = new PolygonMapper(TaskDataMapper);
                tzn.Polygons = polygonMapper.ExportPolygons(shapeLookup.Shape.Polygons, ISOEnumerations.ISOPolygonType.TreatmentZone).ToList();
                task.TreatmentZones.Add(tzn);
            }
        }
        private List <byte> ExportTreatmentZonesForType1(ISOTask task, RasterGridPrescription prescription)
        {
            Dictionary <string, ISOTreatmentZone> treatmentZones = new Dictionary <string, ISOTreatmentZone>();
            List <byte> rateCodePerCell = new List <byte>();

            byte tznCounter = 1;

            foreach (RxCellLookup cellRates in prescription.Rates)
            {
                string key = GetRxRatesKey(cellRates);
                if (!treatmentZones.ContainsKey(key))
                {
                    ISOTreatmentZone tzn = GetNewType1TreatmentZone(cellRates, tznCounter, prescription);
                    treatmentZones.Add(key, tzn);
                    task.TreatmentZones.Add(tzn);
                    tznCounter++;
                }
                rateCodePerCell.Add(treatmentZones[key].TreatmentZoneCode);
            }
            return(rateCodePerCell);
        }
Example #13
0
        private string WriteType2GridFile(RasterGridPrescription prescription, ISOTreatmentZone treatmentZone)
        {
            var gridFileName = GenerateId(5);
            Dictionary <string, ISOUnit> unitsByDDI = new Dictionary <string, ISOUnit>();

            using (var binaryWriter = CreateWriter(Path.ChangeExtension(gridFileName, ".bin")))
            {
                byte[] previousBytes = BitConverter.GetBytes(0);
                foreach (var rxCellLookup in prescription.Rates)
                {
                    if (rxCellLookup.RxRates == null || !rxCellLookup.RxRates.Any())
                    {
                        //If there is null or no rate, write the previous rate (or 0 if we have not yet entered a valid rate)
                        binaryWriter.Write(previousBytes, 0, previousBytes.Length);
                    }
                    else
                    {
                        for (int index = 0; index < rxCellLookup.RxRates.Count; index++)
                        {
                            ISOProcessDataVariable pdv = treatmentZone.ProcessDataVariables[index];
                            var rate = rxCellLookup.RxRates[index].Rate;

                            ISOUnit unit = null;
                            if (!unitsByDDI.ContainsKey(pdv.ProcessDataDDI))
                            {
                                unit = UnitFactory.Instance.GetUnitByDDI(pdv.ProcessDataDDI.AsInt32DDI());
                                unitsByDDI.Add(pdv.ProcessDataDDI, unit);
                            }
                            unit = unitsByDDI[pdv.ProcessDataDDI];

                            previousBytes = BitConverter.GetBytes((int)Math.Round(unit.ConvertToIsoUnit(rate), 0));
                            binaryWriter.Write(previousBytes, 0, previousBytes.Length);
                        }
                    }
                }
            }

            return(gridFileName);
        }
Example #14
0
        internal bool LoadRates(string dataPath, ISOGrid grid, ISOTreatmentZone treatmentZone = null)
        {
            if (string.IsNullOrEmpty(grid.Filename))
            {
                return(false);
            }

            if (grid.GridType == 1)
            {
                TreatmentZoneCodes = grid.GetRatesForGridType1(dataPath);
                return(TreatmentZoneCodes?.Count == RowCount * ColumnCount);
            }
            else if (grid.GridType == 2)
            {
                ProductRates = grid.GetRatesForGridType2(dataPath, treatmentZone);
                return(ProductRates?.Count == RowCount * ColumnCount);
            }
            else
            {
                return(false);
            }
        }
        private ISOTreatmentZone ExportTreatmentZonesForType2(ISOTask task, RasterGridPrescription prescription)
        {
            if (prescription.ProductIds == null)
            {
                TaskDataMapper.AddError($"No Products are present for Grid Type 2 Prescription export: {prescription.Description}", prescription.Id.ReferenceId.ToString());
                return(null);
            }

            var lossOfSignalTreatmentZone = new ISOTreatmentZone {
                TreatmentZoneDesignator = "Loss of GPS", ProcessDataVariables = new List <ISOProcessDataVariable>()
            };
            var outOfFieldTreatmentZone = new ISOTreatmentZone {
                TreatmentZoneDesignator = "Out of Field", ProcessDataVariables = new List <ISOProcessDataVariable>()
            };
            var defaultTreatmentZone = new ISOTreatmentZone {
                TreatmentZoneDesignator = "Default", ProcessDataVariables = new List <ISOProcessDataVariable>()
            };

            foreach (var productId in prescription.ProductIds)
            {
                var isoUnit = DetermineIsoUnit(prescription.RxProductLookups.First(p => p.ProductId == productId).UnitOfMeasure);

                string isoProductId            = TaskDataMapper.InstanceIDMap.GetISOID(productId) ?? string.Empty;
                ISOProcessDataVariable lossPDV = ExportProcessDataVariable(prescription.LossOfGpsRate, isoProductId, isoUnit);
                if (lossPDV != null)
                {
                    lossOfSignalTreatmentZone.ProcessDataVariables.Add(lossPDV);
                }
                ISOProcessDataVariable oofPDV = ExportProcessDataVariable(prescription.OutOfFieldRate, isoProductId, isoUnit);
                if (oofPDV != null)
                {
                    outOfFieldTreatmentZone.ProcessDataVariables.Add(oofPDV);
                }
                ISOProcessDataVariable defaultPDV = ExportProcessDataVariable(prescription.LossOfGpsRate, isoProductId, isoUnit);  //ADAPT doesn't have a separate Default Rate.  Using Loss of GPS Rate as a logical equivalent for a default rate.
                if (defaultPDV == null)
                {
                    //Add 0 as the default rate so that we have at least one PDV to reference
                    var defaultRate = new NumericRepresentationValue(null, new NumericValue(prescription.RxProductLookups.First().UnitOfMeasure, 0));
                    defaultPDV = ExportProcessDataVariable(defaultRate, isoProductId, isoUnit);
                }
                defaultTreatmentZone.ProcessDataVariables.Add(defaultPDV);
            }

            if (lossOfSignalTreatmentZone.ProcessDataVariables.Count > 0)
            {
                lossOfSignalTreatmentZone.TreatmentZoneCode = 253;
                task.TreatmentZones.Add(lossOfSignalTreatmentZone);
                task.PositionLostTreatmentZoneCode = lossOfSignalTreatmentZone.TreatmentZoneCode;
            }

            if (outOfFieldTreatmentZone.ProcessDataVariables.Count > 0)
            {
                outOfFieldTreatmentZone.TreatmentZoneCode = 254;
                task.TreatmentZones.Add(outOfFieldTreatmentZone);
                task.OutOfFieldTreatmentZoneCode = outOfFieldTreatmentZone.TreatmentZoneCode;
            }

            defaultTreatmentZone.TreatmentZoneCode = 1;
            task.TreatmentZones.Add(defaultTreatmentZone);
            task.DefaultTreatmentZoneCode = defaultTreatmentZone.TreatmentZoneCode;

            return(defaultTreatmentZone);
        }
Example #16
0
        private ISOTreatmentZone ExportTreatmentZonesForType2(ISOTask task, RasterGridPrescription prescription)
        {
            if (prescription.ProductIds == null)
            {
                TaskDataMapper.AddError($"No Products are present for Grid Type 2 Prescription export: {prescription.Description}", prescription.Id.ReferenceId.ToString());
                return(null);
            }

            var lossOfSignalTreatmentZone = new ISOTreatmentZone {
                TreatmentZoneDesignator = "Loss of GPS", ProcessDataVariables = new List <ISOProcessDataVariable>()
            };
            var outOfFieldTreatmentZone = new ISOTreatmentZone {
                TreatmentZoneDesignator = "Out of Field", ProcessDataVariables = new List <ISOProcessDataVariable>()
            };
            var defaultTreatmentZone = new ISOTreatmentZone {
                TreatmentZoneDesignator = "Default", ProcessDataVariables = new List <ISOProcessDataVariable>()
            };

            foreach (var productId in prescription.ProductIds)
            {
                var isoUnit = DetermineIsoUnit(prescription.RxProductLookups.First(p => p.ProductId == productId).UnitOfMeasure);

                string          isoProductId  = TaskDataMapper.InstanceIDMap.GetISOID(productId) ?? string.Empty;
                RxProductLookup productLookup = prescription.RxProductLookups.FirstOrDefault(p => p.ProductId == productId);

                ISOProcessDataVariable lossPDV = ExportProcessDataVariable(productLookup?.LossOfGpsRate ?? prescription.LossOfGpsRate, isoProductId, isoUnit);
                if (lossPDV != null)
                {
                    lossOfSignalTreatmentZone.ProcessDataVariables.Add(lossPDV);
                }

                ISOProcessDataVariable oofPDV = ExportProcessDataVariable(productLookup?.OutOfFieldRate ?? prescription.OutOfFieldRate, isoProductId, isoUnit);
                if (oofPDV != null)
                {
                    outOfFieldTreatmentZone.ProcessDataVariables.Add(oofPDV);
                }

                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
                    var unitDimension = isoUnit.ToAdaptUnit().Dimension;
                    if (UnitFactory.DimensionToDdi.ContainsKey(unitDimension))
                    {
                        int ddi = UnitFactory.DimensionToDdi[unitDimension];
                        RepresentationMapper representationMapper = new RepresentationMapper();
                        var representation = representationMapper.Map(ddi) as NumericRepresentation;
                        if (representation == null)
                        {
                            representation = new NumericRepresentation
                            {
                                Code       = ddi.ToString(),
                                CodeSource = RepresentationCodeSourceEnum.ISO11783_DDI
                            };
                        }
                    }
                    else
                    {
                        TaskDataMapper.AddError($"Unable to identify a default representation: {prescription.Description}", prescription.Id.ReferenceId.ToString());
                        return(null);
                    }
                }
                //Add 0 as the default rate in the PDV; actual values are in the binary
                var defaultRate = new NumericRepresentationValue(defaultRepresentation, new NumericValue(prescription.RxProductLookups.First(p => p.ProductId == productId).UnitOfMeasure, 0d));
                ISOProcessDataVariable defaultPDV = ExportProcessDataVariable(defaultRate, isoProductId, isoUnit);
                defaultTreatmentZone.ProcessDataVariables.Add(defaultPDV);
            }

            if (lossOfSignalTreatmentZone.ProcessDataVariables.Count > 0)
            {
                lossOfSignalTreatmentZone.TreatmentZoneCode = 253;
                task.TreatmentZones.Add(lossOfSignalTreatmentZone);
                task.PositionLostTreatmentZoneCode = lossOfSignalTreatmentZone.TreatmentZoneCode;
            }

            if (outOfFieldTreatmentZone.ProcessDataVariables.Count > 0)
            {
                outOfFieldTreatmentZone.TreatmentZoneCode = 254;
                task.TreatmentZones.Add(outOfFieldTreatmentZone);
                task.OutOfFieldTreatmentZoneCode = outOfFieldTreatmentZone.TreatmentZoneCode;
            }

            defaultTreatmentZone.TreatmentZoneCode = 1;
            task.TreatmentZones.Add(defaultTreatmentZone);
            task.DefaultTreatmentZoneCode = defaultTreatmentZone.TreatmentZoneCode;

            return(defaultTreatmentZone);
        }