public VectorPrescription ImportVectorPrescription(ISOTask task, WorkItem workItem) { VectorPrescription vectorRx = new VectorPrescription(); ImportSharedPrescriptionProperties(task, workItem, vectorRx); vectorRx.RxShapeLookups = new List <RxShapeLookup>(); foreach (ISOTreatmentZone treatmentZone in task.TreatmentZones) { RxShapeLookup shapeLookup = new RxShapeLookup(); //Rates shapeLookup.Rates = new List <RxRate>(); foreach (ISOProcessDataVariable pdv in treatmentZone.ProcessDataVariables) { int?productID = TaskDataMapper.InstanceIDMap.GetADAPTID(pdv.ProductIdRef); if (productID.HasValue) { shapeLookup.Rates.Add(PrescriptionMapper.ImportAndConvertRate(productID.Value, pdv, vectorRx)); } } //Shapes PolygonMapper polygonMapper = new PolygonMapper(TaskDataMapper); shapeLookup.Shape = new MultiPolygon(); shapeLookup.Shape.Polygons = polygonMapper.ImportPolygons(treatmentZone.Polygons).ToList(); //Add to the collection vectorRx.RxShapeLookups.Add(shapeLookup); } return(vectorRx); }
public void ProcessPrescription(Prescription prescription) { if (prescription is VectorPrescription) //Only Vector currently supported for the Visualizer map { VectorPrescription vectorPrescription = prescription as VectorPrescription; using (var graphics = _spatialViewer.CreateGraphics()) { _drawingUtil = new DrawingUtil(_spatialViewer.Width, _spatialViewer.Height, graphics); List <Point> allPoints = new List <Point>(); List <List <Point> > projectedPointsPerPolygon = new List <List <Point> >(); foreach (Polygon polygon in vectorPrescription.RxShapeLookups.SelectMany(x => x.Shape.Polygons)) { List <Point> polygonPoints = new List <Point>(); foreach (Point point in polygon.ExteriorRing.Points) { Point projectedPoint = point.ToUtm(); allPoints.Add(projectedPoint); polygonPoints.Add(projectedPoint); } projectedPointsPerPolygon.Add(polygonPoints); } _drawingUtil.SetMinMax(allPoints); foreach (List <Point> polygonPoints in projectedPointsPerPolygon) { var screenPolygon = polygonPoints.Select(point => point.ToXy(_drawingUtil.MinX, _drawingUtil.MinY, _drawingUtil.GetDelta())).ToArray(); graphics.DrawPolygon(DrawingUtil.B_Black, screenPolygon); } } } }
private List <Feature> MapMultiple(VectorPrescription prescription) { List <Feature> features = new List <Feature>(); double outOfFieldRate = -1.0; // something save to compare with if (prescription.OutOfFieldRate != null) { outOfFieldRate = prescription.OutOfFieldRate.Value.Value; } foreach (var shaperate in prescription.RxShapeLookups) { int index = 0; if (shaperate.Shape != null && shaperate.Shape.Polygons.Count > 0) { foreach (var adaptPolygon in shaperate.Shape.Polygons) { Dictionary <string, object> properties = new Dictionary <string, object>(); RxProductLookup product = prescription.RxProductLookups.Where(r => r.Id.ReferenceId == shaperate.Rates[index].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()); } properties.Add("productCode", product.Representation.Code); properties.Add("productUom", product.UnitOfMeasure.Code); } else { properties.Add("productId", shaperate.Rates[index].RxProductLookupId); } properties.Add("rate", shaperate.Rates[index++].Rate); features.Add(new Feature(PolygonMapper.MapPolygon(adaptPolygon, _properties.AffineTransformation), properties)); } } } return(features); }
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 Feature Map(VectorPrescription prescription) { Feature feature = null; Dictionary <string, object> properties = new Dictionary <string, object>(); // 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; foreach (var shaperate in prescription.RxShapeLookups) { if (shaperate.Shape != null && shaperate.Shape.Polygons.Count > 0) { foreach (var rate in shaperate.Rates) { if (rate.Rate != outOfFieldRate) { minRate = Math.Min(minRate, rate.Rate); maxRate = Math.Max(maxRate, rate.Rate); } } } else // outoffield { foreach (var rate in shaperate.Rates) { RxProductLookup product = prescription.RxProductLookups.Where(r => r.Id.ReferenceId == rate.RxProductLookupId).FirstOrDefault(); if (product == null) { Console.WriteLine("outoffield rate? " + rate.Rate + " " + rate.RxProductLookupId); } else { Console.WriteLine("outoffield rate? " + rate.Rate + " " + rate.RxProductLookupId + " " + product.Representation.Code); } } } } 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); } } properties.Add("Products", products.ToArray()); if (prescription.BoundingBox != null) { GeoJSON.Net.Geometry.Polygon polygon = PolygonMapper.MapBoundingBox(prescription.BoundingBox, _properties.AffineTransformation); // MinX, MinY, MaxX, MaxY feature = new Feature(polygon, properties); } else { // @ToDo dissolve multipolygon or use Envelope? // NetTopologySuite.Operation.Union.CascadedPolygonUnion // NetTopologySuite.Algorithm.MinimumDiameter.GetMinimumRectangle var polygons = new List <GeoJSON.Net.Geometry.Polygon>(); foreach (var shaperate in prescription.RxShapeLookups) { if (shaperate.Shape != null && shaperate.Shape.Polygons.Count > 0) { foreach (var adaptPolygon in shaperate.Shape.Polygons) { GeoJSON.Net.Geometry.Polygon polygon = PolygonMapper.MapPolygon(adaptPolygon, _properties.AffineTransformation); if (polygon != null) { polygons.Add(polygon); } } } } feature = new Feature(new GeoJSON.Net.Geometry.MultiPolygon(polygons), properties); } return(feature); }
public static void Export(string path) { ApplicationDataModel adm = new ApplicationDataModel(); adm.Catalog = new Catalog(); adm.Documents = new Documents(); //-------------------------- //Setup information //-------------------------- //Add a crop Crop corn = new Crop() { Name = "Corn" }; adm.Catalog.Crops.Add(corn); //Add some seed varieties CropVarietyProduct seedVariety1 = new CropVarietyProduct() { CropId = corn.Id.ReferenceId, Description = "Variety 1" }; CropVarietyProduct seedVariety2 = new CropVarietyProduct() { CropId = corn.Id.ReferenceId, Description = "Variety 2" }; adm.Catalog.Products.Add(seedVariety1); adm.Catalog.Products.Add(seedVariety2); //Add a liquid product CropNutritionProduct fertilizer = new CropNutritionProduct() { Description = "Starter", Form = ProductFormEnum.Liquid }; fertilizer.ProductType = ProductTypeEnum.Fertilizer; adm.Catalog.Products.Add(fertilizer); //Add a granular product CropProtectionProduct insecticide = new CropProtectionProduct() { Description = "Insecticide", Form = ProductFormEnum.Solid }; insecticide.ProductType = ProductTypeEnum.Chemical; adm.Catalog.Products.Add(insecticide); //GFF Grower grower = new Grower() { Name = "Example Grower" }; adm.Catalog.Growers.Add(grower); Farm farm = new Farm() { Description = "Example Farm", GrowerId = grower.Id.ReferenceId }; adm.Catalog.Farms.Add(farm); Field field = new Field() { Description = "Example Field", FarmId = farm.Id.ReferenceId, GrowerId = grower.Id.ReferenceId }; field.Area = GetNumericRepresentationValue(23d, "ha", "vrReportedFieldArea"); adm.Catalog.Fields.Add(field); //Crop zone TimeScope season = new TimeScope() { DateContext = DateContextEnum.CropSeason, TimeStamp1 = new DateTime(2021, 1, 1) }; CropZone cropZone = new CropZone() { CropId = corn.Id.ReferenceId, FieldId = field.Id.ReferenceId, TimeScopes = new List <TimeScope>() { season } }; adm.Catalog.CropZones.Add(cropZone); //Field boundary FieldBoundary boundary = new FieldBoundary() { SpatialData = new MultiPolygon() { Polygons = new List <Polygon>() { new Polygon() { ExteriorRing = new LinearRing() { Points = new List <Point>() { new Point() { X = -89.488565, Y = 40.478304 }, new Point() { X = -89.485439, Y = 40.478304 }, new Point() { X = -89.485439, Y = 40.475010 }, new Point() { X = -89.488565, Y = 40.475010 } } }, InteriorRings = new List <LinearRing>() { new LinearRing() { Points = new List <Point>() { new Point() { X = -89.487719, Y = 40.478091 }, new Point() { X = -89.487536, Y = 40.478091 }, new Point() { X = -89.487536, Y = 40.477960 }, new Point() { X = -89.487719, Y = 40.477960 }, new Point() { X = -89.487719, Y = 40.478091 } } }, new LinearRing() { Points = new List <Point>() { new Point() { X = -89.486732, Y = 40.478172 }, new Point() { X = -89.486453, Y = 40.478172 }, new Point() { X = -89.486453, Y = 40.478082 }, new Point() { X = -89.486732, Y = 40.478082 }, new Point() { X = -89.486732, Y = 40.478172 } } } } } } }, FieldId = field.Id.ReferenceId }; adm.Catalog.FieldBoundaries.Add(boundary); field.ActiveBoundaryId = boundary.Id.ReferenceId; //-------------------------- //Prescription //-------------------------- //Prescription setup data //Setup the representation and units for seed rate & seed depth prescriptions NumericRepresentation seedRate = GetNumericRepresentation("vrSeedRateSeedsTarget"); UnitOfMeasure seedUOM = UnitInstance.UnitSystemManager.GetUnitOfMeasure("seeds1ac-1"); RxProductLookup seedVariety1RateLookup = new RxProductLookup() { ProductId = seedVariety1.Id.ReferenceId, Representation = seedRate, UnitOfMeasure = seedUOM }; RxProductLookup seedVariety2RateLookup = new RxProductLookup() { ProductId = seedVariety2.Id.ReferenceId, Representation = seedRate, UnitOfMeasure = seedUOM }; NumericRepresentation seedDepth = GetNumericRepresentation("vrSeedDepthTarget"); UnitOfMeasure depthUOM = UnitInstance.UnitSystemManager.GetUnitOfMeasure("cm"); RxProductLookup seedVariety1DepthLookup = new RxProductLookup() { ProductId = seedVariety1.Id.ReferenceId, Representation = seedDepth, UnitOfMeasure = depthUOM }; RxProductLookup seedVariety2DepthLookup = new RxProductLookup() { ProductId = seedVariety2.Id.ReferenceId, Representation = seedDepth, UnitOfMeasure = depthUOM }; //Setup liquid rx representation/units NumericRepresentation fertilizerRate = GetNumericRepresentation("vrAppRateVolumeTarget"); UnitOfMeasure fertilizerUOM = UnitInstance.UnitSystemManager.GetUnitOfMeasure("gal1ac-1"); RxProductLookup fertilizerRateLookup = new RxProductLookup() { ProductId = fertilizer.Id.ReferenceId, Representation = fertilizerRate, UnitOfMeasure = fertilizerUOM }; //Setup granular rx representation/units NumericRepresentation insecticideRate = GetNumericRepresentation("vrAppRateMassTarget"); UnitOfMeasure insecticideUOM = UnitInstance.UnitSystemManager.GetUnitOfMeasure("lb1ac-1"); RxProductLookup insecticideRateLookup = new RxProductLookup() { ProductId = insecticide.Id.ReferenceId, Representation = insecticideRate, UnitOfMeasure = insecticideUOM }; //Prescription zones //Zone 1 - Variety 1 at 32000 seeds/acre, 4 cm depth target; Starter at 7 gal/ac; Insecticide at 5 lb/ac RxShapeLookup zone1 = new RxShapeLookup() { Rates = new List <RxRate>() { new RxRate() { Rate = 32000d, RxProductLookupId = seedVariety1RateLookup.Id.ReferenceId }, new RxRate() { Rate = 4d, RxProductLookupId = seedVariety1DepthLookup.Id.ReferenceId }, new RxRate() { Rate = 7d, RxProductLookupId = fertilizerRateLookup.Id.ReferenceId }, new RxRate() { Rate = 5d, RxProductLookupId = insecticideRateLookup.Id.ReferenceId } }, Shape = new MultiPolygon() { Polygons = new List <Polygon>() { new Polygon() { ExteriorRing = new LinearRing() { Points = new List <Point>() { new Point() { X = -89.488565, Y = 40.478304 }, new Point() { X = -89.485439, Y = 40.478304 }, new Point() { X = -89.485439, Y = 40.477404 }, new Point() { X = -89.488565, Y = 40.477756 }, new Point() { X = -89.488565, Y = 40.478304 } } }, InteriorRings = new List <LinearRing>() { new LinearRing() { Points = new List <Point>() { new Point() { X = -89.487719, Y = 40.478091 }, new Point() { X = -89.487536, Y = 40.478091 }, new Point() { X = -89.487536, Y = 40.477960 }, new Point() { X = -89.487719, Y = 40.477960 }, new Point() { X = -89.487719, Y = 40.478091 } } }, new LinearRing() { Points = new List <Point>() { new Point() { X = -89.486732, Y = 40.478172 }, new Point() { X = -89.486453, Y = 40.478172 }, new Point() { X = -89.486453, Y = 40.478082 }, new Point() { X = -89.486732, Y = 40.478082 }, new Point() { X = -89.486732, Y = 40.478172 } } } } } } } }; //Zone 2 - Variety 1 at 34000 seeds/acre, depth target 5cm; Starter at 4 gal/ac; Insecticide at 2.5 lb/ac RxShapeLookup zone2 = new RxShapeLookup() { Rates = new List <RxRate>() { new RxRate() { Rate = 34000d, RxProductLookupId = seedVariety1RateLookup.Id.ReferenceId }, new RxRate() { Rate = 5d, RxProductLookupId = seedVariety1DepthLookup.Id.ReferenceId }, new RxRate() { Rate = 4d, RxProductLookupId = fertilizerRateLookup.Id.ReferenceId }, new RxRate() { Rate = 2.5, RxProductLookupId = insecticideRateLookup.Id.ReferenceId } }, Shape = new MultiPolygon() { Polygons = new List <Polygon>() { new Polygon() { ExteriorRing = new LinearRing() { Points = new List <Point>() { new Point() { X = -89.488565, Y = 40.477756 }, new Point() { X = -89.485439, Y = 40.477404 }, new Point() { X = -89.485439, Y = 40.476688 }, new Point() { X = -89.488565, Y = 40.476688 }, new Point() { X = -89.488565, Y = 40.477756 } } } } } } }; //Zone 3 - Variety 2 at 29000 seeds/acre, depth target 6 cm; Starter at 6 gal/ac ; Insecticide at 2.75 lb/ac RxShapeLookup zone3 = new RxShapeLookup() { Rates = new List <RxRate>() { new RxRate() { Rate = 29000d, RxProductLookupId = seedVariety2RateLookup.Id.ReferenceId }, new RxRate() { Rate = 6d, RxProductLookupId = seedVariety2DepthLookup.Id.ReferenceId }, new RxRate() { Rate = 6d, RxProductLookupId = fertilizerRateLookup.Id.ReferenceId }, new RxRate() { Rate = 2.75, RxProductLookupId = insecticideRateLookup.Id.ReferenceId } }, Shape = new MultiPolygon() { Polygons = new List <Polygon>() { new Polygon() { ExteriorRing = new LinearRing() { Points = new List <Point>() { new Point() { X = -89.488565, Y = 40.476688 }, new Point() { X = -89.485439, Y = 40.476688 }, new Point() { X = -89.485439, Y = 40.475010 }, new Point() { X = -89.488565, Y = 40.475010 }, new Point() { X = -89.488565, Y = 40.476688 } } } } } } }; //Assembled Rx VectorPrescription vectorPrescription = new VectorPrescription() { Description = "Test Prescription", RxProductLookups = new List <RxProductLookup>() { seedVariety1RateLookup, seedVariety2RateLookup, fertilizerRateLookup, seedVariety1DepthLookup, seedVariety2DepthLookup, insecticideRateLookup }, RxShapeLookups = new List <RxShapeLookup>() { zone1, zone2, zone3 }, CropZoneId = cropZone.Id.ReferenceId, FieldId = field.Id.ReferenceId }; (adm.Catalog.Prescriptions as List <Prescription>).Add(vectorPrescription); //-------------------------- //Export data to file via the Plugin //-------------------------- PrecisionPlanting.ADAPT._2020.Plugin plugin = new Plugin(); plugin.Export(adm, path); }