您的位置:首页 > 其它

【转】ArcGIS Server 9.3 最短路径分析

2009-11-05 19:57 453 查看
要做网络分析,首先你的arcgis server需要具有network analysis 扩展模块的license。其次需要网络数据集,这个问题好解决,直接使用安装目录下的ArcGIS"DeveloperKit"SamplesNET"Server"data"SanFrancisco下的网络数据集将其发布,发布时记得勾上NetWork Anaysis 这项。

然后新建一个网站,加上常用的map,mapresourcemanager,toolbar,toc 控件等,在mapresourcemanager中加入两个资源,一个是graphicslayer类型,命名为 pathLayer,一个为ArcGIS Server Local类型,命名为SanFrancisco,并加上两个textbox和一个按钮,布置好后如图1。

首先在前台的代码如下:

//函数search()即为所加按钮的onclick对应的函数。

<script type="text/javascript">

function search()

{

//起点的名称

var v1=document.getElementById("Text1").value;

//终点的名称

var v2=document.getElementById("Text2").value;

var argument = "ControlID=Map1&ControlType=Map&Type=findPath&p1="+v1+"&p2="+v2;

var context = "Map";

<%= m_Callback %>;

}

function processCallbackError()

{

alert(66);

}

</script>

后台还是callback机制来实现的。代码如下:

public partial class _Default : System.Web.UI.Page,ICallbackEventHandler

{

public string m_Callback = String.Empty;

public string smapstring = String.Empty;

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

m_Callback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "processCallbackResult", "context", "processCallbackError", true);

}

#region ICallbackEventHandler 成员

public string GetCallbackResult()

{

return smapstring;

}

public void RaiseCallbackEvent(string eventArgument)

{

//请求字符串

NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(eventArgument);

if (keyValColl["Type"].ToString() == "findPath")

{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

//起点名称

string Input1 = keyValColl["p1"];

//终点名称

string Input2 = keyValColl["p2"];

//路径分析

doFindPath(Input1, Input2);

}

}

#endregion

//查询最短路径的主体函数

private void doFindPath(string name1, string name2)

{

//ags的服务器名

string SERVER_NAME = "ZHOUWEN";

//ags里发布的Map Service名

string ROUTE_SERVICE_NAME = "SanFrancisco";

//创建NAServerProxy

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(nLayers[0]) as NAServerSolverParams;

//路由分析参数

NAServerRouteParams routeParams = solverParams as NAServerRouteParams;

//不返回地图

routeParams.ReturnMap = false;

//返回RouteGeometries

routeParams.ReturnRouteGeometries = true;

routeParams.ReturnStops = true;

routeParams.ReturnDirections = true;

//设置起点PropertySet参数

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;

//设置终点PropertySet参数

PointN point2 = QueryPoint(name2);

PropertySet propSet2 = new PropertySet();

PropertySetProperty[] propSetProperty_new2 = new PropertySetProperty[2];

propSet2.PropertyArray = propSetProperty_new2;

PropertySetProperty propSetProperty3 = new PropertySetProperty();

propSetProperty3.Key = "Shape";

propSetProperty3.Value = point2;

PropertySetProperty propSetProperty4 = new PropertySetProperty();

propSetProperty4.Key = "Name";

propSetProperty4.Value = name2;

propSet2.PropertyArray[0] = propSetProperty3;

propSet2.PropertyArray[1] = propSetProperty4;

//设置Stops参数

PropertySet[] propSets = new PropertySet[2];

propSets[0] = propSet;

propSets[1] = propSet2;

NAServerPropertySets StopsPropSets = new NAServerPropertySets();

StopsPropSets.PropertySets = propSets;

routeParams.Stops = StopsPropSets;

NAServerSolverResults solverResults;

try

{

//进行分析

solverResults = naServerProxy.Solve(solverParams);

NAServerRouteResults RouteSolverResults = solverResults as NAServerRouteResults;

//显示分析结果

ShowResults(solverResults);

//释放naServerProxy

naServerProxy.Dispose();

}

catch (Exception e)

{

//释放naServerProxy

naServerProxy.Dispose();

}

}

smapstring = Map1.CallbackResults.ToString();

}

//在地图上展示最短路径

public void ShowResults(NAServerSolverResults solverResults)

{

NAServerRouteResults RouteSolverResults = solverResults as NAServerRouteResults;

//开始点终点路径显示

AddRoutesAndStops(RouteSolverResults);

//路径区域全屏显示

PolylineN polylineN = RouteSolverResults.RouteGeometries[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(NAServerRouteResults rResult)
{
//分析结果路径
Polyline[] lines = rResult.RouteGeometries;
RecordSet stops = rResult.Stops;
//获取Buffer的MapFunctionality
ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality mapFunct = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)Map1.GetFunctionality("pathLayer");
//获取Buffer的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 = 5;
sls.Color = System.Drawing.Color.Blue;
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;
int stopCount = stopRecords.Length;
for (int iStop = 0; iStop < stopCount; iStop++)
{

ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom2 = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.

ToAdfPoint(stopRecords[iStop].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 == "SanFrancisco")
{
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);
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();
dt = qfunc.Query(null, lids[3], spatialfilter);
//lnames名称
//[0]: "Stops"
//[1]: "Barriers"
//[2]: "Routes"
//[3]: "Facilities"
//[4]: "Incidents"
//[5]: "Barriers"
//[6]: "Routes"
//[7]: "Facilities"
//[8]: "Barriers"
//[9]: "Lines"
//[10]: "Polygons"
//[11]: "Hospital"
//[12]: "bayareamultiroutestops"
//[13]: "bayareaincident"
//[14]: "bayareafacilities"
//[15]: "HwySt"
//[16]: "MajorSt"
//[17]: "Streets"
//[18]: "Lakes"
//[19]: "Parks"
//[20]: "ShoreLine"

}

}
}

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: