www.pudn.com > Application_web_serviceCSharp.zip > ToxicLocations.asmx.cs, change:2005-02-23,size:5175b


/* 
 Copyright 1995-2005 ESRI 
 
 All rights reserved under the copyright laws of the United States. 
 
 You may freely redistribute and use this sample code, with or without modification. 
 
 Disclaimer: THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED  
 WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS  
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESRI OR  
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  
 INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY  
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY  
 WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF  
 SUCH DAMAGE. 
 
 For additional information contact: Environmental Systems Research Institute, Inc. 
 
 Attn: Contracts Dept. 
 
 380 New York Street 
 
 Redlands, California, U.S.A. 92373  
 
 Email: contracts@esri.com 
*/ 
using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Web; 
using System.Web.Services; 
using System.Runtime.InteropServices; 
using ESRI.ArcGIS.Geodatabase; 
using ESRI.ArcGIS.Geometry; 
using ESRI.ArcGIS.Location; 
using ESRI.ArcGIS.Server; 
using ESRI.ArcGIS.Server.WebControls; 
using ESRI.ArcGIS.esriSystem; 
 
namespace ToxicLocations 
{ 
	/// <summary> 
	/// Summary description for Service1. 
	/// </summary> 
	public class ToxicSiteLocator : System.Web.Services.WebService 
	{ 
		public ToxicSiteLocator() 
		{ 
			//CODEGEN: This call is required by the ASP.NET Web Services Designer 
			InitializeComponent(); 
		} 
 
		#region Component Designer generated code 
		 
		//Required by the Web Services Designer  
		private IContainer components = null; 
				 
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor. 
		/// </summary> 
		private void InitializeComponent() 
		{ 
		} 
 
		/// <summary> 
		/// Clean up any resources being used. 
		/// </summary> 
		protected override void Dispose( bool disposing ) 
		{ 
			if(disposing && components != null) 
			{ 
				components.Dispose(); 
			} 
			base.Dispose(disposing);		 
		} 
		 
		#endregion 
 
		[WebMethod] 
		public ToxicSite[] FindToxicLocations(string Address, string ZipCode, double Distance) 
		{ 
			using (WebObject webObj = new WebObject()) 
			{ 
				if (Address == null || ZipCode == null || Distance == 0.0) 
					return null; 
 
				ESRI.ArcGIS.Server.WebControls.ServerConnection connection = new ESRI.ArcGIS.Server.WebControls.ServerConnection(); 
				connection.Host = "doug"; 
				connection.Connect(); 
 
				IServerObjectManager som = connection.ServerObjectManager; 
 
				IServerContext sc = som.CreateServerContext("PortlandGC","GeocodeServer"); 
				try 
				{ 
					IServerObject so = sc.ServerObject; 
					IGeocodeServer gc = so as IGeocodeServer; 
 
					IPropertySet ps = sc.CreateObject("esriSystem.PropertySet") as IPropertySet; 
					ps.SetProperty("street",Address); 
					ps.SetProperty("Zone",ZipCode); 
 
					IPropertySet res = gc.GeocodeAddress(ps,null); 
					IPoint gcPoint = res.GetProperty("Shape") as IPoint; 
 
					ISegmentCollection segc = sc.CreateObject("esriGeometry.Polygon") as ISegmentCollection; 
					segc.SetCircle(gcPoint, Distance); 
					IGeometry geom = segc as IGeometry; 
 
					IGeocodeServerObjects gcso = gc as IGeocodeServerObjects; 
					IReferenceDataTables reftabs = gcso.AddressLocator as IReferenceDataTables; 
					IEnumReferenceDataTable enumreftabs = reftabs.Tables; 
					enumreftabs.Reset(); 
					IReferenceDataTable reftab = enumreftabs.Next(); 
					IDatasetName dsname = reftab.Name as IDatasetName; 
					IName wsnm = dsname.WorkspaceName as IName; 
					IFeatureWorkspace fws = wsnm.Open()  as IFeatureWorkspace; 
				 
					IFeatureClass fc = fws.OpenFeatureClass("ToxicSites"); 
					ISpatialFilter sf = sc.CreateObject("esriGeoDatabase.SpatialFilter") as ISpatialFilter; 
					sf.Geometry = geom; 
					sf.GeometryField = fc.ShapeFieldName; 
					sf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; 
 
					IFeatureCursor fcursor = fc.Search(sf, true); 
					webObj.ManageLifetime(fcursor); 
 
					int lName; 
					int lType; 
					IFields flds = fc.Fields; 
					lName = flds.FindField("NAME"); 
					lType = flds.FindField("SITETYPE"); 
 
					ArrayList toxicList = new ArrayList(); 
					IFeature f; 
					while ((f = fcursor.NextFeature()) != null) 
					{ 
						IPoint pt = f.Shape as IPoint; 
						toxicList.Add (new ToxicSite((string) f.get_Value(lName), (string)f.get_Value(lType),pt.X,pt.Y)); 
					} 
 
					// copy the array list collection to the ToxicSite array 
					ToxicSite[] toxicArray = new ToxicSite[toxicList.Count]; 
					toxicList.CopyTo(toxicArray); 
 
					return toxicArray; 
 
					sc.ReleaseContext(); 
				} 
				catch 
				{ 
					sc.ReleaseContext(); 
				} 
 
				return null; 
			} 
		} 
 
	} 
}