예제 #1
0
        //函数号:
        //函数名:LoadRasterToCatalogDatabase
        //函数功能:Raster导入Database
        //参数:pWorkspace:数据库,pWorkspace可以是Access 或者SDE
        //		strCata:Catalog名称
        //		pRasterDs:需要入库的Rasterdataset
        //备注:insert a raster dataset with given path and file name to an existing raster catalog
        public void LoadRasterToCatalogDatabase(IWorkspace pWorkspace, string strCata, IRasterDataset pRasterDs, string name)
        {
            //QI IRasterWorkspaceEx to IWorkspace
            IRasterWorkspaceEx pRasWKS = pWorkspace as IRasterWorkspaceEx;

            //Open raster catalog
            //IRasterCatalog相当于一个FeatureClass(表),表中的每一个记录可以存放Raster
            IRasterCatalog pInCatalog = pRasWKS.OpenRasterCatalog(strCata);
            // get raster field index
            int iRaster = pInCatalog.RasterFieldIndex;

            // QI IFeatureClass for inserting
            IFeatureClass pFeatureClass = pInCatalog as IFeatureClass;

            // get insert cursor
            IFeatureCursor pCursor = pFeatureClass.Insert(false);

            // create raster value from raster dataset with default storagedef
            IRasterValue pRasterValue = new RasterValueClass();

            pRasterValue.RasterDataset    = pRasterDs;
            pRasterValue.RasterStorageDef = new RasterStorageDefClass();

            pRasterValue.RasterDatasetName.NameString = name;
            //这里可以设置新的RasterStorageDef

            // insert the rastervalue  ' it will update name, metadata and geometry field
            IFeatureBuffer pRow = pFeatureClass.CreateFeatureBuffer();

            pRow.set_Value(iRaster, pRasterValue);
            pCursor.InsertFeature(pRow);
        }
예제 #2
0
        public override void OnClick()
        {
            IFeatureClass fc = (MoData.v_CurLayer as IFeatureLayer).FeatureClass;

            foreach (KeyValuePair <IGeometry, IElement> item in GeometriesToBePasted)
            {
                if (fc.ShapeType != item.Key.GeometryType)
                {
                    SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "几何类型不匹配!");
                    return;
                }
            }

            MoData.v_CurWorkspaceEdit.StartEditOperation();
            IFeatureCursor cursor = fc.Insert(true);

            IFeatureBuffer buffer  = fc.CreateFeatureBuffer();
            IFeature       feature = buffer as IFeature;

            foreach (KeyValuePair <IGeometry, IElement> item in GeometriesToBePasted)
            {
                Exception Err = null;
                try
                {
                    feature.Shape = item.Key;
                }
                catch (Exception exErr)
                {
                    //******************************************
                    //guozheng added System Exception log
                    if (SysCommon.Log.Module.SysLog == null)
                    {
                        SysCommon.Log.Module.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    }
                    SysCommon.Log.Module.SysLog.Write(exErr);
                    //******************************************

                    Err = exErr;
                    SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", exErr.Message);
                }

                if (Err == null)
                {
                    if (feature.FeatureType == esriFeatureType.esriFTAnnotation)
                    {
                        IAnnotationFeature af = feature as IAnnotationFeature;
                        af.Annotation = item.Value;

                        feature = af as IFeature;
                    }

                    buffer = feature as IFeatureBuffer;
                    cursor.InsertFeature(buffer);
                }
            }

            cursor.Flush();
            MoData.v_CurWorkspaceEdit.StopEditOperation();
            myHook.MapControl.ActiveView.Refresh();
        }
예제 #3
0
        /// <summary>
        /// 将拓扑错误要素存入要素类中
        /// </summary>
        /// <param name="topoErrorFeatures">拓扑错误要素</param>
        /// <param name="resultFeatureClass">保存拓扑错误要素的要素类,注意其坐标系和类型(点/线/面)必须与拓扑错误要素相同</param>
        public static void TopoErrorInsertToFeatureClass(this IEnumerable <ITopologyErrorFeature> topoErrorFeatures, IFeatureClass resultFeatureClass)
        {
            int typeIndex      = resultFeatureClass.AddField("TRuleType", "拓扑规则", esriFieldType.esriFieldTypeInteger);
            int orClassIdIndex = resultFeatureClass.AddField("OriClsID", "源要素类ID", esriFieldType.esriFieldTypeInteger);
            int deClassIdIndex = resultFeatureClass.AddField("DesClsID", "目标要素类ID", esriFieldType.esriFieldTypeInteger);
            int orOidIndex     = resultFeatureClass.AddField("OriOID", "源要素OID", esriFieldType.esriFieldTypeInteger);
            int deOidIndex     = resultFeatureClass.AddField("DesOID", "目标要素OID", esriFieldType.esriFieldTypeInteger);

            IWorkspaceEdit tmpWorkspaceEdit = (IWorkspaceEdit)(resultFeatureClass as IDataset).Workspace;

            tmpWorkspaceEdit.StartEditing(true);
            tmpWorkspaceEdit.StartEditOperation();
            IFeatureBuffer featureBuffer = resultFeatureClass.CreateFeatureBuffer();

            //在目标要素类中插入所有错误要素
            IFeatureCursor featureCursor = resultFeatureClass.Insert(true);

            foreach (var errorFeature in topoErrorFeatures)
            {
                IFeature tmpFeature = errorFeature as IFeature;
                featureBuffer.set_Value(typeIndex, errorFeature.TopologyRuleType);
                featureBuffer.set_Value(orClassIdIndex, errorFeature.OriginClassID);
                featureBuffer.set_Value(deClassIdIndex, errorFeature.DestinationClassID);
                featureBuffer.set_Value(orOidIndex, errorFeature.OriginOID);
                featureBuffer.set_Value(deOidIndex, errorFeature.DestinationOID);

                featureBuffer.Shape = tmpFeature.Shape;
                object featureOID = featureCursor.InsertFeature(featureBuffer);
            }
            featureCursor.Flush();//保存要素
            tmpWorkspaceEdit.StopEditOperation();
            tmpWorkspaceEdit.StopEditing(true);
            Marshal.ReleaseComObject(featureCursor);
        }
