GEOS庫學習之四:幾何關系判斷


原理上一篇已經介紹過了,這篇就直接進行程序練習

#include "geos.h"

GeometryFactory factory;

//創建一條環線,與線的區別就是環線是閉合的。即第一個點和最后一點重合
LinearRing* createGeosRing(double x,double y,double offset)
{
     CoordinateArraySequenceFactory csf; 
    CoordinateSequence* cs = csf.create(6,2);
    cs->setAt(Coordinate(x,y),0);
    cs->setAt(Coordinate(x,y+offset),1);
    cs->setAt(Coordinate(x+offset,y+offset),2);
    cs->setAt(Coordinate(x+2*offset,y+2*offset),3);
    cs->setAt(Coordinate(x+2*offset,y),4);
    cs->setAt(Coordinate(x,y),5); //與第一個點相等
    LinearRing *lr=factory.createLinearRing(cs);
    return lr;
}

//創建一個多邊形,如果多邊形內部沒有孔洞實際上與環線是一樣的
Polygon* createGeosPolygon(double x,double y,double offset)
{
    LinearRing *lr=createGeosRing(x,y,offset);
    Polygon *poly=factory.createPolygon(lr,NULL); //如果多邊形中間沒有孔洞,第二個參數設為NULL
    return poly;
}

int main()
{
       Polygon *p1=createGeosPolygon(12,12,5); //創建第一個多邊形
    
    for(int i=0;i<=20;i++)
    {
        cout<<i<<":   ";
        Polygon *p2=createGeosPolygon(0,0,i); //創建第二個多邊形
        IntersectionMatrix *im=p2->relate(p1);
        cout<<*im<<"    ";    //返回DE-9IM交叉矩陣
        if(p2->disjoint(p1))
            cout<<"不相交"<<endl;
        else
        {
            if(p2->touches(p1))
                cout<<"接觸"<<endl;
            else if(p2->overlaps(p1))
                cout<<"部分重疊"<<endl;
            else if(p2->covers(p1))
                cout<<"覆蓋"<<endl;
            else
                cout<<*im<<endl;
        }
    }
    system("pause");
    return 1;
}

結果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM