Using ESRI.ArcGIS.NetworkAnalysis;
public class Clspathfinder
{
Private Igeometricnetwork m_ipgeometricnetwork;
Private IMap M_ipmap;
Private Ipointcollection m_ippoints;
Private Ipointtoeid M_ippointtoeid;
Private double m_dblpathcost = 0;
Private Ienumneteid m_ipenumneteid_junctions;
Private Ienumneteid m_ipenumneteid_edges;
Private Ipolyline M_ippolyline;
#region Public Function
Returns and sets the current map
Public IMap Setorgetmap
{
set{m_ipmap = value;}
Get{return M_ipmap;}
}
Open Network
public void Openfeaturedatasetnetwork (Ifeaturedataset featuredataset)
{
Closeworkspace ();
if (! Initializenetworkandmap (Featuredataset))
Console.WriteLine ("Open Error");
}
The collection of input points
Public Ipointcollection stoppoints
{
set{m_ippoints= value;}
Get{return m_ippoints;}
}
Path cost
Public double Pathcost
{
get {return m_dblpathcost;}
}
return path
Public Ipolyline Pathpolyline ()
{
Ieidinfo Ipeidinfo;
Igeometry Ipgeometry;
if (m_ippolyline!=null) return m_ippolyline;
M_ippolyline = new Polylineclass ();
Igeometrycollection Ipnewgeometrycoll = m_ippolyline as igeometrycollection;
Ispatialreference ipspatialreference = m_ipmap.spatialreference;
Ieidhelper ipeidhelper = new Eidhelperclass ();
Ipeidhelper.geometricnetwork = M_ipgeometricnetwork;
Ipeidhelper.outputspatialreference = ipspatialreference;
Ipeidhelper.returngeometries = true;
Ienumeidinfo ipenumeidinfo = Ipeidhelper.createenumeidinfo (m_ipenumneteid_edges);
int count = Ipenumeidinfo.count;
Ipenumeidinfo.reset ();
for (int i =0;i<count;i++)
{
Ipeidinfo = Ipenumeidinfo.next ();
Ipgeometry = Ipeidinfo.geometry;
Ipnewgeometrycoll.addgeometrycollection (ipgeometry as Igeometrycollection);
}
return m_ippolyline;
}
Resolve Path
public void Solvepath (string weightname)
{
Try
{
int intedgeuserclassid;
int Intedgeuserid;
int intedgeusersubid;
int Intedgeid;
IPoint Ipfoundedgepoint;
Double dbledgepercent;
Used in/*c#
*itraceflowsolvergen replaces Itraceflowsolver
*/
Itraceflowsolvergen iptraceflowsolver = new Traceflowsolverclass () as Itraceflowsolvergen;
Inetsolver ipnetsolver = iptraceflowsolver as Inetsolver;
Inetwork ipnetwork = m_ipgeometricnetwork.network;
Ipnetsolver.sourcenetwork = Ipnetwork;
Inetelements ipnetelements = ipnetwork as inetelements;
Int intcount = M_ippoints.pointcount;
Defines an array of edge flags
iedgeflag[] pedgeflaglist = new Edgeflagclass[intcount];
for (int i = 0;i<intcount; i++)
{
Inetflag Ipnetflag = new Edgeflagclass () as Inetflag;
IPoint Ipedgepoint = M_ippoints.get_point (i);
Find the nearest edge of the input point
M_ippointtoeid.getnearestedge (Ipedgepoint, out Intedgeid,out Ipfoundedgepoint, out dbledgepercent);
Ipnetelements.queryids (Intedgeid, Esrielementtype.esrietedge, out Intedgeuserclassid, out intEdgeUserID,out Intedgeusersubid);
Ipnetflag.userclassid = Intedgeuserclassid;
Ipnetflag.userid = Intedgeuserid;
Ipnetflag.usersubid = Intedgeusersubid;
Iedgeflag ptemp = (iedgeflag) (Ipnetflag as Iedgeflag);
Pedgeflaglist=ptemp;
}
Iptraceflowsolver.putedgeorigins (ref pedgeflaglist);
Inetschema Ipnetschema = ipnetwork as Inetschema;
Inetweight ipnetweight = Ipnetschema.get_weightbyname (weightname);
Inetsolverweights ipnetsolverweights = iptraceflowsolver as inetsolverweights;
Ipnetsolverweights.fromtoedgeweight = ipnetweight;//The weight of the beginning edge
Ipnetsolverweights.tofromedgeweight = ipnetweight;//The weight of the end edge
Object [] Vares =new object[intcount-1];
Get a set of edges and junctions through Findpath
Iptraceflowsolver.findpath (esriflowmethod.esrifmconnected,
Esrishortestpathobjfn.esrispobjfnminsum,
Out M_ipenumneteid_junctions,out m_ipenumneteid_edges, intCount-1, ref vares);
Calculate cost
M_dblpathcost = 0;
for (int i =0;i<vares.length;i++)
{
Double M_va = (double) vares;
M_dblpathcost = M_dblpathcost + M_va;
}
M_ippolyline = null;
}
catch (Exception ex)
{
Console.WriteLine (ex. message);
}
}
#endregion
#region Private Function
Class
private bool Initializenetworkandmap (Ifeaturedataset featuredataset)
{
Ifeatureclasscontainer Ipfeatureclasscontainer;
Ifeatureclass Ipfeatureclass;
Igeodataset Ipgeodataset;
Ilayer IpLayer;
Ifeaturelayer Ipfeaturelayer;
Ienvelope Ipenvelope, Ipmaxenvelope;
Double Dblsearchtol;
Inetworkcollection ipnetworkcollection = Featuredataset as inetworkcollection;
int count = Ipnetworkcollection.geometricnetworkcount;
Get the Geometry network workspace
M_ipgeometricnetwork = ipnetworkcollection.get_geometricnetwork (0);
Inetwork ipnetwork = m_ipgeometricnetwork.network;
if (m_ipmap!=null)
{
M_ipmap = new Mapclass ();
Ipfeatureclasscontainer = M_ipgeometricnetwork as Ifeatureclasscontainer;
Count = Ipfeatureclasscontainer.classcount;
for (int i =0;i<count;i++)
{
Ipfeatureclass = Ipfeatureclasscontainer.get_class (i);
Ipfeaturelayer = new Featurelayerclass ();
Ipfeaturelayer.featureclass = Ipfeatureclass;
M_ipmap.addlayer (Ipfeaturelayer);
}
}
Count = M_ipmap.layercount;
Ipmaxenvelope = new Envelopeclass ();
for (int i =0;i<count;i++)
{
IpLayer = M_ipmap.get_layer (i);
Ipfeaturelayer = IpLayer as Ifeaturelayer;
Ipgeodataset = Ipfeaturelayer as Igeodataset;
Ipenvelope = ipgeodataset.extent;
Ipmaxenvelope.union (Ipenvelope);
}
M_ippointtoeid = new Pointtoeidclass ();
M_ippointtoeid.sourcemap = M_ipmap;
M_ippointtoeid.geometricnetwork = M_ipgeometricnetwork;
Double dblwidth = ipmaxenvelope.width;
Double dblheight = ipmaxenvelope.height;
if (Dblwidth > Dblheight)
Dblsearchtol = dblwidth/100;
Else
Dblsearchtol = dblheight/100;
M_ippointtoeid.snaptolerance = Dblsearchtol;
return true;
}
private void Closeworkspace ()
{
M_ipgeometricnetwork = null;
M_ippoints = null;
M_ippointtoeid = null;
M_ipenumneteid_junctions = null;
M_ipenumneteid_edges = null;
M_ippolyline = null;
}
#endregion
}
}
Note:
Clspathfinder M_ippathfinder;
if (m_ippathfinder==null)//Open Cyberspace
{
M_ippathfinder = new Clspathfinder ();
Ipmap = This.m_ActiveView.FocusMap;
IpLayer = Ipmap.get_layer (0);
Ipfeaturelayer = IpLayer as Ifeaturelayer;
Ipfdb = IpFeatureLayer.FeatureClass.FeatureDataset;
M_ippathfinder.setorgetmap = Ipmap;
M_ippathfinder.openfeaturedatasetnetwork (IPFDB);
}
private void Viewmap_onmousedown (object sender, ESRI. ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvent e)//Get the mouse input point
{
IPoint ipnew;
if (m_ippoints==null)
{
m_ippoints = new Multipointclass ();
M_ippathfinder.stoppoints = m_ippoints;
}
Ipnew = ViewMap.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint (E.X,E.Y);
Object o = Type.Missing;
M_ippoints.addpoint (Ipnew,ref o,ref o);
}
M_ippathfinder.solvepath ("Weight");//Parse path
Ipolyline Ippolyresult = M_ippathfinder.pathpolyline ();//Return Shortest Path
This article originates from the GIS space Station reprint please link form to indicate the source URL: http://www.gissky.net/Article/1606.htm