Esempio n. 1
0
 /// <summary>
 /// 将IFeatureCursor保存到指定工作空间的指定表
 /// </summary>
 /// <param name="cursor"></param>
 /// <param name="workspace"></param>
 /// <param name="layername"></param>
 /// <param name="clearReords">是否清空原图层的数据</param>
 /// <param name="type"></param>
 /// <param name="ShapeFieldName"></param>
 /// <param name="ConfigKeyword"></param>
 /// <returns></returns>
 public static IFeatureClass SaveToWorkspace(this IFeatureCursor cursor, IFeatureWorkspace workspace, string layername
                                             , bool clearReords      = false
                                             , esriFeatureType type  = esriFeatureType.esriFTSimple
                                             , string ShapeFieldName = "SHAPE"
                                             , string ConfigKeyword  = "")
 {
     try
     {
         IFeatureClass featureClass = workspace.TryOpenFeatureClass(layername);
         if (featureClass == null)
         {
             IFields fields = cursor.Fields.Clone();
             featureClass = workspace.CreateFeatureClass(layername, fields, type, ShapeFieldName, ConfigKeyword);
         }
         else
         {
             if (clearReords)
             {
                 featureClass.Delete();
             }
         }
         cursor.SaveToFeatureClass(featureClass);
         return(featureClass);
     }
     catch
     {
         return(null);
     }
 }
Esempio n. 2
0
        public override bool Exexute()
        {
            IWorkspace workspace = GisDataHelper.OpenWorkspace(dataSourceId);

            if (workspace.HasFeatureClass(layerName) && deleteOld)
            {
                workspace.DeleteFeatureClass(layerName);
            }
            IFeatureClass targetFeatureClass = workspace.TryOpenFeatureClass(layerName);

            if (targetFeatureClass == null)
            {
                if (shpPaths.Length > 0)
                {
                    IFeatureClass shpFeatureClass = XWorkspace.OpenShapeFile(shpPaths[0]);
                    targetFeatureClass = workspace.CreateFeatureClass(layerName, shpFeatureClass.Fields);
                }
            }
            else
            {
                if (clearRecord)
                {
                    targetFeatureClass.Delete();
                }
            }
            for (int i = 0; i < shpPaths.Length; i++)
            {
                IFeatureClass shpFeatureClass = XWorkspace.OpenShapeFile(shpPaths[i]);
                shpFeatureClass.CopyTo(targetFeatureClass);
            }
            Output = targetFeatureClass;
            return(Output != null);
        }
Esempio n. 3
0
        /// <summary>
        /// 从另一张表复制数据(数据结构相同)
        /// </summary>
        /// <param name="ZFClass"></param>
        /// <param name="clear">是否清空原数据</param>
        public static void CopyFrom(this IFeatureClass featureClass, IFeatureClass otherFeatureClass, bool clear = false)
        {
            if (clear)
            {
                featureClass.Delete();
            }
            //MergeStructFrom
            IFeatureCursor cursor = otherFeatureClass.Search();

            featureClass.CopyFrom(cursor);
        }
