• 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;
    }

  • 相关阅读:
    当梦想渐行渐远的时候
    android开发系列之aidl
    看到这页代码,我要疯了
    android开发系列之性能优化
    近期的一个项目反思与总结
    IOS中摇一摇实现截屏(可实现问题反馈的功能)
    IOS设计模式浅析之外观模式(Facade)
    IOS项目开发中的文件和文件夹操作
    XCode 4.6下XIB文件提示“...could not be opened..."的问题
    "Xcode"意外退出
  • 原文地址:https://www.cnblogs.com/roea1/p/14020124.html
Copyright © 2020-2023  润新知