1. 程式人生 > >Geos判斷點是否在多邊形內

Geos判斷點是否在多邊形內

str div pan return class using poi 使用 linear

使用的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即可。

Geos判斷點是否在多邊形內