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指针,自动释放,所以不需要外部来删除.

原文地址:https://www.cnblogs.com/mazhenyu/p/4447459.html