1. 程式人生 > >C#+AE地圖文件的相關操作,包括新建開啟儲存等

C#+AE地圖文件的相關操作,包括新建開啟儲存等

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;
        }