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.ArcGISServer;
using ESRI.ArcGIS.ADF.ArcGISServer;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Collections;
/// <summary>
/// findBarrierPath 的摘要说明
/// </summary>
public class findBarrierPath2
{
public findBarrierPath2()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
ESRI.ArcGIS.ADF.Web.UI.WebControls.Map Map1 = new ESRI.ArcGIS.ADF.Web.UI.WebControls.Map();
string sourceLayername = "";
public void doFindPath(string name1, string name2,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(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及PropertySetProperty数组
PropertySet propSet2 = new PropertySet();
PropertySetProperty[] propSetProperty_new2 = new PropertySetProperty[2];
propSet2.PropertyArray = propSetProperty_new2;
//设置propSet结构
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;
//设置终点PropertySet参数
setBarriers2 setbar = new setBarriers2();
PointN[] pntArray = setbar.getPoint();
PropertySet[] prosetBarriers = new PropertySet[20];
for (int i = 0; i < pntArray.Length; i++)
{
PointN point3 = pntArray[i];
//新建propertyset及PropertySetProperty数组
PropertySet propSet3 = new PropertySet();
PropertySetProperty[] propSetProperty_new3 = new PropertySetProperty[1];
propSet3.PropertyArray = propSetProperty_new3;
//设置propSet结构
PropertySetProperty propSetProperty5= new PropertySetProperty();
propSetProperty5.Key = "Shape";
propSetProperty5.Value = point3;
PropertySetProperty propSetProperty6 = new PropertySetProperty();
propSetProperty4.Key = "Name";
propSetProperty4.Value = "b";
propSet3.PropertyArray[0] = propSetProperty5;
//设置障碍
prosetBarriers[i] = propSet3;
}
////新建propertyset及PropertySetProperty数组
//PropertySet propSet3 = new PropertySet();
//PropertySetProperty[] propSetProperty_new3 = new PropertySetProperty[1];
//propSet3.PropertyArray = propSetProperty_new3;
////设置propSet结构
//PropertySetProperty propSetProperty5 = new PropertySetProperty();
//propSetProperty5.Key = "Shape";
//propSetProperty5.Value = point3;
//PropertySetProperty propSetProperty6 = new PropertySetProperty();
//propSetProperty4.Key = "Name";
//propSetProperty4.Value = "b";
//PropertySet[] prosetBarriers = new PropertySet[1];
//propSet3.PropertyArray[0] = propSetProperty5;
////设置障碍
//prosetBarriers[0] = propSet3;
//设置Stops参数
PropertySet[] propSets = new PropertySet[2];
propSets[0] = propSet;
propSets[1] = propSet2;
NAServerPropertySets StopsPropSets = new NAServerPropertySets();
StopsPropSets.PropertySets = propSets;
//PropertySet[] prosetBarriers = new PropertySet[1];
////设置障碍
//prosetBarriers[0] = propSet3;
NAServerPropertySets barriersPropSets = new NAServerPropertySets();
barriersPropSets.PropertySets = prosetBarriers;
routeParams.Stops = StopsPropSets;
routeParams.Barriers=barriersPropSets;
NAServerSolverResults solverResults;
solverParams = routeParams as NAServerSolverParams;
// Response.Write("<script language='javascript' type='text/javascript'>alert(' HELL')</script>");
try
{
//进行分析
solverResults = naServerProxy.Solve(solverParams);
NAServerRouteResults RouteSolverResults = solverResults as NAServerRouteResults;
//显示分析结果
ShowResults(solverResults);
//释放naServerProxy
naServerProxy.Dispose();
}
catch (Exception e)
{
//释放naServerProxy
naServerProxy.Dispose();
// Response.Write("An error has occurred Mesage=" + e.Message);
// Session["error"] = e.Message;
// Response.Write("<script language='javascript' type='text/javascript'>alert('" + e.Message +"' )</script>");
}
}
}
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 = 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;
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 == "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;
}
private PropertySet GeocodeAddress()
{
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;
}
}
点击获取障碍点
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 System.Collections;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools;
using ESRI.ArcGIS.ADF.ArcGISServer;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;
using ESRI.ArcGIS.ADF.Web.DataSources;
/// <summary>
/// setBarriers 的摘要说明
/// </summary>
public class setBarriers2 : IMapServerToolAction
{
//static double x = 0;
//static double y = 0;
ESRI.ArcGIS.ADF.Web.Geometry.Point adfPoint = new ESRI.ArcGIS.ADF.Web.Geometry.Point();
// ArrayList pnt = new ArrayList();
//用于存储在地图上设置的临时障碍点
static PointN[] pnt = new PointN[20];
//临时障碍点的索引
int i = 0;
public setBarriers2()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public PointN[] getPoint()
{
//PointN pnt = new PointN();
//pnt.X = x;
//pnt.Y = y;
return pnt;
}
public void ServerAction(ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs args)
{
//获取map控件
ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)args.Control;
ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality IFun = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)adfMap.GetFunctionality(1);
ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality adfGraphicsMapFunctionality = null;
if (args is PointEventArgs)
{
//转成点
PointEventArgs pointEventArgs = (PointEventArgs)args;
//屏幕点
System.Drawing.Point screenPoint = pointEventArgs.ScreenPoint;
// adfPoint = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(screenPoint.X, screenPoint.Y, adfMap.Extent, IFun.DisplaySettings.ImageDescriptor.Width, IFun.DisplaySettings.ImageDescriptor.Height);
adfPoint = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(screenPoint.X, screenPoint.Y, adfMap.Extent, (int)adfMap.Width.Value, (int)adfMap.Height.Value);
pnt[i] = new PointN();
pnt[i].X = adfPoint.X;
pnt[i].Y = adfPoint.Y;
//x = adfPoint.X;
//y = adfPoint.Y;
//MapFunctionality
foreach (IMapFunctionality mapFunctionality in adfMap.GetFunctionalities())
{
//当Resource为ADFGraphicsResource,ADFGraphicsResource为GraphicsLayer,保存在内存中用显示临时图层
if (mapFunctionality.Resource.Name == "pathLayer")
{
adfGraphicsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)mapFunctionality;
break;
}
}
//从adfGraphicsMapFunctionality获取名为Element Graphics的DataTable
//ElementGraphicsLayers通常用来显示图形元素,例如显示Map中被选择的图形元素。图层并不用来存储属性,而可以存储不同的图形类型。
ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = null;
foreach (System.Data.DataTable dataTable in adfGraphicsMapFunctionality.GraphicsDataSet.Tables)
{
if (dataTable.TableName == "Element Graphics")
{
elementGraphicsLayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dataTable;
break;
}
}
//如果名为Element Graphics的DataTable为null,就新建Element Graphics DataTable添加到adfGraphicsMapFunctionality.GraphicsDataSet中,同时刷新Toc1显示
if (elementGraphicsLayer == null)
{
elementGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
elementGraphicsLayer.TableName = "Element Graphics";
adfGraphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer);
}
//定义标点样式
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol simpleMarkerSymol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
// simpleMarkerSymol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Star;
simpleMarkerSymol.Color = System.Drawing.Color.Green;
simpleMarkerSymol.Width = 10;
//定义标点选中样式
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol simpleSelectedMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
simpleSelectedMarkerSymbol.Color = System.Drawing.Color.Yellow;
simpleSelectedMarkerSymbol.Width = 12;
simpleSelectedMarkerSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Star;
ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adfPoint, simpleMarkerSymol, simpleSelectedMarkerSymbol);
//把标点添加到elementGraphicsLayer
elementGraphicsLayer.Add(graphicElement);
}
//刷新显示
if (adfMap.ImageBlendingMode == ImageBlendingMode.WebTier)
{
//整个地图控件刷新
adfMap.Refresh();
}
else
{
//只刷新部分Resource
adfMap.RefreshResource(adfGraphicsMapFunctionality.Resource.Name);
}
i++;
}
}