Пример #1
0
        private RasterGridPrescription LoadPrescription(XmlNode inputNode)
        {
            if (!HasPrescription(inputNode))
            {
                return(null);
            }

            var prescription = new RasterGridPrescription();

            // Required fields. Do not proceed if they are missing
            var prescriptionId = inputNode.GetXmlNodeValue("@A");

            if (prescriptionId == null)
            {
                return(null);
            }

            var isoId = ImportHelper.CreateUniqueId(prescriptionId);

            prescription.Id.UniqueIds.Add(isoId);

            // Optional fields
            prescription.Description = inputNode.GetXmlNodeValue("@B");

            LoadFieldAndCropZone(inputNode.GetXmlNodeValue("@E"), prescription);

            LoadGrid(inputNode, prescription);

            _taskDocument.LoadLinkedIds(prescriptionId, prescription.Id);
            return(prescription);
        }
Пример #2
0
 private static bool IsValidPrescription(RasterGridPrescription prescription)
 {
     return(prescription.Rates != null &&
            prescription.CellHeight != null &&
            prescription.CellWidth != null &&
            prescription.Origin != null);
 }
Пример #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);
            }
        }
Пример #4
0
        public RasterGridPrescription Import(ISOTask task, WorkItem workItem)
        {
            RasterGridPrescription rasterPrescription = new RasterGridPrescription();

            _prescriptionMapper.ImportSharedPrescriptionProperties(task, workItem, rasterPrescription);

            GridDescriptor gridDescriptor = LoadGridDescriptor(task, TaskDataPath);

            if (gridDescriptor != null)
            {
                rasterPrescription.BoundingBox      = new BoundingBox();
                rasterPrescription.BoundingBox.MinY = new NumericRepresentationValue(RepresentationInstanceList.vrLatitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), gridDescriptor.Origin.Y));
                rasterPrescription.BoundingBox.MinX = new NumericRepresentationValue(RepresentationInstanceList.vrLongitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), gridDescriptor.Origin.X));
                var maxYValue = rasterPrescription.BoundingBox.MinY.Value.Value + gridDescriptor.CellHeight.Value.Value * gridDescriptor.RowCount;
                var maxXValue = rasterPrescription.BoundingBox.MinX.Value.Value + gridDescriptor.CellWidth.Value.Value * gridDescriptor.ColumnCount;
                rasterPrescription.BoundingBox.MaxY = new NumericRepresentationValue(RepresentationInstanceList.vrLatitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), maxYValue));
                rasterPrescription.BoundingBox.MaxX = new NumericRepresentationValue(RepresentationInstanceList.vrLongitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), maxXValue));
                rasterPrescription.Origin           = gridDescriptor.Origin;
                rasterPrescription.CellHeight       = gridDescriptor.CellHeight;
                rasterPrescription.CellWidth        = gridDescriptor.CellWidth;
                rasterPrescription.ColumnCount      = gridDescriptor.ColumnCount;
                rasterPrescription.RowCount         = gridDescriptor.RowCount;

                ImportRates(task, gridDescriptor, rasterPrescription);
            }
            return(rasterPrescription);
        }
Пример #5
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);
        }
Пример #6
0
        private void LoadRateUnits(TreatmentZone treatmentZone, RasterGridPrescription prescription)
        {
            if (prescription.RxProductLookups == null)
            {
                prescription.RxProductLookups = new List <RxProductLookup>();
            }

            var rxRates = new List <RxRate>();

            foreach (var dataVariable in treatmentZone.Variables)
            {
                var product         = _taskDocument.Products.FindById(dataVariable.ProductId) ?? _taskDocument.ProductMixes.FindById(dataVariable.ProductId);
                var rxProductLookup = new RxProductLookup
                {
                    ProductId     = product == null ? 0 : product.Id.FindIntIsoId(),
                    UnitOfMeasure = dataVariable.IsoUnit.ToAdaptUnit(),
                };
                prescription.RxProductLookups.Add(rxProductLookup);
                var rxRate = new RxRate
                {
                    Rate = dataVariable.Value,
                    RxProductLookupId = rxProductLookup.Id.ReferenceId,
                };
                rxRates.Add(rxRate);
            }
            prescription.Rates = new List <RxRates> {
                new RxRates {
                    RxRate = rxRates
                }
            };
        }
