Geos库的使用

#include <stdio.h>
#include "geos.h"

#include <string>
 using namespace std;

string str(bool flag)
{
    string result=(flag==true)?"相交":"不相交";
    return result;
}

void intersects()
{
    std::cout<<"GEOS库版本为:"<<GEOS_VERSION<<std::endl;

    typedef Coordinate PT;
    GeometryFactory factory;

    CoordinateArraySequenceFactory csf; //构建第一个矩形p1

    CoordinateSequence* cs1 = csf.create(5,2);//五个2维点,第三维度z始终为0
    cs1->setAt(PT(0,0),0);
    cs1->setAt(PT(3,0),1);
    cs1->setAt(PT(3,3),2);
    cs1->setAt(PT(0,3),3);
    cs1->setAt(PT(0,0),4); //与第一个点相等,构成闭合
    LinearRing* ring1 = factory.createLinearRing(cs1); //点构成线
    Geometry* p1 = factory.createPolygon(ring1,NULL); //线构成面

    CoordinateSequence* cs2 = csf.create(5,2); //构建一个四边形p2
    cs2->setAt(PT(2,2),0);
    cs2->setAt(PT(4,5),1);
    cs2->setAt(PT(5,5),2);
    cs2->setAt(PT(5,4),3);
    cs2->setAt(PT(2,2),4);
    LinearRing * ring2 = factory.createLinearRing(cs2);
    Geometry* p2 = (factory.createPolygon(ring2,NULL));

 

    CoordinateSequence *cs3 = new CoordinateArraySequence(); //构建一个三角形p3
    int xoffset=4,yoffset=4,side=2;
    cs3->add(PT(xoffset, yoffset));
    cs3->add(PT(xoffset, yoffset+side));
    cs3->add(PT(xoffset+side, yoffset+side));
    cs3->add(PT(xoffset, yoffset));
    LinearRing * ring3 = factory.createLinearRing(cs3);
    Geometry* p3 = (factory.createPolygon(ring3,NULL));
    bool flag12=p1->intersects(p2);
    bool flag13=p1->intersects(p3);
    bool flag23=p2->intersects(p3);
    cout<<"图1与图2:"<<str(flag12)<<endl;
    cout<<"图1与图3:"<<str(flag13)<<endl;
    cout<<"图2与图3:"<<str(flag23)<<endl;
}

void Union()
{
    std::cout<<"GEOS库版本为:"<<GEOS_VERSION<<std::endl;

    typedef Coordinate PT;
    GeometryFactory factory;

    CoordinateArraySequenceFactory csf; //构建第一个矩形p1

    CoordinateSequence* cs1 = csf.create(5,2);//五个2维点,第三维度z始终为0
    cs1->setAt(PT(0,0),0);
    cs1->setAt(PT(0,10),1);
    cs1->setAt(PT(10,10),2);
    cs1->setAt(PT(10,0),3);
    cs1->setAt(PT(0,0),4); //与第一个点相等,构成闭合
    LinearRing* ring1 = factory.createLinearRing(cs1); //点构成线
    Geometry* p1 = factory.createPolygon(ring1,NULL); //线构成面

    CoordinateSequence* cs2 = csf.create(5,2); //构建一个四边形p2
    cs2->setAt(PT(5,0),0);
    cs2->setAt(PT(5,6),1);
    cs2->setAt(PT(12,6),2);
    cs2->setAt(PT(12,0),3);
    cs2->setAt(PT(5,0),4);
    LinearRing * ring2 = factory.createLinearRing(cs2);
    Geometry* p2 = (factory.createPolygon(ring2,NULL));

 Geometry* p3 = p1->Union(p2);
 int pointNum = p3->getNumPoints();

 CoordinateSequence *cs3 = p3->getCoordinates();
 int pointSize = cs3->getSize();

 for(int i = 0;i< pointSize;i++)
 {
  double x = cs3->getX(i);
  double y = cs3->getY(i);

  cout<<"x="<< x <<"   y="<< y <<endl;
 }

}

