Finish all seven experiments

This commit is contained in:
PeterZhong 2024-12-20 11:30:20 +08:00
parent cb2d49e0d2
commit 45943b7e55
3 changed files with 289 additions and 68 deletions

View File

@ -61,7 +61,12 @@
<MenuItem x:Name="Menu_Query_Envelope" Header="拉框选择" Click="Menu_Query_Envelope_OnClick"></MenuItem>
</MenuItem>
<Separator></Separator>
<MenuItem x:Name="Menu_Clear_Selection" Header="清除选择"></MenuItem>
<MenuItem x:Name="Menu_Clear_Selection" Header="清除选择" Click="Menu_Clear_Selection_OnClick"></MenuItem>
</MenuItem>
<MenuItem Header="要素编辑">
<MenuItem x:Name="Menu_Delete" Header="删除" Click="Menu_Delete_OnClick"></MenuItem>
<MenuItem x:Name="Menu_Add" Header="增加面" Click="Menu_Add_OnClick"></MenuItem>
<MenuItem x:Name="Menu_Modify" Header="修改" Click="Menu_Modify_OnClick"></MenuItem>
</MenuItem>
</Menu>
<esri:MapView x:Name="MainMapView" Map="{Binding Map, Source={StaticResource MapViewModel}}" Margin="0,19,0,0"

View File

@ -47,7 +47,10 @@ enum CURRENTOPERATION
Cal_Buff,
AttributeQuery,
ClickQuery,
ExtentQuery
ExtentQuery,
DelFeature,
AddPolygonFeature,
ModifyFeature,
}
namespace GisDevelop_Exp
@ -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();
}
@ -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,7 +544,22 @@ namespace GisDevelop_Exp
Graphic g1 = listOfClipGraphics[0];
Graphic g2 = listOfClipGraphics[1];
IReadOnlyList<Geometry> 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<Geometry>
resultGeometry = GeometryEngine.Intersections(g1.Geometry, g2.Geometry); //执行求交点操作
// Geometry resultGeometry = GeometryEngine.Intersection(g1.Geometry, g2.Geometry); //执行求交操作
if (resultGeometry != null) //处理结果
{
@ -548,17 +568,21 @@ namespace GisDevelop_Exp
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();
@ -580,12 +604,15 @@ namespace GisDevelop_Exp
_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();
@ -606,10 +633,12 @@ namespace GisDevelop_Exp
_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<MapPoint>();
if (linesList == null)
linesList = new List<Graphic>();
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
@ -1011,25 +1164,27 @@ namespace GisDevelop_Exp
}
}
if(m_CurOper == CURRENTOPERATION.ClickQuery)//检查是否为按照位置查询中的点选
if (m_CurOper == CURRENTOPERATION.ClickQuery) //检查是否为按照位置查询中的点选
{
IInputElement ie = (IInputElement)sender;//转换事件宿主对象为IInputElement对象
IReadOnlyList< IdentifyLayerResult> results =await
MainMapView.IdentifyLayersAsync(e.GetPosition(ie), 15, false);//根据鼠标点在所有图层中进行查询
if(results!=null)//查询结果不为空
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.IsClosed) //位置查询结果子窗体已关闭
{
locationQueryResultsWindow = null;//将子窗体对象置为空
locationQueryResultsWindow = null; //将子窗体对象置为空
}
if (locationQueryResultsWindow == null)
{
locationQueryResultsWindow = new LocationQueryResultsWindow();//构造新的子窗体对象
locationQueryResultsWindow = new LocationQueryResultsWindow(); //构造新的子窗体对象
}
locationQueryResultsWindow.ResultCollection = results;//为子窗体引用查询结果集对象
locationQueryResultsWindow.Show();//显示该子窗体
locationQueryResultsWindow.Activate();//使子窗体处于激活状态
locationQueryResultsWindow.ResultCollection = results; //为子窗体引用查询结果集对象
locationQueryResultsWindow.Show(); //显示该子窗体
locationQueryResultsWindow.Activate(); //使子窗体处于激活状态
}
}
else if (m_CurOper == CURRENTOPERATION.ExtentQuery)
@ -1052,7 +1207,8 @@ 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);
@ -1096,6 +1252,7 @@ namespace GisDevelop_Exp
string featureName = pFeature.FeatureTable.Layer.Name;
attributes.Add(FeatureAttri);
}
queryResults.Add(new QueryResult(featureLayerName, attributes));
}
@ -1135,9 +1292,45 @@ 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)
{
if (pSymbol == null)
@ -1621,30 +1814,51 @@ namespace GisDevelop_Exp
{
List<FeatureTable> myTables;
myTables = new List<FeatureTable>();
for(int i=0;i<MainMapView.Map.OperationalLayers.Count;i++)
for (int i = 0; i < MainMapView.Map.OperationalLayers.Count; i++)
{
FeatureLayer pFL = MainMapView.Map.OperationalLayers[i] as FeatureLayer;
if(pFL != null)
if (pFL != null)
{
myTables.Add(pFL.FeatureTable);
}
}
AttributeQuery qw = new AttributeQuery();//创建新的查询窗体对象
qw.Tables.AddRange(myTables);//为窗体对象的表集合对象添加表集合
bool r =(bool) qw.ShowDialog();//以对话框方式显示该子窗体
AttributeQuery qw = new AttributeQuery(); //创建新的查询窗体对象
qw.Tables.AddRange(myTables); //为窗体对象的表集合对象添加表集合
bool r = (bool)qw.ShowDialog(); //以对话框方式显示该子窗体
}
private void Menu_Query_Point_OnClick(object sender, RoutedEventArgs e)
{
selByGeometryType = 1;//设置其值为1
selByGeometryType = 1; //设置其值为1
m_CurOper = CURRENTOPERATION.ClickQuery;
}
private void Menu_Query_Envelope_OnClick(object sender, RoutedEventArgs e)
{
selByGeometryType = 2;//设置其值为2
selByGeometryType = 2; //设置其值为2
m_CurOper = CURRENTOPERATION.ExtentQuery;
}
private void Menu_Delete_OnClick(object sender, RoutedEventArgs e)
{
m_CurOper = CURRENTOPERATION.DelFeature;
}
private void Menu_Add_OnClick(object sender, RoutedEventArgs e)
{
m_CurOper = CURRENTOPERATION.AddPolygonFeature;
}
private void Menu_Modify_OnClick(object sender, RoutedEventArgs e)
{
m_CurOper = CURRENTOPERATION.ModifyFeature;
}
private void Menu_Clear_Selection_OnClick(object sender, RoutedEventArgs e)
{
_featureLayer.ClearSelection();
}
}
}

