码迷,mamicode.com
首页 > 其他好文 > 详细

Arcengine 操作积累

时间:2019-10-24 15:10:20      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:als   geo   create   代码   use   factory   manage   根据   mars   

修改shp数据字段名字

public bool UpdateArea(IFeatureClass pfeatureclass, string oldfieldName, string newfieldName)
{
    bool updateSuc = false;
    try
    {
        using (ComReleaser comReleaser = new ComReleaser())
        {
            if (pfeatureclass == null) return false;
            ITable pTable = pfeatureclass as ITable;
            IFields pfields = pfeatureclass.Fields;
            IField pfield = null;
            for (int i = 0; i < pfields.FieldCount; i++)
            {
                pfield = pfields.get_Field(i);
                if (pfield.Name.ToLower() == oldfieldName.ToLower())
                {
                    //添加新字段
                    IField pField = new FieldClass();
                    IFieldEdit pFieldEdit = pField as IFieldEdit;
                    pFieldEdit.Name_2 = newfieldName;
                    pFieldEdit.AliasName_2 = newfieldName;
                    pFieldEdit.Type_2 = pfield.Type;
                    pFieldEdit.Length_2 = pfield.Length;
                    pFieldEdit.Precision_2 = pfield.Precision;
                    pFieldEdit.Scale_2 = pfield.Scale;
                    pTable.AddField(pField);
                    break;
                }

            }

            int Index = pTable.FindField(newfieldName);
            int indexP = pTable.FindField(oldfieldName);

            if (Index == -1 || indexP == -1) return false;
            ICursor updateCursor = pTable.Update(null, true);
            comReleaser.ManageLifetime(updateCursor);

            IRow row = null;
            while ((row = updateCursor.NextRow()) != null)
            {
                //旧字段值赋值给新字段
                double v = Convert.ToDouble(row.get_Value(indexP));
                row.set_Value(Index, v);
                updateCursor.UpdateRow(row);
            }
            pTable.DeleteField(pfield); //赋值完后,删除旧字段
        }
        updateSuc = true;
    }
    catch (Exception ex)
    {
        updateSuc = false;
    }
    return updateSuc;
}

批量将一个字段的值赋值给另一个字段

public bool BatchSetValue(string pFeatureClassName, string fromFieldName, string toFieldName)
{
    bool updateSuc = false;
    try
    {
        using (ESRI.ArcGIS.ADF.ComReleaser comReleaser = new ESRI.ArcGIS.ADF.ComReleaser())
        {
            IFeatureClass pFeatureClass = gms.GetFeatureClass(pFeatureClassName);
            if (pFeatureClass == null) return false;
            int fromIndex = pFeatureClass.Fields.FindField(fromFieldName);
            int toIndex = pFeatureClass.Fields.FindField(toFieldName);
            if (fromIndex == -1 || toIndex == -1) return false;

            IQueryFilter pQueryfilter = new QueryFilterClass();
            //pQueryfilter.WhereClause = "1=1";
            pQueryfilter.SubFields = pFeatureClass.OIDFieldName + "," + fromFieldName + "," + toFieldName + ",Shape";
            IFeatureCursor pFeatureCursor = null;
            pFeatureCursor = pFeatureClass.Update(pQueryfilter, true);
            IFeature pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                object A = pFeature.get_Value(fromIndex);
                pFeature.set_Value(toIndex, pFeature.get_Value(fromIndex));
                //pFeature.Store();
                pFeatureCursor.UpdateFeature(pFeature);
                pFeature = pFeatureCursor.NextFeature();
            }
        }
        updateSuc = true;
    }
    catch (Exception ex)
    {
        LogManager.LogHelper.WriteMessage(ex.Message, ex.StackTrace, LogManager.LogHelper.LogType.ERROR);
        updateSuc = false;
    }
    return updateSuc;
}

根据要素选择集,创建新图层

IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.FeatureClass = featureClass;

IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
IQueryFilter queryFilter1 = new QueryFilterClass();
queryFilter1.WhereClause = "XZQDM like '" + xz_xzqdm + "%'";
featureSelection.SelectFeatures(queryFilter1, esriSelectionResultEnum.esriSelectionResultNew, false);

ISelectionSet selectionSet = featureSelection.SelectionSet;
IFeatureLayer newFeatureLayer = null;
if (selectionSet.Count > 0)
{
    IFeatureLayerDefinition featureLayerDefinition = featureLayer as IFeatureLayerDefinition;
    newFeatureLayer = featureLayerDefinition.CreateSelectionLayer(featureClass.AliasName, true, null, null);
}

AE二次开发中,过滤后的图层,实现缩放至图层效果

//实现过滤后的图层,缩放到图层  
IEnvelope  pEnvelope = new EnvelopeClass();
//关键代码,替换了要素遍历,提高效率
IEnumGeometryBind enumGeometryBind = new EnumFeatureGeometryClass();
enumGeometryBind.BindGeometrySource(null, pFeatureClassDLTB);
IEnumGeometry enumGeometry = (IEnumGeometry)enumGeometryBind;
IGeometryFactory geoFactory = new GeometryEnvironment() as IGeometryFactory;
IGeometry geo = geoFactory.CreateGeometryFromEnumerator(enumGeometry);
pEnvelope.Union(geo.Envelope);
System.Runtime.InteropServices.Marshal.ReleaseComObject(geo);
System.Runtime.InteropServices.Marshal.ReleaseComObject(geoFactory);
System.Runtime.InteropServices.Marshal.ReleaseComObject(enumGeometry);
mcView.ActiveView.FullExtent  = pEnvelope;

Arcengine 操作积累

标签:als   geo   create   代码   use   factory   manage   根据   mars   

原文地址:https://www.cnblogs.com/King2019Blog/p/11732181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!