hi,
I want to try DijkstraShortestPathFinder, I construct the graphic via a Esri
shape file. unfortunately I found the return of the pathfinder is always
"null", when i debug the code, I found the calulate()function always return
very quickly, since the DijkstraIterator.DijkstraNode.cost== Double.maxvalue is
true.
I don't know why , My code is as below, and the the attribute of the
shapefile includes "fid","fnode" "tonode" etc.., fnode and tnode uniquelly
specify a node, is the second(ft.getAttribute(2).toString().trim() in the code)
and the third attribute of a feature, thus I use those id as the hashkey.
Thanks a lot for your help.
Vicky
public class ShortPath24 {
public static void main(String argv[]){
try {
//read feature from shapefilestore
BasicLineGraphBuilder lgb = new BasicLineGraphBuilder();
ShapefileDataStore ds= new ShapefileDataStore(new
URL("file:///d:/temp/road/xuhui_polyline.shp"));
FeatureSource fs = (FeatureSource)ds.getFeatureSource("xuhui_polyline");
FeatureCollection fr = fs.getFeatures();
FeatureIterator f = fr.features();
Hashtable nodes = new Hashtable();
Hashtable edges = new Hashtable();
while(f.hasNext()){
//Add nodes and BasicLineGraphBuilder
org.geotools.feature.Feature ft = f.next();
MultiLineString a2=(MultiLineString)ft.getDefaultGeometry();
Coordinate[] coordi = a2.getCoordinates();
OptXYNode tempfromNode =new OptXYNode();
tempfromNode.setCoordinate(coordi[0]);
String key1=ft.getAttribute(2).toString().trim();
if (!nodes.containsKey(key1)){
lgb.addNode(tempfromNode);
nodes.put(key1, tempfromNode);
}
else
tempfromNode =(OptXYNode) nodes.get(key1);
OptXYNode temptoNode =new OptXYNode();
temptoNode.setCoordinate(coordi[coordi.length-1]);
String key2=ft.getAttribute(3).toString().trim();
if (!nodes.containsKey(key2)){
lgb.addNode(temptoNode);
nodes.put(key2, temptoNode);
}
else
temptoNode =(OptXYNode) nodes.get(key1);
Edge ed= new BasicEdge(tempfromNode, temptoNode);
addAnEdge(tempfromNode,ed);
addAnEdge(temptoNode,ed);
edges.put(ft.getAttribute(1).toString(), ed);
lgb.addEdge(ed);
}
Graph g = lgb.getGraph();
System.out.println(g.getNodes().size());
System.out.println(g.getEdges().size());
OptXYNode source=(OptXYNode) nodes.get("89059633400121");
OptXYNode dest= (OptXYNode)nodes.get("89059633300051");
System.out.println(dest);
DijkstraIterator.EdgeWeighter weighter = new
DijkstraIterator.EdgeWeighter() {
public double getWeight(Edge e) {
return 1.0; //constant
}
};
DijkstraShortestPathFinder pf = new DijkstraShortestPathFinder( g, source,
weighter);
pf.calculate();
Path path=pf.getPath(dest);
System.out.println(path);
}
catch (Exception e){
e.printStackTrace();
}
}
public static void addAnEdge(OptXYNode node,Edge e){
Edge array[]=node.getEdgeArray();
node.setDegree(node.getDegree()+1);
for (int i=0;i<array.length;i++)
node.add(array[i]);
node.add(e);
}
}
网易邮箱,中国第一大电子邮件服务商------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users