View File

@ -35,15 +35,15 @@ namespace GisDevelop_Exp
private void QueryResultWindow_OnLoaded(object sender, RoutedEventArgs e)
{
if (featureQuerySet != null)//结果不为空
if (featureQuerySet != null)
{
dataGridResult.AutoGenerateColumns = true;//数据格网控件自动生成列
dataGridResult.AutoGenerateColumns = true;
for(int i=1;i<= featureQuerySet.Count();i++)
{
comboBoxSelectionCount.Items.Add(i);//按照查询结果集中要素的数目为组合框添加项
comboBoxSelectionCount.Items.Add(i);
}
Feature ft = featureQuerySet.First();//取得查询结果集合中第一个要素
dataGridResult.ItemsSource = ft.Attributes;//设置数据格网对象中项的数据源
Feature ft = featureQuerySet.First();
dataGridResult.ItemsSource = ft.Attributes;
// ft.FeatureTable.FeatureLayer.SelectFeature(ft);
comboBoxSelectionCount.SelectedIndex = 0;
((FeatureLayer)ft.FeatureTable.Layer).SelectFeature(ft);
@ -60,6 +60,8 @@ namespace GisDevelop_Exp
{
Feature ft = featureQuerySet.ElementAt(idx);//获取索引所在的要素对象
dataGridResult.ItemsSource = ft.Attributes;//设置数据格网对象中项的数据源
((FeatureLayer)ft.FeatureTable.Layer).ClearSelection();
((FeatureLayer)ft.FeatureTable.Layer).SelectFeature(ft);
// ft.FeatureTable.FeatureLayer.ClearSelection();//清空图层中的原有的选择
// ft.FeatureTable.FeatureLayer.SelectFeature(ft);//在图层中重新选择当前所选要素
}