Пример #7
0
        private string WriteGridFile(RasterGridPrescription prescription, TreatmentZone treatmentZone)
        {
            var gridFileName = GenerateId(5);

            using (var binaryWriter = CreateWriter(Path.ChangeExtension(gridFileName, ".BIN")))
            {
                byte[] previousBytes = BitConverter.GetBytes(0);
                foreach (var rxRate in prescription.Rates)
                {
                    if (rxRate.RxRate == null || !rxRate.RxRate.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 < rxRate.RxRate.Count; index++)
                        {
                            var dataVariable = treatmentZone.Variables[index];
                            var rate         = rxRate.RxRate[index].Rate;
                            if (dataVariable.UserUnit != null)
                            {
                                rate = _unitConverter.Convert(dataVariable.UserUnit.ToInternalUom(), dataVariable.IsoUnit.ToAdaptUnit().ToInternalUom(), rate);
                            }

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

            return(gridFileName);
        }
Пример #8
0
 private static double GetOriginY(RasterGridPrescription prescription)
 {
     if (prescription.BoundingBox != null && prescription.BoundingBox.MinY != null)
     {
         return(prescription.BoundingBox.MinY.Value.Value);
     }
     return(prescription.Origin.Y);
 }
Пример #9
0
        private TreatmentZone WriteTreatmentZones(XmlWriter writer, RasterGridPrescription prescription)
        {
            if (prescription.ProductIds == null)
            {
                return(null);
            }

            var lossOfSignalTreatmentZone = new TreatmentZone {
                Name = "Loss of GPS", Variables = new List <DataVariable>()
            };
            var outOfFieldTreatmentZone = new TreatmentZone {
                Name = "Out of Field", Variables = new List <DataVariable>()
            };
            var defaultTreatmentZone = new TreatmentZone {
                Name = "Default", Variables = new List <DataVariable>()
            };

            var defaultRate = new NumericRepresentationValue(null, new NumericValue(prescription.RxProductLookups.First().UnitOfMeasure, 0));
            var isoUnit     = DetermineIsoUnit(prescription.RxProductLookups.First().UnitOfMeasure);

            foreach (var productId in prescription.ProductIds)
            {
                var isoProductId = TaskWriter.Products.FindById(productId) ?? TaskWriter.CropVarieties.FindById(productId);

                AddDataVariable(lossOfSignalTreatmentZone, prescription.LossOfGpsRate, isoProductId, isoUnit);
                AddDataVariable(outOfFieldTreatmentZone, prescription.OutOfFieldRate, isoProductId, isoUnit);
                AddDataVariable(defaultTreatmentZone, defaultRate, isoProductId, isoUnit);
            }

            var lossOfSignalZoneId = "253";

            if (lossOfSignalTreatmentZone.Variables.Count > 0)
            {
                writer.WriteXmlAttribute("I", lossOfSignalZoneId);
            }

            var outOfFieldZoneId = "254";

            if (outOfFieldTreatmentZone.Variables.Count > 0)
            {
                writer.WriteXmlAttribute("J", outOfFieldZoneId);
            }

            TreatmentZoneWriter.Write(writer, "1", defaultTreatmentZone);
            if (lossOfSignalTreatmentZone.Variables.Count > 0)
            {
                TreatmentZoneWriter.Write(writer, lossOfSignalZoneId, lossOfSignalTreatmentZone);
            }
            if (outOfFieldTreatmentZone.Variables.Count > 0)
            {
                TreatmentZoneWriter.Write(writer, outOfFieldZoneId, outOfFieldTreatmentZone);
            }

            return(defaultTreatmentZone);
        }
Пример #10
0
        private void WritePrescription(RasterGridPrescription prescription)
        {
            var writer = TaskWriter.RootWriter;

            if (!IsValidPrescription(prescription))
            {
                return;
            }

            var prescriptionId = prescription.Id.FindIsoId() ?? GenerateId();

            writer.WriteStartElement(XmlPrefix);
            writer.WriteAttributeString("A", prescriptionId);
            writer.WriteAttributeString("B", prescription.Description);

            WriteFieldMeta(writer, prescription.FieldId);

            // Task status - planned
            writer.WriteAttributeString("G", "1");

            var defaultTreatmentZone = WriteTreatmentZones(writer, prescription);

            _gridWriter.Write(writer, prescription, defaultTreatmentZone);
            var matchingLoggedData = null as LoggedData;

            if (TaskWriter.DataModel.Documents != null && TaskWriter.DataModel.Documents.LoggedData != null)
            {
                matchingLoggedData = TaskWriter.DataModel.Documents.LoggedData.Where(ld => ld.OperationData != null).SingleOrDefault(x => x.OperationData.FirstOrDefault(y => y.PrescriptionId == prescription.Id.ReferenceId) != null);
            }

            if (matchingLoggedData != null)
            {
                var taskMapper = new TaskMapper();
                var isoInt     = Convert.ToInt32(prescriptionId.Remove(0, 3)) - 1;


                var mappedTsk = taskMapper.Map(new List <LoggedData> {
                    matchingLoggedData
                }, TaskWriter.DataModel.Catalog, TaskWriter.BaseFolder, isoInt, TaskWriter).First();

                foreach (var item in mappedTsk.Items)
                {
                    item.WriteXML(TaskWriter.RootWriter);
                }
            }
            else
            {
                TaskWriter.Ids.Add(prescriptionId, prescription.Id);
            }

            writer.WriteEndElement();
        }
Пример #11
0
 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);
     }
 }
