From 45943b7e55cbbad83c05cae966089dba0ed72006 Mon Sep 17 00:00:00 2001 From: PeterZhong Date: Fri, 20 Dec 2024 11:30:20 +0800 Subject: [PATCH] Finish all seven experiments --- GisDevelop_Exp/MainWindow.xaml | 7 +- GisDevelop_Exp/MainWindow.xaml.cs | 338 ++++++++++++++++++----- GisDevelop_Exp/QueryResultWindow.xaml.cs | 12 +- 3 files changed, 289 insertions(+), 68 deletions(-) diff --git a/GisDevelop_Exp/MainWindow.xaml b/GisDevelop_Exp/MainWindow.xaml index 07de407..9e2f539 100644 --- a/GisDevelop_Exp/MainWindow.xaml +++ b/GisDevelop_Exp/MainWindow.xaml @@ -61,7 +61,12 @@ - + + + + + + listOfClipGraphics; //几何操作时选择的图形 - + private int selByGeometryType = 0; - + LocationQueryResultsWindow locationQueryResultsWindow; private MapPoint _startLocation; @@ -168,6 +171,7 @@ namespace GisDevelop_Exp String filePath = fileDialog.FileNames.GetValue(i).ToString(); ShapefileFeatureTable openedFeature = await ShapefileFeatureTable.OpenAsync(filePath); FeatureLayer opendFeatureLayer = new FeatureLayer(openedFeature); + _featureLayer = opendFeatureLayer; MainMapView.Map.OperationalLayers.Add(opendFeatureLayer); await MainMapView.SetViewpointGeometryAsync(openedFeature.Extent); } @@ -207,7 +211,6 @@ namespace GisDevelop_Exp MainMapView.Map.InitialViewpoint = geodatabaseViewPoint; // MainMapView.Map = geodatabaseMap; } - AddLayerNameToList(); } @@ -356,7 +359,7 @@ namespace GisDevelop_Exp { AddPrivateOnlineMap(); } - + private async void AddPrivateOnlineMap() { Map map = new Map(BasemapStyle.ArcGISTopographic); @@ -462,7 +465,7 @@ namespace GisDevelop_Exp _pGraphicsOverLay.ClearSelection(); return; } - + Esri.ArcGISRuntime.Geometry.Geometry resultGeometry = GeometryEngine.Union(g1.Geometry, g2.Geometry); //执行联合操作 if (resultGeometry != null) //处理结果 @@ -494,7 +497,7 @@ namespace GisDevelop_Exp { Graphic g1 = listOfClipGraphics[0]; Graphic g2 = listOfClipGraphics[1]; - + if (g1.Geometry.GeometryType != GeometryType.Polygon || g2.Geometry.GeometryType != GeometryType.Polyline) //如果所选图形不是多边形,则退出 { @@ -512,7 +515,7 @@ namespace GisDevelop_Exp { _pGraphicsOverLay.Graphics.Remove(g1); _pGraphicsOverLay.Graphics.Remove(g2); - + for (int z = 0; z < resultGeometry.Length; z++) { Graphic clipedGraphic = new Graphic(resultGeometry[z], @@ -526,9 +529,11 @@ namespace GisDevelop_Exp listOfClipGraphics.Clear(); //清空图形选择集合 _pGraphicsOverLay.ClearSelection(); //清空图形层所选 } - }else if (m_CurOper == CURRENTOPERATION.Cal_Intersection) + } + else if (m_CurOper == CURRENTOPERATION.Cal_Intersect) { - IdentifyGraphicsOverlayResult gResult_Intersect = await MainMapView.IdentifyGraphicsOverlayAsync(_pGraphicsOverLay, e.Position, 5, false); + IdentifyGraphicsOverlayResult gResult_Intersect = + await MainMapView.IdentifyGraphicsOverlayAsync(_pGraphicsOverLay, e.Position, 5, false); if (gResult_Intersect.Graphics.Count < 1) return; Graphic selGraphic_Intersect = gResult_Intersect.Graphics.First(); @@ -539,26 +544,45 @@ namespace GisDevelop_Exp Graphic g1 = listOfClipGraphics[0]; Graphic g2 = listOfClipGraphics[1]; - IReadOnlyList resultGeometry = GeometryEngine.Intersections(g1.Geometry, g2.Geometry); //执行求交点操作 + Boolean isIntersect = GeometryEngine.Intersects(g1.Geometry, g2.Geometry); + + if (isIntersect) + { + MessageBox.Show("两个图形相交!"); + } + else + { + MessageBox.Show("两个图形不相交!"); + } + + m_CurOper = CURRENTOPERATION.NullOpe; + + + IReadOnlyList + resultGeometry = GeometryEngine.Intersections(g1.Geometry, g2.Geometry); //执行求交点操作 // Geometry resultGeometry = GeometryEngine.Intersection(g1.Geometry, g2.Geometry); //执行求交操作 if (resultGeometry != null) //处理结果 { _pGraphicsOverLay.Graphics.Remove(g1); _pGraphicsOverLay.Graphics.Remove(g2); - + foreach (var pointGeometry in resultGeometry) { - Graphic clipedGraphic = new Graphic(pointGeometry, pointSymbol); //利用求交结果构建新的图形 + Graphic clipedGraphic = new Graphic(pointGeometry, fillSymbol); //利用求交结果构建新的图形 _pGraphicsOverLay.Graphics.Add(clipedGraphic); } + m_CurOper = CURRENTOPERATION.NullOpe; } - listOfClipGraphics.Clear();//清空图形选择集合 - _pGraphicsOverLay.ClearSelection();//清空图形层所选 + + listOfClipGraphics.Clear(); //清空图形选择集合 + _pGraphicsOverLay.ClearSelection(); //清空图形层所选 } - }else if (m_CurOper == CURRENTOPERATION.Cal_Intersect) + } + else if (m_CurOper == CURRENTOPERATION.Cal_Intersection) { - IdentifyGraphicsOverlayResult gResult_Intersect = await MainMapView.IdentifyGraphicsOverlayAsync(_pGraphicsOverLay, e.Position, 5, false); + IdentifyGraphicsOverlayResult gResult_Intersect = + await MainMapView.IdentifyGraphicsOverlayAsync(_pGraphicsOverLay, e.Position, 5, false); if (gResult_Intersect.Graphics.Count < 1) return; Graphic selGraphic_Intersect = gResult_Intersect.Graphics.First(); @@ -575,17 +599,20 @@ namespace GisDevelop_Exp { _pGraphicsOverLay.Graphics.Remove(g1); _pGraphicsOverLay.Graphics.Remove(g2); - + Graphic clipedGraphic = new Graphic(resultGeometry, pointSymbol); //利用求交结果构建新的图形 _pGraphicsOverLay.Graphics.Add(clipedGraphic); m_CurOper = CURRENTOPERATION.NullOpe; } - listOfClipGraphics.Clear();//清空图形选择集合 - _pGraphicsOverLay.ClearSelection();//清空图形层所选 + + listOfClipGraphics.Clear(); //清空图形选择集合 + _pGraphicsOverLay.ClearSelection(); //清空图形层所选 } - }else if (m_CurOper == CURRENTOPERATION.Cal_Clip) + } + else if (m_CurOper == CURRENTOPERATION.Cal_Clip) { - IdentifyGraphicsOverlayResult gResult_Intersect = await MainMapView.IdentifyGraphicsOverlayAsync(_pGraphicsOverLay, e.Position, 5, false); + IdentifyGraphicsOverlayResult gResult_Intersect = + await MainMapView.IdentifyGraphicsOverlayAsync(_pGraphicsOverLay, e.Position, 5, false); if (gResult_Intersect.Graphics.Count < 1) return; Graphic selGraphic_Intersect = gResult_Intersect.Graphics.First(); @@ -595,21 +622,23 @@ namespace GisDevelop_Exp { Graphic g1 = listOfClipGraphics[0]; Graphic g2 = listOfClipGraphics[1]; - + Geometry resultGeometry = GeometryEngine.Clip(g1.Geometry, g2.Geometry.Extent); //执行求交操作 if (resultGeometry != null) //处理结果 { _pGraphicsOverLay.Graphics.Remove(g1); _pGraphicsOverLay.Graphics.Remove(g2); - + Graphic clipedGraphic = new Graphic(resultGeometry, fillSymbol); //利用求交结果构建新的图形 _pGraphicsOverLay.Graphics.Add(clipedGraphic); m_CurOper = CURRENTOPERATION.NullOpe; } - listOfClipGraphics.Clear();//清空图形选择集合 - _pGraphicsOverLay.ClearSelection();//清空图形层所选 + + listOfClipGraphics.Clear(); //清空图形选择集合 + _pGraphicsOverLay.ClearSelection(); //清空图形层所选 } - }else if (m_CurOper == CURRENTOPERATION.Cal_Simplify) + } + else if (m_CurOper == CURRENTOPERATION.Cal_Simplify) { IdentifyGraphicsOverlayResult gResult_Buff = await MainMapView.IdentifyGraphicsOverlayAsync(_pGraphicsOverLay, e.Position, 5, false); @@ -629,18 +658,23 @@ namespace GisDevelop_Exp if (resultGeometry != null) //处理结果 { _pGraphicsOverLay.Graphics.Remove(g1); //从图形层中移除原图形 - if(resultGeometry.GeometryType == GeometryType.Point || resultGeometry.GeometryType == GeometryType.Multipoint){ + if (resultGeometry.GeometryType == GeometryType.Point || + resultGeometry.GeometryType == GeometryType.Multipoint) + { Graphic simplifiedGraphic = new Graphic(resultGeometry, pointSymbol); //利用剪切结果构造新的图形 _pGraphicsOverLay.Graphics.Add(simplifiedGraphic); - }else if (resultGeometry.GeometryType == GeometryType.Polyline) + } + else if (resultGeometry.GeometryType == GeometryType.Polyline) { Graphic simplifiedGraphic = new Graphic(resultGeometry, lineSymbol); //利用剪切结果构造新的图形 _pGraphicsOverLay.Graphics.Add(simplifiedGraphic); - }else if (resultGeometry.GeometryType == GeometryType.Polygon) + } + else if (resultGeometry.GeometryType == GeometryType.Polygon) { Graphic simplifiedGraphic = new Graphic(resultGeometry, fillSymbol); //利用剪切结果构造新的图形 _pGraphicsOverLay.Graphics.Add(simplifiedGraphic); } + m_CurOper = CURRENTOPERATION.NullOpe; } @@ -648,6 +682,125 @@ namespace GisDevelop_Exp _pGraphicsOverLay.ClearSelection(); //清空图形层所选 } } + else if (m_CurOper == CURRENTOPERATION.DelFeature) + { + if (_featureLayer == null) + return; + _featureLayer.ClearSelection(); + // 取消标注 + MainMapView.DismissCallout(); + try + { + // 执行点击查询 + IdentifyLayerResult identifyResult = await + MainMapView.IdentifyLayerAsync(_featureLayer, e.Position, 2, false); + // 判断是否选中要素 + if (!identifyResult.GeoElements.Any()) + return; + //获取选中的第一个要素 + Feature tappedFeature = (Feature)identifyResult.GeoElements[0]; + // 将要素放到选择集中 + // _featureLayer.SelectFeature(tappedFeature); + // 显示callout,提示是否删除要素 + ShowDeletionCallout(tappedFeature); + //// 删除要素. + // await _featureLayer.FeatureTable.DeleteFeatureAsync(tappedFeature); + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString(), "There was a problem."); + } + }else if ( m_CurOper == CURRENTOPERATION.AddPolygonFeature) + { + if (linePoints == null) + linePoints = new List(); + if (linesList == null) + linesList = new List(); + linePoints.Add(e.Location);//将当前点加入点集 + if (linePoints.Count >= 2)//如果点集中点数足够 + { + if (linesList.Count > 0 && linePoints.Count > 2)//已有加入的线图形,则移除当前的图形,重新加入新的图形 + { + Graphic curGraphic = linesList[linesList.Count - 1]; + MainMapView.GraphicsOverlays[0].Graphics.Remove(curGraphic); + linesList.Remove(curGraphic); + } + //SimpleLineSymbol lineSymbol = new SimpleLineSymbol(); + //lineSymbol.Color = Color.Blue; + //lineSymbol.Style = SimpleLineSymbolStyle.Dash; + //lineSymbol.Width = 3; + Esri.ArcGISRuntime.Geometry.Polygon line = new + Esri.ArcGISRuntime.Geometry.Polygon(linePoints);//由点集构造线 + Graphic lineGraphic = new Graphic(line, fillSymbol);//由线创建图形 + MainMapView.GraphicsOverlays[0].Graphics.Add(lineGraphic);//加入该图形元素到覆盖层中 + linesList.Add(lineGraphic);//同时,将该图形加入链表 + } + }else if (m_CurOper== CURRENTOPERATION.ModifyFeature) + { + if (_featureLayer == null) + return; + _featureLayer.ClearSelection(); + // 取消标注 + MainMapView.DismissCallout(); + try + { + // 执行点击查询 + IdentifyLayerResult identifyResult = await + MainMapView.IdentifyLayerAsync(_featureLayer, e.Position, 2, false); + // 判断是否选中要素 + if (!identifyResult.GeoElements.Any()) + return; + //获取选中的第一个要素 + Feature tappedFeature = (Feature)identifyResult.GeoElements[0]; + // 将要素放到选择集中 + _featureLayer.SelectFeature(tappedFeature); + // 显示callout,提示是否修改要素 + // ShowModifyCallout(tappedFeature); + // 修改要素. + string sNewName = "New"; + tappedFeature.SetAttributeValue("XZQMC", sNewName); + await _featureLayer.FeatureTable.UpdateFeatureAsync(tappedFeature); + MessageBox.Show("Modify feature with Success!"); + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString(), "There was a problem."); + } + } + } + + private void ShowDeletionCallout(Feature tappedFeature) + { +// 为删除要素创建一个button. + Button deleteButton = new Button(); + deleteButton.Content = "删除要素"; + deleteButton.Padding = new Thickness(5); + deleteButton.Tag = tappedFeature; + // Click事件委托 + deleteButton.Click += DeleteButton_Click; + // 显示标注 + MainMapView.ShowCalloutAt(tappedFeature.Geometry.Extent.GetCenter(), deleteButton); + } + + private async void DeleteButton_Click(object sender, RoutedEventArgs e) + { + // 取消标注 + MainMapView.DismissCallout(); + try + { + Button deleteButton = (Button)sender; + Feature featureToDelete = (Feature)deleteButton.Tag; + // 删除要素. + await _featureLayer.FeatureTable.DeleteFeatureAsync(featureToDelete); + // 如果是网络数据,需要调用以下语句进行网络服务器端的数据同步更新Sync the change with the service. + // ServiceFeatureTable serviceTable = (ServiceFeatureTable)_featureLayer.FeatureTable; + // await serviceTable.ApplyEditsAsync(); + MessageBox.Show("Deleted feature with Success!"); + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString(), "Couldn't delete feature"); + } } private void Menu_Simple_Identify_OnClick(object sender, RoutedEventArgs e) @@ -901,7 +1054,7 @@ namespace GisDevelop_Exp CheckBox cb = sender as CheckBox; cb.IsChecked = true; int index = GetLayerIndex(cb.ToolTip as string); - MainMapView.Map.OperationalLayers[index].IsVisible = true; + MainMapView.Map.OperationalLayers[index-1].IsVisible = true; } internal static class Utils @@ -1010,27 +1163,29 @@ namespace GisDevelop_Exp linesList.Add(lineGraphic); } } - - if(m_CurOper == CURRENTOPERATION.ClickQuery)//检查是否为按照位置查询中的点选 - { - IInputElement ie = (IInputElement)sender;//转换事件宿主对象为IInputElement对象 - IReadOnlyList< IdentifyLayerResult> results =await - MainMapView.IdentifyLayersAsync(e.GetPosition(ie), 15, false);//根据鼠标点在所有图层中进行查询 - if(results!=null)//查询结果不为空 - { - if (locationQueryResultsWindow != null && - locationQueryResultsWindow.IsClosed)//位置查询结果子窗体已关闭 - { - locationQueryResultsWindow = null;//将子窗体对象置为空 - } - if (locationQueryResultsWindow == null) - { - locationQueryResultsWindow = new LocationQueryResultsWindow();//构造新的子窗体对象 - } - locationQueryResultsWindow.ResultCollection = results;//为子窗体引用查询结果集对象 - locationQueryResultsWindow.Show();//显示该子窗体 - locationQueryResultsWindow.Activate();//使子窗体处于激活状态 - } + + if (m_CurOper == CURRENTOPERATION.ClickQuery) //检查是否为按照位置查询中的点选 + { + IInputElement ie = (IInputElement)sender; //转换事件宿主对象为IInputElement对象 + IReadOnlyList results = await + MainMapView.IdentifyLayersAsync(e.GetPosition(ie), 15, false); //根据鼠标点在所有图层中进行查询 + if (results != null) //查询结果不为空 + { + if (locationQueryResultsWindow != null && + locationQueryResultsWindow.IsClosed) //位置查询结果子窗体已关闭 + { + locationQueryResultsWindow = null; //将子窗体对象置为空 + } + + if (locationQueryResultsWindow == null) + { + locationQueryResultsWindow = new LocationQueryResultsWindow(); //构造新的子窗体对象 + } + + locationQueryResultsWindow.ResultCollection = results; //为子窗体引用查询结果集对象 + locationQueryResultsWindow.Show(); //显示该子窗体 + locationQueryResultsWindow.Activate(); //使子窗体处于激活状态 + } } else if (m_CurOper == CURRENTOPERATION.ExtentQuery) { @@ -1052,11 +1207,12 @@ namespace GisDevelop_Exp SimpleLineSymbol lineSymbol = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol(SimpleLineSymbolStyle.Dash, lineColor, 2.0); Color fillColor = Color.FromArgb(0, 0, 0, 0); - SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, fillColor, lineSymbol); + SimpleFillSymbol fillSymbol = + new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, fillColor, lineSymbol); Graphic polygonGraphic = new Graphic(polygon, fillSymbol); MainMapView.GraphicsOverlays.Add(new GraphicsOverlay()); MainMapView.GraphicsOverlays[0].Graphics.Add(polygonGraphic); - + Envelope selEnv = new Envelope(this._startLocation.X, this._startLocation.Y, location.X, location.Y, MainMapView.SpatialReference); QueryParameters pQueryPara = new QueryParameters(); @@ -1096,9 +1252,10 @@ namespace GisDevelop_Exp string featureName = pFeature.FeatureTable.Layer.Name; attributes.Add(FeatureAttri); } + queryResults.Add(new QueryResult(featureLayerName, attributes)); } - + ExtentLocationQueryResultsWindow resultsWindow = new ExtentLocationQueryResultsWindow(); resultsWindow.resultCollection = queryResults; resultsWindow.Show(); @@ -1135,8 +1292,44 @@ namespace GisDevelop_Exp private void MainMapView_OnMouseRightButtonDown(object sender, MouseButtonEventArgs e) { + if (m_CurOper== CURRENTOPERATION.AddPolygonFeature && linePoints != null) + { + Polygon poly = new Polygon(linePoints);//由点集构造线 + AddFeature(poly, _featureLayer); + if (linePoints != null) + linePoints.Clear(); + if (linesList != null) + linesList.Clear(); + } m_CurOper = CURRENTOPERATION.NullOpe; } + + private async void AddFeature(Geometry pGeo, FeatureLayer pFeatureL) + { + try + { + if (pGeo.GeometryType != pFeatureL.FeatureTable.GeometryType) + { + MessageBox.Show("几何类型和图层要素类型不匹配"); + return; + } + // 创建要素 + Feature feature = (Feature)pFeatureL.FeatureTable.CreateFeature(); + feature.Geometry = pGeo; + // 设置要素属性. + feature.SetAttributeValue("XZQMC", "Test"); + // 将要素增加到数据表中. + await pFeatureL.FeatureTable.AddFeatureAsync(feature); + // 如果图层是网上数据,需同步跟新数据 + //await pFeatureL.FeatureTable.ApplyEditsAsync(); + feature.Refresh(); + MessageBox.Show("Created feature Success!"); + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString(), "Error adding feature"); + } + } private void SetSimpleRender(FeatureLayer pLayer, Symbol pSymbol) { @@ -1621,30 +1814,51 @@ namespace GisDevelop_Exp { List myTables; myTables = new List(); - for(int i=0;i