Esempio n. 4
0
        public override void Run(object sender, System.EventArgs e)
        {
            DF3DApplication app = DF3DApplication.Application;

            if (app == null || app.Current3DMapControl == null)
            {
                return;
            }
            try
            {
                Map3DCommandManager.Push(this);
                RenderControlEditServices.Instance().StopGeometryEdit(true);
                app.Current3DMapControl.PauseRendering(false);
                System.Collections.Generic.IList <System.Collections.Generic.KeyValuePair <int, string> > list = new System.Collections.Generic.List <System.Collections.Generic.KeyValuePair <int, string> >();
                HashMap          featureClassInfoMap              = SelectCollection.Instance().FeatureClassInfoMap;
                DF3DFeatureClass featureClassInfo                 = null;
                System.Collections.Generic.IList <int> list2      = new System.Collections.Generic.List <int>();
                System.Collections.IEnumerator         enumerator = featureClassInfoMap.Keys.GetEnumerator();
                try
                {
                    if (enumerator.MoveNext())
                    {
                        DF3DFeatureClass featureClassInfo2 = (DF3DFeatureClass)enumerator.Current;
                        featureClassInfo = featureClassInfo2;
                        ResultSetInfo resultSetInfo = featureClassInfoMap[featureClassInfo2] as ResultSetInfo;
                        foreach (DataRow dataRow in resultSetInfo.ResultSetTable.Rows)
                        {
                            int    num   = int.Parse(dataRow[featureClassInfo.GetFeatureClass().FidFieldName].ToString());
                            string value = num.ToString();
                            System.Collections.Generic.KeyValuePair <int, string> item = new System.Collections.Generic.KeyValuePair <int, string>(num, value);
                            list.Add(item);
                            list2.Add(num);
                        }
                    }
                }
                finally
                {
                    System.IDisposable disposable = enumerator as System.IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
                if (featureClassInfo != null)
                {
                    using (MergeDlg mergeDlg = new MergeDlg(list))
                    {
                        if (mergeDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            if (System.Windows.Forms.DialogResult.No != XtraMessageBox.Show("模型合并不支持撤销操作,是否继续?", "提示", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Exclamation))
                            {
                                int fid = mergeDlg.Fid;
                                using (new WaitDialogForm("", "正在进行模型合并,请稍后..."))
                                {
                                    IFeatureClass    featureClass      = featureClassInfo.GetFeatureClass();
                                    string           geometryFieldName = featureClassInfo.GetFeatureLayer().GeometryFieldName;
                                    IModelPoint      model             = this.GetModel(featureClass, geometryFieldName, fid);
                                    IResourceManager resourceManager   = CommonUtils.Instance().GetCurrentFeatureDataset() as IResourceManager;
                                    if (resourceManager != null)
                                    {
                                        if (!this.MergeModels(featureClass, geometryFieldName, list2.ToArray <int>(), resourceManager, ref model))
                                        {
                                            XtraMessageBox.Show("模型合并失败!");
                                        }
                                        else
                                        {
                                            if (list2.Remove(fid))
                                            {
                                                featureClass.Delete(new QueryFilterClass
                                                {
                                                    IdsFilter = list2.ToArray <int>()
                                                });
                                                CommonUtils.Instance().Delete(featureClassInfo, list2.ToArray <int>());
                                                app.Current3DMapControl.FeatureManager.DeleteFeatures(featureClass, list2.ToArray <int>());
                                            }
                                            app.Current3DMapControl.RefreshModel(CommonUtils.Instance().GetCurrentFeatureDataset(), model.ModelName);
                                            IFdeCursor fdeCursor = featureClass.Update(new QueryFilterClass
                                            {
                                                IdsFilter = new int[]
                                                {
                                                    fid
                                                }
                                            });
                                            IRowBuffer rowBuffer = fdeCursor.NextRow();
                                            if (rowBuffer != null)
                                            {
                                                int num2 = rowBuffer.FieldIndex(geometryFieldName);
                                                if (num2 != -1)
                                                {
                                                    rowBuffer.SetValue(num2, model);
                                                }
                                                fdeCursor.UpdateRow(rowBuffer);
                                                System.Runtime.InteropServices.Marshal.ReleaseComObject(fdeCursor);
                                                app.Current3DMapControl.FeatureManager.EditFeature(featureClass, rowBuffer);
                                            }
                                            //System.Runtime.InteropServices.Marshal.ReleaseComObject(featureClass);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
            catch (System.UnauthorizedAccessException var_23_389)
            {
                XtraMessageBox.Show("拒绝访问");
            }
            catch (System.Exception ex2)
            {
                XtraMessageBox.Show(ex2.Message);
            }
            finally
            {
                app.Current3DMapControl.ResumeRendering();
            }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            //创建要素类要先定义属性字段
            IFieldInfoCollection fields = new FieldInfoCollection();
            {
                //名称列
                IFieldInfo field = new FieldInfo();
                field.Name      = "Name";
                field.FieldType = gviFieldType.gviFieldString;
                field.Length    = 255;
                fields.Add(field);

                //空间列
                field                  = new FieldInfo();
                field.Name             = "Geometry";
                field.FieldType        = gviFieldType.gviFieldGeometry;                              //列类型为空间类型
                field.GeometryDef      = new GeometryDef();                                          //需要实例化一个几何定义对象,里面有大量与空间有关的内容,比如:
                field.GeometryDef.HasZ = true;                                                       //是否在Z值,默认为false,只能存XY
                field.GeometryDef.GeometryColumnType = gviGeometryColumnType.gviGeometryColumnPoint; //空间列的几何类型
                field.RegisteredRenderIndex          = true;                                         //注册渲染索引,允许在renderControl中显示
                fields.Add(field);

                //多空间列
                field                  = new FieldInfo();
                field.Name             = "Model";
                field.FieldType        = gviFieldType.gviFieldGeometry;                                   //列类型为空间类型
                field.GeometryDef      = new GeometryDef();                                               //需要实例化一个几何定义对象,里面有大量与空间有关的内容,比如:
                field.GeometryDef.HasZ = true;                                                            //是否在Z值,默认为false,只能存XY
                field.GeometryDef.GeometryColumnType = gviGeometryColumnType.gviGeometryColumnModelPoint; //空间列的几何类型
                field.RegisteredRenderIndex          = true;                                              //注册渲染索引,允许在renderControl中显示
                fields.Add(field);
                //... 其它列
            }

            //创建要素类
            IFeatureClass FC = FDS.CreateFeatureClass("FC", fields);

            int nameID   = FC.GetFields().IndexOf("Name");      //获取对应列的索引,
            int geomID   = FC.GetFields().IndexOf("Geometry");
            int fidIndex = FC.GetFields().IndexOf(FC.FidFieldName);

            //增加单条数据,并同步到FeatureLayer
            IFdeCursor fcu = FC.Insert();                                                               //通过FC创建一个插入游标,用来操作rowBuffer
            IRowBuffer rb  = FC.CreateRowBuffer();                                                      //通过FC创建一条空要素实例,用来设置数据,设置完成后将其塞入FC
            IPoint     p   = new GeometryFactory().CreatePoint(gviVertexAttribute.gviVertexAttributeZ); // 将要塞入空间列的几何对象,类型要对应 FC的field.GeometryDef.GeometryColumnType ,否则塞不进

            rb.SetValue(nameID, "testPointA");                                                          //塞名称
            rb.SetValue(geomID, p);                                                                     //塞几何
            fcu.InsertRow(rb);                                                                          //塞进FC
            int fid = fcu.LastInsertId;                                                                 //成功后反回这条buffer的主键ID

            rb.SetValue(fidIndex, fid);                                                                 //Cursor(FC)增加数据后会产生一个对应的FID,要把这个FID赋值给row才能在FLayer中显示
            ax.FeatureManager.CreateFeature(FC, rb);                                                    //同步到renderControl


            //增加很大量数据时,参考以下方法
            IFdeCursor           cu   = FC.Insert();
            IRowBufferCollection rows = new RowBufferCollection();

            for (int i = 0; i < 100000; i++)
            {
                IRowBuffer row = FC.CreateRowBuffer();
                // do something ...
                row.SetValue(geomID, p);
                cu.InsertRow(row);
                row.SetValue(fidIndex, cu.LastInsertId);
                rows.Add(row);
            }
            ax.FeatureManager.CreateFeatures(FC, rows);
            //或直接整体更新
            ax.FeatureManager.RefreshFeatureClass(FC);



            //查
            string       tagStr = "tagName";
            IQueryFilter qf     = new QueryFilter();        //过滤器

            qf.WhereClause = $"Name = '{tagStr}'";
            IFdeCursor res     = FC.Search(qf, true);       //条件查
            IRowBuffer aBuffer = res.NextRow();             //遍历查询结果

            //改
            IFdeCursor upCu     = FC.Update(qf);
            IRowBuffer upBuffer = upCu.NextRow();

            upCu.UpdateRow(upBuffer);

            //删
            FC.Delete(qf);
            FC.DeleteRow(0);
            FC.Truncate();          //保留表结构,清空表
        }
Esempio n. 6
0
        /// <summary>
        /// 在要素类里删除要素
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonDeleteFeature_Click(object sender, EventArgs e)
        {
            if (dsFactory == null)
            {
                dsFactory = new DataSourceFactory();
            }

            IDataSource     ds      = null;
            IFeatureDataSet dataset = null;
            IFeatureClass   fc      = null;

            try
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    if (MessageBox.Show("该操作无法恢复,请确定是否删除所选择的要素?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                    {
                        ds      = dsFactory.OpenDataSource(Info.ci);
                        dataset = ds.OpenFeatureDataset(Info.datasetName);
                        fc      = dataset.OpenFeatureClass(Info.featureclassName);

                        int oid = int.Parse(dataGridView1.SelectedRows[0].Cells["oid"].Value.ToString());

                        // 从数据库中删除
                        IQueryFilter filter = new QueryFilter();
                        filter.WhereClause = "oid = " + oid;
                        int nRet = fc.Delete(filter);
                        MessageBox.Show("成功删除" + nRet + "条记录");

                        // 从界面DataTable中删除
                        int index = dataGridView1.SelectedRows[0].Index;
                        AttriTable.Rows.RemoveAt(index);
                        this.dataGridView1.Refresh();
                        if (index < dataGridView1.RowCount)
                        {
                            this.dataGridView1.Rows[index].Selected            = true;
                            this.dataGridView1.FirstDisplayedScrollingRowIndex = index;
                        }
                        else
                        {
                            this.dataGridView1.Rows[dataGridView1.RowCount - 1].Selected = true;
                            this.dataGridView1.FirstDisplayedScrollingRowIndex           = dataGridView1.RowCount - 1;
                        }


                        // 更新表头文字
                        if (FilterWhereClause.Equals(""))
                        {
                            this.Text = "Attributes of " + Info.featureclassName + "  [Total records: " + fc.GetCount(null).ToString() + "]";
                        }
                        else
                        {
                            this.Text = "Attributes of " + Info.featureclassName + "  [Total records: " + AttriTable.Rows.Count.ToString() + "]" + "  Filter: " + FilterWhereClause;
                        }
                    }
                }
            }
            catch (COMException ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
            finally
            {
                if (ds != null)
                {
                    //Marshal.ReleaseComObject(ds);
                    ds = null;
                }
                if (dataset != null)
                {
                    //Marshal.ReleaseComObject(dataset);
                    dataset = null;
                }
                if (fc != null)
                {
                    //Marshal.ReleaseComObject(fc);
                    fc = null;
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Delete the Rows in the database selected by the specified query.
        /// </summary>
        /// <param name="where"></param>
        /// <param name="zGeo"></param>
        /// <param name="rel"></param>
        public static bool Delete(this IFeatureClass featureClass, string where = "1=1", IGeometry geo = null, esriSpatialRelEnum rel = esriSpatialRelEnum.esriSpatialRelIntersects)
        {
            IQueryFilter filter = XFilter.CreateFilter(where, geo, featureClass.ShapeFieldName, rel);

            return(featureClass.Delete(filter));
        }