日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Server操作Mxd文件详细讲解

發布時間:2023/12/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Server操作Mxd文件详细讲解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Server操作Mxd文件詳細講解

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


下面是根據圖層名刪除圖層的操作
public string removeLayerFromMxd(IServerContext serverContext, layerName,string mxdPath)
? ?? ???{
? ?? ?? ?? ?IMapServer pMapServer = serverContext.ServerObject as IMapServer;
? ?? ?? ?? ?IMapServerObjects pMapServerObjs = pMapServer as IMapServerObjects;
? ?? ?? ?? ?IMap pMap = pMapServerObjs.get_Map(pMapServer.DefaultMapName);
? ?? ?? ?? ?IMapLayers pMapLayers = pMap as IMapLayers;
? ?? ?? ?? ?
? ?? ?? ?? ?ILayer removeLayer = getLayerByName(serverContext, layerName);
? ?? ?? ?? ?if (removeLayer == null)
? ?? ?? ?? ?? ? return "操作失敗,找不到要刪除的圖層";
? ?? ?? ?? ?pMapLayers.DeleteLayer(removeLayer);
? ?? ?? ?? ?//獲取pMapDocument對象
? ?? ?? ?? ?IMxdContents 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;
? ?? ???}


從上面的函數可看出,對添加刪除圖層這些操作,步驟大概就是 獲取圖層對要素集,設置圖層樣式,打開當前地圖,插入圖層,保存Mxd,
所有的這些都是AO的操作,對于習慣使用AE的人來說,這些都很熟悉,唯一不同的是,Server不能用new去創建對象,細心人會發現在創建
地圖文檔的時候,使用的是CreateObject的方式,如下面語句
IMapDocument pMapDocument = serverContext.CreateObject("esriCarto.MapDocument") as IMapDocument;



當然,通過代碼對服務器文件進行讀寫,還必須要注意網絡的安全設置。
首先要確保有足夠的權限對Mxd進行修改。
懶羊羊要指出,所謂的權限,第一是確保你的Server具有足夠的授權。第二,服務器文件必須有足夠的寫入權限。
對于第二點,主要是考慮磁盤格式。網絡開發人員都知道,NTFS格式下面,要訪問文件,必須設置安全屬性。
設置方法如下。
1。在服務器Mxd文件右鍵屬性--點擊"安全"標簽
2.查找soc用戶
3.給soc用戶寫入的權限。
懶羊羊這里只是舉個例子,看官們只管使用自己的soc用戶就行了。

下載 (35.63 KB)
2008-10-22 20:29

下載 (34.97 KB)
2008-10-22 20:29

下載 (36.99 KB)
2008-10-22 20:29


如果是fat格式就不用上述設置,畢竟NTFS格式安全系數比較高。至于Linux,懶羊羊沒有做過,嘗試過的朋友可以發表一下心得體會




還有一點必須主要的,Mxd更改以后,地圖服務是沒有發生變化的,所發布的地圖一直駐留在內存中。因此,這時候需要更新一下地圖服務。
方法有很多,最傻瓜最直接的方法就是跑到機房去重啟mxd所對應的service。但是,懶羊羊比較懶,所以,還是希望通過代碼去控制,update
一下。下面是更新地圖服務的函數
private void updateService(string serviceName,string serverName)
? ? {
? ?? ???ServerConnection pServerConnection = new ESRI.ArcGIS.Server.WebControls.ServerConnection(serverName);//地圖服務機器名
? ?? ???pServerConnection.Connect();
? ?? ???IServerObjectAdmin pServerSOA = pServerConnection.ServerObjectAdmin;
? ?? ???IServerObjectConfiguration pConfig = pServerSOA.GetConfiguration(serviceName, "MapServer");
? ?? ???pServerSOA.UpdateConfiguration(pConfig);
? ? }

細心的朋友應該注意到了,這里面主要是使用了IServerObjectAdmin 接口,IServerObjectAdmin 接口功能十分強大,建議去查一下幫助,
對其加深了解。
下載 (35.46 KB)
2008-10-22 20:42



下載 (155.91 KB)
2008-10-22 20:42



描述
RemarksAny application that runs as a user account in the agsadmin user group on the ArcGIS Server can use the IGISServerConnection interface to connect to the ArcGIS Server and to get a reference to the ServerObjectAdmin. If the user account is not part of the agsadmin user group the ServerObjectAdmin property on IServerConnection will return an error. Applications that are running as accounts that can connect to the server but are not part of the agsadmin user group can use the ServerObjectManager property on IGISServerConnection to get a reference on the ServerObjectManager.
The IServerObjectAdmin interface has the necessary methods for an application to adminstrate both the set of server object configurations and types associated with the server, and to administer aspects of the server itself. The following administration functionality of the ArcGIS Server is exposed by methods and properties on IServerObjectAdmin :
Adminster server object configurations:

  • Add and delete server object configurations
  • Update a server object configuration's properties
  • Start, stop and pause server object configurations
  • Report the status of a server object configuration
  • Get all server object configurations and their properties
  • Get all server object types and their properties

Administer aspects of the server itself:

  • Add and remove server container machines
  • Get all server container machines
  • Add and remove server directories
  • Get all server directories
  • Configure the server's logging properties


實際上,上述的操大多數都是常規的操作,AE程序員都能輕松搞定。但細微的地方還是要注意的,例如Server環境下創建新對象,文件的權限設置等等
對server的一些特性也必須了解。例如mxd更新以后必須重啟服務,確保當前服務與地圖文檔一致,不然就可能導致災難性的出錯。

前面漏掉的一個函數現在補上
/// <summary>
? ? /// 通過圖層名稱返回圖層
? ? /// </summary>
? ? /// <param name="pSOC">地圖控件</param>
? ? /// <param name="LayerName">圖層名稱</param>
? ? /// <returns></returns>
? ? public static ILayer getLayerByName(IServerContext pSOC, string LayerName)
? ? {
? ?? ???IMapServer pMapServer = pSOC.ServerObject as IMapServer;
? ?? ???IMapServerObjects pMapServerObjs = pMapServer as IMapServerObjects;
? ?? ???IMap pMap = pMapServerObjs.get_Map(pMapServer.DefaultMapName);
? ?? ???//獲取所有的圖層
? ?? ???for (int i = 0; i < pMap.LayerCount; i++)
? ?? ???{
? ?? ?? ?? ?ILayer lyr = pMap.get_Layer(i);
? ?? ?? ?? ?if (lyr.Name == LayerName)
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? return lyr;
? ?? ?? ?? ?}
? ?? ?? ?? ?else if (lyr is ICompositeLayer)
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? //圖層為復合圖層,查找其子圖層
? ?? ?? ?? ?? ? ICompositeLayer comLayer = lyr as ICompositeLayer;
? ?? ?? ?? ?? ? for (int j = 0; j < comLayer.Count; j++)
? ?? ?? ?? ?? ? {
? ?? ?? ?? ?? ?? ???ILayer cLayer = comLayer.get_Layer(j);
? ?? ?? ?? ?? ?? ???if (cLayer.Name == layerName)
? ?? ?? ?? ?? ?? ?? ?? ?return cLayer;
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?}
? ?? ???}
? ?? ???return null;
? ? }








轉載于:https://www.cnblogs.com/liuyang-1037/archive/2009/08/11/1543481.html

總結

以上是生活随笔為你收集整理的Server操作Mxd文件详细讲解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。