使用的geo版本是3.5.1
1 #include <iostream> 2 #include "geos.h" 3 using namespace std; 4 GeometryFactory factory; 5 Point* createGeosPoint(double x,double y) 6 { 7 Coordinate pt(x,y); 8 Point* p=factory.createPoint(pt); 9 return p; 10 } 11 Polygon* createGeosPolygon(double x,double y,double offset) 12 { 13 CoordinateArraySequence *cas=new CoordinateArraySequence(); //构建点序列 14 cas->add(Coordinate(x,y)); 15 cas->add(Coordinate(x,y+offset)); 16 cas->add(Coordinate(x+offset,y+offset)); 17 cas->add(Coordinate(x+offset,y+2*offset)); 18 cas->add(Coordinate(x+2*offset,y+2*offset)); 19 cas->add(Coordinate(x+2*offset,y)); 20 cas->add(Coordinate(x,y)); //与第一个点相等,形成闭合 21 LinearRing *lr=factory.createLinearRing(cas);//构建闭合环线 22 Polygon *poly=factory.createPolygon(lr,NULL); //把闭合线转成多边形;如果多边形中间没有孔洞,第二个参数设为NULL 23 return poly; 24 } 25 int main() 26 { 27 cout<<"GEOS库版本为:"<<GEOS_VERSION<<endl; 28 double x=9; 29 double y=11; 30 Polygon *poly=createGeosPolygon(0,0,5); 31 Point* p = createGeosPoint(x,y); 32 if(poly->contains(p))//判断点p是否在多边形poly内,与边缘重合不算在内 33 { 34 cout<<"p在多边形内"<<endl; 35 } 36 }
遇到的问题:提示GeometryFactory是protected型,无法调用
解决办法: 一开始版本是3.6.2,将版本降低到3.5.1即可。