void intersection()
{
    std::cout<<"GEOS库版本为:"<<GEOS_VERSION<<std::endl;

    typedef Coordinate PT;
    GeometryFactory factory;

    CoordinateArraySequenceFactory csf; //构建第一个矩形p1

    CoordinateSequence* cs1 = csf.create(5,2);//五个2维点,第三维度z始终为0
    cs1->setAt(PT(0,0),0);
    cs1->setAt(PT(0,10),1);
    cs1->setAt(PT(5,10),2);
    cs1->setAt(PT(5,0),3);
    cs1->setAt(PT(0,0),4); //与第一个点相等,构成闭合
    LinearRing* ring1 = factory.createLinearRing(cs1); //点构成线
    Geometry* p1 = factory.createPolygon(ring1,NULL); //线构成面

    CoordinateSequence* cs2 = csf.create(5,2); //构建一个四边形p2
    cs2->setAt(PT(0,0),0);
    cs2->setAt(PT(0,5),1);
    cs2->setAt(PT(10,5),2);
    cs2->setAt(PT(10,0),3);
    cs2->setAt(PT(0,0),4);
    LinearRing * ring2 = factory.createLinearRing(cs2);
    Geometry* p2 = (factory.createPolygon(ring2,NULL));

 Geometry* p3 = p1->intersection(p2);
 int pointNum = p3->getNumPoints();

 CoordinateSequence *cs3 = p3->getCoordinates();
 int pointSize = cs3->getSize();

 for(int i = 0;i< pointSize;i++)
 {
  double x = cs3->getX(i);
  double y = cs3->getY(i);

  cout<<"x="<< x <<"   y="<< y <<endl;
 }

}

void difference()
{
    std::cout<<"GEOS库版本为:"<<GEOS_VERSION<<std::endl;

    typedef Coordinate PT;
    GeometryFactory factory;

    CoordinateArraySequenceFactory csf; //构建第一个矩形p1

    CoordinateSequence* cs1 = csf.create(5,2);//五个2维点,第三维度z始终为0
    cs1->setAt(PT(0,0),0);
    cs1->setAt(PT(0,10),1);
    cs1->setAt(PT(5,10),2);
    cs1->setAt(PT(5,0),3);
    cs1->setAt(PT(0,0),4); //与第一个点相等,构成闭合
    LinearRing* ring1 = factory.createLinearRing(cs1); //点构成线
    Geometry* p1 = factory.createPolygon(ring1,NULL); //线构成面

    CoordinateSequence* cs2 = csf.create(5,2); //构建一个四边形p2
    cs2->setAt(PT(0,0),0);
    cs2->setAt(PT(0,5),1);
    cs2->setAt(PT(10,5),2);
    cs2->setAt(PT(10,0),3);
    cs2->setAt(PT(0,0),4);
    LinearRing * ring2 = factory.createLinearRing(cs2);
    Geometry* p2 = (factory.createPolygon(ring2,NULL));

 Geometry* p3 = p1->difference(p2);
 int pointNum = p3->getNumPoints();

 CoordinateSequence *cs3 = p3->getCoordinates();
 int pointSize = cs3->getSize();

 for(int i = 0;i< pointSize;i++)
 {
  double x = cs3->getX(i);
  double y = cs3->getY(i);

  cout<<"x="<< x <<"   y="<< y <<endl;
 }

}

void symDifference()
{
    std::cout<<"GEOS库版本为:"<<GEOS_VERSION<<std::endl;

    typedef Coordinate PT;
    GeometryFactory factory;

    CoordinateArraySequenceFactory csf; //构建第一个矩形p1

    CoordinateSequence* cs1 = csf.create(5,2);//五个2维点,第三维度z始终为0
    cs1->setAt(PT(0,0),0);
    cs1->setAt(PT(0,10),1);
    cs1->setAt(PT(5,10),2);
    cs1->setAt(PT(5,0),3);
    cs1->setAt(PT(0,0),4); //与第一个点相等,构成闭合
    LinearRing* ring1 = factory.createLinearRing(cs1); //点构成线
    Geometry* p1 = factory.createPolygon(ring1,NULL); //线构成面

    CoordinateSequence* cs2 = csf.create(5,2); //构建一个四边形p2
    cs2->setAt(PT(0,0),0);
    cs2->setAt(PT(0,5),1);
    cs2->setAt(PT(10,5),2);
    cs2->setAt(PT(10,0),3);
    cs2->setAt(PT(0,0),4);
    LinearRing * ring2 = factory.createLinearRing(cs2);
    Geometry* p2 = (factory.createPolygon(ring2,NULL));

 Geometry* p3 = p1->symDifference(p2);
 int pointNum = p3->getNumPoints();

 CoordinateSequence *cs3 = p3->getCoordinates();
 int pointSize = cs3->getSize();

 for(int i = 0;i< pointSize;i++)
 {
  double x = cs3->getX(i);
  double y = cs3->getY(i);

  cout<<"x="<< x <<"   y="<< y <<endl;
 }

}


int main(int argc,char *argv[])
{
  ntersection();

 


    system("pause");
    return 0;
}

posted @ 2020-11-22 17:43  红叶空间  阅读(695)  评论(0编辑  收藏  举报