zoukankan      html  css  js  c++  java
  • 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指针,自动释放,所以不需要外部来删除.

  • 相关阅读:
    c#类,接口,结构,抽象类介绍 以及抽象和接口的比较
    存储过程基本语法
    有关uploadifive的使用经验
    堆栈的浅析
    JavaScript语言精粹4递归(汉诺塔游戏寻常解)及作用域
    JavaScript语言精粹3异常,扩充类型功能
    有关this,4种调用模式小例子
    JavaScript语言精粹2函数对象,函数字面量,调用
    JavaScript语言精粹1字符串,对象字面量
    JQUERY选择器第一天
  • 原文地址:https://www.cnblogs.com/mazhenyu/p/4447459.html
Copyright © 2011-2022 走看看