GEOS 使用的例子
typedef Coordinate PT;
geos::geom::Geometry* CGlbGlobePolygonSymbol::Interection(CGlbPolygon *geom, CGlbExtent &ext)
{// 计算ext与geom的相交区域
// CGlbPolygon ---> Geometry(GEOS_MULTIPOLYGON/GEOS_POLYGON)
if (geom==NULL) return NULL;
CGlbPolygon* poly = geom;
glbDouble minX,minY,maxX,maxY;
ext.Get(&minX,&maxX,&minY,&maxY);
geos::geom::CoordinateArraySequenceFactory csf;
// 1. 由ext生成rectPoly
geos::geom::CoordinateSequence* cs1 = csf.create(5,2);
if (cs1==NULL) return NULL;
cs1->setAt(PT(minX,minY,0),0);
cs1->setAt(PT(maxX,minY,0),1);
cs1->setAt(PT(maxX,maxY,0),2);
cs1->setAt(PT(minX,maxY,0),3);
cs1->setAt(PT(minX,minY,0),4);
geos::geom::LinearRing* ring1 = factory.createLinearRing(cs1);
if (ring1==NULL) return NULL;
geos::geom::Polygon* rectPoly = factory.createPolygon(ring1,NULL);
if (rectPoly==NULL) return NULL;
// 2. 由geom生成Poly
geos::geom::Polygon* otherPoly = NULL;
CGlbLine* extRing = poly->GetExtRing();
geos::geom::LinearRing* shell = NULL;
if (extRing)
{
glbInt32 ptCnt = extRing->GetCount();
const glbDouble* pts = extRing->GetPoints();
geos::geom::CoordinateSequence* cs2 = csf.create(ptCnt+1,2);
if (cs2)
{
for(glbInt32 k = 0; k < ptCnt; k++)
{
cs2->setAt(PT(pts[2*k],pts[2*k+1],0),k);
}
cs2->setAt(PT(pts[0],pts[1],0),ptCnt);
shell = factory.createLinearRing(cs2);
}
}
glbInt32 inRingCnt = poly->GetInRingCount();
std::vector<geos::geom::Geometry *>* holes = NULL;
if (inRingCnt>0)
holes = new vector<Geometry *>(inRingCnt);
for (glbInt32 idx = 0; idx < inRingCnt; idx++)
{
CGlbLine* inRing = poly->GetInRing(idx);
if (inRing)
{
glbInt32 ptCnt = inRing->GetCount();
const glbDouble* pts = inRing->GetPoints();
geos::geom::CoordinateSequence* cs2 = csf.create(ptCnt+1,2);
if (cs2)
{
for(glbInt32 k = 0; k < ptCnt; k++)
{
cs2->setAt(PT(pts[2*k],pts[2*k+1],0),k);
}
cs2->setAt(PT(pts[0],pts[1],0),ptCnt);
geos::geom::LinearRing* ring = factory.createLinearRing(cs2);
if (ring)
(*holes)[idx] = ring;
}
}
}
if (shell && holes==NULL)
otherPoly = factory.createPolygon(shell,NULL);
else if (shell && holes)
otherPoly = factory.createPolygon(shell,holes);
geos::geom::Geometry* outGeom=NULL;
//3. 求交集
if (otherPoly && rectPoly)
{
try
{
outGeom = rectPoly->intersection(otherPoly);
}
catch (std::exception* e)
{
outGeom = NULL;
}
}
//4. 清除创建的对象
if (rectPoly)
factory.destroyGeometry(rectPoly);
if (otherPoly)
factory.destroyGeometry(otherPoly);
return outGeom;
}
线,多线形中的点集是AutoPtr指针,自动释放,所以不需要外部来删除.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了