arcserver中的最近设施分析

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.ADF.ArcGISServer;
using System.Collections;

/// <summary>
/// findClosestfacility 的摘要说明
/// </summary>
public class findClosestfacility
{
    ESRI.ArcGIS.ADF.Web.UI.WebControls.Map Map1 = new ESRI.ArcGIS.ADF.Web.UI.WebControls.Map();

    string sourceLayername = "";
 public findClosestfacility()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    public void doFindPath(string name1, string name2,string name3, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapcontrl)
    {
        Map1 = mapcontrl;
        sourceLayername = (string)Map1.Page.Session["SourceLayer"];
        //ags的服务器名
        string SERVER_NAME = "cinasoft-yfs";

        //ags里发布的Map Service名
        string ROUTE_SERVICE_NAME = "NDTest2";
        //创建NAServerProxy
        ESRI.ArcGIS.ADF.ArcGISServer.NAServerProxy naServerProxy = NAServerProxy.Create(SERVER_NAME, ROUTE_SERVICE_NAME, null);
        if (naServerProxy == null)
        {
            naServerProxy.Dispose();
            throw (new System.Exception("Error"));
        }
        else
        {
            //获取网络层的名称

          //  string[] nLayers = naServerProxy.GetNALayerNames(esriNAServerLayerType.esriNAServerRouteLayer);
            //
            NAServerSolverParams solverParams = naServerProxy.GetSolverParameters("Closest Facility") as NAServerSolverParams;
                // = naServerProxy.GetSolverParameters(nLayers[0]) as NAServerSolverParams;
          //  NAServerRouteParams routParams = solverParams as NAServerRouteParams;

            //最近设施分析参数
            NAServerClosestFacilityParams closestfacilityParams = solverParams as NAServerClosestFacilityParams;

            //不返回地图
            closestfacilityParams.ReturnMap = false;
            //返回RouteGeometries
            closestfacilityParams.ReturnCFRouteGeometries = true;
            closestfacilityParams.ReturnDirections = true;
            closestfacilityParams.ReturnIncidents = true;
            closestfacilityParams.ReturnFacilities = true;

         //   LoadLocations(solverParams,name1 ,name2,name3);

            //设置设施点1参数
            PointN point = QueryPoint(name1);
            PropertySet propSet = new PropertySet();
            PropertySetProperty[] propSetProperty_new = new PropertySetProperty[2];
            propSet.PropertyArray = propSetProperty_new;

            PropertySetProperty propSetProperty = new PropertySetProperty();
            propSetProperty.Key = "Shape";
            propSetProperty.Value = point;

            PropertySetProperty propSetProperty2 = new PropertySetProperty();
            propSetProperty2.Key = "Name";
            propSetProperty2.Value = name1;

            propSet.PropertyArray[0] = propSetProperty;
            propSet.PropertyArray[1] = propSetProperty2;
            ////设置设施点2参数
            //PointN point1 = QueryPoint(name2);
            //PropertySet propSet1 = new PropertySet();
            //PropertySetProperty[] propSetProperty_new1 = new PropertySetProperty[2];
            //propSet.PropertyArray = propSetProperty_new;

            //PropertySetProperty propSetProperty3 = new PropertySetProperty();
            //propSetProperty3.Key = "Shape";
            //propSetProperty3.Value = point;

            //PropertySetProperty propSetProperty4 = new PropertySetProperty();
            //propSetProperty4.Key = "Name";
            //propSetProperty4.Value = name1;

            //propSet1.PropertyArray[0] = propSetProperty3;
            //propSet1.PropertyArray[1] = propSetProperty4;

            //设置事件PropertySet参数
            PointN point2 = QueryPoint(name3);
            //新建propertyset及PropertySetProperty数组
            PropertySet propSet2 = new PropertySet();
            PropertySetProperty[] propSetProperty_new2 = new PropertySetProperty[2];
            propSet2.PropertyArray = propSetProperty_new2;
            //设置propSet结构
            PropertySetProperty propSetProperty5 = new PropertySetProperty();
            propSetProperty5.Key = "Shape";
            propSetProperty5.Value = point2;
            PropertySetProperty propSetProperty6 = new PropertySetProperty();
            propSetProperty6.Key = "Name";
            propSetProperty6.Value = name2;

            propSet2.PropertyArray[0] = propSetProperty5;
            propSet2.PropertyArray[1] = propSetProperty6;

            //设置Facility参数
            PropertySet[] propSets = new PropertySet[2];
            propSets[0] = propSet;
         //   propSets[1] = propSet1;
            NAServerPropertySets FacilitySets = new NAServerPropertySets();
            FacilitySets.PropertySets = propSets;

            //设置Incident参数
            PropertySet[] propSets2 = new PropertySet[1];
            propSets2[0] = propSet2;
            NAServerPropertySets IncidentSets = new NAServerPropertySets();
            IncidentSets.PropertySets = propSets2;

            closestfacilityParams.Facilities = FacilitySets;
            closestfacilityParams.Incidents = IncidentSets;
  
            NAServerSolverResults solverResults;
            solverParams = closestfacilityParams as NAServerClosestFacilityParams;
            try
            {
                //进行分析
                solverResults = naServerProxy.Solve(solverParams);
                NAServerClosestFacilityResults ClosestFacilityResults = solverResults as NAServerClosestFacilityResults;
                //显示分析结果
                ShowResults(solverResults);
                naServerProxy.Dispose();
            }
            catch (Exception e)
            {
                //释放naServerProxy
                naServerProxy.Dispose();
            }
        }
    }
    private void LoadLocations(NAServerSolverParams solverParams,string name1,string name2,string name3)
    {
        // Geocode Addresses
        //设施
        PropertySet[] propSets = new PropertySet[2];
        propSets[0] = GeocodeAddress(name1);
        propSets[1] = GeocodeAddress(name2);
        //事件
        PropertySet[] propSets2=new PropertySet[1];
        propSets2[0] = GeocodeAddress(name3);

        NAServerPropertySets FacilityPropSets = new NAServerPropertySets();
        FacilityPropSets.PropertySets = propSets;
        NAServerPropertySets IncidentPropSets=new NAServerPropertySets();
        IncidentPropSets.PropertySets = propSets2;

        NAServerClosestFacilityParams closefacilityParams = solverParams as NAServerClosestFacilityParams;
        closefacilityParams.Facilities = FacilityPropSets;
        closefacilityParams.Incidents = IncidentPropSets;
       
    }
    private PropertySet GeocodeAddress(string name)
    {
        PropertySet propSet = new PropertySet();
        PointN pnt=QueryPoint(name);
        //setBarriers setbar = new setBarriers();
        //PointN pnt = setbar.getPoint();
        PropertySetProperty[] propSetProperty_new = new PropertySetProperty[1];
        propSet.PropertyArray[0] = CreatePropertySetProperty("Shape", pnt);
        // propSet.PropertyArray[1] = CreatePropertySetProperty("Name", "barriers");
        propSet.PropertyArray = propSetProperty_new;

        return propSet;
    }
    private PropertySetProperty CreatePropertySetProperty(string key, object value)
    {
        PropertySetProperty propSetProperty = new PropertySetProperty();
        propSetProperty.Key = key;
        propSetProperty.Value = value;
        return propSetProperty;
    }
    public void ShowResults(NAServerSolverResults solverResults)
    {
        NAServerClosestFacilityResults ClosestFacilityResults = solverResults as NAServerClosestFacilityResults;
       
        //设施事件路径显示
        AddRoutesAndStops(ClosestFacilityResults);
        //路径区域全屏显示
        PolylineN polylineN = ClosestFacilityResults.CFRouteGeometries[0] as PolylineN;
        EnvelopeN envelopeN = polylineN.Extent as EnvelopeN;
        double width = envelopeN.XMax - envelopeN.XMin;
        double height = envelopeN.YMax - envelopeN.YMin;
        double fivePercent;
        if (width > height)
        {
            fivePercent = width * .05;
        }
        else
        {
            fivePercent = height * .05;
        }
        envelopeN.XMin = envelopeN.XMin - fivePercent;
        envelopeN.YMin = envelopeN.YMin - fivePercent;
        envelopeN.XMax = envelopeN.XMax + fivePercent;
        envelopeN.YMax = envelopeN.YMax + fivePercent;
        Map1.Extent = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfEnvelope(envelopeN);
        Map1.Refresh();
    }

