C#+AE地图文档的相关操作,包括新建打开保存等

12 篇文章 0 订阅
public static bool NewMxdFile(string filePath)//新建工作空间
        {
            try
            {
                string tmp_fileName = filePath;
                IMapDocument pMapDocument = new MapDocumentClass();
                pMapDocument.New(tmp_fileName);
                pMapDocument.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void OpenMxdFile(AxMapControl axMapControl1, string filePath)//打开工作空间
        {
            try
            {
                if (File.Exists(filePath))
                {
                    axMapControl1.LoadMxFile(filePath);
                }
                else
                {
                    if (MessageBox.Show("工作空间文件丢失,是否要创建新的工作空间?", "OpenMxdFile", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (NewMxdFile(filePath) == false)
                        {
                            MessageBox.Show("工作空间创建失败!", "OpenMxdFile", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"OpenMxdFile");
            }
        }

        public static bool SaveMxdFile(AxMapControl tmp_mapControl)//保存工作空间
        {
            try
            {
                IMxdContents pMxdC;
                pMxdC = tmp_mapControl.Map as IMxdContents;
                IMapDocument pMapDocument = new MapDocumentClass();
                pMapDocument.Open(tmp_mapControl.DocumentFilename, "");
                pMapDocument.ReplaceContents(pMxdC);
                pMapDocument.Save(true,true);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool MxdSaveAs(AxMapControl axMapControl1, string filePath)//另存为工作空间
        {
            try
            {
                IMxdContents pMxdC;
                pMxdC = axMapControl1.Map as IMxdContents;
                IMapDocument pMapDocument = new MapDocumentClass();
                pMapDocument.Open(axMapControl1.DocumentFilename, "");
                pMapDocument.ReplaceContents(pMxdC);
                pMapDocument.SaveAs(filePath, true, true);
                MessageBox.Show("保存成功");
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "MxdSaveAs");
                return false;
            }
        }
        public static IFeatureLayer CreateShapeFileFeatureLayer(AxMapControl ax, esriGeometryType featureType, ISpatialReference spatialReference, string layerName, List<string> fieldName, List<esriFieldType> fieldType)//新建shp文件
        {
            IFeatureLayer featureLayer = new FeatureLayerClass();
            if (fieldName.Count != fieldType.Count)
            {
                MessageBox.Show("字段和类型数目不同");
                return null;
            }
            if (ax == null)
            {
                return null;
            }
            string strShapeFolder = ".\\MapFile\\Temp\\";
            string strShapeFile = layerName + DateTime.Now.ToString("yyMMddhhmmss") + ".shp";
            string shapeFileFullName = strShapeFolder + strShapeFile;
            try
            {
                IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory();
                IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(strShapeFolder, 0);
                IFeatureClass featureClass;
                if (File.Exists(shapeFileFullName))
                {
                    featureClass = featureWorkspace.OpenFeatureClass(strShapeFile);
                    IDataset dataset = (IDataset)featureClass;
                    dataset.Delete();
                }
                IFields fields = new FieldsClass();
                IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
                IField field = new FieldClass();
                IFieldEdit fieldEdit = (IFieldEdit)field;
                fieldEdit.Name_2 = "SHAPE";
                fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                IGeometryDefEdit geoDef = new GeometryDefClass();
                IGeometryDefEdit geoDefEdit = (IGeometryDefEdit)geoDef;

                geoDefEdit.GeometryType_2 = featureType;//设置图层类型
                geoDefEdit.SpatialReference_2 = spatialReference;
                fieldEdit.GeometryDef_2 = geoDef;
                fieldsEdit.AddField(field);

                for (int i = 0; i < fieldName.Count; i++)
                {
                    field = new FieldClass();
                    fieldEdit = (IFieldEdit)field;
                    fieldEdit.Name_2 = fieldName[i];
                    fieldEdit.Type_2 = fieldType[i];
                    fieldsEdit.AddField(field);
                }

                featureClass = featureWorkspace.CreateFeatureClass(strShapeFile, fields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
                featureLayer.FeatureClass = featureClass;
                featureLayer.Name = layerName;
                AddLayerToRightPlace(ax, featureLayer);
                //设置点层符号
                ISimpleMarkerSymbol simplefillsymbol = new SimpleMarkerSymbolClass();
                simplefillsymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
                simplefillsymbol.Size = 0.1;
                IGeoFeatureLayer geoFeatureLayer = featureLayer as IGeoFeatureLayer;
                ISimpleRenderer simperRender = new SimpleRendererClass();
                simperRender.Symbol = (ISymbol)simplefillsymbol;
                geoFeatureLayer.Renderer = simperRender as IFeatureRenderer;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "CreateShapeFileFeatureLayer");
            }
            return featureLayer;
        }
        public static void AddLayerToRightPlace(AxMapControl ax, ILayer layer)//添加图层到合适的位置
        {
            if (layer is IFeatureLayer)
            {
                switch ((layer as IFeatureLayer).FeatureClass.ShapeType)
                {
                    case esriGeometryType.esriGeometryPoint:
                        ax.AddLayer(layer, 0);
                        break;
                    case esriGeometryType.esriGeometryPolyline:
                        int polylineIndex = 0;
                        for (int i = ax.LayerCount - 1; i >= 0; i--)
                        {
                            if (GetLayerType(ax.get_Layer(i)) == "Point")
                            {
                                polylineIndex = i;
                                break;
                            }
                        }
                        ax.AddLayer(layer, polylineIndex + 1);
                        break;
                    case esriGeometryType.esriGeometryPolygon:
                        int polygonIndex = 0;
                        for (int i = ax.LayerCount - 1; i >= 0; i--)
                        {
                            if (GetLayerType(ax.get_Layer(i)) == "Polyline")
                            {
                                polygonIndex = i;
                                break;
                            }
                        }
                        ax.AddLayer(layer, polygonIndex + 1);
                        break;
                }
            }
            else if (layer is IRasterLayer)
            {
                int polygonIndex = 0;
                for (int i = ax.LayerCount - 1; i >= 0; i--)
                {
                    if (GetLayerType(ax.get_Layer(i)) == "Polyline")
                    {
                        polygonIndex = i;
                        break;
                    }
                }
                ax.AddLayer(layer, polygonIndex + 1);
            }
        }
        public static string GetLayerType(ILayer layer)//获得图层类型
        {
            string type = "";
            if (layer is IFeatureLayer)
            {
                IFeatureLayer pFeatLyr = layer as IFeatureLayer;
                switch (pFeatLyr.FeatureClass.ShapeType)
                {
                    case esriGeometryType.esriGeometryPoint:
                        type = "Point";
                        break;
                    case esriGeometryType.esriGeometryPolyline:
                        type = "Polyline";
                        break;
                    case esriGeometryType.esriGeometryPolygon:
                        type = "Polygon";
                        break;
                    default:
                        break;
                }
            }
            else if (layer is IRasterLayer)
            {
                type = "Raster";
            }
            return type;
        }
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值