开发者

Hardcore SQL(ite) : fetch segment intersection

So this one is a very difficult one I think, so I'll try to make it as clear as possible.

So basically, I have geographic datas :

Nodes (#ID,lat,lng)
W开发者_如何转开发ayNodes (#ID,#node_id,#way_id, sequence)
Ways(#id,name)

And what I want is to get the intersection of two GROUPS of ways. For example I need to find intersection(s) between the ways called "name1" : {way1, way2, way3} and the ways called "name2" : {way4, way5, way6}

So what I need is to do an equivalent to this :

float x;
float y;

float A1 = Y2-Y1;
float B1 = X1-X2;
float C1 = A1*X1+B1*Y1;

float A2 = Y4-Y3;
float B2 = X3-X4;
float C2 = A2*X3+B2*Y3;

float det = A1*B2 - A2*B1;

if(det == 0){
    //Lines are parallel
    x = 0.0;
    y = 0.0;

}else{
    x = (B2*C1 - B1*C2)/det;
    y = (A1*C2 - A2*C1)/det;
}

BOOL intersection = (x<MAX(X1,X2) && x<MAX(X3,X4) && x>MIN(X1,X2) && x>MIN(X3,X4));

But in SQL !

I kind of think it is possible, my request looks like that : (F1, F2 and F3 replace two very long functions, which compute X, Y and det, they should be correct.)

SELECT F1(n1.lat,n1.lng,n2.lat,n2.lng,n3.lat,n3.lng,n4.lat,n4.lng) AS x,
F2(n1.lat,n1.lng,n2.lat,n2.lng,n3.lat,n3.lng,n4.lat,n4.lng) AS y,
F3(n1.lat,n1.lng,n2.lat,n2.lng,n3.lat,n3.lng,n4.lat,n4.lng) AS det, 
FROM Nodes n1, Nodes n2, Nodes n3, Nodes n4
JOIN WayNodes wn1 ON n1.id = wn1.node_id
JOIN WayNodes wn2 ON n2.id = wn1.node_id
JOIN WayNodes wn3 ON n3.id = wn1.node_id
JOIN WayNodes wn4 ON n4.id = wn1.node_id
JOIN Way w1 ON wn1.way_id = w1.id AND wn2..way_id = w1.id
JOIN Way w2 ON wn3..way_id = w2.id AND wn4..way_id = w2.id
WHERE det != 0 AND
x < MAX(n1.lng, n2.lng)
AND  x > MIN(n1.lng, n2.lng)
AND x < MAX(n3.lng, n4.lng)
AND  x > MIN(n3.lng, n4.lng)
AND wn1.sequence=wn2.sequence - 1
AND wn3.sequence=wn4.sequence - 1
AND w1.name = "name1"
AND w2.name  = "name2"

Apparently something doesn't work in the junction... any idea ?


Is it that you need...

JOIN WayNodes wn1 ON n1.id = wn1.node_id
JOIN WayNodes wn2 ON n2.id = wn2.node_id
JOIN WayNodes wn3 ON n3.id = wn3.node_id
JOIN WayNodes wn4 ON n4.id = wn4.node_id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