Example #1
0
        /// <summary>
        /// 为GeometryBag生成空间索引(提高查询效率)
        /// </summary>
        /// <param name="geometryBag"></param>
        public static void CreateSpatialIndex(this IGeometryBag geometryBag)
        {
            ISpatialIndex spatialIndex = (ISpatialIndex)geometryBag;

            spatialIndex.AllowIndexing = true;
            spatialIndex.Invalidate();
        }
Example #2
0
        /// <summary>
        /// union几个面要素
        /// </summary>
        /// <param name="lstGeometry">需要操作的面要素集合</param>
        /// <returns>返回union后的图形</returns>
        public static IGeometry GetUnion(List <IGeometry> lstGeometry)
        {
            IGeometryBag        pGeoBag = new GeometryBagClass();
            IGeometryCollection pGeoCol = pGeoBag as IGeometryCollection;

            if (lstGeometry.Count < 1)
            {
                return(null);
            }
            if (lstGeometry[0].SpatialReference != null)
            {
                pGeoBag.SpatialReference = lstGeometry[0].SpatialReference;
            }

            object obj = System.Type.Missing;

            for (int i = 0; i < lstGeometry.Count; i++)
            {
                IGeometry pTempGeo = lstGeometry[i];
                pGeoCol.AddGeometry(pTempGeo, ref obj, ref obj);
            }
            ISpatialIndex pSI = pGeoBag as ISpatialIndex;

            pSI.AllowIndexing = true;
            pSI.Invalidate();

            ITopologicalOperator pTopo = new PolygonClass();

            pTopo.ConstructUnion(pGeoBag as IEnumGeometry);
            IGeometry pGeo = pTopo as IGeometry;

            return(pGeo);
        }
Example #3
0
        private void iSpatialFilterGeometryBagToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stopwatch myWatch = Stopwatch.StartNew();

            //从MapControl中获取的点图层
            IFeatureLayer     pointFeatureLayer     = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureSelection pointFeatureSelection = pointFeatureLayer as IFeatureSelection;
            //从MapControl中获取的面图层
            IFeatureLayer polygonFeatureLayer = axMapControl1.get_Layer(1) as IFeatureLayer;

            //构建GeometryBag
            IGeometryBag        geometryBag        = new GeometryBagClass();
            IGeometryCollection geometryCollection = (IGeometryCollection)geometryBag;
            IGeoDataset         geoDataset         = (IGeoDataset)polygonFeatureLayer;
            ISpatialReference   spatialReference   = geoDataset.SpatialReference;

            //一定要给GeometryBag赋空间参考
            geometryBag.SpatialReference = spatialReference;

            IQueryFilter queryFilter = new QueryFilterClass();

            //Search如果返回属性值的话设置SubFields会提高效率
            queryFilter.SubFields = "Shape";

            //遍历面要素类,逐一获取Geometry并添加到GeometryBag中
            IFeatureCursor cursor = polygonFeatureLayer.Search(queryFilter, true);

            IFeature polygonFeature = null;

            while ((polygonFeature = cursor.NextFeature()) != null)
            {
                geometryCollection.AddGeometry(polygonFeature.ShapeCopy);
            }
            //为GeometryBag生成空间索引,以提高效率
            ISpatialIndex spatialIndex = (ISpatialIndex)geometryBag;

            spatialIndex.AllowIndexing = true;
            spatialIndex.Invalidate();
            //构建空间查询
            ISpatialFilter spatialFilter = new SpatialFilterClass();

            spatialFilter.Geometry      = geometryBag;
            spatialFilter.GeometryField = pointFeatureLayer.FeatureClass.ShapeFieldName;
            spatialFilter.SpatialRel    = esriSpatialRelEnum.esriSpatialRelContains;
            //选择的话可以不设置SubFields
            pointFeatureSelection.SelectFeatures(spatialFilter as IQueryFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);

            int count = pointFeatureSelection.SelectionSet.Count;

            axMapControl1.Refresh();
            //释放游标
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cursor);

            myWatch.Stop();
            string time = myWatch.Elapsed.TotalSeconds.ToString();

            MessageBox.Show("The selected point count is " + count.ToString() + "! and " + time + " Seconds");
        }
