private IGeometry GetGeometryByCoor() { //直接获得坐标范围 double dblMinX = 0; double dblMaxX = 0; double dblMinY = 0; double dblMaxY = 0; double.TryParse(this.txtMinX.Text, out dblMinX); double.TryParse(this.txtMaxX.Text, out dblMaxX); double.TryParse(this.txtMinY.Text, out dblMinY); double.TryParse(this.txtMaxY.Text, out dblMaxY); //定义范围 IPointCollection pPntCollection = new PolygonClass(); object obj = System.Type.Missing; IPoint pPnt1 = new PointClass(); pPnt1.PutCoords(dblMinX, dblMinY); pPntCollection.AddPoint(pPnt1, ref obj, ref obj); IPoint pPnt2 = new PointClass(); pPnt2.PutCoords(dblMaxX, dblMinY); pPntCollection.AddPoint(pPnt2, ref obj, ref obj); IPoint pPnt3 = new PointClass(); pPnt3.PutCoords(dblMaxX, dblMaxY); pPntCollection.AddPoint(pPnt3, ref obj, ref obj); IPoint pPnt4 = new PointClass(); pPnt4.PutCoords(dblMinX, dblMaxY); pPntCollection.AddPoint(pPnt4, ref obj, ref obj); // IPolygon2 pPolygon2 = pPntCollection as IPolygon2; pPolygon2.Close(); if (pPolygon2.IsEmpty) { return(null); } if (m_pMap != null && m_pMap.SpatialReference != null) { pPolygon2.Project(m_pMap.SpatialReference); } return(pPolygon2 as IGeometry); }
//元素方式 //private void addGeometryEle(IPolygon pPolygon) //{ // ISimpleFillSymbol pFillSymbol = new SimpleFillSymbolClass(); // ISimpleLineSymbol pLineSymbol = new SimpleLineSymbolClass(); // IPolygonElement pPolygonEle = new PolygonElementClass(); // try // { // //颜色对象 // IRgbColor pRGBColor = new RgbColorClass(); // pRGBColor.UseWindowsDithering = false; // ISymbol pSymbol = (ISymbol)pFillSymbol; // pSymbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen; // pRGBColor.Red = 255; // pRGBColor.Green = 170; // pRGBColor.Blue = 0; // pLineSymbol.Color = pRGBColor; // pLineSymbol.Width = 0.8; // pLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid; // pFillSymbol.Outline = pLineSymbol; // pFillSymbol.Color = pRGBColor; // pFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross; // IElement pEle = pPolygonEle as IElement; // pEle.Geometry = pPolygon as IGeometry; // pEle.Geometry.SpatialReference = pPolygon.SpatialReference; // (pEle as IFillShapeElement).Symbol = pFillSymbol; // (pEle as IElementProperties).Name="ImpExtentOutMap"; // (m_Hook.ArcGisMapControl.Map as IGraphicsContainer).AddElement(pEle, 0); // (m_Hook.ArcGisMapControl.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); // IGraphicsContainer pGra = m_Hook.ArcGisMapControl.Map as IGraphicsContainer; // IActiveView pAv = pGra as IActiveView; // pGra.Reset(); // IElement pEle2 = pGra.Next(); // while (pEle2 != null) // { // if ((pEle2 as IElementProperties).Name == "ImpExtentOutMap") // { // //pGra.DeleteElement(pEle); // //pGra.Reset(); // } // pEle2 = pGra.Next(); // } // } // catch (Exception ex) // { // MessageBox.Show("绘制导入范围出错:" + ex.Message, "提示"); // pFillSymbol = null; // } //} #region /// <summary> /// 从文件路径获得一个PolyGon /// </summary> /// <param name="path">文件全路径</param> /// <returns></returns> private IPolygon GetPolyGonFromFile(string path) { IPolygon pGon = null; if (path.EndsWith(".mdb")) { string errmsg = ""; IWorkspaceFactory pwf = new AccessWorkspaceFactoryClass(); IWorkspace pworkspace = pwf.OpenFromFile(path, 0); IEnumDataset pEnumdataset = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureClass); pEnumdataset.Reset(); IDataset pDataset = pEnumdataset.Next(); while (pDataset != null) { IFeatureClass pFeatureclass = pDataset as IFeatureClass; if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline) { pDataset = pEnumdataset.Next(); continue; } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { pGon = pFeature.Shape as IPolygon; break; } else { pDataset = pEnumdataset.Next(); continue; } } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { IPolyline pPolyline = pFeature.Shape as IPolyline; pGon = GetPolygonFormLine(pPolyline); if (pGon.IsClosed == false) { errmsg = "选择的要素不能构成封闭多边形!"; pGon = null; pDataset = pEnumdataset.Next(); continue; } else { break; } } } } if (pGon == null) { IEnumDataset pEnumdataset1 = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset); pEnumdataset1.Reset(); pDataset = pEnumdataset1.Next(); while (pDataset != null) { IFeatureDataset pFeatureDataset = pDataset as IFeatureDataset; IEnumDataset pEnumDataset2 = pFeatureDataset.Subsets; pEnumDataset2.Reset(); IDataset pDataset1 = pEnumDataset2.Next(); while (pDataset1 != null) { if (pDataset1 is IFeatureClass) { IFeatureClass pFeatureclass = pDataset1 as IFeatureClass; if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline) { pDataset1 = pEnumDataset2.Next(); continue; } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { pGon = pFeature.Shape as IPolygon; break; } else { pDataset1 = pEnumDataset2.Next(); continue; } } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { IPolyline pPolyline = pFeature.Shape as IPolyline; pGon = GetPolygonFormLine(pPolyline); if (pGon.IsClosed == false) { errmsg = "选择的要素不能构成封闭多边形!"; pGon = null; pDataset1 = pEnumDataset2.Next(); continue; } else { break; } } } } } if (pGon != null) { break; } pDataset = pEnumdataset1.Next(); } } if (pGon == null) { if (errmsg != "") { MessageBox.Show(errmsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("请选择一个包含面要素和线要素的文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } return(pGon); } } else if (path.EndsWith(".shp")) { IWorkspaceFactory pwf = new ShapefileWorkspaceFactoryClass(); string filepath = System.IO.Path.GetDirectoryName(path); string filename = path.Substring(path.LastIndexOf("\\") + 1); IFeatureWorkspace pFeatureworkspace = (IFeatureWorkspace)pwf.OpenFromFile(filepath, 0); IFeatureClass pFeatureclass = pFeatureworkspace.OpenFeatureClass(filename); if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { pGon = pFeature.Shape as IPolygon; } } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { IPolyline pPolyline = pFeature.Shape as IPolyline; pGon = GetPolygonFormLine(pPolyline); if (pGon.IsClosed == false) { MessageBox.Show("选择的线要素不能构成封闭多边形!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(null); } } } else { MessageBox.Show("请选择一个面或者线要素文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return(null); } } else if (path.EndsWith(".txt")) { string txtpath = path; System.IO.StreamReader smRead = new System.IO.StreamReader(txtpath, System.Text.Encoding.Default); //设置路径 string line; pGon = new PolygonClass(); IPointCollection pc = pGon as IPointCollection; double x, y; while ((line = smRead.ReadLine()) != null) { if (line.IndexOf(",") > 0) { try { x = double.Parse(line.Substring(0, line.IndexOf(","))); y = double.Parse(line.Substring(line.IndexOf(",") + 1)); } catch { MessageBox.Show("文本文件格式不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); smRead.Close(); return(null); } IPoint tmpPoint = new ESRI.ArcGIS.Geometry.Point(); tmpPoint.X = x; tmpPoint.Y = y; object ep = System.Reflection.Missing.Value; pc.AddPoint(tmpPoint, ref ep, ref ep); } } smRead.Close(); ICurve pCurve = pGon as ICurve; if (pCurve.IsClosed == false) { if (pCurve.IsClosed == false) { //自动闭合 IPolygon2 pPolygon2 = pGon as IPolygon2; pPolygon2.Close(); } } } return(pGon); }