Exemplo n.º 1
0
        public void F2Test()
        {
            var indicator = new EcosystemServicesIndicator {
                EvidenceLevel = Confidence.F2
            };

            var results = new ObservableCollection <ObjectiveResult>();

            foreach (var i in Enumerable.Range(1, 12))
            {
                results.Add(new ObjectiveResult
                {
                    Time         = new DateTime(2018, i, 1),
                    NonCompliant = i % 2 == 0
                });
            }

            foreach (var i in Enumerable.Range(0, 10))
            {
                var su = new F2SpatialUnit
                {
                    Name = $"SU {i}"
                };

                foreach (var r in results)
                {
                    su.Results.Add(r);
                }
                indicator.SpatialUnits.Add(su);
            }

            Assert.IsTrue(indicator.Value == 29);
            Assert.IsTrue(indicator.F1 == 100);
            Assert.IsTrue(indicator.F2 == 50);
        }
        private void Editor(Object o)
        {
            var su     = o as SpatialUnit;
            var create = su == null; // null is "create" mode.

            var width  = 800;
            var height = 450;
            SpatialUnitViewModel vm;

            switch (Indicator.EvidenceLevel)
            {
            case Confidence.F1:
                width  = 300;
                height = 250;
                if (su == null)
                {
                    su = new F1SpatialUnit {
                        NonCompliant = false
                    }
                }
                ;
                vm = new F1ViewModel(su);
                break;

            case Confidence.F2:
                width = 600;
                if (su == null)
                {
                    su = new F2SpatialUnit();
                }
                vm = new F2ViewModel(su);
                break;

            case Confidence.F3Sharp:
                if (su == null)
                {
                    su = new F3SharpSpatialUnit();
                }
                vm = new F3SharpViewModel(su);
                break;

            case Confidence.F3Fuzzy:
                width = 600;
                if (su == null)
                {
                    su = new F3FuzzySpatialUnit();
                }
                vm = new F3FuzzyViewModel(su);
                break;

            default:
                throw new ArgumentException("Unknown evidence level.");
            }

            var dialog = new SpatialUnitEditorWindow
            {
                DataContext = vm,
                Owner       = Application.Current?.MainWindow,
                Title       = create ? "Create Spatial Unit" : "Modify Spatial Unit",
                Width       = width,
                Height      = height
            };

            if (dialog.ShowDialog() != true)
            {
                return;
            }

            if (create)
            {
                Indicator.SpatialUnits.Add(vm.SpatialUnit);
            }
            else
            {
                Indicator.SpatialUnits.Remove(su);
                Indicator.SpatialUnits.Add(vm.SpatialUnit);
                // for some reason, replace isn't working with the ListView
                //var index = Indicator.SpatialUnits.IndexOf(su);
                //Indicator.SpatialUnits[index] = vm.SpatialUnit;
            }
        }
        private async Task ImportFromShapefile()
        {
            var dialog = new OpenFileDialog
            {
                Title      = "Open shapefile with Spatial Unit locations",
                Filter     = "Shapefile (*.shp)|*.shp",
                DefaultExt = ".shp"
            };

            if (dialog.ShowDialog() != true)
            {
                return;
            }
            var filename = dialog.FileName;
            var sf       = await ShapefileFeatureTable.OpenAsync(filename);

            var qp = new QueryParameters();

            var esi = Model.EcosystemServices.FetchIndicators <EcosystemServicesIndicator>().ToList();
            var res = await sf.QueryFeaturesAsync(qp);

            foreach (var r in res)
            {
                Trace.WriteLine($"{r.Geometry}");

                if (!(r.Geometry is MapPoint point))
                {
                    if (!(r.Geometry is Polygon polygon))
                    {
                        continue;
                    }
                    point = polygon.Extent.GetCenter();
                }
                var name = r.Attributes.FirstOrDefault(x => x.Key.ToLowerInvariant() == "name").Value as String;

                var success = false;
                foreach (var es in esi)
                {
                    foreach (var su in es.SpatialUnits)
                    {
                        if (su.Name.ToLowerInvariant() != name?.ToLowerInvariant())
                        {
                            continue;
                        }
                        su.Location.Latitude  = point.Y;
                        su.Location.Longitude = point.X;
                        su.Location.Wkid      = point.SpatialReference.Wkid;
                        success = true;
                    }
                }
                if (success)
                {
                    continue;
                }

                var wkid = point.SpatialReference.Wkid;
                if (wkid == 0)
                {
                    wkid = 4326;    // WGS84
                }
                SpatialUnit nsu = null;

                switch (Indicator.EvidenceLevel)
                {
                case Confidence.F1:
                    nsu = new F1SpatialUnit
                    {
                        Name         = name,
                        NonCompliant = false,
                        Location     = { Latitude = point.Y, Longitude = point.X, Wkid = wkid }
                    };
                    break;

                case Confidence.F2:
                    nsu = new F2SpatialUnit
                    {
                        Name     = name,
                        Location = { Latitude = point.Y, Longitude = point.X, Wkid = wkid }
                    };

                    break;

                case Confidence.F3Sharp:
                    nsu = new F3SharpSpatialUnit
                    {
                        Name     = name,
                        Location = { Latitude = point.Y, Longitude = point.X, Wkid = wkid }
                    };
                    break;

                case Confidence.F3Fuzzy:
                    nsu = new F3FuzzySpatialUnit
                    {
                        Name     = name,
                        Location = { Latitude = point.Y, Longitude = point.X, Wkid = wkid }
                    };
                    break;

                default:
                    throw new ArgumentException("Unknown evidence level.");
                }
                Indicator.SpatialUnits.Add(nsu);
                if (String.IsNullOrWhiteSpace(nsu.Name))
                {
                    nsu.Name = $"SU [{Indicator.SpatialUnits.IndexOf(nsu) + 1}]";
                }
            }
        }