Server releases maps based on mxd, which is similar to the Axl file used by IMS. In general, do not modify mxd after publishing, or re-publish after using ArcMAP for editing.
Modifying the mxd will change the map service. Therefore, it is a dangerous operation. However, sometimes you need to modify the mxd, add and modify layers, and re-release the service.
Of course, server can cope with these demanding requirements, but it is not recommended to do so. There are always methods, and the more dangerous the problem is, the more interesting it is. I 'd like to share my experiences in this area with you.
The following function adds a layer to the mxd file and sets the style. For better expression, the function uses a string that returns the operation result.
/// <Summary>
/// Add a layer to the mxd File
/// </Summary>
/// <Param name = "servercontext"> iservercontext </param>
/// <Param name = "NFC"> element set corresponding to the new layer </param>
/// <Param name = "groupindex"> serial number of the composite layer </param>
/// <Param name = "mxdpath"> mxd path </param>
/// <Param name = "picpath"> image used for layer rendering </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 "This named layer already exists, the operation cannot be completed"; // If the layer already exists, do not add
If (groupindex> = pmap. layercount) Return "the serial number of the composite layer is out of bounds and the operation cannot be completed ";
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;
// Set the style
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 );
// Obtain the pmapdocument object
Imxdcontents pmxdc;
Pmxdc = pmap as imxdcontents;
Imapdocument pmapdocument = servercontext. Createobject ("esricarto. mapdocument") as imapdocument;
Pmapdocument. Open (mxdpath ,"");
Pmapdocument. replacecontents (pmxdc );
If (pmapdocument = NULL) Return "operations cannot be completed if the document is empty ";
// Check whether the map document is read-only
If (pmapdocument. get_isreadonly (mxdpath) = true)
{
Return "the map document is read-only and cannot be completed ";
}
// Save the map document based on the relative path
Pmapdocument. Save (pmapdocument. usesrelativepaths, false );
Return "operation successful ";
}
/// <Summary>
/// Whether a layer with the alias layername exists
/// </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;
}
// The following operations are performed to delete a layer based on the layer name.
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 "operation failed. The layer to be deleted cannot be found ";
Pmaplayers. deletelayer (removelayer );
// Obtain the pmapdocument object
Imxdcontents pmxdc = pmap as imxdcontents ;;
Imapdocument pmapdocument = servercontext. Createobject ("esricarto. mapdocument") as imapdocument;
Pmapdocument. Open (mxdpath ,"");
Pmapdocument. replacecontents (pmxdc );
If (pmapdocument = NULL) Return "operation failed, map document is empty ";
// Check whether the map document is read-only
If (pmapdocument. get_isreadonly (mxdpath) = true)
{
Return "operation failed, map document read-only ";
}
// Save the map document based on the relative path
Pmapdocument. Save (pmapdocument. usesrelativepaths, false );
Return "operation successful ";
}
/// <Summary>
/// Whether a layer with the alias layername exists
/// </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;
}
From the above function, we can see that the steps for adding or deleting layers are to obtain the layer pair Element Set, set the layer style, open the current map, insert the layer, and save the mxd,
All these are AO operations. For those who are used to using AE, these operations are very familiar. The only difference is that the server cannot use new to create objects.
The Createobject method is used for map documents, as shown in the following statement.
Imapdocument pmapdocument = servercontext. Createobject ("esricarto. mapdocument") as imapdocument;
Of course, you must pay attention to the network security settings when using code to read and write server files.
Make sure that you have sufficient permissions to modify the mxd.
Lazy goat should point out that the first thing to say is to ensure that your server has sufficient permissions. Second, the server file must have sufficient write permissions.
For the second point, we mainly consider the disk format. Network developers know that security attributes must be set to access files in the NTFS format.
The setting method is as follows.
1. Right-click Properties in the mxd file on the server and click the "Security" tab.
2. Find SOC users
3. write permissions to SOC users.
Here is just an example of lazy goat. Just use their own SOC users.
If it is a fat format, the above settings are not required. After all, the security factor of the NTFS format is relatively high. As for Linux, lazy goat has never done it before. If you have tried it, you can share your experiences.
Another point is important. After mxd is changed, the map service does not change, and the published map remains in the memory. Therefore, you need to update the map service.
There are many methods. The most direct method for Dummies is to run to the data center to restart the service corresponding to mxd. However, lazy goat is relatively lazy, so we still want to control it through code, update
. The following are functions for Updating the map service.
Private void UpdateService (string servicename, string servername)
{
Serverconnection pserverconnection = new ESRI. ArcGIS. server. webcontrols. serverconnection (servername); // map service machine name
Pserverconnection. Connect ();
Iserverobjectadmin pserversoa = pserverconnection. serverobjectadmin;
Iserverobjectconfiguration pconfig = pserversoa. getconfiguration (servicename, "mapserver ");
// Pserversoa. updateconfiguration (pconfig );
Pserverobjectadmin. stopconfiguration (pconfig. Name, pconfig. typename );
Pserverobjectadmin. startconfiguration (pconfig. Name, pconfig. typename );
}
Careful friends should have noticed that the iserverobjectadmin interface is mainly used, and the iserverobjectadmin interface is very powerful. It is recommended to check the help and learn more about it.
Description
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 mappings 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 invocations
Update a Server Object configuration's properties
Start, stop and pause Server Object deployments
Report the status of a server object Configuration
Get all Server Object invocations 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
In fact, most of the above operations are conventional operations, which can be easily done by AE programmers. However, you should pay attention to the nuances, such as creating new objects and setting file permissions in the server environment.
You must also understand some server features. For example, after the mxd is updated, you must restart the Service to ensure that the current service is consistent with the map document. Otherwise, a catastrophic error may occur.
The missing function is now supplemented.
/// <Summary>
/// Return the layer by layer name
/// </Summary>
/// <Param name = "psoc"> map control </param>
/// <Param name = "layername"> layer name </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 );
// Obtain all layers
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)
{
// The layer is a composite layer. Find its child layers.
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;
}
This article comes from the GIS space station. Web: http://www.gissky.net/Article/1641_5.htm