基于javascript对geojson数据格式的地图进行astar算法的实践
对于geojson数据格式的处理
pp=JSON.parse(pp) //pp为地图数据
console.log(pp);
var lineObclo=[];var linecooclo=[]
for(i=0;i<pp.features.length;i++)
{
if(pp.features[i].geometry.type=="LineString")
{lineObclo.push(pp.features[i]);linecooclo.push(pp.features[i].geometry.coordinates)}
}//提取线特征
var linecooclo2=[]
for(i=0;i<linecooclo.length;i++)
{
var linenode1=[(linecooclo[i][0][0]-121.29)*150000,(linecooclo[i][0][1]-31.187)*150000];
var linenode2=[(linecooclo[i][1][0]-121.29)*150000,(linecooclo[i][1][1]-31.187)*150000] //这里是针对地图中心经纬度进行的放缩
linecooclo2.push([linenode1,linenode2])
}//特征做变化
linecooclo=linecooclo2
var pointclo=[];//var p=[];
console.log("linecooclo",linecooclo,linecooclo.length)
var list=linecooclo;
点处理
function Node(x,y,parent) {
this.node={
x:x,
y:y,
parent:parent,//之前走的路点
g:0,//当前点到起点的值
h:0,//当前点到终点的距离
f:0//f=g+h
};
}
Node.prototype.getX=function() {
return this.node.x;
}
Node.prototype.setX=function(x) {
this.node.x=x;
}
Node.prototype.getY=function() {
return this.node.y;
}
Node.prototype.setY=function(y) {
this.node.y=y;
}
Node.prototype.getParent=function() {
return this.node.parent;
}
Node.prototype.setParent=function(parent) {
this.node.parent=parent;
}
Node.prototype.getG=function() {
return this.node.g;
}
Node.prototype.setG=function(g) {
this.node.g=g;
}
Node.prototype.getH=function() {
return this.node.h;
}
Node.prototype.setH=function(h) {
this.node.h=h;
}
Node.prototype.getF=function() {
return this.node.f;
}
Node.prototype.setF=function(f) {
this.node.f=f;
}
function count(a,b,c,d){
{
var aa=Math.abs(a-c);var bb=Math.abs(b-d)
var cc,dd;
if(aa>bb){cc=aa-bb;dd=bb;}else{cc=bb-aa;dd=aa;}
return cc+1.4*bb
}
}
Node.prototype.getpath=function(){
var path=[];path.push([this.node.x,this.node.y]);var nodee=this.node.parent;
while(nodee.getParent()!=null)
{path.push([nodee.getX(),nodee.getY()]);nodee=nodee.getParent();}
return path
}
Astar算法主体
function input(a,b,c,d)
{//g精确,h估计
var snode=new Node(a,b,null)
var enode=new Node(c,d,null)
snode.setG(0);snode.setH(count(a,b,c,d));snode.setF(count(a,b,c,d));
var open=[];
var t=0;
open.push(snode)
var close=[];
var openaclose=[];
openaclose.push(snode)
var node=snode;
var result=[]
var isFind=false;
while(open.length>0)
{ node=open[0];
if(node.getX()==enode.getX()&&node.getY()==enode.getY())
{
return node;
}
var children=[];
for(i=0;i<map.length;i++)
{
if((((node.getX()-map[i][0])>-3)&&((node.getX()-map[i][0])<3)&&((node.getY()-map[i][1])>-3)
&&((node.getY()-map[i][1])<3)) &&((node.getX()!=map[i][0])||(node.getY()!=map[i][1])))
{children.push(map[i]);}
}
for(i=0;i<children.length;i++)
{ var k=true;
for(j=0;j<openaclose.length;j++)
{if((openaclose[j].getX()==children[i][0])&&(openaclose[j].getY()==children[i][1])){k=false;break;}}
if(k==true){var nodee=new Node(children[i][0],children[i][1],node)
nodee.setG(node.getG()+count(nodee.getX(),nodee.getY(),node.getX(),node.getY()));
nodee.setH(count(nodee.getX(),nodee.getY(),c,d));nodee.setF(nodee.getH()+nodee.getG());//未完成
openaclose.push(nodee);open.push(nodee)/*;console.log("已添加子节点",nodee,open)*/;}
}
var instant=node.getG()
for(i=0;i<children.length;i++)
{
for(j=0;j<openaclose.length;j++)
{
if((openaclose[j].getX()==children[i][0])&&(openaclose[j].getY()==children[i][0]))
{
if((openaclose[j].getG()+count(openaclose[j].getX(),close[j].getY(),node.getX(),node.getY()))<instant)
{node.setParent(open[j]);
node.setG(openaclose[j].getG()+count(openaclose[j].getX(),openaclose[j].getY(),node.getX(),node.getY()));
node.setF(count(node.getH()+node.getG()));instant=node.getG()/*console.log("已更改父节点")*/}
}
}
}
//更改父节点,同时更改fgh值
//添加子节点,透视添加fgh值
for(i=open.length-1;i>0;i--)
{for(j=0;j<i;j++)
{
if(open[j].getF()>open[j+1].getF())
{
var temp=open[j];open[j]=open[j+1];open[j+1]=temp;//console.log("已排序")
}
}
} //排列大小,按照f
close.push(open[0])
open.splice(0,1);
if(open.length==0){alert("无路经");return false;}
t++
}
}
处理效果分析
目前还无法做到十分完整的效果:
1.大地图数据处理效率过低
2.地图处理会出现断点,地图无法保证Astar算法完全有效
3.对于相同的起始点,二者更换位置寻路结果可能不同。