예제 #4
0
        /// <summary>
        /// 使用游标插入要素
        /// </summary>
        /// <param name="selectionLayer">旧图层</param>
        /// <param name="newFeatureClass">新要素集合</param>
        /// <param name="fidValue">FID值</param>
        /// <param name="selectFieldName">选中字段名称</param>
        private static void InsertFeaturesUsingCursor(IFeatureLayer selectionLayer, IFeatureClass newFeatureClass,
                                                      Dictionary <string, int> fidValue, List <string> selectFieldName)
        {
            IFeatureCursor featureCursor = selectionLayer.FeatureClass.Search(null, false);
            IFeature       oldFeature    = featureCursor.NextFeature();
            IFeatureCursor insertCursor  = newFeatureClass.Insert(true);

            while (oldFeature != null)
            {
                //判断当前要素FID是否在FID集合中,在的话,则赋值给新图层
                if (fidValue.ContainsKey(oldFeature.Value[0].ToString()))
                {
                    // Create a feature buffer.
                    IFeatureBuffer featureBuffer = newFeatureClass.CreateFeatureBuffer();
                    for (int i = 0; i < featureBuffer.Fields.FieldCount; i++)
                    {
                        IField field = featureBuffer.Fields.Field[i];
                        //字段是选中且可编辑的
                        if (selectFieldName.Contains(field.Name) && field.Editable)
                        {
                            featureBuffer.set_Value(i, oldFeature.Value[i]); //字段赋值
                        }
                    }
                    insertCursor.InsertFeature(featureBuffer);
                    // Flush the buffer to the geodatabase.
                    insertCursor.Flush();
                }
                oldFeature = featureCursor.NextFeature(); //游标移动
            }
        }
예제 #5
0
        public static void InsertLoadOnly(IFeatureClass featureCls, IList <IGeometry> geoList)
        {
            IFeatureClassLoad featureClsLoad = (IFeatureClassLoad)featureCls;
            ISchemaLock       schemaLock     = (ISchemaLock)featureCls;

            try
            {
                schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
                featureClsLoad.LoadOnlyMode = true;
                using (var comReleaser = new ComReleaser())
                {
                    IFeatureBuffer featureBuffer = featureCls.CreateFeatureBuffer();
                    IFeatureCursor featureCursor = featureCls.Insert(true);
                    comReleaser.ManageLifetime(featureCursor);
                    featureBuffer.set_Value(featureBuffer.Fields.FindField("InstBy"), "B Pierce");
                    for (int ic = 0; ic < geoList.Count; ic++)
                    {
                        featureBuffer.Shape = geoList[0];
                        var featureOID = featureCursor.InsertFeature(featureBuffer);
                        if (ic % 10 == 0)
                        {
                            featureCursor.Flush();
                        }
                    }
                    featureCursor.Flush();
                }
            }
            finally
            {
                featureClsLoad.LoadOnlyMode = false;
                schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
            }
        }
예제 #6
0
        public void AddFeature(IFeatureLayer fealyr, IGeometry geometry)
        {
            IFeatureClass featurecls = fealyr.FeatureClass;

            if (featurecls.ShapeType != geometry.GeometryType)
            {
                return;                                           //这个if语句也是迷
            }
            IDataset       dataset       = featurecls as IDataset;
            IWorkspace     workspace     = dataset.Workspace;
            IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit;

            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();

            IFeatureBuffer featurebuffer = featurecls.CreateFeatureBuffer();

            featurebuffer.Shape = geometry;                ///实锤

            ////以下代码为测试设置其他字段值
            //int index = featureBuffer.Fields.FindField("Name");
            //featureBuffer.set_Value(index, "测试点");
            IFeatureCursor featurecusor = featurecls.Insert(true);

            featurecusor.InsertFeature(featurebuffer);
            featurecusor.Flush();

            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);            //C#庐山真面目,让你感觉到可以这样写
        }
예제 #7
0
        private void method_0(IFeatureCursor ifeatureCursor_0, IFeatureBuffer ifeatureBuffer_0, IFields ifields_0,
                              IFeature ifeature_0)
        {
            IFields fields = ifeatureBuffer_0.Fields;
            IFields field  = ifeature_0.Fields;

            for (int i = 0; i < field.FieldCount; i++)
            {
                IField field1 = field.Field[i];
                if ((field1.Type == esriFieldType.esriFieldTypeGeometry || field1.Type == esriFieldType.esriFieldTypeOID
                    ? false
                    : field1.Editable))
                {
                    int value = fields.FindField(ifields_0.Field[i].Name);
                    try
                    {
                        ifeatureBuffer_0.Value[value] = ifeature_0.Value[i];
                    }
                    catch (Exception exception)
                    {
                        Logger.Current.Error("", exception, "");
                    }
                }
            }
            ifeatureBuffer_0.Shape = ifeature_0.ShapeCopy;
            ifeatureCursor_0.InsertFeature(ifeatureBuffer_0);
        }