    private void AddRoutesAndStops(NAServerClosestFacilityResults  rResult)
    {
        //分析结果路径
        Polyline[] lines = rResult.CFRouteGeometries ;
     //  RecordSet stops = rResult.Stops;
        RecordSet Facility = rResult.Facilities;
        RecordSet Incident = rResult.Incidents;
        //获取地图的MapFunctionality

        ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality mapFunct = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)Map1.GetFunctionality("pathLayer");
        //获取地图的MapResource
        ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)mapFunct.Resource;

        //把buffer结果范围进行显示
        ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer glayer = null;
        //查找ElementGraphicsLayer在Buffer中
        foreach (System.Data.DataTable dt in gResource.Graphics.Tables)
        {
            if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)
            {
                glayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;
                break;
            }

        }
        //如果Buffer中没有ElementGraphicsLayer就新增加一个ElementGraphicsLayer
        if (glayer == null)
        {
            glayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
            gResource.Graphics.Tables.Add(glayer);
        }
        //清除ElementGraphicsLayer中的内容
        glayer.Clear();
        ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfPolyline((PolylineN)lines[0]);
        //设置点显示
        ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom, System.Drawing.Color.Red);
        //设置透明度
        ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol sls = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol();
        sls.Width = 3;
        sls.Color = System.Drawing.Color.Red;
        sls.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.LineType.Dash;
        sls.Transparency = 50;
        ge.Symbol = sls;
        // ge.Symbol.Transparency = 50;
        //添加到Buffer中进行显示
        glayer.Add(ge);

       // Record[] stopRecords = stops.Records;
        Record[] FacilityRecords = Facility.Records;
        Record[] IncidentRecords = Incident.Records;
        int facilityCount = FacilityRecords.Length;
        int incidentCount = IncidentRecords.Length;
        for (int iFacility = 0; iFacility < facilityCount; iFacility++)
        {
            ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom2 = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfPoint(FacilityRecords[iFacility].Values[1] as PointN);
            //设置点显示
            ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge2 = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom2, System.Drawing.Color.Red);
            //设置透明度
            ge2.Symbol.Transparency = 50;
            glayer.Add(ge2);
        }

        for (int iIncident = 0; iIncident < incidentCount; iIncident++)
        {
            ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom2 = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfPoint(IncidentRecords[iIncident].Values[1] as PointN);
            //设置点显示
            ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge2 = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom2, System.Drawing.Color.Red);
            //设置透明度

            ge2.Symbol.Transparency = 50;

            //添加到Buffer中进行显示
            glayer.Add(ge2);
        }
    }

    //按名称查找点
    private PointN QueryPoint(string name)
    {
        PointN point = new PointN();
        IEnumerable func_enum = Map1.GetFunctionalities();
        DataTable dt = null;
        foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality gisfunctionality in func_enum)
        {
            if (gisfunctionality.Resource.Name == "NDMetro")
            {
                bool supported = false;
                ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = gisfunctionality.Resource;
                supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));

                if (supported)
                {
                    ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
                    string[] lids;
                    string[] lnames;
                    qfunc.GetQueryableLayers(null, out lids, out lnames);

                    //进行单层分析
                    //取得想要做路径分析的图层的图层
                    int layer_index = 0;
                    for (int i = 0; i < lnames.Length; i++)
                    {
                        if (lnames[i] == sourceLayername)
                        {
                            //if (lids[i] is string)
                            //{
                            //    if (!int.TryParse((string)lids[i], out layer_index))
                            //    {
                            //        layer_index = i;
                            //    }
                            //}
                            //else
                            layer_index = i;
                            break;
                        }
                    }
                    ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
                    spatialfilter.ReturnADFGeometries = false;
                    spatialfilter.MaxRecords = 1;
                    spatialfilter.WhereClause = "NAME LIKE  '%" + name + "%'";
                    spatialfilter.Geometry = Map1.GetFullExtent();
                    //对所有的图层进行分析
                    for (int i = 0; i < lnames.Length; i++)
                    {
                        dt = qfunc.Query(null, lids[i], spatialfilter);
                        if (dt.Rows.Count != 0)
                        {
                            break;
                        }
                    }
                }
            }
        }

        DataRowCollection drs = dt.Rows;

        int shpind = -1;
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            if (dt.Columns[i].DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))
            {
                shpind = i;
                break;
            }
        }
        foreach (DataRow dr in drs)
        {
            ESRI.ArcGIS.ADF.Web.Geometry.Point geom = (ESRI.ArcGIS.ADF.Web.Geometry.Point)dr[shpind];
            //ESRI.ArcGIS.ADF.Web.Geometry.PointCollection points = geom.Points;
            point.X = geom.X;
            point.Y = geom.Y;

        }
        return point;
    }
 

}

原文地址:https://www.cnblogs.com/gisdream/p/1812901.html