Example #4
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            //生成被查询的图层集以传入查询结果窗体
            lstQueryedLayer = new List <ILayer>();
            foreach (ListViewItem lvi in lstLayer.Items)
            {
                if (lvi.Checked == true)
                {
                    lstQueryedLayer.Add(lvi.Tag as ILayer);
                }
            }
            //生成选择的查询图层的搜索图形
            IFeatureLayer pFL = null;

            foreach (ILayer pLayer in lstSelectLayer)
            {
                if (pLayer.Name == cboxSearchLayer.Text)
                {
                    pFL = pLayer as IFeatureLayer;
                }
            }
            IGeometryBag gmBag = new GeometryBagClass();

            gmBag.SpatialReference = pMap.SpatialReference;//定义空间参考,否则加入的图形将失去参考
            IGeometryCollection gmCollection = gmBag as IGeometryCollection;

            if (rdSelect.Checked)//如果单选框 选择的要素是选中状态
            {
                ISelectionSet pSelSet = (pFL as IFeatureSelection).SelectionSet;
                ICursor       pCursor;
                pSelSet.Search(null, false, out pCursor);
                IFeature pFeature = (pCursor as IFeatureCursor).NextFeature();
                object   obj      = Type.Missing;
                while (pFeature != null)
                {
                    gmCollection.AddGeometry(pFeature.ShapeCopy, ref obj, ref obj);
                    pFeature = (pCursor as IFeatureCursor).NextFeature();
                }
            }
            else//如果单选框 全部要素是选择状态
            {
                IFeatureCursor pFeaCursor = pFL.FeatureClass.Search(null, false);
                IFeature       pFea       = pFeaCursor.NextFeature();
                object         obj        = Type.Missing;
                while (pFea != null)
                {
                    gmCollection.AddGeometry(pFea.ShapeCopy, ref obj, ref obj);
                    pFea = pFeaCursor.NextFeature();
                }
            }
            ISpatialIndex pSI = gmBag as ISpatialIndex;//重建索引

            pSI.AllowIndexing = true;
            pSI.Invalidate();
            GeometryBag = gmBag;
        }
Example #5
0
        /// <summary>
        /// 筛选要素类中,与指定图形满足一定空间关系的要素(空间筛选时添加空间索引)
        /// (参考:http://blog.csdn.net/lanpy88/article/details/7173063)
        /// </summary>
        /// <param name="featureClass">从中筛选要素的要素类(A)</param>
        /// <param name="geometry">过滤条件图形(B)</param>
        /// <param name="spatialRefEnum">空间关系类型(举例:esriSpatialRelContains表示A包含B)</param>
        /// <param name="whereClause">查询条件</param>
        /// <returns></returns>
        public static List <IFeature> FilterFeaturesEx(this IFeatureClass featureClass, IGeometry geometry, esriSpatialRelEnum spatialRefEnum, string whereClause = "")
        {
            IGeometryBag        geometryBag        = new GeometryBagClass();
            IGeometryCollection geometryCollection = (IGeometryCollection)geometryBag;

            geometryBag.SpatialReference = ((IGeoDataset)featureClass).SpatialReference; //一定要给GeometryBag赋空间参考
            geometryCollection.AddGeometry(geometry);

            //为GeometryBag生成空间索引,以提高效率
            ISpatialIndex spatialIndex = (ISpatialIndex)geometryBag;

            spatialIndex.AllowIndexing = true;
            spatialIndex.Invalidate();

            return(FilterFeatures(featureClass, geometry, spatialRefEnum, whereClause));
        }
Example #6
0
        public override bool Check(ref List <Error> checkResult)
        {
            IQueryFilter pQueryFilter = new QueryFilterClass();

            pQueryFilter.WhereClause = m_structPara.strWhereClause;
            //满足条件查询
            IFeatureCursor      ipFeatCursor        = pSrcFeatClass.Search(pQueryFilter, true);
            IFeature            ipFeature           = ipFeatCursor.NextFeature();
            IGeometryCollection pGeometryCollection = new GeometryBagClass();

            ///获取满足该条件的geometry
            while (ipFeature != null)
            {
                IGeometry ipGeometry = ipFeature.Shape;
                if (ipGeometry == null)
                {
                    ipFeature = ipFeatCursor.NextFeature();
                    continue;
                }
                object Missing = Type.Missing;
                pGeometryCollection.AddGeometry(ipGeometry, ref Missing, ref Missing);

                ipFeature = ipFeatCursor.NextFeature();
            }

            ISpatialIndex pSpatialIndex = (ISpatialIndex)pGeometryCollection;

            pSpatialIndex.AllowIndexing = true;
            pSpatialIndex.Invalidate();

            ///构建两个图层地物重叠的空间查询
            ISpatialFilter pSpatialFilter = new SpatialFilterClass();

            pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelOverlaps;
            ///将大的GeometryCollection放入spatialfilter
            pSpatialFilter.Geometry = (IGeometry)pGeometryCollection;
            string Fields = "OBJECTID,Shape";

            pSpatialFilter.SubFields = Fields;

            IFeatureCursor ipResultFtCur = pRelFeatClass.Search(pSpatialFilter, true);

            //保存数据
            List <Error> pRuleResult = new List <Error>();

            AddResult(ref pRuleResult, ipResultFtCur);

            checkResult = pRuleResult;

            if (ipResultFtCur != null)
            {
                Marshal.ReleaseComObject(ipResultFtCur);
                ipResultFtCur = null;
            }
            if (pSrcFeatClass != null)
            {
                Marshal.ReleaseComObject(pSrcFeatClass);
                pSrcFeatClass = null;
            }
            if (pRelFeatClass != null)
            {
                Marshal.ReleaseComObject(pRelFeatClass);
                pRelFeatClass = null;
            }
            return(true);
        }
