private async void MeasureButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                pauseButton.IsEnabled            = true;
                pauseButton.Icon                 = new SymbolIcon(Symbol.Pause);
                pauseButton.Label                = "Suspend";
                MyMapView.SketchEditor.IsEnabled = true;
                measurementOutput.Text           = "Click the map to begin the measure";
                var geometry = await MyMapView.SketchEditor.StartAsync(Esri.ArcGISRuntime.UI.SketchCreationMode.Polyline);

                var polyline = geometry as Polyline;
                if (polyline != null && polyline.Parts != null && polyline.Parts.Count > 0)
                {
                    var result      = new StringBuilder();
                    var totalLength = GeometryEngine.LengthGeodetic(polyline);
                    result.AppendFormat("Total Length\t:\t{0:0} m\n", totalLength);
                    var polygon = new Polygon(polyline.Parts, polyline.SpatialReference);
                    var area    = GeometryEngine.AreaGeodetic(polygon);
                    result.AppendFormat("Area\t\t:\t{0:0} m²\n", area);
                    measurementOutput.Text = result.ToString();
                }
            }
            catch (TaskCanceledException)
            {
            }
            catch (Exception ex)
            {
                measurementOutput.Text = ex.Message;
            }
            pauseButton.IsEnabled = false;
            pauseButton.Icon      = new SymbolIcon(Symbol.Play);
        }
