Esempio n. 1
0
 private static bool IsValidPrescription(RasterGridPrescription prescription)
 {
     return prescription.Rates != null &&
         prescription.CellHeight != null &&
         prescription.CellWidth != null &&
         prescription.Origin != null;
 }
Esempio n. 2
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;
 }
Esempio n. 3
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;
 }
Esempio n. 4
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();
        }
Esempio n. 5
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));
 }
Esempio n. 6
0
        private static void AddRate(int productId, double productRate, RxRates rates, RasterGridPrescription prescription, UnitOfMeasure uom)
        {
            RxProductLookup rxProductLookup;
            if (prescription.RxProductLookups.Any(x => x.ProductId == productId))
                rxProductLookup = prescription.RxProductLookups.Single(x => x.ProductId == productId);
            else
            {
                rxProductLookup = new RxProductLookup
                {
                    ProductId = productId,
                    UnitOfMeasure = uom

                };
                prescription.RxProductLookups.Add(rxProductLookup);
            }

            var rxRate = new RxRate
            {
                Rate = productRate,
                RxProductLookupId = rxProductLookup.Id.ReferenceId,
            };

            rates.RxRate.Add(rxRate);
        }
Esempio n. 7
0
        private void LoadRates(XmlNode inputNode, GridDescriptor gridDescriptor, Dictionary<int, TreatmentZone> treatmentZones, RasterGridPrescription prescription)
        {
            prescription.LossOfGpsRate = LoadRateFromTreatmentZones(inputNode.GetXmlNodeValue("@I"), treatmentZones).FirstOrDefault();
            prescription.OutOfFieldRate = LoadRateFromTreatmentZones(inputNode.GetXmlNodeValue("@J"), treatmentZones).FirstOrDefault();

            if (gridDescriptor.TreatmentZones != null)
            {
                var treatmentZone = treatmentZones.FindById(gridDescriptor.TreatmentZones.First());
                if (treatmentZone == null)
                    return;

                LoadProducts(treatmentZone, prescription);
                LoadRateUnits(treatmentZone, prescription);
                prescription.Rates = LoadRatesFromTreatmentZones(gridDescriptor, treatmentZones, prescription.ProductIds, prescription);
            }
            else if (gridDescriptor.ProductRates != null)
            {
                var treatmentZoneTemplate = treatmentZones.FindById(gridDescriptor.ProductRateTemplateId);
                if (treatmentZoneTemplate == null)
                    return;

                LoadProducts(treatmentZoneTemplate, prescription);
                LoadRateUnits(treatmentZoneTemplate, prescription);
                prescription.Rates = LoadRatesFromProducts(gridDescriptor, prescription.ProductIds, prescription);
            }
        }
Esempio n. 8
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;
 }
Esempio n. 9
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;
        }
Esempio n. 10
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);
        }
Esempio n. 11
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;
            }
        }
Esempio n. 12
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;
        }
Esempio n. 13
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();
        }
Esempio n. 14
0
        public void GivenTskWithGrdWhenMapThenPrescriptionIdFromCatalogIsPassedToOperationMapper()
        {
            var prescription = new RasterGridPrescription();
            prescription.Id.UniqueIds = new List<UniqueId>
            {
                new UniqueId
                {
                    CiTypeEnum = CompoundIdentifierTypeEnum.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 { CiTypeEnum = CompoundIdentifierTypeEnum.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);
        }
Esempio n. 15
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;
 }
Esempio n. 16
0
        private List<RxRates> LoadRatesFromProducts(GridDescriptor gridDescriptor, List<int> productIds, RasterGridPrescription prescription)
        {
            var rates = new List<RxRates>();
            foreach (var productRates in gridDescriptor.ProductRates)
            {
                var rate = new RxRates { RxRate = new List<RxRate>() };

                for (int productIndex = 0; productIndex < productRates.Count; productIndex++)
                {
                    var adaptProductId = productIds[productIndex];
                    UnitOfMeasure uom = null;
                    AddRate(adaptProductId, productRates[productIndex], rate, prescription, uom);
                }

                rates.Add(rate);
            }

            return rates;
        }
Esempio n. 17
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 }};
        }
Esempio n. 18
0
        private static List<RxRates> LoadRatesFromTreatmentZones(GridDescriptor gridDescriptor, Dictionary<int, TreatmentZone> treatmentZones, List<int> productIds, RasterGridPrescription prescription)
        {
            var rates = new List<RxRates>();
            foreach (var treatmentZoneId in gridDescriptor.TreatmentZones)
            {
                var treatmentZone = treatmentZones.FindById(treatmentZoneId);
                if (treatmentZone == null)
                    return null;

                var rate = new RxRates { RxRate = new List<RxRate>() };

                for (int i = 0; i < treatmentZone.Variables.Count; i++)
                {
                    var dataVariable = treatmentZone.Variables[i];
                    AddRate(productIds[i], dataVariable.Value, rate, prescription, treatmentZone.Variables[i].IsoUnit.ToAdaptUnit());
                }

                rates.Add(rate);
            }

            return rates;
        }
Esempio n. 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;
        }
Esempio n. 20
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;
        }