Example #7
0
        public override bool Check(ref List <Hy.Check.Define.Error> checkResult)
        {
            //得到目标图层和关系图层的featureclass
            IFeatureClass  pSrcFeatClass = null;
            IFeatureClass  pRelFeatClass = null;
            IFeatureCursor ipFeatCursor  = null;

            checkResult = new List <Error>();
            try
            {
                string         shapeFieldName;
                IFeature       ipFeature;
                ISpatialFilter pSpatialFilter = new SpatialFilterClass(); //(CLSID_SpatialFilter);

                //先取得要进行空间关系查询的ILayer
                IFeatureWorkspace ipFtWS = null;

                ipFtWS = (IFeatureWorkspace)this.m_BaseWorkspace;

                pSrcFeatClass = ipFtWS.OpenFeatureClass(strSrcLayer);
                pRelFeatClass = ipFtWS.OpenFeatureClass(strRelLayer);

                // 1. 设置空间关系
                if (!m_pPara.bCustomRel) // 使用engine预定义的空间关系
                {
                    pSpatialFilter.SpatialRel = m_pPara.eSpatialRel;
                }
                else //自定义空间关系
                {
                    pSpatialFilter.SpatialRelDescription = m_pPara.strSpatialRel;
                }

                // 2.设置过滤几何
                shapeFieldName = pSrcFeatClass.ShapeFieldName;
                pSpatialFilter.GeometryField = shapeFieldName;

                // 3.设置选择先后关系,虽然对PGDB不适用,但是我也放在这儿试试
                // Sets the order in which spatial searches are applied by the RDBMS (ArcSDE).
                //pSpatialFilter.put_SearchOrder(esriSearchOrderSpatial);

                // 4.设置where语句
                if (m_pPara.strWhereClause.Length > 0)
                //hehy注释,2008年2月1日,将该判断条件改为pSpatialFilter.SpatialRel = m_pPara.eSpatialRel;
                {
                    pSpatialFilter.WhereClause = m_pPara.strWhereClause;
                }

                //pSpatialFilter.SpatialRel = m_pPara.eSpatialRel;
                // 5.目标层生成一个大的GeometryCollection
                IGeometryCollection pGeometryCollection = new GeometryBagClass(); //(CLSID_GeometryBag);
                IQueryFilter        pQueryFilter        = new QueryFilterClass(); //(CLSID_QueryFilter);
                string SubFields = "Shape";
                pQueryFilter.SubFields = SubFields;
                ipFeatCursor           = pRelFeatClass.Search(pQueryFilter, true);

                ipFeature = ipFeatCursor.NextFeature();

                while (ipFeature != null)
                {
                    IGeometry ipGeometry = ipFeature.Shape;
                    if (ipGeometry == null)
                    {
                        ipFeature = ipFeatCursor.NextFeature();
                        continue;
                    }

                    object Missing = Type.Missing;

                    if (!(m_pPara.bBuffer)) //不用缓冲区
                    {
                        pGeometryCollection.AddGeometry(ipGeometry, ref Missing, ref Missing);
                    }
                    else //使用缓冲
                    {
                        ITopologicalOperator ipTopo = (ITopologicalOperator)ipGeometry;
                        ipTopo.Simplify();
                        IGeometry ipGeobuffer = ipTopo.Buffer(m_pPara.dBuffer);
                        pGeometryCollection.AddGeometry(ipGeobuffer, ref Missing, ref Missing);
                    }

                    ipFeature = ipFeatCursor.NextFeature();
                }

                ISpatialIndex pSpatialIndex = (ISpatialIndex)pGeometryCollection;
                pSpatialIndex.AllowIndexing = true;
                pSpatialIndex.Invalidate();

                // 6.将大的GeometryCollection放入spatialfilter
                pSpatialFilter.Geometry = (IGeometry)pGeometryCollection;

                // 7.目标图层中采用生成的spatialfilter进行查询
                IFeatureCursor ipResultFtCur;
                string         Fields = "OBJECTID,Shape";
                pSpatialFilter.SubFields = Fields;

                //IQueryFilter queryFilter = new QueryFilterClass();
                //queryFilter = (IQueryFilter) pSpatialFilter;

                ipResultFtCur = pSrcFeatClass.Search(pSpatialFilter, true);

                // 8.保存数据
                AddResult(ref checkResult, ipResultFtCur);
            }

            catch (Exception ex)
            {
                return(false);
            }
            finally
            {
                if (ipFeatCursor != null)
                {
                    Marshal.ReleaseComObject(ipFeatCursor);
                    ipFeatCursor = null;
                }
                if (pSrcFeatClass != null)
                {
                    Marshal.ReleaseComObject(pSrcFeatClass);
                    pSrcFeatClass = null;
                }
                if (pRelFeatClass != null)
                {
                    Marshal.ReleaseComObject(pRelFeatClass);
                    pRelFeatClass = null;
                }
            }
            return(true);
        }