Example #2
0
 private void MySceneView_MouseDoubleClick(object sender, MouseEventArgs e)
 {
     if (type == "line" && mapPoint_list.Count != 0)
     {
         BorderContentText.Visibility = Visibility.Visible;
         var boatPositions = new PolylineBuilder(SpatialReferences.Wgs84);
         for (var i = 0; i < mapPoint_list.Count; i++)
         {
             boatPositions.AddPoint(new MapPoint(mapPoint_list[i].X, mapPoint_list[i].Y));
         }
         var boatRoute = boatPositions.ToGeometry();
         _mapViewModel.Line(boatRoute, "line");
         LineText.Text = "Line Length: " + GeometryEngine.LengthGeodetic(boatRoute).ToString("N2") + " meters";
     }
     else if (type == "area" && mapPoint_list.Count != 0)
     {
         BorderContentText.Visibility = Visibility.Visible;
         var boatPositions = new PolygonBuilder(SpatialReferences.Wgs84);
         for (var i = 0; i < mapPoint_list.Count; i++)
         {
             boatPositions.AddPoint(new MapPoint(mapPoint_list[i].X, mapPoint_list[i].Y));
         }
         var boatRoute = boatPositions.ToGeometry();
         _mapViewModel.Area(boatRoute);
         AreaText.Text = "Area Size: " + GeometryEngine.AreaGeodetic(boatRoute).ToString("N2") + "  square meters";
     }
     mapPoint_list.Clear();
 }
        private void SketchEditor_GeometryChanged(object sender, Esri.ArcGISRuntime.UI.GeometryChangedEventArgs e)
        {
            var polyline    = e.NewGeometry as Polyline;
            var result      = new StringBuilder();
            var totalLength = GeometryEngine.LengthGeodetic(polyline);

            result.AppendFormat("Total Length\t:\t{0:0} m\n", totalLength);
            var polygon = new Polygon(polyline.Parts, polyline.SpatialReference);
            var area    = GeometryEngine.AreaGeodetic(polygon);

            result.AppendFormat("Area\t\t:\t{0:0} m²\n", area);
            measurementOutput.Text = result.ToString();
        }
        /// <summary>
        /// Displays the measurement result
        /// </summary>
        /// <param name="geometry">geometry to measure</param>
        private void DisplayResult(Geometry.Geometry geometry = null)
        {
            if (_measureResultTextBlock != null)
            {
                double measurement = 0;
                if (geometry == null)
                {
                    switch (Mode)
                    {
                    case MeasureToolbarMode.Line:
                    {
                        geometry = LineSketchEditor.Geometry;
                        break;
                    }

                    case MeasureToolbarMode.Area:
                    {
                        geometry = AreaSketchEditor.Geometry;
                        break;
                    }

                    case MeasureToolbarMode.Feature:
                    {
                        geometry = _measureFeatureResultOverlay.Graphics.FirstOrDefault()?.Geometry;
                        break;
                    }
                    }
                }

                if (geometry is Polyline)
                {
                    measurement = GeometryEngine.LengthGeodetic(geometry, SelectedLinearUnit, GeodeticCurveType.ShapePreserving);
                }
                else if (geometry is Polygon || geometry is Envelope)
                {
                    measurement = GeometryEngine.AreaGeodetic(geometry, SelectedAreaUnit, GeodeticCurveType.ShapePreserving);
                }

                if (geometry == null)
                {
                    var instruction = Mode == MeasureToolbarMode.None ?
                                      "Toggle a measure mode" : (Mode == MeasureToolbarMode.Feature ? "Tap a feature" : "Tap to sketch");
                    _measureResultTextBlock.Text = instruction;
                }
                else
                {
                    _measureResultTextBlock.Text = string.Format("{0:0,0.00}", measurement);
                }
            }
        }
        private void Update(ShapefileFeatureTable sf, CancellationToken ct, IProgress <int> p)
        {
            Task.Run(async() =>
            {
                var directory = Globals.Model.Assets.PathTo("BasinShapefile");
                if (String.IsNullOrWhiteSpace(directory))
                {
                    MessageBox.Show("You need to have imported a basin shapefile.");
                    Clear();
                    return;
                }

                var file = Directory.EnumerateFiles(directory, "*.shp").FirstOrDefault();
                if (String.IsNullOrWhiteSpace(file))
                {
                    MessageBox.Show("Error reading the basin shapefile.");
                    Clear();
                    return;
                }

                var ci      = Globals.Model.EcosystemVitality.FetchIndicator <ConnectivityIndicator>();
                var reaches = ci?.Reaches;
                if (reaches == null)
                {
                    MessageBox.Show(
                        "You must import a river network for the Connectivity Indicator in Ecosystem Vitality.");
                    Clear();
                    return;
                }

                var bsf      = await ShapefileFeatureTable.OpenAsync(file);
                var allQuery = new QueryParameters
                {
                    Geometry            = bsf.Extent,
                    SpatialRelationship = SpatialRelationship.Contains
                };
                ConservationAreaIndicator.TotalProtectedArea = 0;
                ConservationAreaIndicator.TotalArea          = 0;

                foreach (var mapFeature in await bsf.QueryFeaturesAsync(allQuery))
                {
                    if (ct.IsCancellationRequested)
                    {
                        Clear();
                        return;
                    }

                    ConservationAreaIndicator.TotalArea += GeometryEngine.AreaGeodetic(mapFeature.Geometry);

                    var areaFeatures = await sf.QueryFeaturesAsync(new QueryParameters
                    {
                        Geometry            = mapFeature.Geometry,
                        SpatialRelationship = SpatialRelationship.Contains
                    });
                    foreach (var feature in areaFeatures)
                    {
                        ConservationAreaIndicator.TotalProtectedArea += GeometryEngine.AreaGeodetic(feature.Geometry);
                        if (ct.IsCancellationRequested)
                        {
                            Clear();
                            return;
                        }
                    }
                }

                p.Report(25);

                // todo: this is not an efficient algorithm
                var pb = new PolylineBuilder(new SpatialReference(Model.Attributes.Wkid));
                foreach (var reach in reaches)
                {
                    pb.AddPart(
                        reach.Nodes.Select(node => new MapPoint(node.Location.Longitude, node.Location.Latitude)));
                }
                var mapGeometry = pb.ToGeometry();
                ConservationAreaIndicator.TotalLength = GeometryEngine.Length(mapGeometry);

                if (ct.IsCancellationRequested)
                {
                    Clear();
                    return;
                }

                var lengthFeatures = await sf.QueryFeaturesAsync(new QueryParameters
                {
                    Geometry            = sf.Extent,
                    SpatialRelationship = SpatialRelationship.Contains
                });

                var tpl         = 0.0;
                var reachNumber = 0;
                foreach (var reach in reaches)
                {
                    var poly = new PolylineBuilder(new SpatialReference(Model.Attributes.Wkid));
                    poly.AddPart(reach.Nodes.Select(node =>
                                                    new MapPoint(node.Location.Longitude, node.Location.Latitude)));
                    var pg = poly.ToGeometry();
                    foreach (var feature in lengthFeatures)
                    {
                        var intersection = GeometryEngine.Intersection(pg, feature.Geometry) as Polyline;
                        if (intersection?.Parts.Count > 0)
                        {
                            tpl += GeometryEngine.Length(pg);
                        }
                        if (ct.IsCancellationRequested)
                        {
                            Clear();
                            return;
                        }
                    }

                    p.Report(25 + (int)(75.0 * reachNumber++ / reaches.Count));
                }

                ConservationAreaIndicator.TotalProtectedLength = tpl;
            }, ct).Wait(ct);
        }