private string LoadMapFileInPreviewControl(string fileName) { string msg = string.Empty; if (File.Exists(fileName)) { string ext = Path.GetExtension(fileName).ToLower(); if (ext == ".mxd") { if (mapControl.CheckMxFile(fileName)) { try { mapControl.LoadMxFile(fileName); mapControl.Extent = mapControl.FullExtent; } catch (Exception ex) { msg = "ESRI Map Control generated an error.\nFile: " + fileName + "\nError: " + ex; } } else { msg = "Map document not valid: " + fileName; } } else if (ext == ".lyr") { try { mapControl.ClearLayers(); mapControl.SpatialReference = null; mapControl.AddLayerFromFile(fileName); mapControl.get_Layer(0).Visible = true; //Make sure the layer is visible //Set the Spatial Ref to match the current layer, not the previous layer. //mapControl.SpatialReference = mapControl.get_Layer(0).SpatialReference; mapControl.Extent = mapControl.FullExtent; } catch (Exception ex) { msg = "ESRI Map Control generated an error.\nFile: " + fileName + "\nError: " + ex; } } else { msg = "File must be a map document (.mxd) or a layer file (.lyr): " + fileName; } } else { msg = "File not found: " + fileName; } return(msg); }
//添加lyr图层数据 private void addLyrLayer(string fullfilepath, AxMapControl mapControl) { if (fullfilepath == "") { return; } mapControl.AddLayerFromFile(fullfilepath, 0);//添加lyr图层 IActiveView activeView = mapControl.Map as IActiveView; activeView.Extent = mapControl.FullExtent; mapControl.Refresh(); }
/// <summary> /// 将指定路径下的要素图层加载到地图控件中 /// <para>可加载lyr/shp/mdb/gdb/dwg等多种数据源图层,允许的路径形式参考<see cref="FeatureClassEx.FromPath"/>方法</para> /// </summary> /// <param name="mapControl">地图控件</param> /// <param name="layerPath">要素类图层的完整路径,允许的路径形式参考<see cref="FeatureClassEx.FromPath"/>方法</param> public static void LoadFeatureLayer(this AxMapControl mapControl, string layerPath) { if (Path.GetExtension(layerPath) == ".lyr" && File.Exists(layerPath)) { mapControl.AddLayerFromFile(layerPath); return; } var featureClass = FeatureClassEx.FromPath(layerPath); mapControl.AddLayer(new FeatureLayerClass { FeatureClass = featureClass, Name = featureClass.AliasName }); }
/// <summary> /// 将指定路径下的要素数据源数据,加载到地图控件中 /// <para>可加载lyr/shp/mdb/gdb/dwg等多种数据源数据,允许的路径形式参考<see cref="FeatureClassEx.AllFromPath"/>方法</para> /// </summary> /// <param name="mapControl">地图控件</param> /// <param name="connStrOrPath">要素数据源路径,允许的路径形式参考<see cref="FeatureClassEx.AllFromPath(string)"/>方法</param> public static void LoadFeatureLayers(this AxMapControl mapControl, string connStrOrPath) { if (Path.GetExtension(connStrOrPath) == ".lyr" && File.Exists(connStrOrPath)) { mapControl.AddLayerFromFile(connStrOrPath); return; } var featureClasses = FeatureClassEx.AllFromPath(connStrOrPath); foreach (var featureClass in featureClasses) { mapControl.AddLayer(new FeatureLayerClass { FeatureClass = featureClass, Name = featureClass.AliasName }); } }
public static bool LoadKZBJ(string filePath, AxMapControl mapControl) { IWorkspaceFactory factory = new ShapefileWorkspaceFactory(); IWorkspace workspace = factory.OpenFromFile(System.IO.Path.GetDirectoryName(filePath), 0); IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileName(filePath)); mapControl.AddLayerFromFile(string.Format("{0}\\扩展边界.lyr", Application.StartupPath), 3); for (var i = 0; i < mapControl.LayerCount; i++) { var layer = mapControl.get_Layer(i); if (layer.Name == "扩展边界") { var fl = layer as IFeatureLayer; fl.FeatureClass = featureClass; return(true); } } return(false); }