ArcGIS Server操作Mxd文件详细讲解
Server发布地图都是基于Mxd去发布的,这点与IMS使用axl文件差不多。一般来说,发布后mxd尽可能不要修改,或者在通过使用arcMap进行编辑后在重新发布。修改mxd会导致地图服务发生变化,因此,相对来说是一种危险的操作。但有时客户需要对Mxd进行修改,自定...
- 作者:liyuanxiang来源:liyuanxiang|2009年06月20日
Server发布地图都是基于Mxd去发布的,这点与IMS使用axl文件差不多。一般来说,发布后mxd尽可能不要修改,或者在通过使用arcMap进行编辑后在重新发布。
修改mxd会导致地图服务发生变化,因此,相对来说是一种危险的操作。但有时客户需要对Mxd进行修改,自定义的添加修改图层,并重新发布服务。
当然,这些苛刻的需求server同样可以应付,但懒羊羊还是不建议这样做。方法总是有的,越危险的事也就越有趣。懒羊羊还是跟大家分享一下这方面的心得吧。
下面函数实现添加一个图层到mxd文件,并设置样式。为更好的表达,函数使用返回操作结果的字符串。
/// <summary>
/// 添加图层到Mxd文件
/// </summary>
/// <param name="serverContext">IServerContext</param>
/// <param name="nfc">新图层对应的要素集</param>
/// <param name="groupIndex">复合图层的序号</param>
/// <param name="mxdPath">mxd所在的路径</param>
/// <param name="picPath">用于对图层渲染的图片</param>
/// <returns></returns>
public string addLayerInMxd(IServerContext serverContext, IFeatureClass nfc, int groupIndex, string mxdPath,string picPath)
{
IMapServer pMapServer = serverContext.ServerObject as IMapServer;
IMapServerObjects pMapServerObjs = pMapServer as IMapServerObjects;
IMap pMap = pMapServerObjs.get_Map(pMapServer.DefaultMapName);
bool hasLayer = hasTheLayer(pMap, nfc.AliasName);
if (hasLayer) return "已存在该命名图层,操作未能完成"; //如果图层已经存在了,那就不添加
if (groupIndex >= pMap.LayerCount) return "组合图层序号越界,操作未能完成";
IMapLayers mapLayer = pMap as IMapLayers;
IGroupLayer gLayer = pMap.get_Layer(groupIndex) as IGroupLayer;
IFeatureLayer fl = serverContext.CreateObject("esriCarto.FeatureLayer") as IFeatureLayer;
fl.FeatureClass = nfc;
fl.Name = nfc.AliasName;
//设置样式
ISimpleRenderer pRen = serverContext.CreateObject("esriCarto.SimpleRenderer") as ISimpleRenderer;
IGeoFeatureLayer pGeoLayer = fl as IGeoFeatureLayer;
IPictureMarkerSymbol picMark = serverContext.CreateObject("esriDisplay.PictureMarkerSymbol") as IPictureMarkerSymbol;
picMark.Size = 20;
picMark.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, picPath);
pRen.Symbol = (ISymbol)picMark;
pGeoLayer.Renderer = (IFeatureRenderer)pRen;
mapLayer.InsertLayerInGroup(gLayer, pGeoLayer as ILayer, false, 3);
//获取pMapDocument对象
IMxdContents pMxdC;
pMxdC = pMap as IMxdContents;
IMapDocument pMapDocument = serverContext.CreateObject("esriCarto.MapDocument") as IMapDocument;
pMapDocument.Open(mxdPath, "");
pMapDocument.ReplaceContents(pMxdC);
if (pMapDocument == null) return "文档为空不能完成操作";
//检查地图文档是否是只读
if (pMapDocument.get_IsReadOnly(mxdPath) == true)
{
return "地图文档只读,未能完成操作";
}
//根据相对的路径保存地图文档
pMapDocument.Save(pMapDocument.UsesRelativePaths, false);
return "操作成功";
}
/// <summary>
/// 是否存在layerName为别名的图层
/// </summary>
/// <param name="pMap"></param>
/// <param name="layerName"></param>
/// <returns></returns>
public bool hasTheLayer(IMap pMap, string layerName)
{
for (int i = 0; i < pMap.LayerCount; i++)
{
ILayer pLayer = pMap.get_Layer(i);
if (pLayer.Name == layerName)
return true;
if (pLayer is ICompositeLayer)
{
ICompositeLayer comLayer = pLayer as ICompositeLayer;
for (int j = 0; j < comLayer.Count; j++)
{
ILayer cLayer = comLayer.get_Layer(j);
if (cLayer.Name == layerName)
return true;
}
}
}
return false;
}