예제 #8
0
        /// <summary>
        /// 刷新Mobike数据
        /// </summary>
        public void RefreshmobikeInfo()
        {
            //获取车辆位置信息
            var mobikecars = new List <mobikeInfo>();

            NetUtils.GetmobikeCars(out mobikecars);
            if (mobikecars == null)
            {
                view.ShowError("数据出现问题,可能是mobike用户认证出错 返回示例数据");
                mobikecars = SampleData.mobikeSampleList;
            }

            IFeatureClass  featureclass  = CreateFeatureClass("mobike");
            IFeatureCursor featureCursor = featureclass.Insert(true);

            //遍历照片链表 以创建缓存的形式插入数据
            foreach (var c in mobikecars)
            {
                IPoint pPoint = new PointClass();
                //坐标转换
                var t = CoordinateUtils.gcj02_To_Wgs84(c.distY, c.distX);
                pPoint.PutCoords(t.longtitude, t.latitude);
                pPoint.SpatialReference = ApplicationV.GlobalMapControl.SpatialReference;
                pPoint.Project(ApplicationV.GlobalMapControl.SpatialReference);
                IFeatureBuffer featureBuffer = featureclass.CreateFeatureBuffer();
                featureBuffer.Shape = pPoint;
                featureBuffer.set_Value(featureBuffer.Fields.FindField("Latitude"), t.latitude);
                featureBuffer.set_Value(featureBuffer.Fields.FindField("Longtitude"), t.longtitude);
                featureCursor.InsertFeature(featureBuffer);
            }
            featureCursor.Flush();

            //创建图层
            IFeatureLayer pFeaturelayer = new FeatureLayerClass();

            pFeaturelayer.FeatureClass = featureclass;
            pFeaturelayer.Name         = "mobike分布";

            //修饰该图层
            IPictureMarkerSymbol mobikeMarkerSymbol = new PictureMarkerSymbolClass();

            mobikeMarkerSymbol.Size = 18;
            mobikeMarkerSymbol.CreateMarkerSymbolFromFile(esriIPictureType.esriIPicturePNG, ApplicationV.Data_ImgPath + "//mobike.png");

//            ISimpleMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbol();
//            pMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
//            var pRgbColor = ColorUtils.GetRgbColor(226, 61, 14);
//            pMarkerSymbol.Color = pRgbColor;

            ISimpleRenderer pSimpleRenderer = new SimpleRendererClass();

            pSimpleRenderer.Symbol = (ISymbol)mobikeMarkerSymbol;
            (pFeaturelayer as IGeoFeatureLayer).Renderer = pSimpleRenderer as IFeatureRenderer;

            //正式归为图层
            ofoLayer = pFeaturelayer as ILayer;

            ApplicationV.GlobalMapControl.AddLayer(ofoLayer);
            ApplicationV.GlobalMapControl.Refresh();
        }
예제 #9
0
        /// <summary>
        /// 复制featureclass图形和属性的方法
        /// </summary>
        /// <param name="name">待创建要素类的名称</param>
        /// <param name="target_FeatureClass">需要写入图形和属性的空要素</param>
        /// <param name="CADFeatureClass">需要复制的CAD要素类</param>
        public void CreateFeatureClassByAtt(string name, IFeatureClass target_FeatureClass, IFeatureClass CADFeatureClass)
        {
            List <string>  AttList      = new List <string>();
            IQueryFilter   pQueryFilter = new QueryFilterClass();
            IFeatureCursor pFeaCursor;
            IFeature       pFeature;
            IFeatureBuffer pFeaBuffer = null;

            AttList = getUniqueValue(CADFeatureClass, "Layer");
            pQueryFilter.WhereClause = "Layer = '" + name.Substring(name.IndexOf('-') + 1) + "'";
            pFeaCursor = CADFeatureClass.Search(pQueryFilter, false);
            //用IFeatureBuffer提高运算速度
            IFeatureCursor targetCursor = target_FeatureClass.Insert(true);

            while ((pFeature = pFeaCursor.NextFeature()) != null)
            {
                pFeaBuffer = target_FeatureClass.CreateFeatureBuffer();
                IGeoDataset pGeoDataset = pFeature.Class as IGeoDataset;
                pFeaBuffer.Shape = pFeature.Shape;
                for (int j = 2; j < CADFeatureClass.Fields.FieldCount; j++)
                {
                    try
                    {
                        pFeaBuffer.set_Value(j, pFeature.Value[CADFeatureClass.FindField(target_FeatureClass.Fields.Field[j].Name)]);
                    }catch
                    {
                        continue;
                    }
                }
                targetCursor.InsertFeature(pFeaBuffer);
                targetCursor.Flush();
            }
        }
