Hardcore SQL(ite):获取段交集

时间:2010-07-16 06:11:11

标签: sql sqlite geometry

所以我觉得这个很难非常,所以我会尽量让它变得清晰。

基本上,我有地理数据:

Nodes (#ID,lat,lng)
WayNodes (#ID,#node_id,#way_id, sequence)
Ways(#id,name)

我想要的是获得两种 GROUPS 方式的交集。 例如,我需要找到名为“name1”的方式之间的交集:{way1,way2,way3}和名为“name2”的方式:{way4,way5,way6}

所以我需要做的就是这样做:

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));

但是在SQL中!

我觉得有可能,我的要求看起来像这样: (F1,F2和F3取代了两个非常长的函数,它们计算X,Y和det,它们应该是正确的。)

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"

显然某些东西在交界处不起作用......任何想法?

1 个答案:

答案 0 :(得分:1)

你需要......

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