First, introduce the corresponding namespace:
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.ADF.Connection;
using ESRI.ArcGIS.ADF.Connection.AGS;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
1. Specify the user used for the connection.
ESRI. ArcGIS. ADF. Identity id = new ESRI. ArcGIS. ADF. Identity ();
Id. UserName = "Administrator ";
Id. Password = "password ";
Id. Domain = "Name of the server computer ";
String agsServerName = "Server IP Address ";
2. Connect to the server.
To connect to GISServer through a program, there are two main objects available: ESRI. ArcGIS. Server. GISServerConnection (implementing the IGISServerConnection2 Interface) and ESRI. ArcGIS. ADF. Connection. AGS. AGSServerConnection. The former is a com object, and the latter is a native. net object.
Let's take a look at ESRI. ArcGIS. Server. GISServerConnection. The usage is as follows:
ESRI. ArcGIS. Server. IGISServerConnection2 pGISSC = new ESRI. ArcGIS. Server. GISServerConnectionClass ();
PGISSC. Connect ("yourservername ");
ESRI. ArcGIS. Server. IServerObjectAdmin pAdm = pGISSC. ServerObjectAdmin;
Note: To successfully obtain the pGISSC. ServerObjectAdmin attribute, you must have a prerequisite that the user running the current AGS program must be a member of the agsadmin group. Then, you can use IServerObjectAdmin to manage the GISServer. If the user running the current AGS program is only a member of The agsuser group, you can only obtain ServerObjectManager and create an AO object through IServerObjectManager, however, the GISServer cannot be managed. If the user running the current AGS program is neither an agsuser nor an agsadmin member, an error will be reported during connect. We can see that the ESRI. ArcGIS. Server. GISServerConnection object cannot explicitly specify the user connected to the GIS Server.
Next let's take a look at ESRI. ArcGIS. ADF. Connection. AGS. AGSServerConnection. This is a. net object in the ADF. It is generally recommended to use this object for connection, because it can specify to use a specific account Identity to connect to the GIS Server, which is the following Identity:
ESRI. ArcGIS. ADF. Identity identity = new ESRI. ArcGIS. ADF. Identity ("username", "password", "domain ");
ESRI. ArcGIS. ADF. Connection. AGS. AGSServerConnection agsconnection = newESRI. ArcGIS. ADF. Connection. AGS. AGSServerConnection ("yourgisservername", identity );
Agsconnection. Connect ();
ESRI. ArcGIS. Server. IServerObjectAdmin pAdm = agsconnection. ServerObjectAdmin;
Similarly, to successfully obtain pAdm, the user specified in Identity must be a member of agsadmin. If your website can run during debugging and "access is denied" after release, check the web first. config identity. If the above code is used, make sure that the user you are using is in the correct user group.
Here I will use the first method, as shown below:
AGSServerConnection agsConn = new AGSServerConnection(agsServerName, id);
try
{
agsConn.Connect();
if (!agsConn.IsConnected)
{
agsConn.Dispose();
return "";
}
}
catch (Exception ex)
{
return "";
}
3. Get the SOM object.
IServerObjectManager som = agsConn. ServerObjectManager;
4. Set the corresponding map name and service type to obtain the context (IServerContext)
To obtain the current severContext. There are two methods to obtain IServerContext:
1), obtained through the current Resorces
UIComponent form = facesContext. getViewRoot (). findComponent (String) paramMap. get ("formId "));
If (form = null)
{
Return;
}
MapControl mapControl = (MapControl) form. findComponent (String) paramMap. get ("mapId "));
If (mapControl = null)
{
Return;
}
WebMap webMap = mapControl. getWebMap ();
WebContext webContext = webMap. getWebContext ();
// Obtain the IServerContext of the current service
AGSLocalMapResource mapResource = (AGSLocalMapResource) webContext. getResources (). get ("ags1 ");
MapServer mapServer = mapResource. getLocalMapServer ();
IServerContext serverContext = mapResource. getServerContext ();
2) get through the IP Link (that is, the method I want to use)
IServerObjectManager som = agsConn. ServerObjectManager;
String servertype = "MapServer ";
String serverobjectname = "GZ"; // Map Name
IServerContext severContext = som. CreateServerContext (serverobjectname, servertype );
IMapServer pMapServer = severContext. ServerObject as IMapServer;
IMapServerObjects pMapServerObjs = pMapServer as IMapServerObjects;
IMap pMap = pMapServerObjs. get_Map (pMapServer. DefaultMapName );
5. Get IMapServerObjects
IMapServer pMapServer = severContext. ServerObject as IMapServer;
IMapServerObjects pMapServerObjs = pMapServer as IMapServerObjects;
6. Get the IMap object through IMapServerObjects
IMap pMap = pMapServerObjs. get_Map (pMapServer. DefaultMapName );
7. Get the layers and attributes to be queried
// Obtain the queried Layer
ILayer workAreaLayer = pMap. get_Layer (7 );
IFeatureLayer loopFeatureLayer = (FeatureLayer) (workAreaLayer );
IFeatureClass loopFeatureClass = loopFeatureLayer. FeatureClass;
8. Set the Filter used for query.
ISpatialFilter spatialFilter = (ISpatialFilter) severContext. CreateObject ("esriGeoDatabase. SpatialFilter ");
String shpFld = loopFeatureClass. ShapeFieldName;
SpatialFilter. GeometryField = shpfield;
// Specify the Space Operation to be used
SpatialFilter. SpatialRel = esriSpatialRelEnum. esriSpatialRelIntersects;
// Create a where expression. Only elements are needed here.
SpatialFilter. WhereClause = "image no. = '" + no + "'";
IQueryFilter queryFilter = new QueryFilterClass ();
QueryFilter = (IQueryFilter) spatialFilter;
9. query by calling Search
IFeatureCursor featureCursor = loopFeatureClass. Search (queryFilter, false );
// The first element returned
IFeature feature = featureCursor. NextFeature ();
10. processing result
IGeometry geo = feature.Shape;
string box = geo.Envelope.XMin.ToString() + "," + geo.Envelope.YMin.ToString();
box += "," + geo.Envelope.XMax.ToString() + "," + geo.Envelope.YMax.ToString();
This Shape is the external rectangle of the first figure in the query result. You can use it according to your own business. Here I want to use the top left and top right coordinate points of the Shape to splice them into WMS.
Reprinted Please note: Tianping water Blog