예제 #10
0
        /// <summary>
        /// 批次新增圖徵
        /// </summary>
        /// <param name="points"></param>
        /// <param name="featureclass"></param>
        public void InsertFeature(List <IPoint> points, ref IFeatureClass featureclass)
        {
            if (featureclass == null)
            {
                throw new ArgumentNullException(nameof(featureclass));
            }
            if (points == null || points.Count == 0)
            {
                throw new ArgumentNullException(nameof(points));
            }

            IFeatureCursor pFeatCur     = featureclass.Insert(true);
            int            mustFlush    = 1000; //每n筆儲存一次
            int            featureCount = 0;

            foreach (var item in points)
            {
                IFeatureBuffer pFeatBuffer = featureclass.CreateFeatureBuffer();
                pFeatBuffer.Shape = item;
                pFeatCur.InsertFeature(pFeatBuffer);
                ++featureCount;
                if (featureCount % mustFlush == 0)
                {
                    pFeatCur.Flush();
                }
            }

            pFeatCur.Flush();
        }
        public void ExecuteSpatialInsert(IFeatureClass pFeatureClass, IGeometry geometry, string[] columnNames, object[] insertValues)
        {
            try
            {
                //创建一个插入的游标
                IFeatureCursor featureCursorInsert = pFeatureClass.Insert(true);

                //创建要插入的缓冲对象
                IFeatureBuffer featureBufferInsert = pFeatureClass.CreateFeatureBuffer();

                // 给Shape字段赋值
                featureBufferInsert.Shape = geometry;

                // 根据字段名称和值,给属性表赋值
                for (int columnCounter = 0; columnCounter < columnNames.Length; columnCounter++)
                {
                    string columnName    = columnNames.GetValue(columnCounter).ToString();
                    int    fieldPosition = featureBufferInsert.Fields.FindField(columnName);
                    object insertValue   = insertValues.GetValue(columnCounter);
                    featureBufferInsert.set_Value(fieldPosition, insertValue);
                }

                //保存到数据库中,看是否要扩到try-catch中
                featureCursorInsert.InsertFeature(featureBufferInsert);
                featureCursorInsert.Flush();
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存图形数据时出错: " + ex.Message, "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
예제 #12
0
        private static void InsertFeaturesBoun(IFeatureClass newFeatureClass, IFeatureCursor featureCursorSearch, IGeometry clipGeo)
        {
            int intFeatureCount = 0;

            IFeatureCursor featureCursorInsert = newFeatureClass.Insert(true);
            IFeatureBuffer featureBufferInsert = newFeatureClass.CreateFeatureBuffer();

            IFeature feature = featureCursorSearch.NextFeature();

            while (feature != null)
            {
                ITopologicalOperator topoOpe   = feature.Shape as ITopologicalOperator;
                IGeometry            intersect = topoOpe.Intersect(clipGeo, feature.Shape.Dimension);
                featureBufferInsert.Shape = intersect;

                AddFields(featureBufferInsert, feature);

                featureCursorInsert.InsertFeature(featureBufferInsert);

                if (++intFeatureCount == 100)
                {
                    featureCursorInsert.Flush();
                    intFeatureCount = 0;
                }

                feature = featureCursorSearch.NextFeature();
            }

            featureCursorInsert.Flush();
        }
예제 #13
0
        //添加点实体
        private void addFeature(string layerName, IPoint point)
        {
            int    i     = 0;
            ILayer layer = null;

            for (i = 0; i < axMapControl1.LayerCount; i++)
            {
                layer = axMapControl1.Map.get_Layer(i);
                if (layer.Name.ToLower() == layerName)
                {
                    break;
                }
            }
            IFeatureLayer  featureLayer  = layer as IFeatureLayer;
            IFeatureClass  featureClass  = featureLayer.FeatureClass;
            IDataset       dataset       = (IDataset)featureClass;
            IWorkspace     workspace     = dataset.Workspace;
            IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;

            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();
            IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
            IFeatureCursor featureCursor = featureClass.Insert(true);

            featureBuffer.Shape = point;
            object featureOID = featureCursor.InsertFeature(featureBuffer);

            featureCursor.Flush();
            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
        }
예제 #14
0
파일: JZDCustom.cs 프로젝트: YB9527/CG
        /// <summary>
        /// jzd保存进入map
        /// </summary>
        /// <param name="jzd"></param>
        public static void SaveMap(IList <JZD> jzds)
        {
            IFeatureClass           featureClass = GetJZDLayer().FeatureClass;
            Dictionary <int, Clazz> dic          = ArcGisService.GetTitleClzz <JZD>(featureClass.Fields, false);

            dic.Remove(-1);
            IFeatureCursor featureCursor = featureClass.Insert(true);
            IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();

            foreach (JZD jzd in jzds)
            {
                if (jzd.Point == null)
                {
                    featureBuffer.Shape = jzd.Feature.ShapeCopy;
                }
                else
                {
                    IPoint point = new PointClass();
                    point.PutCoords(jzd.Point.X, jzd.Point.Y);
                    featureBuffer.Shape = point;
                }
                SetFeatureBufferValue(featureBuffer, dic, jzd);
                featureCursor.InsertFeature(featureBuffer);
            }
        }
예제 #15
0
        private void InsertFeature(IFeatureCursor inCurr,
                                   IFeatureBuffer inBuff,
                                   IFeature origF,
                                   IGeometry inGeo)
        {
            mLog.Debug("inside insert feature");
            IField  tmpField;
            IFields fieldColl;

            //object Missing = Type.Missing;
            fieldColl = origF.Fields;
            mLog.Debug("this feature has " + fieldColl.FieldCount.ToString() + " fields to work with");
            for (int i = 0; i < fieldColl.FieldCount; i++)
            {
                tmpField = fieldColl.get_Field(i);
                mLog.Debug("this field is " + tmpField.Name);
                mLog.Debug("this field type is " + tmpField.VarType.ToString());

                if (tmpField.Type != esriFieldType.esriFieldTypeGeometry &&
                    tmpField.Type != esriFieldType.esriFieldTypeOID &&
                    tmpField.Editable)
                {
                    mLog.Debug(inBuff.get_Value(i).ToString());
                    mLog.Debug("seting the value to " + origF.get_Value(i));
                    inBuff.set_Value(i, origF.get_Value(i));
                }
            }



            inBuff.Shape = inGeo;
            inCurr.InsertFeature(inBuff);
            inCurr.Flush();
        }
예제 #16
0
        private void CopyGeometryToFeatureClass(List <IGeometry> pGeometrys, IFeatureClass targetFeatureclass)
        {
            // if (pGeometry == null) { return; }
            IFeatureCursor pFeatureCursor = targetFeatureclass.Insert(true);
            IFeatureBuffer pFeatureBuffer = targetFeatureclass.CreateFeatureBuffer();

            foreach (IGeometry geo in pGeometrys)
            {
                //IGeometry geo2=null;
                //if (index == 2)
                //{
                //    ITopologicalOperator top = geo as ITopologicalOperator;
                //    top.Simplify();
                //    geo2 = top.Boundary;
                //}
                //else
                //{
                //    geo2 = geo;
                //}
                pFeatureBuffer.Shape = geo;
                pFeatureCursor.InsertFeature(pFeatureBuffer);
                pFeatureCursor.Flush();
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
            pFeatureCursor = null;
        }
예제 #17
0
        /// <summary>
        /// 插入从MapServer获取到的JSON数据
        /// </summary>
        /// <param name="features"></param>
        /// <returns></returns>
        public static int InsertFeaturesByJson(this IFeatureClass featureClass, JArray features)
        {
            int featureNum = 0;

            try
            {
                IFeatureCursor cursor = featureClass.Insert(true);
                foreach (JObject feature in features)
                {
                    try
                    {
                        IFeatureBuffer buffer = featureClass.CreateFeatureBuffer();
                        buffer.SetShapeByJson(feature.Value <JObject>("geometry"));
                        buffer.SetAttrsByJson(feature.Value <JObject>("attributes"));
                        cursor.InsertFeature(buffer);
                    }
                    catch (Exception ex)
                    {
                        // Logger.log("ZFeatureClass.InsertFeaturesByJson", ex);
                    }
                    featureNum++;
                }
                cursor.Flush();
            }
            catch (Exception ex)
            {
                throw new Exception($"将JSON数据插入图层【{featureClass.AliasName}】时报错", ex);
            }
            return(featureNum);
        }
예제 #18
0
        /// <summary>
        /// 从查询结果复制数据(数据结构相同)
        /// </summary>
        /// <param name="cursor"></param>
        public static void CopyFrom(this IFeatureClass featureClass, IFeatureCursor otherCursor)
        {
            IFeatureCursor thisCursor   = featureClass.Insert(true);
            IFeature       otherFeature = null;

            while (null != (otherFeature = otherCursor.NextFeature()))
            {
                IFeatureBuffer thisBuffer = featureClass.CreateFeatureBuffer();
                // thisBuffer.Shape = otherFeature.Shape;
                for (int i = 0; i < featureClass.Fields.FieldCount; i++)
                {
                    IField thisField = featureClass.Fields.get_Field(i);
                    if (thisField.Editable)
                    {
                        int otherFieldIndex = otherCursor.Fields.FindField(thisField.Name);
                        if (otherFieldIndex > -1)
                        {
                            thisBuffer.Value[i] = otherFeature.GetValue(otherFieldIndex);
                        }
                    }
                }
                thisCursor.InsertFeature(thisBuffer);
            }
            thisCursor.Flush();
        }
예제 #19
0
        /// <summary>
        /// 创建标准要素类
        /// </summary>
        /// <param name="sourceFeatureClass">原要素类</param>
        /// <param name="target_FeatureClass">标准要素类</param>
        /// <param name="contrastTable">字段对照表,二维数组</param>
        /// <returns>返回创建的标准要素类</returns>
        public IFeatureClass CreateStandardFeatureClass(IFeatureClass sourceFeatureClass, IFeatureClass target_FeatureClass
                                                        , string[,] contrastTable)
        {
            IFeature       pFeature;
            IFeatureCursor sourceCursor = sourceFeatureClass.Search(null, false);
            //用IFeatureBuffer提高运算速度
            IFeatureBuffer pFeaBuffer   = null;
            IFeatureCursor targetCursor = target_FeatureClass.Insert(true);

            while ((pFeature = sourceCursor.NextFeature()) != null)
            {
                pFeaBuffer       = target_FeatureClass.CreateFeatureBuffer();
                pFeaBuffer.Shape = pFeature.Shape;
                for (int i = 0; i < contrastTable.Length / 2; i++)
                {
                    pFeaBuffer.set_Value(target_FeatureClass.FindField(contrastTable[i, 0]), pFeature.Value[sourceFeatureClass.Fields.FindField(contrastTable[i, 1])]);
                }
                targetCursor.InsertFeature(pFeaBuffer);
            }
            targetCursor.Flush();
            //运用IfeatureBuffer和IfeatureCursor时需要手动释放非托管对象
            System.Runtime.InteropServices.Marshal.ReleaseComObject(sourceCursor);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeaBuffer);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(targetCursor);

            return(target_FeatureClass);
        }
        //step.3 遍历待复制的要素类,复制图形和属性并改变图形的坐标系
        /// <summary>
        /// 遍历待复制的要素类,复制图形和属性并改变图形的坐标系
        /// </summary>
        /// <param name="fromFeatureClass">待复制的要素类</param>
        /// <param name="toFeatureClass">目标要素类</param>
        private void featureClassTransSpatialRef(IFeatureClass fromFeatureClass, IFeatureClass toFeatureClass)
        {
            IFeature       pFeature;
            IGeometry2     pGeometry;
            IFeatureCursor toCursor     = toFeatureClass.Insert(true);
            int            FeatureCount = fromFeatureClass.FeatureCount(null);

            for (int i = 0; i < FeatureCount; i++)
            {
                pFeature  = fromFeatureClass.GetFeature(i);
                pGeometry = pFeature.Shape as IGeometry2;
                IZAware pZAware = pGeometry as IZAware;
                pZAware.ZAware = true;
                pGeometry.ProjectEx(spatialRefTo, esriTransformDirection.esriTransformForward, geoTransformation, false, 0, 0);
                IFeatureBuffer pFeaBuffer = toFeatureClass.CreateFeatureBuffer();
                pFeaBuffer.Shape = pGeometry;
                for (int j = 2; j < fromFeatureClass.Fields.FieldCount; j++)
                {
                    try
                    {
                        pFeaBuffer.set_Value(j, pFeature.Value[fromFeatureClass.FindField(toFeatureClass.Fields.Field[j].Name)]);
                    }
                    catch
                    {
                        continue;
                    }
                }
                toCursor.InsertFeature(pFeaBuffer);
                toCursor.Flush();
            }
        }
예제 #21
0
        //不剪裁输出forShp
        public static void notcutExportShp(IFeatureCursor pCursor, IFeatureClass pToFeatureClass, int featurecount)
        {
            progressStep.Minimum = 0;
            progressStep.Maximum = featurecount;
            progressStep.Step    = 1;
            IFeature       pFeature       = pCursor.NextFeature();
            IFeatureCursor pFeatureCursor = pToFeatureClass.Insert(true);
            int            iCount         = 0;

            while (pFeature != null)
            {
                IFeatureBuffer pFeatureBuffer = pToFeatureClass.CreateFeatureBuffer();

                for (int i = 0; i < pFeature.Fields.FieldCount; i++)
                {
                    //string sFieldName = pFeature.Fields.get_Field(i).Name;

                    //int iIndex = pFeatureBuffer.Fields.FindField(toShpField[sFieldName]);
                    string sFieldName = pFeature.Fields.get_Field(i).Name;

                    int iIndex = -1;
                    if (m_DicFields.Keys.Contains(sFieldName))
                    {
                        iIndex = pFeatureBuffer.Fields.FindField(m_DicFields[sFieldName]);
                    }
                    else
                    {
                        iIndex = pFeatureBuffer.Fields.FindField(sFieldName);
                    }

                    try
                    {
                        if ((iIndex > -1) && (pFeatureBuffer.Fields.get_Field(iIndex).Editable == true))
                        {
                            pFeatureBuffer.set_Value(iIndex, pFeature.get_Value(i));
                        }
                    }
                    catch
                    { }
                }
                pFeatureBuffer.Shape = pFeature.ShapeCopy;
                pFeatureCursor.InsertFeature(pFeatureBuffer);
                if (iCount == 500)
                {
                    pFeatureCursor.Flush();
                    iCount = 0;
                }
                iCount++;
                progressStep.PerformStep();
                pFeature = pCursor.NextFeature();
            }
            if (iCount > 0)
            {
                pFeatureCursor.Flush();
            }

            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
            pFeatureCursor = null;
        }
예제 #22
0
파일: ArcgisService.cs 프로젝트: YB9527/CG
        /// <summary>
        /// 添加实体对象
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="geometry"></param>
        /// <param name="featureClass"></param>
        public static void AddFeature(object obj, IGeometry geometry, IFeatureClass featureClass)
        {
            Dictionary <int, Clazz> dic           = ArcGisService.GetTitleClzz(obj, featureClass.Fields, false);
            IFeatureCursor          featureCursor = featureClass.Insert(true);
            IFeatureBuffer          featureBuffer = featureClass.CreateFeatureBuffer();

            featureBuffer.Shape = geometry;
            SetFeatureBufferValue(featureBuffer, dic, obj);
            featureCursor.InsertFeature(featureBuffer);
        }
        private void CopyFeatureClassData(IFeatureClass sourceFeatureClass, IFeatureClass targetFeatureClass, IDictionary <int, int> filedIndexDictionary)
        {
            errorRowCount = 0;
            using (var comReleaser = new ComReleaser())
            {
                IFeatureCursor sourceFeatureCursor = sourceFeatureClass.Search(null, false);
                IFeatureCursor targetFeatureCursor = targetFeatureClass.Insert(true);
                comReleaser.ManageLifetime(sourceFeatureCursor);
                comReleaser.ManageLifetime(targetFeatureCursor);

                List <int> targeFieldIndexs = filedIndexDictionary.Keys.ToList();
                int        fieldsLen        = targeFieldIndexs.Count;

                IFeature sourceFeature = sourceFeatureCursor.NextFeature();
                var      count         = 0;
                while (sourceFeature != null)
                {
                    try
                    {
                        //错误:Geometry cannot have Z values 处理
                        IGeometry geometry = sourceFeature.ShapeCopy;
                        IZAware   zAware   = geometry as IZAware;
                        if (zAware != null && zAware.ZAware == true)
                        {
                            zAware.ZAware = false;
                        }

                        IFeatureBuffer targetFatureBuffer = targetFeatureClass.CreateFeatureBuffer();
                        targetFatureBuffer.Shape = geometry;//sourceFeature.ShapeCopy;
                        for (int i = 0; i < fieldsLen; i++)
                        {
                            var targetIndex = targeFieldIndexs[i];
                            int sourceIndex = filedIndexDictionary[targeFieldIndexs[i]];
                            var value       = sourceFeature.get_Value(sourceIndex);
                            targetFatureBuffer.set_Value(targetIndex, value);
                        }
                        targetFeatureCursor.InsertFeature(targetFatureBuffer);
                        count++;
                        if (count == 1000)
                        {
                            targetFeatureCursor.Flush();
                            count = 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        errorRowCount++;
                        var message = "字段赋值错误";
                        //LoggingManager.GetLogger(GetType()).Error(message, ex);
                    }
                    sourceFeature = sourceFeatureCursor.NextFeature();
                }
                targetFeatureCursor.Flush();
            }
        }
예제 #24
0
        /// <summary>
        /// 插入新要素
        /// </summary>
        /// <param name="featureLayer">图层</param>
        /// <param name="geom">插入要素几何图形</param>
        /// <param name="ID">要素ID(绑定ID)</param>
        /// <returns></returns>
        public static bool InsertNewFeature(IFeatureLayer featureLayer, IGeometry geom, string ID)
        {
            IWorkspaceEdit workspaceEdit = null;
            IFeatureCursor featureCursor = null;

            try
            {
                IFeatureClass featureClass = featureLayer.FeatureClass;

                IDataset   dataset   = (IDataset)featureClass;
                IWorkspace workspace = dataset.Workspace;
                workspaceEdit = workspace as IWorkspaceEdit;

                workspaceEdit.StartEditing(true);
                workspaceEdit.StartEditOperation();

                IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
                DataEditCommon.ZMValue(featureBuffer, geom);    //几何图形Z值处理
                featureBuffer.Shape = geom;

                int iFieldID = featureBuffer.Fields.FindField("ID");
                featureBuffer.Value[iFieldID] = ID.ToString();

                //开始插入要素
                featureCursor = featureClass.Insert(true);
                object featureOID = featureCursor.InsertFeature(featureBuffer);

                //保存要素
                featureCursor.Flush();

                IFeature feature = featureCursor.NextFeature();

                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

                workspaceEdit.AbortEditOperation();
                workspaceEdit.StopEditing(false);

                return(false);
            }
            finally
            {
                if (featureCursor != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);  //释放指针
                }
            }
        }
        public bool Work()
        {
            if (!Init())
            {
                return(false);
            }
            IFeatureCursor cursor = _polygonFeatureClass.Insert(true);

            IFeatureCursor featurecursor = _polylineFeatureClass.Search(null, false);
            IFeature       feature       = featurecursor.NextFeature();

            while (feature != null)
            {
                if (PolylineManager.IsPolygon(feature))
                {
                    var pg = PolylineManager.GeneratePolygon(feature.ShapeCopy);
                    if (pg != null)
                    {
                        var buffer = _polygonFeatureClass.CreateFeatureBuffer();
                        buffer.Shape = pg;
                        for (var i = 0; i < feature.Fields.FieldCount; i++)
                        {
                            IField field = feature.Fields.get_Field(i);
                            if (field != null)
                            {
                                if (field.Name == "OID" || field.Name == "FID" || field.Name.ToUpper() == "SHAPE" || field.Name.ToUpper().Contains("SHAPE"))
                                {
                                    continue;
                                }
                                var val   = feature.get_Value(i);
                                var index = buffer.Fields.FindField(field.Name);
                                if (index > -1)
                                {
                                    buffer.set_Value(index, val);
                                }
                            }
                        }

                        try
                        {
                            object featureOID = cursor.InsertFeature(buffer);
                            cursor.Flush();
                        }catch (Exception ex)
                        {
                            _error += string.Format("存在保存到总FeatureCLass错误,错误信息:{0}", ex.ToString());
                        }
                    }
                }
                feature = featurecursor.NextFeature();
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(featurecursor);
            return(string.IsNullOrEmpty(_error));
        }
예제 #26
0
            public void Write(NRCPStatisticFeatureClassRow a_Row)
            {
                if (m_FeatureCursor == null)
                {
                    m_FeatureCursor = m_FeatureClass.Insert(true);
                    m_FeatureBuffer = m_FeatureClass.CreateFeatureBuffer();
                }

                a_Row.Write(m_FeatureBuffer);
                m_FeatureCursor.InsertFeature(m_FeatureBuffer);
            }
예제 #27
0
        /// <summary>
        /// 构造小区的一个立体覆盖网格
        /// </summary>
        /// <param name="cellName"></param>
        /// <param name="lac"></param>
        /// <param name="ci"></param>
        /// <param name="gxid"></param>
        /// <param name="gyid"></param>
        /// <param name="level"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        /// <param name="z"></param>
        /// <param name="recePower"></param>
        /// <param name="pathLoss"></param>
        public void constructGrid3D(string cellName, int lac, int ci, int gxid, int gyid, int level, double x1, double y1, double x2, double y2, double z, double recePower, double pathLoss)
        {
            IDataset   dataset   = (IDataset)pFeatureLayer.FeatureClass;
            IWorkspace workspace = dataset.Workspace;
            //Cast for an IWorkspaceEdit
            IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;

            //start an edit session and operation
            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();

            IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
            IFeatureBuffer pFeatureBuffer;

            Geometric.Point p   = GridHelper.getInstance().GridToGeo(gxid, gyid);
            double          lon = p.X;
            double          lat = p.Y;

            IPoint pointA = GeometryUtilities.ConstructPoint3D(x1, y1, z);
            IPoint pointB = GeometryUtilities.ConstructPoint3D(x2, y1, z);
            IPoint pointC = GeometryUtilities.ConstructPoint3D(x2, y2, z);
            IPoint pointD = GeometryUtilities.ConstructPoint3D(x1, y2, z);

            IGeometryCollection pGeometryColl = GeometryUtilities.ConstructPolygon(new IPoint[] { pointA, pointB, pointC, pointD });

            pFeatureBuffer       = pFeatureClass.CreateFeatureBuffer();
            pFeatureBuffer.Shape = pGeometryColl as IGeometry;
            pFeatureBuffer.set_Value(this.GXIDIndex, gxid);
            pFeatureBuffer.set_Value(this.GYIDIndex, gyid);
            pFeatureBuffer.set_Value(this.LevelIndex, level);
            pFeatureBuffer.set_Value(this.eNodeBIndex, lac);
            pFeatureBuffer.set_Value(this.CIIndex, ci);
            pFeatureBuffer.set_Value(this.cellNameIndex, cellName);
            pFeatureBuffer.set_Value(this.RecePowerIndex, recePower);
            pFeatureBuffer.set_Value(this.PathLossIndex, pathLoss);
            pFeatureBuffer.set_Value(this.LongitudeIndex, lon);
            pFeatureBuffer.set_Value(this.LatitudeIndex, lat);

            pFeatureCursor.InsertFeature(pFeatureBuffer);

            //一次性提交
            pFeatureCursor.Flush();

            //stop editing
            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);

            //IFeatureClassManage pFeatureClassManage = (IFeatureClassManage)pFeatureClass;
            //pFeatureClassManage.UpdateExtent();

            GISMapApplication.Instance.RefreshLayer(pFeatureLayer);
            //GISMapApplication.Instance.FullExtent(pFeatureLayer.AreaOfInterest);
        }
예제 #28
0
 private bool InsertFea(ref IFeatureCursor pFeaCursor, ref IFeatureBuffer pFeaBuf)
 {
     try
     {
         pFeaCursor.InsertFeature(pFeaBuf);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
예제 #29
0
파일: JZDCustom.cs 프로젝트: YB9527/CG
        public static void AddJzdFeature(object obj, IPoint geometry, IFeatureClass featureClass)
        {
            Dictionary <int, Clazz> dic           = ArcGisService.GetTitleClzz(obj, featureClass.Fields);
            IFeatureCursor          featureCursor = featureClass.Insert(true);
            IFeatureBuffer          featureBuffer = featureClass.CreateFeatureBuffer();
            IPoint point = new PointClass();

            point.PutCoords(geometry.X, geometry.Y);
            featureBuffer.Shape = point;
            SetFeatureBufferValue(featureBuffer, dic, obj);
            featureCursor.InsertFeature(featureBuffer);
        }
예제 #30
0
        /// <summary>
        /// Add Value to destination
        /// </summary>
        public void Insert()
        {
            int currentRow = 0;

            /// Check output is GDB or not
            if (_OutputType == OutputType.GDB)
            {
                /*
                 *  Cast IFeatureClass to IFeatureClassLoad for set LoadOnlyMode
                 *  and because LoadOnlyMode can use only GDB and SDE type
                 *  LoadOnlyMode will improve performace for insert
                 */
                IFeatureClassLoad fClassLoad = _DesFeatureClass as IFeatureClassLoad;
                fClassLoad.LoadOnlyMode = true;
            }

            #region Old code

            /// Create IFeatureCursor for Insert
            IFeatureCursor fCursor = _DesFeatureClass.Insert(true);

            /// Loop list Info
            foreach (T info in _LstInfo)
            {
                /// Create IFeatureBuffer and cast to IFeature
                IFeatureBuffer fBuffer  = _DesFeatureClass.CreateFeatureBuffer();
                IFeature       oFeature = fBuffer as IFeature;

                /// Add info to IFeature
                AddInfo2Feature(info, oFeature);

                /// Insert IFeatureBuffer to IFeatureCursor
                fCursor.InsertFeature(fBuffer);

                /// Save every N row
                if (currentRow % _SaveEveryNRecord == 0)
                {
                    fCursor.Flush();
                }

                /// Update Row
                currentRow++;

                /// CallBack
                int percentTotal = CalPercentageFromTotal(currentRow, _LstInfo.Count);
                _ActionCallBack?.Invoke(percentTotal);
            }

            /// Save All Insert Feature
            fCursor.Flush();

            #endregion Old code
        }
        /// <summary>
        ///     Insert a row into the feature class.
        /// </summary>
        /// <param name="featureClass">
        ///     The feature class that will have rows inserted
        /// </param>
        /// <param name="key">
        ///     The key.
        /// </param>
        /// <param name="insertCur">
        ///     The insert cur.
        /// </param>
        /// <param name="resultDictionary">
        ///     The result dictionary.
        /// </param>
        /// <param name="uniqueFieldNames">
        ///     The unique field names.
        /// </param>
        private void InsertRow(
            IFeatureClass featureClass,
            string key,
            IFeatureCursor insertCur,
            Dictionary<string, Dictionary<string, double>> resultDictionary,
            Dictionary<string, string> uniqueFieldNames)
        {
            try
            {
                // get the polygon of the geohash
                var poly = this.GetGeoHashPoly(key);
                var buffer = featureClass.CreateFeatureBuffer();

                // Setup the features geometry.
                buffer.Shape = (IGeometry)poly;
                buffer.Value[featureClass.FindField("GeoHash")] = key;
                foreach (var subKey in resultDictionary[key].Keys)
                {
                    var field = uniqueFieldNames[subKey];
                    var value = resultDictionary[key][subKey];
                    var index = featureClass.FindField("DG_" + field);

                    if (index != -1)
                    {
                        buffer.Value[index] = value;
                    }
                }

                // Feature has been created so add to the feature class.
                insertCur.InsertFeature(buffer);
            }
            catch (Exception error)
            {
                Jarvis.Logger.Error(error);
            }
        }