Пример #12
0
        private void LoadProducts(TreatmentZone treatmentZone, RasterGridPrescription prescription)
        {
            var productIds = new List <int>();

            foreach (var dataVariable in treatmentZone.Variables)
            {
                Product product = _taskDocument.CropVarieties.FindById(dataVariable.ProductId)
                                  ?? (_taskDocument.Products.FindById(dataVariable.ProductId)
                                      ?? _taskDocument.ProductMixes.FindById(dataVariable.ProductId));
                productIds.Add(product == null ? 0 : product.Id.ReferenceId);
            }
            prescription.ProductIds = productIds;
        }
Пример #13
0
        private void LoadGrid(XmlNode inputNode, RasterGridPrescription prescription)
        {
            var treatmentZones = TreatmentZoneLoader.Load(inputNode, _taskDocument);
            var gridDescriptor = GridLoader.Load(inputNode, treatmentZones, _taskDocument.BaseFolder);

            if (gridDescriptor == null)
            {
                return;
            }

            LoadDefinition(gridDescriptor, prescription);
            LoadRates(inputNode, gridDescriptor, treatmentZones, prescription);
        }
Пример #14
0
        public void Write(XmlWriter writer, RasterGridPrescription prescription, TreatmentZone treatmentZone)
        {
            writer.WriteStartElement(XmlPrefix);

            WriteGridDefinition(writer, prescription);

            var gridFileName = WriteGridFile(prescription, treatmentZone);

            writer.WriteAttributeString("G", gridFileName);
            writer.WriteAttributeString("I", "2");
            writer.WriteAttributeString("J", "1");

            writer.WriteEndElement();
        }
Пример #15
0
        private static double GetCellWidth(RasterGridPrescription prescription)
        {
            var cellWidth = 0.0;

            if (prescription.CellWidth != null)
            {
                cellWidth = prescription.CellWidth.Value.Value;
            }
            if (prescription.BoundingBox != null && prescription.BoundingBox.MinX != null && prescription.BoundingBox.MaxX != null)
            {
                cellWidth = Math.Abs(prescription.BoundingBox.MaxX.Value.Value - prescription.BoundingBox.MinX.Value.Value) / prescription.ColumnCount;
            }
            return(cellWidth);
        }
Пример #16
0
        private static double GetCellHeight(RasterGridPrescription prescription)
        {
            var cellHeight = 0.0;

            if (prescription.CellHeight != null)
            {
                cellHeight = prescription.CellHeight.Value.Value;
            }
            if (prescription.BoundingBox != null && prescription.BoundingBox.MinY != null && prescription.BoundingBox.MaxY != null)
            {
                cellHeight = Math.Abs(prescription.BoundingBox.MaxY.Value.Value - prescription.BoundingBox.MinY.Value.Value) / prescription.RowCount;
            }
            return(cellHeight);
        }
