• C#+Arcengine创建内存图层


    #region 在内存中创建图层
            /// <summary>
            /// 在内存中创建图层
            /// </summary>
            /// <param name="DataSetName">数据集名称</param>
            /// <param name="AliaseName">别名</param>
            /// <param name="SpatialRef">空间参考</param>
            /// <param name="GeometryType">几何类型</param>
            /// <param name="PropertyFields">属性字段集合</param>
            /// <returns>IfeatureLayer</returns>
            public static IFeatureLayer CreateFeatureLayerInmemeory(string DataSetName, string AliaseName, ISpatialReference SpatialRef, esriGeometryType GeometryType, IFields PropertyFields)
            {
                IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass();
                ESRI.ArcGIS.Geodatabase.IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0);
                ESRI.ArcGIS.esriSystem.IName name = (IName)workspaceName;
                ESRI.ArcGIS.Geodatabase.IWorkspace inmemWor = (IWorkspace)name.Open();

                IField oField = new FieldClass();
                IFields oFields = new FieldsClass();
                IFieldsEdit oFieldsEdit = null;
                IFieldEdit oFieldEdit = null;
                IFeatureClass oFeatureClass = null;
                IFeatureLayer oFeatureLayer = null;

                try
                {
                    oFieldsEdit = oFields as IFieldsEdit;
                    oFieldEdit = oField as IFieldEdit;

                    for (int i = 0; i < PropertyFields.FieldCount; i++)
                    {
                        oFieldsEdit.AddField(PropertyFields.get_Field(i));
                    }

                    IGeometryDef geometryDef = new GeometryDefClass();
                    IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                    geometryDefEdit.AvgNumPoints_2 = 5;
                    geometryDefEdit.GeometryType_2 = GeometryType;
                    geometryDefEdit.GridCount_2 = 1;
                    geometryDefEdit.HasM_2 = false;
                    geometryDefEdit.HasZ_2 = false;
                    geometryDefEdit.SpatialReference_2 = SpatialRef;
                    geometryDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();//没有这一句就报错,说尝试读取或写入受保护的内存。
                    geometryDefEdit.SpatialReference.SetDomain(-200, 200, -200, 200);//没有这句就抛异常来自HRESULT:0x8004120E。

                    oFieldEdit.Name_2 = "SHAPE";
                    oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                    oFieldEdit.GeometryDef_2 = geometryDef;
                    oFieldEdit.IsNullable_2 = true;
                    oFieldEdit.Required_2 = true;
                    oFieldsEdit.AddField(oField);

                    oFeatureClass = (inmemWor as IFeatureWorkspace).CreateFeatureClass(DataSetName, oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
                    (oFeatureClass as IDataset).BrowseName = DataSetName;

                    oFeatureLayer = new FeatureLayerClass();
                    oFeatureLayer.Name = AliaseName;
                    oFeatureLayer.FeatureClass = oFeatureClass;
                }
                catch
                {
                }
                return oFeatureLayer;
            }

            #endregion

            下面就是根据采集的坐标点创建点内存图层:

                IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                IFieldsEdit curFileds = new FieldsClass();
                IFieldEdit curField = new FieldClass();
                curField = new FieldClass();
                curField.Name_2 = "名称";
                curField.Type_2 = esriFieldType.esriFieldTypeString;
                curFileds.AddField(curField);

                curField = new FieldClass();
                curField.Name_2 = "经度";
                curField.Type_2 = esriFieldType.esriFieldTypeDouble;
                curFileds.AddField(curField);

                curField = new FieldClass();
                curField.Name_2 = "纬度";
                curField.Type_2 = esriFieldType.esriFieldTypeDouble;
                curFileds.AddField(curField);

                pFeatureLayer = CreateFeatureLayerInmemeory("Position", "采集点", new     UnknownCoordinateSystemClass(), esriGeometryType.esriGeometryPoint, curFileds as IFields);
                p_axMap.AddLayer(pFeatureLayer as ILayer);
                IFeatureCursor FeatureCursor;
                IFeature pFeature;
                IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
                pFeature = pFeatureClass.CreateFeature();
                IQueryFilter pQueryFilter = new QueryFilterClass();
                FeatureCursor = pFeatureClass.Search(pQueryFilter, true);
                pQueryFilter.WhereClause = null;
                pFeature = FeatureCursor.NextFeature();
                pFeature.set_Value(0, str1);
                pFeature.set_Value(1, x);
                pFeature.set_Value(2, y);
                IEnvelope pEnvelope = new EnvelopeClass();

                //如果双击的对象是一个点,这样子才能够缩放到该点
                pEnvelope.PutCoords(0, 0, 0.3, 0.3);//确定envelope大小

                IPoint xpoint = new PointClass();
                xpoint.PutCoords(x, y);

                pFeature.Shape = xpoint;
                pFeature.Store();

                //设置颜色
                IRgbColor pcolor = new RgbColorClass();

                pcolor.Red = 255;

                pcolor.Green = 0;

                pcolor.Blue = 0;

                //设置图形
                ISimpleMarkerSymbol psm = new SimpleMarkerSymbolClass();
                psm.Style = esriSimpleMarkerStyle.esriSMSCircle;
                psm.Size = 10;
                psm.Color = pcolor;        

                (pFeatureLayer as IFeatureSelection).SelectionSymbol = (ISymbol)psm;
                (pFeatureLayer as IFeatureSelection).SetSelectionSymbol = true;

                (pFeatureLayer as IFeatureSelection).SelectionSet.Add(pFeature.OID);

                pEnvelope.CenterAt(xpoint);//地图缩放到该点      
                p_axMap.Extent = pEnvelope;
                p_axMap.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, pFeatureLayer, null);
                p_axMap.ActiveView.ScreenDisplay.UpdateWindow();

  • 相关阅读:
    java 获取文本一行一行读
    postman 测试api接口
    MariaDB 默认是禁止远程访问的 我们改掉它
    mysql 查询近三个月数据
    Springboot配置拦截器
    springboot 基于@Scheduled注解 实现定时任务
    springboot 配置访问本地图片
    springboot上传文件大小限制的配置
    vue中toggle切换的3种写法
    vue怎么给自定义组件绑定原生事件
  • 原文地址:https://www.cnblogs.com/adodo1/p/4328147.html
Copyright © 2020-2023  润新知