private async void GenerateGeodesicBuffer_Click(object sender, RoutedEventArgs e)
        {
            string message = null;

            try
            {
                GraphicsLayer inputGraphicsLayer = null;
                var           drawShape          = (DrawShape)DrawShapes.SelectedItem;
                inputGraphicsLayer = drawShape == DrawShape.Point ? mapView1.Map.Layers["PointInputLayer"] as GraphicsLayer :
                                     ((drawShape == DrawShape.Polyline) ? mapView1.Map.Layers["LineInputLayer"] as GraphicsLayer : mapView1.Map.Layers["PolygonInputLayer"] as GraphicsLayer);

                if (inputGraphicsLayer.Graphics.Count == 0)
                {
                    throw new Exception("No input shape. Please draw shape to generate buffer");
                }
                Esri.ArcGISRuntime.Geometry.Geometry geom = inputGraphicsLayer.Graphics.FirstOrDefault().Geometry;

                if (geom != null)
                {
                    string json   = geom.ToJson();
                    var    buffer = GeometryEngine.GeodesicBuffer(geom, 10, LinearUnits.Meters);
                    if (buffer != null)
                    {
                        GraphicsLayer resultGraphicsLayer = mapView1.Map.Layers["GeometryResultGraphicsLayer"] as GraphicsLayer;
                        resultGraphicsLayer.Graphics.Add(new Graphic()
                        {
                            Geometry = buffer
                        });
                        mapView1.SetView(resultGraphicsLayer.Graphics.First().Geometry.Extent);
                    }
                }
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
            if (message != null)
            {
                await new MessageDialog(message).ShowAsync();
            }
        }
        private async void QueryFeatures(Geometry buffer, MapPoint location)
        {
            //create query
            var query = new QueryParameters();

            query.Geometry           = buffer;
            query.MaxAllowableOffset = 0;
            System.Diagnostics.Debug.WriteLine(buffer.ToJson());

            var layer   = MyMapView.Map.OperationalLayers[0] as FeatureLayer;
            var table   = layer.FeatureTable;
            var results = await table.QueryFeaturesAsync(query);

            layer.ClearSelection();


            /* // ===================================================================
             * //                       Single Closest Bar
             * // -------------------------------------------------------------------
             *
             * double shortDist = -1;
             * Feature feature = null;
             *
             * //check for closest bars within buffer
             * foreach (var item in results)
             * {
             *  var calcDistance = GeometryEngine.Distance(location, item.Geometry);
             *
             *  if (shortDist == -1)
             *  {
             *      shortDist = calcDistance;
             *      feature = item;
             *      continue;
             *  }
             *
             *  if (calcDistance < shortDist)
             *  {
             *      shortDist = calcDistance;
             *      feature = item;
             *  }
             * }
             *
             * //exit if no closest bar
             * if (feature == null)
             *  return;
             *
             * if (!(GeometryEngine.Intersects(buffer, feature.Geometry)))
             *  return;
             *
             * //highlight closest bar
             * layer.SelectFeature(feature);
             * layer.SelectionColor = Colors.DarkRed;
             *
             * //draw line to closest bar
             * var linePoints = new PolylineBuilder(SpatialReferences.WebMercator);
             * linePoints.AddPoint(location);
             * linePoints.AddPoint(feature.Geometry as MapPoint);
             * var line = linePoints.ToGeometry();
             *
             * var lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.DashDotDot, Colors.Maroon, 2);
             * var lineGraphic = new Graphic(line, lineSymbol);
             * MyOverlay.Graphics.Add(lineGraphic);
             *
             *
             * //create text symbol for distance at midpoint of line
             * var x = (line.Extent.XMin + line.Extent.XMax) / 2;
             * var y = (line.Extent.YMin + line.Extent.YMax) / 2;
             * var textPoint = new MapPoint(x, y);
             * var text = String.Format("{0:0.00}", shortDist / 1609.34) + " miles";
             * var textSymbol = new TextSymbol(text, Colors.Black, 15, Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Center, Esri.ArcGISRuntime.Symbology.VerticalAlignment.Baseline);
             * textSymbol.FontWeight = Esri.ArcGISRuntime.Symbology.FontWeight.Bold;
             * //textSymbol.BackgroundColor = Colors.Maroon;
             * //textSymbol.Angle = Math.Atan2(line.Extent.YMax - line.Extent.YMin, line.Extent.XMax - line.Extent.XMin) * 180.0 / Math.PI;
             * var textGraphic = new Graphic(textPoint, textSymbol);
             * MyOverlay.Graphics.Add(textGraphic);
             *
             * //===================================================================*/



            //==================================================================
            //                    Two Closest Bars
            //------------------------------------------------------------------

            double[]  shortDist = { -1, -1 };
            Feature[] feature   = new Feature[2];
            System.Diagnostics.Debug.WriteLine("#1: shortdist is at: " + shortDist[0].ToString() + " " + shortDist[1].ToString());

            //check for closest bars within buffer
            foreach (var item in results)
            {
                var calcDistance = GeometryEngine.Distance(location, item.Geometry);
                System.Diagnostics.Debug.WriteLine("feature distance: " + (calcDistance / 1609.34).ToString() + " meters; shordist = " + shortDist[0].ToString() + " " + shortDist[1].ToString());
                if (shortDist[0] == -1)
                {
                    shortDist[0] = calcDistance;
                    System.Diagnostics.Debug.WriteLine("setting first");
                    feature[0] = item;
                    continue;
                }
                if (shortDist[1] == -1)
                {
                    System.Diagnostics.Debug.WriteLine("setting second");
                    shortDist[1] = calcDistance;
                    feature[1]   = item;
                    continue;
                }

                if (calcDistance < shortDist[0])
                {
                    System.Diagnostics.Debug.WriteLine("less than first");
                    shortDist[1] = shortDist[0];
                    feature[1]   = feature[0];
                    shortDist[0] = calcDistance;
                    feature[0]   = item;
                }
                else if (calcDistance < shortDist[1])
                {
                    System.Diagnostics.Debug.WriteLine("less than second");
                    shortDist[1] = calcDistance;
                    feature[1]   = item;
                }
            }

            //exit if no closest bar
            if (feature[0] == null || feature[1] == null)
            {
                return;
            }

            if (!(GeometryEngine.Intersects(buffer, feature[0].Geometry)))
            {
                return;
            }

            //highlight closest bar
            for (int i = 0; i < 2; i++)
            {
                layer.SelectFeature(feature[i]);
                layer.SelectionColor = Colors.DarkRed;

                //draw line to closest bar
                var linePoints = new PolylineBuilder(SpatialReferences.WebMercator);
                linePoints.AddPoint(location);
                linePoints.AddPoint(feature[i].Geometry as MapPoint);
                var line = linePoints.ToGeometry();

                var lineSymbol  = new SimpleLineSymbol(SimpleLineSymbolStyle.DashDotDot, Colors.Maroon, 2);
                var lineGraphic = new Graphic(line, lineSymbol);
                MyOverlay.Graphics.Add(lineGraphic);

                //create text symbol for distance at midpoint of line
                var x          = (line.Extent.XMin + line.Extent.XMax) / 2;
                var y          = (line.Extent.YMin + line.Extent.YMax) / 2;
                var textPoint  = new MapPoint(x, y);
                var text       = String.Format("{0:0.00}", shortDist[i] / 1609.34) + " miles";
                var textSymbol = new TextSymbol(text, Colors.Black, 15, Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Center, Esri.ArcGISRuntime.Symbology.VerticalAlignment.Baseline);
                textSymbol.FontWeight = Esri.ArcGISRuntime.Symbology.FontWeight.Bold;
                //textSymbol.BackgroundColor = Colors.Maroon;
                //textSymbol.Angle = Math.Atan2(line.Extent.YMax - line.Extent.YMin, line.Extent.XMax - line.Extent.XMin) * 180.0 / Math.PI;
                var textGraphic = new Graphic(textPoint, textSymbol);
                MyOverlay.Graphics.Add(textGraphic);
            }
        }
Ejemplo n.º 3
0
        // myMapView 事件
        private async void MyMapView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IInputElement ie  = (IInputElement)(sender);
            MapPoint      loc = myMapView.ScreenToLocation(e.GetPosition(ie));

            switch (operation)
            {
            case OperateType.DrawPoint:     //画点
                Graphic pt = new Graphic(loc, pointSymbol);
                graphicsLayer.Graphics.Add(pt);
                break;

            case OperateType.DrawPolyline:    //画线
                pointCollection.Add(loc);
                if (pointCollection.Count >= 2)
                {
                    if (pointCollection.Count > 2)
                    {
                        Graphic         g  = graphicsLayer.Graphics[graphicsLayer.Graphics.Count - 1];
                        PolylineBuilder lb = new PolylineBuilder(pointCollection);
                        g.Geometry = lb.ToGeometry();
                    }
                    else
                    {
                        Esri.ArcGISRuntime.Geometry.Polyline l = new Esri.ArcGISRuntime.Geometry.Polyline(pointCollection);
                        Graphic lg = new Graphic(l, lineSymbol);
                        graphicsLayer.Graphics.Add(lg);
                    }
                }
                break;

            case OperateType.DrawPolygon:    //画多边形
                pointCollection.Add(loc);
                if (pointCollection.Count >= 3)
                {
                    if (pointCollection.Count > 3)
                    {
                        Graphic        g  = graphicsLayer.Graphics[graphicsLayer.Graphics.Count - 1];
                        PolygonBuilder pb = new PolygonBuilder(pointCollection);
                        g.Geometry = pb.ToGeometry();
                    }
                    else
                    {
                        Esri.ArcGISRuntime.Geometry.Polygon p = new Esri.ArcGISRuntime.Geometry.Polygon(pointCollection);
                        Graphic pg = new Graphic(p, fillSymbol);
                        graphicsLayer.Graphics.Add(pg);
                    }
                }
                break;

            case OperateType.None:    //缺省状态
                graphicsLayer.ClearSelection();
                IdentifyGraphicsOverlayResult result = await myMapView.IdentifyGraphicsOverlayAsync(graphicsLayer, e.GetPosition(ie), 5, false);

                //选择图形元素
                if (result.Graphics.Count < 1)
                {
                    curSelGraphic = null;
                    EditVertexMenuItem.IsEnabled   = false;
                    UneditVertexMenuItem.IsEnabled = false;
                    return;
                }
                curSelGraphic                = result.Graphics.First();
                curSelGraphic.IsSelected     = true;
                EditVertexMenuItem.IsEnabled = true;
                break;

            case OperateType.Cal_Clip:     //选择图形
                IdentifyGraphicsOverlayResult gResult = await myMapView.IdentifyGraphicsOverlayAsync(graphicsLayer, e.GetPosition(ie), 5, false);

                if (gResult.Graphics.Count < 1)
                {
                    return;
                }
                Graphic selGraphic = gResult.Graphics.First();
                selGraphic.IsSelected = true; listOfClipGraphics.Add(selGraphic); //记录所选图形
                if (listOfClipGraphics.Count == 2)                                //图形数目为2时,进行剪切计算
                {
                    Graphic g1 = listOfClipGraphics[0];
                    Graphic g2 = listOfClipGraphics[1];
                    if (g1.Geometry.GeometryType != GeometryType.Polygon || g2.Geometry.GeometryType != GeometryType.Polygon)     //如果所选图形不是多边形,则退出
                    {
                        MessageBox.Show("请选择两个多边形图形!");
                        listOfClipGraphics.Clear();
                        graphicsLayer.ClearSelection();
                        return;
                    }
                    Esri.ArcGISRuntime.Geometry.Geometry resultGeometry = GeometryEngine.Clip(g1.Geometry, g2.Geometry.Extent); //执行剪切操作
                    if (resultGeometry != null)                                                                                 //处理结果
                    {
                        graphicsLayer.Graphics.Remove(g1);                                                                      //从图形层中移除原图形
                        graphicsLayer.Graphics.Remove(g2);
                        Graphic clipedGraphic = new Graphic(resultGeometry, fillSymbol);                                        //利 用剪切结果构建新的图形
                        graphicsLayer.Graphics.Add(clipedGraphic); operation = OperateType.None;
                    }
                    listOfClipGraphics.Clear();     //清空图形选择集合
                    graphicsLayer.ClearSelection(); //清空图形层所选
                }
                break;

            case OperateType.Cal_Union:     // 联合
                IdentifyGraphicsOverlayResult gResultUnion = await myMapView.IdentifyGraphicsOverlayAsync(graphicsLayer, e.GetPosition(ie), 5, false);

                if (gResultUnion.Graphics.Count < 1)
                {
                    return;
                }
                Graphic selGraphicUnion = gResultUnion.Graphics.First();
                selGraphicUnion.IsSelected = true;
                listOfClipGraphics.Add(selGraphicUnion); //记录所选图形
                if (listOfClipGraphics.Count == 2)       //图形数目为2时,进行剪切计算
                {
                    Graphic g1 = listOfClipGraphics[0];
                    Graphic g2 = listOfClipGraphics[1];
                    if (g1.Geometry.GeometryType != GeometryType.Polygon || g2.Geometry.GeometryType != GeometryType.Polygon)     //如果所选图形不是多边形,则退出
                    {
                        MessageBox.Show("请选择两个多边形图形!");
                        listOfClipGraphics.Clear();
                        graphicsLayer.ClearSelection();
                        return;
                    }
                    Esri.ArcGISRuntime.Geometry.Geometry resultGeometry = GeometryEngine.Union(g1.Geometry, g2.Geometry.Extent); //执行剪切操作
                    if (resultGeometry != null)                                                                                  //处理结果
                    {
                        graphicsLayer.Graphics.Remove(g1);                                                                       //从图形层中移除原图形
                        graphicsLayer.Graphics.Remove(g2);
                        Graphic clipedGraphic = new Graphic(resultGeometry, fillSymbol);                                         //利 用剪切结果构建新的图形
                        graphicsLayer.Graphics.Add(clipedGraphic);
                        operation = OperateType.None;
                    }
                    listOfClipGraphics.Clear();     //清空图形选择集合
                    graphicsLayer.ClearSelection(); //清空图形层所选
                }
                break;

            case OperateType.Cal_Cut:     // 剪切
                IdentifyGraphicsOverlayResult gResult_Cut = await myMapView.IdentifyGraphicsOverlayAsync(graphicsLayer, e.GetPosition(ie), 5, false);

                if (gResult_Cut.Graphics.Count < 1)
                {
                    return;
                }
                Graphic selGraphic_Cut = gResult_Cut.Graphics.First();
                selGraphic_Cut.IsSelected = true;
                listOfClipGraphics.Add(selGraphic_Cut); //记录所选图形
                if (listOfClipGraphics.Count == 2)      //图形数目为1时,进行剪切计算
                {
                    Graphic g1 = listOfClipGraphics[0];
                    Graphic g2 = listOfClipGraphics[1];
                    if (g1.Geometry.GeometryType != GeometryType.Polygon || g2.Geometry.GeometryType != GeometryType.Polyline)     //如果所选图形不是多边形,则退出
                    {
                        MessageBox.Show("请先选择一个面要素后再选择一个线要素.");
                        listOfClipGraphics.Clear();
                        graphicsLayer.ClearSelection();
                        return;
                    }
                    Esri.ArcGISRuntime.Geometry.Polyline   polyLine       = (Esri.ArcGISRuntime.Geometry.Polyline)g2.Geometry;
                    Esri.ArcGISRuntime.Geometry.Geometry[] resultGeometry = GeometryEngine.Cut(g1.Geometry, polyLine); //执行剪切操作
                    if (resultGeometry != null)                                                                        //处理结果
                    {
                        graphicsLayer.Graphics.Remove(g1);
                        for (int z = 0; z < resultGeometry.Length; z++)
                        {
                            Graphic clipedGraphic = new Graphic(resultGeometry[z], fillSymbol);     //利 用剪切结果构建新的图形
                            graphicsLayer.Graphics.Add(clipedGraphic);
                        }
                        operation = OperateType.None;
                    }
                    listOfClipGraphics.Clear();     //清空图形选择集合
                    graphicsLayer.ClearSelection(); //清空图形层所选
                }
                break;

            case OperateType.Cal_Simplify:     // 拓扑纠正
                IdentifyGraphicsOverlayResult gResult_Simplify = await myMapView.IdentifyGraphicsOverlayAsync(graphicsLayer, e.GetPosition(ie), 5, false);

                if (gResult_Simplify.Graphics.Count < 1)
                {
                    return;
                }
                Graphic selGraphic_Simplify = gResult_Simplify.Graphics.First();
                selGraphic_Simplify.IsSelected = true;
                listOfClipGraphics.Add(selGraphic_Simplify); //记录所选图形
                if (listOfClipGraphics.Count == 1)           //图形数目为1时,进行剪切计算
                {
                    Graphic g1 = listOfClipGraphics[0];
                    if (g1.Geometry.GeometryType == GeometryType.Point)     //如果所选图形不是多边形,则退出
                    {
                        MessageBox.Show("请先选择一个面要素或线要素.");
                        listOfClipGraphics.Clear();
                        graphicsLayer.ClearSelection();
                        return;
                    }
                    Esri.ArcGISRuntime.Geometry.Geometry resultGeometry = GeometryEngine.Simplify(g1.Geometry); //执行剪切操作
                    if (resultGeometry != null)                                                                 //处理结果
                    {
                        graphicsLayer.Graphics.Remove(g1);                                                      //从图形层中移除原图形
                        Graphic clipedGraphic = new Graphic(resultGeometry, fillSymbol);                        //利 用剪切结果构建新的图形
                        graphicsLayer.Graphics.Add(clipedGraphic);
                        operation = OperateType.None;
                    }
                    listOfClipGraphics.Clear();     //清空图形选择集合
                    graphicsLayer.ClearSelection(); //清空图形层所选
                }
                break;

            case OperateType.Cal_Gene:     // 简化
                IdentifyGraphicsOverlayResult gResult_Gene = await myMapView.IdentifyGraphicsOverlayAsync(graphicsLayer, e.GetPosition(ie), 5, false);

                if (gResult_Gene.Graphics.Count < 1)
                {
                    return;
                }
                Graphic selGraphic_Gene = gResult_Gene.Graphics.First();
                selGraphic_Gene.IsSelected = true;
                listOfClipGraphics.Add(selGraphic_Gene); //记录所选图形
                if (listOfClipGraphics.Count == 1)       //图形数目为1时
                {
                    Graphic g1 = listOfClipGraphics[0];
                    if (g1.Geometry.GeometryType == GeometryType.Point)     //如果所选图形是点,则退出
                    {
                        MessageBox.Show("请先选择一个面要素或线要素.");
                        listOfClipGraphics.Clear();
                        graphicsLayer.ClearSelection();
                        return;
                    }
                    Esri.ArcGISRuntime.Geometry.Geometry resultGeometry = GeometryEngine.Generalize(g1.Geometry, 1000000.0, true); //执行剪切操作
                    if (resultGeometry != null)                                                                                    //处理结果
                    {
                        MessageBox.Show(resultGeometry.ToJson() + "\n" + resultGeometry.GeometryType);
                        graphicsLayer.Graphics.Remove(g1);                               //从图形层中移除原图形
                        Graphic clipedGraphic = new Graphic(resultGeometry, fillSymbol); //利 用剪切结果构建新的图形
                        graphicsLayer.Graphics.Add(clipedGraphic);
                        operation = OperateType.None;
                    }
                    listOfClipGraphics.Clear();     //清空图形选择集合
                    graphicsLayer.ClearSelection(); //清空图形层所选
                }
                break;

            case OperateType.Cal_Buff:     // 缓冲
                IdentifyGraphicsOverlayResult gResult_Buff = await myMapView.IdentifyGraphicsOverlayAsync(graphicsLayer, e.GetPosition(ie), 5, false);

                if (gResult_Buff.Graphics.Count < 1)
                {
                    return;
                }
                Graphic selGraphic_Buff = gResult_Buff.Graphics.First();
                selGraphic_Buff.IsSelected = true;
                listOfClipGraphics.Add(selGraphic_Buff); //记录所选图形
                if (listOfClipGraphics.Count == 1)       //图形数目为1时,进行剪切计算
                {
                    Graphic g1 = listOfClipGraphics[0];
                    Esri.ArcGISRuntime.Geometry.Geometry resultGeometry = GeometryEngine.Buffer(g1.Geometry, 1000000.0);                                                                                                                           //执行剪切操作
                    if (resultGeometry != null)                                                                                                                                                                                                    //处理结果
                    {
                        graphicsLayer.Graphics.Remove(g1);                                                                                                                                                                                         //从图形层中移除原图形
                        Graphic clipedGraphic = new Graphic(resultGeometry, new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.FromArgb(125, 255, 250, 0), new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.FromArgb(0, 0, 0), 4.0))); //利 用剪切结果构建新的图形
                        graphicsLayer.Graphics.Add(clipedGraphic);
                        operation = OperateType.None;
                    }
                    listOfClipGraphics.Clear();     //清空图形选择集合
                    graphicsLayer.ClearSelection(); //清空图形层所选
                }
                break;

            case OperateType.Cal_Jiaodian:     // 交点
                IdentifyGraphicsOverlayResult gResult_Jiaodian = await myMapView.IdentifyGraphicsOverlayAsync(graphicsLayer, e.GetPosition(ie), 5, false);

                if (gResult_Jiaodian.Graphics.Count < 1)
                {
                    return;
                }
                Graphic selGraphic_Jiaodian = gResult_Jiaodian.Graphics.First();
                selGraphic_Jiaodian.IsSelected = true;
                listOfClipGraphics.Add(selGraphic_Jiaodian); //记录所选图形
                if (listOfClipGraphics.Count == 2)           //图形数目为1时,进行剪切计算
                {
                    Graphic g1 = listOfClipGraphics[0];
                    Graphic g2 = listOfClipGraphics[1];
                    IReadOnlyList <Geometry> resultGeometry = GeometryEngine.Intersections(g1.Geometry, g2.Geometry); //执行剪切操作
                    if (resultGeometry != null)                                                                       //处理结果
                    {
                        Graphic clipedGraphic = new Graphic(resultGeometry[0], pointSymbol);                          //利 用剪切结果构建新的图形
                        graphicsLayer.Graphics.Add(clipedGraphic);
                        operation = OperateType.None;
                    }
                    listOfClipGraphics.Clear();     //清空图形选择集合
                    graphicsLayer.ClearSelection(); //清空图形层所选
                }
                break;
            }
        }