Пример #17
0
        private static void WriteGridDefinition(XmlWriter writer, RasterGridPrescription prescription)
        {
            var cellWidth  = GetCellWidth(prescription);
            var cellHeight = GetCellHeight(prescription);
            var originY    = GetOriginY(prescription);

            writer.WriteAttributeString("A", originY.ToString());
            var originX = GetOriginX(prescription);

            writer.WriteAttributeString("B", originX.ToString());
            writer.WriteAttributeString("C", cellHeight.ToString("F14", CultureInfo.InvariantCulture));
            writer.WriteAttributeString("D", cellWidth.ToString("F14", CultureInfo.InvariantCulture));
            writer.WriteAttributeString("E", prescription.ColumnCount.ToString(CultureInfo.InvariantCulture));
            writer.WriteAttributeString("F", prescription.RowCount.ToString(CultureInfo.InvariantCulture));
        }
Пример #18
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);
        }
Пример #19
0
        private static void LoadDefinition(GridDescriptor gridDescriptor, RasterGridPrescription prescription)
        {
            prescription.BoundingBox      = new BoundingBox();
            prescription.BoundingBox.MinY = new NumericRepresentationValue(RepresentationInstanceList.vrLatitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), gridDescriptor.Origin.Y));
            prescription.BoundingBox.MinX = new NumericRepresentationValue(RepresentationInstanceList.vrLongitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), gridDescriptor.Origin.X));
            var maxYValue = prescription.BoundingBox.MinY.Value.Value + gridDescriptor.CellHeight.Value.Value * gridDescriptor.RowCount;
            var maxXValue = prescription.BoundingBox.MinX.Value.Value + gridDescriptor.CellWidth.Value.Value * gridDescriptor.ColumnCount;

            prescription.BoundingBox.MaxY = new NumericRepresentationValue(RepresentationInstanceList.vrLatitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), maxYValue));
            prescription.BoundingBox.MaxX = new NumericRepresentationValue(RepresentationInstanceList.vrLongitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), maxXValue));

            prescription.Origin     = gridDescriptor.Origin;
            prescription.CellHeight = gridDescriptor.CellHeight;
            prescription.CellWidth  = gridDescriptor.CellWidth;

            prescription.ColumnCount = gridDescriptor.ColumnCount;
            prescription.RowCount    = gridDescriptor.RowCount;
        }
Пример #20
0
        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);
        }
Пример #21
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);
        }
Пример #22
0
        public void GivenTskWithGrdWhenMapThenPrescriptionIdFromCatalogIsPassedToOperationMapper()
        {
            var prescription = new RasterGridPrescription();

            prescription.Id.UniqueIds = new List <UniqueId>
            {
                new UniqueId
                {
                    IdType = IdTypeEnum.String,
                    Id     = "FIX1",
                    Source = "http://dictionary.isobus.net/isobus/"
                }
            };

            _catalog.Prescriptions = new List <Prescription> {
                prescription
            };


            var existingLoggedData = new LoggedData();

            existingLoggedData.Id.UniqueIds.Add(new UniqueId {
                IdType = IdTypeEnum.String, Id = "FIX1", Source = UniqueIdMapper.IsoSource
            });
            _documents.LoggedData = new List <LoggedData> {
                existingLoggedData
            };

            var grd = new GRD();

            _tsk.Items = new List <IWriter> {
                grd
            }.ToArray();
            _tsk.A = "FIX1";

            MapSingle();

            _operationDataMapper.Verify(x => x.Map(It.IsAny <List <TLG> >(), prescription.Id.ReferenceId, _dataPath, existingLoggedData.Id.ReferenceId, _linkIds), Times.Once);
        }
Пример #23
0
        private void LoadFieldAndCropZone(string fieldId, RasterGridPrescription prescription)
        {
            if (string.IsNullOrEmpty(fieldId))
            {
                return;
            }

            var cropZone = _taskDocument.CropZones.FindById(fieldId);

            if (cropZone != null)
            {
                prescription.CropZoneId = cropZone.Id.ReferenceId;
                prescription.FieldId    = cropZone.FieldId;
            }
            else
            {
                var field = _taskDocument.Fields.FindById(fieldId);
                if (field != null)
                {
                    prescription.FieldId = field.Id.ReferenceId;
                }
            }
        }
Пример #24
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;
                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);
        }
Пример #25
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);
        }
