private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { if (miStartEditing.Enabled == false) //Start Editing { DataOperator dataOperator = new DataOperator(axMapControl1.Map); ILayer layer = dataOperator.GetLayerByName(sEditLayerName); // Get the layer to edit IFeatureLayer featureLayer = layer as IFeatureLayer; IFeature feature; if (featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint) //Edit point { IPoint point = new PointClass(); point.PutCoords(e.mapX, e.mapY); feature = dataOperator.AddGeometryToLayer(sEditLayerName, point); AddFeature addFeatureName = new AddFeature(feature, axMapControl1.Map, this); addFeatureName.ShowDialog(); } else if (featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon) { if (drawPolygon == null)//Continue creating polygons when editting is not stopped { drawPolygon = new DrawPolygon(sEditLayerName, axMapControl1.Map, this); drawPolygon.set_drawStart(true); IPoint point = new PointClass(); point.PutCoords(e.mapX, e.mapY); drawPolygon.set_startPoint(point); //set start point drawPolygon.OnCreate(axMapControl1.Object); drawPolygon.set_polyFeedback(point); } } return; } }
private void polyshp_Click(object sender, EventArgs e) { DataOperator dataOperator = new DataOperator(axMapControl1.Map); IFeatureClass featureClass = dataOperator.CreateShapefile("E:\\", "ShapefileWorespace", "ShapefileSample", "Polygon"); if (featureClass == null) { MessageBox.Show("创建shapefile文件失败!"); return; } //将要素类添加到地图中,图层名为"Observation Stations" //如果创建成功,将"创建shapefile"按钮禁用 //如果创建失败,弹出消息框提示 bool bRes = dataOperator.AddFeatureClassToMap(featureClass, "Observation Stations"); if (bRes) { polyshp.Enabled = false; return; } else { MessageBox.Show("将新建shapefile文件加入地图失败!"); return; } }
private void simpleRenderToolStripMenuItem_Click(object sender, EventArgs e) { //获取"World Cities"图层 DataOperator dataoperator = new DataOperator(axMapControl1.Map); ILayer layer = dataoperator.GetLayerByName("World Cities"); //通过IRgbColor接口新建rgbcolor对象,并设置颜色为蓝色 IRgbColor rgbcolor = new RgbColorClass(); rgbcolor.Red = 0; rgbcolor.Blue = 255; rgbcolor.Green = 0; //获取"World Cities"图层的要素信息,并通过IColor接口访问设置好的颜色对象 ISymbol symbol = DataOperator.GetSymbolByLayer(layer); IColor color = rgbcolor as IColor; //判断图层和颜色是否获取成功,如果获取成功,则当前视图刷新,并显示简单渲染效果 //使simplerender菜单项不可再用 //如果获取失败,则弹出消息框提示"简单渲染失败!" bool t = DataOperator.SetSymbolColor(layer, color); if (t) { axTOCControl1.ActiveView.ContentsChanged(); axMapControl1.ActiveView.Refresh(); simpleRender.Enabled = false; } else { MessageBox.Show("简单渲染失败!"); } }
public bool Buffer(string layerName, string sWhere, int iSize, IMap iMap) { IFeatureClass featureClass; IFeature feature; IGeometry geometry; DataOperator dataOperator = new DataOperator(iMap); IFeatureLayer featureLayer = (IFeatureLayer)dataOperator.GetLayerByName(layerName); featureClass = featureLayer.FeatureClass; IQueryFilter queryFilter = new QueryFilter(); queryFilter.WhereClause = sWhere; IFeatureCursor featureCursor; featureCursor = (IFeatureCursor)featureClass.Search(queryFilter, false); int count = featureClass.FeatureCount(queryFilter); feature = featureCursor.NextFeature(); geometry = feature.Shape; ITopologicalOperator ipTO = (ITopologicalOperator)geometry; IGeometry iGeomBuffer = ipTO.Buffer(iSize); ISpatialFilter spatialFilter = new SpatialFilter(); spatialFilter.Geometry = iGeomBuffer; spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIndexIntersects; IFeatureSelection featureSelection = (IFeatureSelection)featureLayer; featureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); return(true); }
//Query Filter private void btnOK_Click(object sender, EventArgs e) { m_spatialFilter.WhereClause = tbQuery.Text; IFeatureSelection featureSelection = (IFeatureSelection)m_layer; try { if (cbMethod.Text == "Create a new selection") { featureSelection.SelectFeatures(m_spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } else if (cbMethod.Text == "Add to current selection") { featureSelection.SelectFeatures(m_spatialFilter, esriSelectionResultEnum.esriSelectionResultAdd, false); } else if (cbMethod.Text == "Remove from current selection") { featureSelection.SelectFeatures(m_spatialFilter, esriSelectionResultEnum.esriSelectionResultSubtract, false); } else if (cbMethod.Text == "Select from current selection") { featureSelection.SelectFeatures(m_spatialFilter, esriSelectionResultEnum.esriSelectionResultAnd, false); } } catch { MessageBox.Show("Invalid query!"); this.Close(); } //Show the attributes of the selected features ISelectionSet selectionSet = featureSelection.SelectionSet; ICursor cursor; selectionSet.Search(null, true, out cursor); //Get the cursor m_FeatureCursor = cursor as IFeatureCursor; IFeatureLayer featureLayer = m_layer as IFeatureLayer; DataOperator dataOperator = new DataOperator(m_map); if (selectionSet.Count > 0) { DataTable dataTable = dataOperator.GetDataTable(m_FeatureCursor, featureLayer); selectionSet.Search(null, true, out cursor); //Get the cursor m_FeatureCursor = cursor as IFeatureCursor; DataBoard dataBoard = new DataBoard(m_map, dataTable, m_FeatureCursor, selectionSet); dataBoard.SetSelectedLayer(m_layer.Name); dataBoard.cbEnabledFalse(); dataBoard.Show(); } m_frmMain.ActiveViewPartialRefresh(); MessageBox.Show(selectionSet.Count + " features are selected."); this.Close(); }
public DataBoard(IMap map, DataTable dataTable) { //Initialize components InitializeComponent(); m_map = map; //Input data source dataGridView1.DataSource = dataTable; AddLayerNameIntoComboBox(); m_dataOperator = new DataOperator(m_map); }
public bool QueryIntersect(string srcLayerName, string tgtLayerName, IMap iMap, esriSpatialRelationEnum spatialRel) { DataOperator dataOperator = new DataOperator(iMap); //Get layer by name IFeatureLayer iSrcLayer = (IFeatureLayer)dataOperator.GetLayerByName(srcLayerName); IFeatureLayer iTgtLayer = (IFeatureLayer)dataOperator.GetLayerByName(tgtLayerName); //using query filter to get geom IGeometry geom; IFeature feature; IFeatureCursor featCursor; IFeatureClass srcFeatClass; IQueryFilter queryFilter = new QueryFilter(); queryFilter.WhereClause = "CONTINENT='Asia'"; //set query clause featCursor = iTgtLayer.FeatureClass.Search(queryFilter, false); feature = featCursor.NextFeature(); geom = feature.Shape; //city attributes filter srcFeatClass = iSrcLayer.FeatureClass; ISpatialFilter spatialFilter = new SpatialFilter(); spatialFilter.Geometry = geom; spatialFilter.WhereClause = "POP_RANK>5"; spatialFilter.SpatialRel = (ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum)spatialRel; //select features IFeatureSelection featureSelection = (IFeatureSelection)iSrcLayer; featureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); ISelectionSet selectionSet = featureSelection.SelectionSet; ICursor cursor; selectionSet.Search(null, true, out cursor); IFeatureCursor featureCursor = cursor as IFeatureCursor; if (selectionSet.Count > 0) { DataBoard dataBoard = new DataBoard(iMap, dataOperator.GetDataTable(featureCursor, iSrcLayer)); dataBoard.cbEnabledFalse(); dataBoard.Show(); } return(true); }
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { //当"添加要素"菜单项被勾选时,进行如下操作 if (AddPoint.Checked == true) { IPoint point = new PointClass(); point.PutCoords(e.mapX, e.mapY); DataOperator dataOperator = new DataOperator(axMapControl1.Map); dataOperator.AddFeatureToLayer("Observation Stations", "观测站", point); return; } //以下使用的是trackline和trackpolygon函数来进行线和多边形的绘制 //注释的代码部分为另一种绘制线的方式 if (AddLine.Checked == true) { /*ISimpleLineSymbol SimpleLineSymbol = new SimpleLineSymbolClass(); * SimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDot; * IRgbColor rgbColor = new RgbColorClass(); * rgbColor.Red = 255; * SimpleLineSymbol.Color = rgbColor; * SimpleLineSymbol.Width = 3; * object o = (object)SimpleLineSymbol; * IGeometry Geometry; * Geometry = axMapControl1.TrackLine(); * axMapControl1.DrawShape(Geometry, ref o);*/ IGeometry polyline = axMapControl1.TrackLine(); ILineElement LineElement; LineElement = new LineElementClass(); IElement Element = LineElement as IElement; Element.Geometry = polyline; IGraphicsContainer GraphicsContainer = axMapControl1.Map as IGraphicsContainer; GraphicsContainer.AddElement((IElement)LineElement, 0); axMapControl1.ActiveView.Refresh(); } if (AddPolygon.Checked == true) { IGeometry Polygon = axMapControl1.TrackPolygon(); IPolygonElement PolygonElement = new PolygonElementClass(); IElement Element = PolygonElement as IElement; Element.Geometry = Polygon; IGraphicsContainer GraphicsContainer = axMapControl1.Map as IGraphicsContainer; GraphicsContainer.AddElement((IElement)PolygonElement, 0); axMapControl1.ActiveView.Refresh(); } }
private void miAccessDataToolStripMenuItem_Click(object sender, EventArgs e) { //获取保存有亚洲城市名的属性表,将其作为构造函数的参数 //新建DataBook对象 DataOperator dataOperator = new DataOperator(axMapControl1.Map); DataBook dataBook = new DataBook("亚洲城市人口", dataOperator.GetCitiesName("World Cities", "Continents", esriSpatialRelationEnum.esriSpatialRelationIntersection)); //在地图中显示选中的要素 IActiveView activeview = axMapControl1.ActiveView; activeview.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, 0, axMapControl1.Extent); //展示DataBook对象 dataBook.Show(); }
/// <summary> /// button: confirm /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { ISpatialReference currentSpatialRef = currentMap.SpatialReference; IFeatureClass featureClass = DataOperator.CreateShp(textBox3.Text, textBox2.Text, textBox1.Text, comboBox1.SelectedItem.ToString(), currentSpatialRef); if (featureClass == null) { MessageBox.Show("FAIL"); return; } DataOperator.AddFeatureClass_Map(featureClass, textBox1.Text, currentMap); this.Close(); }
public DataBoard(IMap map, DataTable dataTable, IFeatureCursor cursor, ISelectionSet set) { //Initialize components InitializeComponent(); m_map = map; m_SelectionSet = set; //Input data source m_dataOperator = new DataOperator(m_map); dataGridView1.DataSource = dataTable; AddLayerNameIntoComboBox(); //Don't change the data table IndexChangeFlag = false; m_FeatureCursor = cursor; }
private void btnAdmit_Click(object sender, EventArgs e) { IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = fields as IFieldsEdit; //add fields for (int i = 3; i < listBoxFieldName.Items.Count; i++) { AddNewField(fields, listBoxFieldName.Items[i].ToString(), listBoxAliasName.Items[i].ToString(), listBoxFieldType.Items[i].ToString()); } //Show message box when the input is not complete if (tbFilePath.Text == "") { MessageBox.Show("Please input file path!"); } else if (tbShapefileName.Text == "") { MessageBox.Show("Please input file name!"); } else if (System.IO.File.Exists(tbFilePath.Text + "\\" + tbShapefileName.Text + ".shp")) { MessageBox.Show("The shapefile already exists!"); } else { //Get parent directory and workspace names String sParentDirectory = tbFilePath.Text.Substring(0, fbd.SelectedPath.LastIndexOf("\\")); String sWorkspace = tbFilePath.Text.Substring(fbd.SelectedPath.LastIndexOf("\\") + 1); DataOperator dataOperator = new DataOperator(m_map); IFeatureClass featureClass = dataOperator.CreateShapefile(fields, sParentDirectory, sWorkspace, tbShapefileName.Text, cbGeometryType.SelectedItem.ToString()); if (featureClass == null) { MessageBox.Show("Failed to create shapefile!"); } //add shapefile to the map if (MessageBox.Show("Do you want to add the shapefile to the map?", "", MessageBoxButtons.OKCancel) == DialogResult.OK) { dataOperator.AddFeatureClassToMap(featureClass, tbShapefileName.Text); } this.Close(); } }
private void myBuffer_Click(object sender, EventArgs e) { DataOperator dataOperator = new DataOperator(axMapControl1.Map); //dataOperator.Buffer("World Cities","CITY_NAME='Istanbul'",1,axMapControl1.Map); int col; Form form2 = new BufferAnalysis(); //form2.Show(); if (form2.ShowDialog() == DialogResult.OK) { string str = form2.Controls["textBox1"].Text; col = int.Parse(str); DataBook databook = new DataBook("Istanbul's Cities", dataOperator.Buffer("World Cities", "CITY_NAME='Istanbul'", col, axMapControl1.Map)); //在地图中显示选中的要素 IActiveView activeview = axMapControl1.ActiveView; activeview.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, 0, axMapControl1.Extent); databook.Show(); } }
private void cbLayerName_SelectedIndexChanged(object sender, EventArgs e) { //Get the chosen layer DataOperator dataOperator = new DataOperator(m_map); m_layer = dataOperator.GetLayerByName(cbLayerName.SelectedItem.ToString()); //Add the fields of the layer IFeatureLayer featureLayer = m_layer as IFeatureLayer; IField field = null; lbxFields.Items.Clear(); for (int i = 0; i < featureLayer.FeatureClass.Fields.FieldCount; i++) { field = featureLayer.FeatureClass.Fields.get_Field(i); lbxFields.Items.Add(field.Name); } //Change layer name label lbLayerName.Text = cbLayerName.Text; }
public void HighLight(int row) { DataOperator dataoperator = new DataOperator(m_map); IMarkerSymbol markerSymbol = new SimpleMarker3DSymbolClass(); //markerSymbol的类型和分辨率 ((ISimpleMarker3DSymbol)markerSymbol).Style = esriSimple3DMarkerStyle.esriS3DMSSphere; ((ISimpleMarker3DSymbol)markerSymbol).ResolutionQuality = 1.0; //markerSymbol的颜色和大小 IRgbColor color = new RgbColorClass(); color.Red = 250; color.Green = 230; color.Blue = 20; markerSymbol.Size = 8; markerSymbol.Color = color as IColor; // 将showElement对象赋予markerSymbol的属性 IElement showElement = new MarkerElementClass(); ((IMarkerElement)showElement).Symbol = markerSymbol; showElement.Geometry = pointCollection.Point[row]; //在绘制前,清楚图像容器内的所有Elements pGraphicsContainer.DeleteAllElements();//通过name删除 //为了使点在上面,先显示polygon IGeometry polygon = pointCollection as IGeometry; IPolygonElement polygonElement = new PolygonElementClass(); IElement polyElement = polygonElement as IElement; polyElement.Geometry = polygon; pGraphicsContainer.AddElement((IElement)polygonElement, 0); //绘制高亮显示的点 pGraphicsContainer.AddElement((IElement)showElement, 0); //重新绘制除了高亮点外的构造点 for (int i = 0; (i < pointCollection.PointCount); i++) { if (i != row) { IRgbColor color3 = new RgbColorClass(); //如果出现重复 将函数独立出来 color3.Red = 255; color3.Green = 0; color3.Blue = 0; markerSymbol.Size = 5; markerSymbol.Color = color3; IElement pElement = new MarkerElementClass(); ((IMarkerElement)pElement).Symbol = markerSymbol; pElement.Geometry = pointCollection.Point[i]; pGraphicsContainer.AddElement(pElement, 0); /*IElement pElement=new MarkerElementClass(); * pElement.Geometry = pointCollection.Point[i]; * pGraphicsContainer.AddElement(pElement, 0);*/ } } m_form.axMapControl1.ActiveView.Refresh();//partialrefresh }
/// <summary> /// attribute + spatial selection altogether in one /// </summary> private void SelectFeatures() { //complemented by the same class of IFeatureLayer IFeatureSelection featureSelection = currentFeatureLayer as IFeatureSelection; //IQueryFilter is implemented by coclass QueryFilter IQueryFilter queryFilter = new QueryFilterClass(); //IQueryFilter.WhereClause queryFilter.WhereClause = textBoxWhere.Text; //this part of code can be both used by two ways of selection switch (comboBoxSelectingResult.SelectedIndex) { case 0: //create new selection currentMap.ClearSelection(); featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); break; case 1: //add to current selection featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultAdd, false); break; case 2: featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultXOR, false); break; case 3: featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultAnd, false); break; default: currentMap.ClearSelection(); featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); break; } //class SpatialFilter : QueryFilter ISpatialFilter spatialFilter = new SpatialFilterClass(); //attribute + spatial query if (this.Width == this.MaxWidth) { ICursor pCursor; featureSelection.SelectionSet.Search(null, false, out pCursor); IFeatureCursor featureCursor = pCursor as IFeatureCursor; IFeature pFeature = featureCursor.NextFeature(); IGeometry pGeometry = null; ITopologicalOperator topologicalOperator; while (pFeature != null) { if (pGeometry != null) { //this temp topo_Buffer is used to create a searching range by ITopologicalOperator.Buffer ITopologicalOperator topo_Buffer = pFeature.Shape as ITopologicalOperator; //Union is a add-to-existing method topologicalOperator = pGeometry as ITopologicalOperator; pGeometry = topologicalOperator.Union(topo_Buffer.Buffer(double.Parse(textBox1.Text))); } //first in else { topologicalOperator = pFeature.Shape as ITopologicalOperator; pGeometry = topologicalOperator.Buffer(double.Parse(textBox1.Text)); } //update pFeature using IFeatureCursor.NextFeature() pFeature = featureCursor.NextFeature(); } //ISpatialFilter.Geometry spatialFilter.Geometry = pGeometry; switch (comboBoxSpatialOperation.SelectedIndex) { case 0: spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; break; case 1: spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin; break; case 2: spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains; break; case 3: spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelTouches; break; case 4: spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses; break; default: spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; break; } //spatial query's real selection is on the targetlayer featureSelection.Clear(); for (int i = 0; i < checkedListBoxTargetLayers.CheckedItems.Count; i++) { IFeatureLayer pFeatureLayer = DataOperator.GetFeatureLayerByName(currentMap, checkedListBoxTargetLayers.CheckedItems[i].ToString()); featureSelection = pFeatureLayer as IFeatureSelection; featureSelection.SelectFeatures((IQueryFilter)spatialFilter, esriSelectionResultEnum.esriSelectionResultAdd, false); } } IActiveView activeView = currentMap as IActiveView; //PartialRefresh: esriViewDrawPhase activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent); }