this.locateBylonlat=function(featId,lons,lats,extendSelection){
var lonArr=lons.split(',');
var latArr=lats.split(',');
var len=lonArr.length;
var realLen=len-1;
var lon;
var lat;
if(len<=4){
var lonMide=(this.myParseFloat(lonArr[1])+this.myParseFloat(lonArr[2]))/2;
var latMide=(this.myParseFloat(latArr[1])+this.myParseFloat(latArr[2]))/2;
lon=(this.myParseFloat(lonArr[0])+lonMide)/2;
lat=(this.myParseFloat(latArr[0])+latMide)/2;
this.clickRightTree(featId,lon,lat,extendSelection)
}else{
var lonV=this.myParseFloat(lonArr[0]);
var latV=this.myParseFloat(latArr[0]);
var a;
var b;
var index=0;
for(var i=1;i <len-1;i++){ //寻找一个凸顶点,最低点肯定是凸顶点
if(this.myParseFloat(lonArr[i]) <lonV){
lonV=this.myParseFloat(lonArr[i]);
latV=this.myParseFloat(latArr[i]);
index=i;
}
}
a=(index-1+realLen)%realLen; //得到v的前一个顶点
b=(index+1)%realLen; //得到v的后一个顶点
var q;
var latTri=[this.myParseFloat(latArr[a]),this.myParseFloat(latArr[index]),this.myParseFloat(latArr[b])];
var lonTri=[this.myParseFloat(lonArr[a]),this.myParseFloat(lonArr[index]),this.myParseFloat(lonArr[b])];
var md=Number.MAX_VALUE;
var bin=false;
for(var i=0;i <realLen;i++) { //寻找在三角形avb内且离顶点v最近的顶点q
if(i==index)continue;
if(i==(index-1+realLen)%realLen)continue;
if(i==(index+1)%realLen)continue;
if(!this.pnpoly(3,latTri,lonTri,this.myParseFloat(latArr[i]),this.myParseFloat(lonArr[i])))continue;
bin=true;
var distance=this.dist(lonArr,latArr,index,i);
if(distance <md) {
q=i;
md=distance;
}
}
if(!bin){ //没有顶点在三角形avb内,返回线段ab中点
lon1=(this.myParseFloat(lonArr[a])+this.myParseFloat(lonArr[b]))/2;
lat1=(this.myParseFloat(latArr[a])+this.myParseFloat(latArr[b]))/2;
lon=(this.myParseFloat(lonArr[index])+lon1)/2;
lat=(this.myParseFloat(latArr[index])+lat1)/2;
}else{
lon=(this.myParseFloat(lonArr[index])+this.myParseFloat(lonArr[q]))/2; //返回线段vq的中点
lat=(this.myParseFloat(latArr[index])+this.myParseFloat(latArr[q]))/2;
}
this.clickRightTree(featId,lon,lat,extendSelection)
}
}
this.myParseFloat=function(num){
return OpenLayers.Util.toFloat(num);
}
this.dist=function(lonArr,latArr,i,j){
var num1=(lonArr[i]-lonArr[j])*(lonArr[i]-lonArr[j]);
var num2=(latArr[i]-latArr[j])*(latArr[i]-latArr[j]);
return Math.sqrt(num1+num2);
}
//google的判断点在不在多边形区域内
this.pnpoly=function(nvert, vertx, verty,testx,testy)
{
var i, j, c = false;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
求多边形区域内一点
最新推荐文章于 2021-01-16 12:50:11 发布