Пример #26
0
        private List <RxCellLookup> ImportRatesFromProducts(GridDescriptor gridDescriptor, List <int> productIds, RasterGridPrescription prescription)
        {
            var  rates = new List <RxCellLookup>();
            bool binaryDataMatchesDefinition = true;

            foreach (var productRates in gridDescriptor.ProductRates)
            {
                var lookup = new RxCellLookup {
                    RxRates = new List <RxRate>()
                };
                for (int productIndex = 0; productIndex < productRates.Count; productIndex++)
                {
                    int adaptProductId = 0;
                    if (productIds.Count > productIndex)
                    {
                        adaptProductId = productIds[productIndex];
                    }
                    else if (productIds.Count > 0)
                    {
                        binaryDataMatchesDefinition = false;
                    }
                    lookup.RxRates.Add(PrescriptionMapper.ImportRate(adaptProductId, productRates[productIndex], prescription));
                }
                rates.Add(lookup);
            }
            if (!binaryDataMatchesDefinition)
            {
                TaskDataMapper.AddError($"Binary Grid Data for Type-2 Grid {TaskDataMapper.InstanceIDMap.GetISOID(prescription.Id.ReferenceId)} does not match its definition.  Product data will be omitted.");
            }
            return(rates);
        }
        private Feature Map(RasterGridPrescription prescription)
        {
            GeoJSON.Net.Geometry.IGeometryObject geometry   = null;
            Dictionary <string, object>          properties = new Dictionary <string, object>();

            if (prescription.BoundingBox != null)
            {
                geometry = PolygonMapper.MapBoundingBox(prescription.BoundingBox, _properties.AffineTransformation);
            }
            else
            {
                geometry = PointMapper.MapPoint2Point(prescription.Origin, _properties.AffineTransformation);
            }

            properties.Add("RowCount", prescription.RowCount);
            properties.Add("ColumnCount", prescription.ColumnCount);
            properties.Add("CellWidth", prescription.CellWidth.Value.Value);
            properties.Add("CellHeight", prescription.CellHeight.Value.Value);
            //properties.Add("RxProductLookups", prescription.RxProductLookups);  // Id, ProductId, Representation, UnitOfMeasure

            // SpatialPrescription: not sure these rates mean anything for the geojson output
            double outOfFieldRate = -1.0;               // something save to compare with

            if (prescription.OutOfFieldRate != null)
            {
                properties.Add("OutOfFieldRate", prescription.OutOfFieldRate.Value.Value);
                outOfFieldRate = prescription.OutOfFieldRate.Value.Value;
            }
            if (prescription.LossOfGpsRate != null)
            {
                properties.Add("LossOfGpsRate", prescription.LossOfGpsRate.Value.Value);
            }

            double minRate = double.MaxValue;
            double maxRate = -1.0;

            foreach (var rate in prescription.Rates)
            {
                if (rate.RxRates[0].Rate != outOfFieldRate)
                {
                    minRate = Math.Min(minRate, rate.RxRates[0].Rate);
                    maxRate = Math.Max(maxRate, rate.RxRates[0].Rate);
                }
            }
            properties.Add("MinRate", minRate);
            properties.Add("MaxRate", maxRate);

            List <Dictionary <string, object> > products = new List <Dictionary <string, object> > {
            };

            foreach (var rxproduct in prescription.RxProductLookups)
            {
                if (products.Where(p => p.ContainsKey("productId") && (int)p["productId"] == rxproduct.ProductId).FirstOrDefault() == null)
                {
                    Dictionary <string, object> product = new Dictionary <string, object>();
                    product.Add("productId", rxproduct.ProductId);
                    product.Add("productCode", rxproduct.Representation.Code);
                    product.Add("productUom", rxproduct.UnitOfMeasure.Code);
                    Product adaptProduct = _dataModel.Catalog.Products.Where(p => p.Id.ReferenceId == rxproduct.ProductId).FirstOrDefault();
                    if (adaptProduct != null)
                    {
                        product.Add("productDescription", adaptProduct.Description);
                        product.Add("productType", adaptProduct.ProductType.ToString());                         // or via GetName?
                    }
                    products.Add(product);
                }
            }
            //Array Values = products.Select(x => (Object)x).ToArray(); ;
            properties.Add("Products", products.ToArray());

            return(new Feature(geometry, properties));
        }
        private List <Feature> MapMultiple(RasterGridPrescription prescription)
        {
            List <Feature> features = new List <Feature>();

            // Based on Open.Topology.TestRunner.Functions/CreateShapeFunctions.Grid
            var    grid = new List <BoundingBox>();
            int    nCellsOnSideX = (int)prescription.ColumnCount;
            int    nCellsOnSideY = (int)prescription.RowCount;
            double cellSizeX = prescription.CellWidth.Value.Value;
            double cellSizeY = prescription.CellHeight.Value.Value;
            double dMinX, dMinY;

            if (prescription.BoundingBox != null)
            {
                dMinX = prescription.BoundingBox.MinX.Value.Value;
                dMinY = prescription.BoundingBox.MinY.Value.Value;
            }
            else
            {
                dMinX = prescription.Origin.X;
                dMinY = prescription.Origin.Y;
            }

            for (int j = 0; j < nCellsOnSideY; j++)
            {
                for (int i = 0; i < nCellsOnSideX; i++)
                {
                    double x1 = dMinX + i * cellSizeX;
                    double y1 = dMinY + j * cellSizeY;
                    double x2 = dMinX + (i + 1) * cellSizeX;
                    double y2 = dMinY + (j + 1) * cellSizeY;

                    var bbox = new BoundingBox();
                    bbox.MinY = new NumericRepresentationValue(RepresentationInstanceList.vrLatitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), y1));
                    bbox.MinX = new NumericRepresentationValue(RepresentationInstanceList.vrLongitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), x1));
                    bbox.MaxY = new NumericRepresentationValue(RepresentationInstanceList.vrLatitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), y2));
                    bbox.MaxX = new NumericRepresentationValue(RepresentationInstanceList.vrLongitude.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("arcdeg"), x2));
                    grid.Add(bbox);
                }
            }

            int    index          = 0;
            double outOfFieldRate = -1.0;               // something save to compare with

            if (prescription.OutOfFieldRate != null)
            {
                outOfFieldRate = prescription.OutOfFieldRate.Value.Value;
            }

            Console.WriteLine($"PrescriptionMapper outOfFieldRate: {outOfFieldRate}, grids {grid.Count}");

            // @ToDo merge adjacent grid boxes with same property values?
            foreach (var bbox in grid)
            {
                // skip outOfField grids
                if (prescription.Rates[index].RxRates[0].Rate != outOfFieldRate)
                {
                    GeoJSON.Net.Geometry.IGeometryObject geometry   = PolygonMapper.MapBoundingBox(bbox, _properties.AffineTransformation);
                    Dictionary <string, object>          properties = new Dictionary <string, object>();
                    RxProductLookup product = prescription.RxProductLookups.Where(r => r.Id.ReferenceId == prescription.Rates[index].RxRates[0].RxProductLookupId).FirstOrDefault();
                    if (product != null)
                    {
                        properties.Add("productId", product.ProductId);
                        Product adaptProduct = _dataModel.Catalog.Products.Where(p => p.Id.ReferenceId == product.ProductId).FirstOrDefault();
                        if (adaptProduct != null)
                        {
                            properties.Add("productDescription", adaptProduct.Description);
                            properties.Add("productType", adaptProduct.ProductType.ToString());                                 // or via GetName?
                        }
                        properties.Add("productCode", product.Representation.Code);
                        properties.Add("productUom", product.UnitOfMeasure.Code);
                    }
                    else
                    {
                        properties.Add("productId", prescription.Rates[index].RxRates[0].RxProductLookupId);
                    }
                    properties.Add("rate", prescription.Rates[index].RxRates[0].Rate);

                    features.Add(new Feature(geometry, properties));
                }
                index++;
            }

            return(features);
        }
Пример #29
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);
        }
Пример #30
0
        private List <RxCellLookup> ImportRatesFromProducts(GridDescriptor gridDescriptor, List <int> productIds, RasterGridPrescription prescription)
        {
            var rates = new List <RxCellLookup>();

            foreach (var productRates in gridDescriptor.ProductRates)
            {
                var lookup = new RxCellLookup {
                    RxRates = new List <RxRate>()
                };
                for (int productIndex = 0; productIndex < productRates.Count; productIndex++)
                {
                    int adaptProductId = productIds[productIndex];
                    lookup.RxRates.Add(PrescriptionMapper.ImportRate(adaptProductId, productRates[productIndex], prescription));
                }
                rates.Add(lookup);
            }

            return(rates);
        }