Esempio n. 1
0
        private void AddDataVariable(TreatmentZone treatmentZone, NumericRepresentationValue value, string productId, IsoUnit unit)
        {
            if (value != null && value.Value != null)
            {
                var targetValue = value.Value.Value;

                // Convert input value to Iso unit
                var adaptUnit = unit.ToAdaptUnit();
                UnitOfMeasure userUnit = null;
                if (adaptUnit != null && value.Value.UnitOfMeasure != null &&
                    adaptUnit.Dimension == value.Value.UnitOfMeasure.Dimension)
                {
                    userUnit = value.Value.UnitOfMeasure;
                    targetValue = _unitConverter.Convert(userUnit.ToInternalUom(), adaptUnit.ToInternalUom(), targetValue);
                }

                var dataVariable = new DataVariable
                {
                    ProductId = productId,
                    Value = targetValue,
                    IsoUnit = unit,
                    UserUnit = userUnit
                };

                treatmentZone.Variables.Add(dataVariable);
            }
        }
Esempio n. 2
0
 private void LoadDataVariables(XmlNodeList inputNodes, TreatmentZone zone)
 {
     foreach (XmlNode inputNode in inputNodes)
     {
         var dataVariable = LoadDataVariable(inputNode);
         if (dataVariable != null)
             zone.Variables.Add(dataVariable);
     }
 }
Esempio n. 3
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. 4
0
        public static string Write(XmlWriter writer, string zoneId, TreatmentZone treatmentZone)
        {
            if (treatmentZone == null)
                return null;

            writer.WriteStartElement("TZN");
            writer.WriteAttributeString("A", zoneId);
            writer.WriteXmlAttribute("B", treatmentZone.Name);

            if (treatmentZone.Variables != null)
            {
                foreach (var dataVariable in treatmentZone.Variables)
                {
                    WriteDataVariable(writer, dataVariable);
                }
            }

            writer.WriteEndElement();
            return zoneId;
        }
Esempio n. 5
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. 6
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. 7
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. 8
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;
        }
Esempio n. 9
0
        private TreatmentZone LoadTreatmentZone(XmlNode inputNode, out int zoneId)
        {
            // Required fields. Do not proceed if they are missing
            if (!inputNode.GetXmlNodeValue("@A").ParseValue(out zoneId))
                return null;

            // Optional fields
            var zone = new TreatmentZone { Variables = new List<DataVariable>() };
            zone.Name = inputNode.GetXmlNodeValue("@B");
            LoadDataVariables(inputNode.SelectNodes("PDV"), zone);

            return zone;
        }