Code
1public partial class _Default : System.Web.UI.Page
2{
3 Instance Variable Declarations#region Instance Variable Declarations
4
5 System.Data.DataTable m_layerDataTable;
6
7 #endregion
8
9 ASP.NET Page Life Cycle Event Handlers#region ASP.NET Page Life Cycle Event Handlers
10
11 protected void Page_Init(object sender, System.EventArgs e)
12 {
13 try
14 {
15 // Read the contents of the layers.config file, which is assumed to contained the names and paths of the
16 // shapefiles that can be dynamically added to the map. Store this data in a data table member variable.
17 if (!Page.IsPostBack)
18 {
19 // Create an empty data table to hold the shapefile names and paths
20 m_layerDataTable = new System.Data.DataTable("layerTable");
21 System.Data.DataColumn layerNameDataColumn = new System.Data.DataColumn("layerName");
22 System.Data.DataColumn layerPathDataColumn = new System.Data.DataColumn("layerPath");
23 m_layerDataTable.Columns.Add(layerNameDataColumn);
24 m_layerDataTable.Columns.Add(layerPathDataColumn);
25
26 // Get the root path of the web application and append the name of the file containing the
27 // shapefile list
28 string rootPath = Server.MapPath("~");
29 string layerCatalogPath = string.Format("{0}/layers.catalog", rootPath);
30
31 // Get a reader to access the layer catalog
32 System.IO.StreamReader layerCatalogReader = new System.IO.StreamReader(layerCatalogPath);
33
34 // Initialize the delimiter that separates shapefile names and paths in the layer catalog
35 string layerDelimiterString = "=";
36 char[] layerDelimiter = layerDelimiterString.ToCharArray();
37
38 // Iterate through the lines of the layer catalog, adding each pair of shapefile names and paths
39 // to the data table
40 string[] layerSpecs = null;
41 string currentLine = null;
42 while (layerCatalogReader.Peek() != -1)
43 {
44 currentLine = layerCatalogReader.ReadLine();
45
46 // If the current line does not contain the delimiter, assume the end of the file has been reached
47 if (!currentLine.Contains("="))
48 break;
49
50 // Split the current line into the shapefile name and path and add this data to the data table
51 layerSpecs = currentLine.Split(layerDelimiter);
52 System.Data.DataRow dataRow = m_layerDataTable.NewRow();
53 dataRow[0] = layerSpecs[0];
54 dataRow[1] = layerSpecs[1];
55 m_layerDataTable.Rows.Add(dataRow);
56 }
57 layerCatalogReader.Close();
58 }
59 }
60 catch (System.Exception exception)
61 {
62 // Since the page has not yet rendered, we write the javascript to show an alert containing error info
63 // directly to the page response
64 string jsErrorAlert = string.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception));
65 Response.Write(jsErrorAlert);
66 }
67 }
68
69 protected void Page_PreRender(object sender, System.EventArgs e)
70 {
71 try
72 {
73 // Bind DropDownList to data table containing layer names
74 DropDownList1.DataSource = m_layerDataTable;
75 DropDownList1.DataTextField = "layerName";
76 DropDownList1.DataValueField = "layerPath";
77 DropDownList1.DataBind();
78
79 if (!Page.IsPostBack)
80 {
81 if (Session.IsNewSession)
82 {
83 // On initial page prerender, add session variables to track whether a layer was added and its id
84 Session["dynamicLayerAdded"] = false;
85 Session["addedLayerID"] = null;
86
87 // Get the default map description from the server
88 ESRI.ArcGIS.Server.IServerContext serverContext = this.GetServerContext();
89 ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
90 ESRI.ArcGIS.Carto.IMapServerInfo mapServerInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
91 ESRI.ArcGIS.Carto.IMapDescription aoMapDescription = mapServerInfo.DefaultMapDescription;
92
93 // Create a new map image and display in the page
94 CreateMapImage(serverContext, aoMapDescription);
95
96 serverContext.ReleaseContext();
97 }
98 else
99 {
100 // If this is not the initial page prerender, then the map image must be recreated with the
101 // dynamically added layer, if one has been added. So we create the server context, get the
102 // map description that's holding layer visibility information from session, add the dynamic
103 // layer to the server state, create the map image based on server state, then remove the layer
104 // from server state so it does not remain part of the map service
105 ESRI.ArcGIS.Server.IServerContext serverContext = GetServerContext();
106 ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
107 ESRI.ArcGIS.Carto.IMapServerInfo mapServerInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
108 ESRI.ArcGIS.Carto.IMapDescription aoMapDescription = mapServerInfo.DefaultMapDescription;
109
110 if ((bool)Session["dynamicLayerAdded"])
111 AddSelectedLayer(serverContext);
112
113 CreateMapImage(serverContext, aoMapDescription);
114
115 RemoveDynamicLayer(serverContext);
116 serverContext.ReleaseContext();
117 }
118 }
119 }
120 catch (System.Exception exception)
121 {
122 // Since the page has not yet rendered, we write the javascript to show an alert containing error info
123 // directly to the page response
124 string jsErrorAlert = string.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception));
125 Response.Write(jsErrorAlert);
126 }
127 }
128
129 #endregion
130
131 ASP.NET Web Control Event Handlers#region ASP.NET Web Control Event Handlers
132
133 // Fires when the Add Layer button is clicked
134 protected void AddLayer_Click(object sender, System.EventArgs e)
135 {
136 try
137 {
138 // Set the session variable indicating that a layer has been added during the current session
139 Session["dynamicLayerAdded"] = true;
140
141 // Add the currently selected layer to the map service
142 ESRI.ArcGIS.Server.IServerContext serverContext = GetServerContext();
143 AddSelectedLayer(serverContext);
144
145 // Create a map image. The image will contain the dynamic layer because that layer has been added
146 // to the map service, meaning the default map description will include this layer.
147 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
148 ESRI.ArcGIS.Carto.IMapServerInfo mapServerInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
149 ESRI.ArcGIS.Carto.IMapDescription aoMapDescription = mapServerInfo.DefaultMapDescription;
150 CreateMapImage(serverContext, aoMapDescription);
151
152 // Since we do not want to make a persistent change to the map service, we remove the layer once we
153 // have created an image containing it
154 RemoveDynamicLayer(serverContext);
155
156 serverContext.ReleaseContext();
157 }
158 catch (System.Exception exception)
159 {
160 // Since the page is in a full page postback, we write the javascript to show an alert containing error info
161 // directly to the page response
162 string jsErrorAlert = string.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception));
163 Response.Write(jsErrorAlert);
164 }
165 }
166
167 // Fires when the Change Extent button is clicked
168 protected void ChangeExtent_Click(object sender, System.EventArgs e)
169 {
170 try
171 {
172 // If a dynamic layer has been added during the current session, it needs to be re-added, since we
173 // are not persistently adding the layer to the service
174 ESRI.ArcGIS.Server.IServerContext serverContext = this.GetServerContext();
175
176 if ((bool)Session["dynamicLayerAdded"])
177 AddSelectedLayer(serverContext);
178
179 // Get the map service's description and extent
180 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
181 ESRI.ArcGIS.Carto.IMapServerInfo mapServerInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
182 ESRI.ArcGIS.Carto.IMapDescription mapDescription = mapServerInfo.DefaultMapDescription;
183
184 ESRI.ArcGIS.Carto.IMapArea mapArea = mapDescription.MapArea;
185 ESRI.ArcGIS.Carto.IMapExtent mapExtent = mapArea as ESRI.ArcGIS.Carto.IMapExtent;
186
187 // Create a new envelope and update the service extent with it
188 ESRI.ArcGIS.Geometry.IEnvelope aoEnvelope =
189 (ESRI.ArcGIS.Geometry.IEnvelope)serverContext.CreateObject("esriGeometry.Envelope");
190 aoEnvelope.PutCoords(-120, 30, -50, 50);
191 mapExtent.Extent = aoEnvelope;
192
193 // Create a map image
194 CreateMapImage(serverContext, mapDescription);
195
196 // Since we do not want to make a persistent change to the map service, we remove the layer once we
197 // have created the map image containing it
198 RemoveDynamicLayer(serverContext);
199
200 serverContext.ReleaseContext();
201 }
202 catch (System.Exception exception)
203 {
204 // Since the page has not yet rendered, we write the javascript to show an alert containing error info
205 // directly to the page response
206 string jsErrorAlert = string.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception));
207 Response.Write(jsErrorAlert);
208 }
209 }
210
211 #endregion
212
213 Instance Methods#region Instance Methods
214
215 // Retrieves the server context of the map service specified by the serverName and mapServiceName variables
216 private ESRI.ArcGIS.Server.IServerContext GetServerContext()
217 {
218 string serverName = "localhost";
219 string mapServiceName = "USA_data";
220 ESRI.ArcGIS.Server.IServerObjectManager serverObjectManager;
221
222 // Check whether the session variable storing the ServerObjectManager is null and initialize it if so.
223 // This code only executes once because we only want to create one connection per session.
224 if (Session["SOM"] == null)
225 {
226 // Using ADF connection library
227 ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsServerConnection =
228 new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection();
229 agsServerConnection.Host = serverName;
230 agsServerConnection.Connect();
231
232 serverObjectManager = agsServerConnection.ServerObjectManager;
233 Session["SOM"] = serverObjectManager;
234 }
235 else
236 {
237 serverObjectManager = Session["SOM"] as ESRI.ArcGIS.Server.IServerObjectManager;
238 }
239
240 // Create a map server context with the specified map service name
241 ESRI.ArcGIS.Server.IServerContext serverContext =
242 serverObjectManager.CreateServerContext(mapServiceName, "MapServer");
243
244 return serverContext;
245 }
246
247 // Adds the currently selected layer to the map service
248 private void AddSelectedLayer(ESRI.ArcGIS.Server.IServerContext serverContext)
249 {
250 // Use ArcObjects to get the ArcObjects map underlying the map service
251 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
252 ESRI.ArcGIS.Carto.IMapServerObjects2 mapServerObjects = (ESRI.ArcGIS.Carto.IMapServerObjects2)mapServer;
253 string mapName = mapServer.DefaultMapName;
254 ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapName);
255
256 // Retrieve the currently selected item and parse its value into the shapefile directory path and file name
257 System.Web.UI.WebControls.ListItem listItem = DropDownList1.SelectedItem;
258 int lastSlashIndex = listItem.Value.LastIndexOf("/");
259 string filePath = listItem.Value.Substring(0, lastSlashIndex);
260 string fileName = listItem.Value.Substring(lastSlashIndex + 1);
261
262 // Get a reference to the shapefile directory as an ArcObjects FeatureWorkspace
263 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory =
264 (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory)serverContext.CreateObject("esriDataSourcesFile.ShapefileWorkspaceFactory");
265 ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(filePath, 0);
266 ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
267
268 // Get a reference to the shapefile as a GeoFeatureLayer
269 ESRI.ArcGIS.Carto.IFeatureLayer aoFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)serverContext.CreateObject("esriCarto.FeatureLayer");
270 // Or use the guid for esriCarto.FeatureLayer
271 //IFeatureLayer layer = (IFeatureLayer)in_mapcontext.CreateObject("{E663A651-8AAD-11D0-BEC7-00805F7C4268}");
272 aoFeatureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(fileName);
273 aoFeatureLayer.Name = listItem.Text;
274 ESRI.ArcGIS.Carto.IGeoFeatureLayer geoFeatureLayer = (ESRI.ArcGIS.Carto.IGeoFeatureLayer)aoFeatureLayer;
275
276 // Create an ArcObjects color object with the color set to blue for use by the layer's renderer
277 ESRI.ArcGIS.Display.IRgbColor rgbColor = (ESRI.ArcGIS.Display.IRgbColor)serverContext.CreateObject("esriDisplay.RgbColor");
278 rgbColor.Red = 0;
279 rgbColor.Green = 0;
280 rgbColor.Blue = 210;
281
282 // Set the symbol of the GeoFeatureLayer's renderer to use the color initialized above
283 ESRI.ArcGIS.Carto.ISimpleRenderer aoSimpleRenderer = (ESRI.ArcGIS.Carto.ISimpleRenderer)geoFeatureLayer.Renderer;
284 ESRI.ArcGIS.Geometry.esriGeometryType geometryType = aoFeatureLayer.FeatureClass.ShapeType;
285 if (geometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
286 {
287 ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol =
288 (ESRI.ArcGIS.Display.ISimpleMarkerSymbol)aoSimpleRenderer.Symbol;
289 simpleMarkerSymbol.Color = (ESRI.ArcGIS.Display.IColor)rgbColor;
290 }
291 else if (geometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
292 {
293 ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = (ESRI.ArcGIS.Display.ISimpleLineSymbol)aoSimpleRenderer.Symbol;
294 simpleLineSymbol.Color = (ESRI.ArcGIS.Display.IColor)rgbColor;
295 }
296 else if (geometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
297 {
298 ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = (ESRI.ArcGIS.Display.ISimpleFillSymbol)aoSimpleRenderer.Symbol;
299 simpleFillSymbol.Color = (ESRI.ArcGIS.Display.IColor)rgbColor;
300 }
301 else
302 {
303 throw new System.Exception("No renderer or symbol selected. Shape type undetermined.");
304 }
305
306 // Add the layer to the map service's map
307 aoMap.AddLayer(aoFeatureLayer);
308 mapServerObjects.RefreshServerObjects();
309
310 // Store the map service ID of the layer in session
311 Session["addedLayerID"] = mapServerObjects.get_LayerID(mapName, aoFeatureLayer);
312 }
313
314 // Creates a JPEG image based on the passed-in server context and map description and sets the MapImage web
315 // control to display it
316 private void CreateMapImage(ESRI.ArcGIS.Server.IServerContext serverContext, ESRI.ArcGIS.Carto.IMapDescription aoMapDescription)
317 {
318 ESRI.ArcGIS.Carto.IImageType imageType;
319 ESRI.ArcGIS.Carto.IImageDescription imageDescription;
320 ESRI.ArcGIS.Carto.IImageDisplay imageDisplay;
321
322 imageType = serverContext.CreateObject("esriCarto.ImageType") as ESRI.ArcGIS.Carto.IImageType;
323 imageDescription = serverContext.CreateObject("esriCarto.ImageDescription") as ESRI.ArcGIS.Carto.ImageDescription;
324 imageDisplay = serverContext.CreateObject("esriCarto.ImageDisplay") as ESRI.ArcGIS.Carto.ImageDisplay;
325
326 // Set properties to have the image output a JPEG and refer to it via URL
327 imageType.Format = ESRI.ArcGIS.Carto.esriImageFormat.esriImageJPG;
328 imageType.ReturnType = ESRI.ArcGIS.Carto.esriImageReturnType.esriImageReturnURL;
329
330 // Initialize image height and width based on the dimensions of the MapImage Image control
331 imageDisplay.Height = (int)MapImage.Height.Value;
332 imageDisplay.Width = (int)MapImage.Width.Value;
333 imageDisplay.DeviceResolution = 96;
334
335 imageDescription.Display = imageDisplay;
336 imageDescription.Type = imageType;
337
338 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
339
340 // Create the map image and set the MapImage control to display it
341 ESRI.ArcGIS.Carto.IImageResult mapImage = mapServer.ExportMapImage(aoMapDescription, imageDescription);
342 MapImage.ImageUrl = mapImage.URL;
343 MapImage.Visible = true;
344 }
345
346 // Removes the dynamically added layer from the map service
347 private void RemoveDynamicLayer(ESRI.ArcGIS.Server.IServerContext serverContext)
348 {
349 // Check whether a dynamic layer has been added during the session
350 bool layerAdded = (bool)Session["dynamicLayerAdded"];
351
352 if (layerAdded)
353 {
354 // Retrieve the dynamic layer from the map service and delete it. Note that we know the layer has been
355 // added at index 0, so we can safely delete the layer at this index.
356 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
357 ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = (ESRI.ArcGIS.Carto.IMapServerObjects)mapServer;
358 ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);
359 ESRI.ArcGIS.Carto.ILayer aoLayer = aoMap.get_Layer(0);
360
361 aoMap.DeleteLayer(aoLayer);
362 mapServerObjects.RefreshServerObjects();
363 }
364 }
365
366 #endregion
367}
1public partial class _Default : System.Web.UI.Page
2{
3 Instance Variable Declarations#region Instance Variable Declarations
4
5 System.Data.DataTable m_layerDataTable;
6
7 #endregion
8
9 ASP.NET Page Life Cycle Event Handlers#region ASP.NET Page Life Cycle Event Handlers
10
11 protected void Page_Init(object sender, System.EventArgs e)
12 {
13 try
14 {
15 // Read the contents of the layers.config file, which is assumed to contained the names and paths of the
16 // shapefiles that can be dynamically added to the map. Store this data in a data table member variable.
17 if (!Page.IsPostBack)
18 {
19 // Create an empty data table to hold the shapefile names and paths
20 m_layerDataTable = new System.Data.DataTable("layerTable");
21 System.Data.DataColumn layerNameDataColumn = new System.Data.DataColumn("layerName");
22 System.Data.DataColumn layerPathDataColumn = new System.Data.DataColumn("layerPath");
23 m_layerDataTable.Columns.Add(layerNameDataColumn);
24 m_layerDataTable.Columns.Add(layerPathDataColumn);
25
26 // Get the root path of the web application and append the name of the file containing the
27 // shapefile list
28 string rootPath = Server.MapPath("~");
29 string layerCatalogPath = string.Format("{0}/layers.catalog", rootPath);
30
31 // Get a reader to access the layer catalog
32 System.IO.StreamReader layerCatalogReader = new System.IO.StreamReader(layerCatalogPath);
33
34 // Initialize the delimiter that separates shapefile names and paths in the layer catalog
35 string layerDelimiterString = "=";
36 char[] layerDelimiter = layerDelimiterString.ToCharArray();
37
38 // Iterate through the lines of the layer catalog, adding each pair of shapefile names and paths
39 // to the data table
40 string[] layerSpecs = null;
41 string currentLine = null;
42 while (layerCatalogReader.Peek() != -1)
43 {
44 currentLine = layerCatalogReader.ReadLine();
45
46 // If the current line does not contain the delimiter, assume the end of the file has been reached
47 if (!currentLine.Contains("="))
48 break;
49
50 // Split the current line into the shapefile name and path and add this data to the data table
51 layerSpecs = currentLine.Split(layerDelimiter);
52 System.Data.DataRow dataRow = m_layerDataTable.NewRow();
53 dataRow[0] = layerSpecs[0];
54 dataRow[1] = layerSpecs[1];
55 m_layerDataTable.Rows.Add(dataRow);
56 }
57 layerCatalogReader.Close();
58 }
59 }
60 catch (System.Exception exception)
61 {
62 // Since the page has not yet rendered, we write the javascript to show an alert containing error info
63 // directly to the page response
64 string jsErrorAlert = string.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception));
65 Response.Write(jsErrorAlert);
66 }
67 }
68
69 protected void Page_PreRender(object sender, System.EventArgs e)
70 {
71 try
72 {
73 // Bind DropDownList to data table containing layer names
74 DropDownList1.DataSource = m_layerDataTable;
75 DropDownList1.DataTextField = "layerName";
76 DropDownList1.DataValueField = "layerPath";
77 DropDownList1.DataBind();
78
79 if (!Page.IsPostBack)
80 {
81 if (Session.IsNewSession)
82 {
83 // On initial page prerender, add session variables to track whether a layer was added and its id
84 Session["dynamicLayerAdded"] = false;
85 Session["addedLayerID"] = null;
86
87 // Get the default map description from the server
88 ESRI.ArcGIS.Server.IServerContext serverContext = this.GetServerContext();
89 ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
90 ESRI.ArcGIS.Carto.IMapServerInfo mapServerInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
91 ESRI.ArcGIS.Carto.IMapDescription aoMapDescription = mapServerInfo.DefaultMapDescription;
92
93 // Create a new map image and display in the page
94 CreateMapImage(serverContext, aoMapDescription);
95
96 serverContext.ReleaseContext();
97 }
98 else
99 {
100 // If this is not the initial page prerender, then the map image must be recreated with the
101 // dynamically added layer, if one has been added. So we create the server context, get the
102 // map description that's holding layer visibility information from session, add the dynamic
103 // layer to the server state, create the map image based on server state, then remove the layer
104 // from server state so it does not remain part of the map service
105 ESRI.ArcGIS.Server.IServerContext serverContext = GetServerContext();
106 ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
107 ESRI.ArcGIS.Carto.IMapServerInfo mapServerInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
108 ESRI.ArcGIS.Carto.IMapDescription aoMapDescription = mapServerInfo.DefaultMapDescription;
109
110 if ((bool)Session["dynamicLayerAdded"])
111 AddSelectedLayer(serverContext);
112
113 CreateMapImage(serverContext, aoMapDescription);
114
115 RemoveDynamicLayer(serverContext);
116 serverContext.ReleaseContext();
117 }
118 }
119 }
120 catch (System.Exception exception)
121 {
122 // Since the page has not yet rendered, we write the javascript to show an alert containing error info
123 // directly to the page response
124 string jsErrorAlert = string.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception));
125 Response.Write(jsErrorAlert);
126 }
127 }
128
129 #endregion
130
131 ASP.NET Web Control Event Handlers#region ASP.NET Web Control Event Handlers
132
133 // Fires when the Add Layer button is clicked
134 protected void AddLayer_Click(object sender, System.EventArgs e)
135 {
136 try
137 {
138 // Set the session variable indicating that a layer has been added during the current session
139 Session["dynamicLayerAdded"] = true;
140
141 // Add the currently selected layer to the map service
142 ESRI.ArcGIS.Server.IServerContext serverContext = GetServerContext();
143 AddSelectedLayer(serverContext);
144
145 // Create a map image. The image will contain the dynamic layer because that layer has been added
146 // to the map service, meaning the default map description will include this layer.
147 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
148 ESRI.ArcGIS.Carto.IMapServerInfo mapServerInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
149 ESRI.ArcGIS.Carto.IMapDescription aoMapDescription = mapServerInfo.DefaultMapDescription;
150 CreateMapImage(serverContext, aoMapDescription);
151
152 // Since we do not want to make a persistent change to the map service, we remove the layer once we
153 // have created an image containing it
154 RemoveDynamicLayer(serverContext);
155
156 serverContext.ReleaseContext();
157 }
158 catch (System.Exception exception)
159 {
160 // Since the page is in a full page postback, we write the javascript to show an alert containing error info
161 // directly to the page response
162 string jsErrorAlert = string.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception));
163 Response.Write(jsErrorAlert);
164 }
165 }
166
167 // Fires when the Change Extent button is clicked
168 protected void ChangeExtent_Click(object sender, System.EventArgs e)
169 {
170 try
171 {
172 // If a dynamic layer has been added during the current session, it needs to be re-added, since we
173 // are not persistently adding the layer to the service
174 ESRI.ArcGIS.Server.IServerContext serverContext = this.GetServerContext();
175
176 if ((bool)Session["dynamicLayerAdded"])
177 AddSelectedLayer(serverContext);
178
179 // Get the map service's description and extent
180 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
181 ESRI.ArcGIS.Carto.IMapServerInfo mapServerInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
182 ESRI.ArcGIS.Carto.IMapDescription mapDescription = mapServerInfo.DefaultMapDescription;
183
184 ESRI.ArcGIS.Carto.IMapArea mapArea = mapDescription.MapArea;
185 ESRI.ArcGIS.Carto.IMapExtent mapExtent = mapArea as ESRI.ArcGIS.Carto.IMapExtent;
186
187 // Create a new envelope and update the service extent with it
188 ESRI.ArcGIS.Geometry.IEnvelope aoEnvelope =
189 (ESRI.ArcGIS.Geometry.IEnvelope)serverContext.CreateObject("esriGeometry.Envelope");
190 aoEnvelope.PutCoords(-120, 30, -50, 50);
191 mapExtent.Extent = aoEnvelope;
192
193 // Create a map image
194 CreateMapImage(serverContext, mapDescription);
195
196 // Since we do not want to make a persistent change to the map service, we remove the layer once we
197 // have created the map image containing it
198 RemoveDynamicLayer(serverContext);
199
200 serverContext.ReleaseContext();
201 }
202 catch (System.Exception exception)
203 {
204 // Since the page has not yet rendered, we write the javascript to show an alert containing error info
205 // directly to the page response
206 string jsErrorAlert = string.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception));
207 Response.Write(jsErrorAlert);
208 }
209 }
210
211 #endregion
212
213 Instance Methods#region Instance Methods
214
215 // Retrieves the server context of the map service specified by the serverName and mapServiceName variables
216 private ESRI.ArcGIS.Server.IServerContext GetServerContext()
217 {
218 string serverName = "localhost";
219 string mapServiceName = "USA_data";
220 ESRI.ArcGIS.Server.IServerObjectManager serverObjectManager;
221
222 // Check whether the session variable storing the ServerObjectManager is null and initialize it if so.
223 // This code only executes once because we only want to create one connection per session.
224 if (Session["SOM"] == null)
225 {
226 // Using ADF connection library
227 ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsServerConnection =
228 new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection();
229 agsServerConnection.Host = serverName;
230 agsServerConnection.Connect();
231
232 serverObjectManager = agsServerConnection.ServerObjectManager;
233 Session["SOM"] = serverObjectManager;
234 }
235 else
236 {
237 serverObjectManager = Session["SOM"] as ESRI.ArcGIS.Server.IServerObjectManager;
238 }
239
240 // Create a map server context with the specified map service name
241 ESRI.ArcGIS.Server.IServerContext serverContext =
242 serverObjectManager.CreateServerContext(mapServiceName, "MapServer");
243
244 return serverContext;
245 }
246
247 // Adds the currently selected layer to the map service
248 private void AddSelectedLayer(ESRI.ArcGIS.Server.IServerContext serverContext)
249 {
250 // Use ArcObjects to get the ArcObjects map underlying the map service
251 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
252 ESRI.ArcGIS.Carto.IMapServerObjects2 mapServerObjects = (ESRI.ArcGIS.Carto.IMapServerObjects2)mapServer;
253 string mapName = mapServer.DefaultMapName;
254 ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapName);
255
256 // Retrieve the currently selected item and parse its value into the shapefile directory path and file name
257 System.Web.UI.WebControls.ListItem listItem = DropDownList1.SelectedItem;
258 int lastSlashIndex = listItem.Value.LastIndexOf("/");
259 string filePath = listItem.Value.Substring(0, lastSlashIndex);
260 string fileName = listItem.Value.Substring(lastSlashIndex + 1);
261
262 // Get a reference to the shapefile directory as an ArcObjects FeatureWorkspace
263 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory =
264 (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory)serverContext.CreateObject("esriDataSourcesFile.ShapefileWorkspaceFactory");
265 ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(filePath, 0);
266 ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
267
268 // Get a reference to the shapefile as a GeoFeatureLayer
269 ESRI.ArcGIS.Carto.IFeatureLayer aoFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)serverContext.CreateObject("esriCarto.FeatureLayer");
270 // Or use the guid for esriCarto.FeatureLayer
271 //IFeatureLayer layer = (IFeatureLayer)in_mapcontext.CreateObject("{E663A651-8AAD-11D0-BEC7-00805F7C4268}");
272 aoFeatureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(fileName);
273 aoFeatureLayer.Name = listItem.Text;
274 ESRI.ArcGIS.Carto.IGeoFeatureLayer geoFeatureLayer = (ESRI.ArcGIS.Carto.IGeoFeatureLayer)aoFeatureLayer;
275
276 // Create an ArcObjects color object with the color set to blue for use by the layer's renderer
277 ESRI.ArcGIS.Display.IRgbColor rgbColor = (ESRI.ArcGIS.Display.IRgbColor)serverContext.CreateObject("esriDisplay.RgbColor");
278 rgbColor.Red = 0;
279 rgbColor.Green = 0;
280 rgbColor.Blue = 210;
281
282 // Set the symbol of the GeoFeatureLayer's renderer to use the color initialized above
283 ESRI.ArcGIS.Carto.ISimpleRenderer aoSimpleRenderer = (ESRI.ArcGIS.Carto.ISimpleRenderer)geoFeatureLayer.Renderer;
284 ESRI.ArcGIS.Geometry.esriGeometryType geometryType = aoFeatureLayer.FeatureClass.ShapeType;
285 if (geometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
286 {
287 ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol =
288 (ESRI.ArcGIS.Display.ISimpleMarkerSymbol)aoSimpleRenderer.Symbol;
289 simpleMarkerSymbol.Color = (ESRI.ArcGIS.Display.IColor)rgbColor;
290 }
291 else if (geometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
292 {
293 ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = (ESRI.ArcGIS.Display.ISimpleLineSymbol)aoSimpleRenderer.Symbol;
294 simpleLineSymbol.Color = (ESRI.ArcGIS.Display.IColor)rgbColor;
295 }
296 else if (geometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
297 {
298 ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = (ESRI.ArcGIS.Display.ISimpleFillSymbol)aoSimpleRenderer.Symbol;
299 simpleFillSymbol.Color = (ESRI.ArcGIS.Display.IColor)rgbColor;
300 }
301 else
302 {
303 throw new System.Exception("No renderer or symbol selected. Shape type undetermined.");
304 }
305
306 // Add the layer to the map service's map
307 aoMap.AddLayer(aoFeatureLayer);
308 mapServerObjects.RefreshServerObjects();
309
310 // Store the map service ID of the layer in session
311 Session["addedLayerID"] = mapServerObjects.get_LayerID(mapName, aoFeatureLayer);
312 }
313
314 // Creates a JPEG image based on the passed-in server context and map description and sets the MapImage web
315 // control to display it
316 private void CreateMapImage(ESRI.ArcGIS.Server.IServerContext serverContext, ESRI.ArcGIS.Carto.IMapDescription aoMapDescription)
317 {
318 ESRI.ArcGIS.Carto.IImageType imageType;
319 ESRI.ArcGIS.Carto.IImageDescription imageDescription;
320 ESRI.ArcGIS.Carto.IImageDisplay imageDisplay;
321
322 imageType = serverContext.CreateObject("esriCarto.ImageType") as ESRI.ArcGIS.Carto.IImageType;
323 imageDescription = serverContext.CreateObject("esriCarto.ImageDescription") as ESRI.ArcGIS.Carto.ImageDescription;
324 imageDisplay = serverContext.CreateObject("esriCarto.ImageDisplay") as ESRI.ArcGIS.Carto.ImageDisplay;
325
326 // Set properties to have the image output a JPEG and refer to it via URL
327 imageType.Format = ESRI.ArcGIS.Carto.esriImageFormat.esriImageJPG;
328 imageType.ReturnType = ESRI.ArcGIS.Carto.esriImageReturnType.esriImageReturnURL;
329
330 // Initialize image height and width based on the dimensions of the MapImage Image control
331 imageDisplay.Height = (int)MapImage.Height.Value;
332 imageDisplay.Width = (int)MapImage.Width.Value;
333 imageDisplay.DeviceResolution = 96;
334
335 imageDescription.Display = imageDisplay;
336 imageDescription.Type = imageType;
337
338 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
339
340 // Create the map image and set the MapImage control to display it
341 ESRI.ArcGIS.Carto.IImageResult mapImage = mapServer.ExportMapImage(aoMapDescription, imageDescription);
342 MapImage.ImageUrl = mapImage.URL;
343 MapImage.Visible = true;
344 }
345
346 // Removes the dynamically added layer from the map service
347 private void RemoveDynamicLayer(ESRI.ArcGIS.Server.IServerContext serverContext)
348 {
349 // Check whether a dynamic layer has been added during the session
350 bool layerAdded = (bool)Session["dynamicLayerAdded"];
351
352 if (layerAdded)
353 {
354 // Retrieve the dynamic layer from the map service and delete it. Note that we know the layer has been
355 // added at index 0, so we can safely delete the layer at this index.
356 ESRI.ArcGIS.Carto.IMapServer mapServer = (ESRI.ArcGIS.Carto.IMapServer)serverContext.ServerObject;
357 ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = (ESRI.ArcGIS.Carto.IMapServerObjects)mapServer;
358 ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);
359 ESRI.ArcGIS.Carto.ILayer aoLayer = aoMap.get_Layer(0);
360
361 aoMap.DeleteLayer(aoLayer);
362 mapServerObjects.RefreshServerObjects();
363 }
364 }
365
366 #endregion
367}