Qgis开发13-矢量空间分析

来源:互联网 发布:淘宝打折软件能打5折的 编辑:程序博客网 时间:2024/05/28 05:18

Qgis开发-矢量空间分析

sf2gis@163.com

2014年8月27日

 

1  概述

矢量分析是指两个或多个矢量元素之间的相互关系算法。

矢量分析包含关系测试,关系计算两类。

关系测试是指元素之间有无相关关系。

关系计算是指元素之间相互关系的确切几何结果。

 

2  原理

矢量计算包算常规的并、交、差、异或等各种关系。

2.1 关系测试

不相交disjoint:由QgsGeometry::disjoint()实现。(参考:http://en.wikipedia.org/wiki/Disjoint_sets)

叠置overlaps:相交,但交集只是两个输入元素的一部分。由QgsGeometry::overlaps()实现

相交intersects:相交,比较宽泛的相交。由QgsGeometry::intersects()实现

包含contains:由QgsGeometry::contains()实现

相等equals:由QgsGeometry::equals()实现。

相切touches:由QgsGeometry::touches()实现。

内部within:与contains()相反。由QgsGeometry::within()实现。

经过crosses:由QgsGeometry::crosses()实现。

参考:ArcMapHelp:Relational functions for ST_Geometry

2.2 关系运算

并Union:指元素相互合并,相交部分从原元素中独立为单独元素。由QgsGeometry::difference()+QgsGeometry::intersection()实现。

交Intersection:表示两个几何图形之间共同的部分。交集多用来表示两个区域的重叠区域。由QgsGeometry::intersection()实现。注意:对于凹多边形,计算不准确。

差Difference:表示一个元素与另一个元素的不同之处。由QgsGeometry::difference()实现。

异或SymmatricDifference:表示两个元素中去除相交的部分。由QgsGeometry::symDifference()

融合Dissolve:多个元素合并为一个元素。由QgsGeometry::combine实现。

插值Interpolate:向指定的点按照一定的距离插值,形成一个新点。由由QgsGeometry::interpolate()实现。

缓冲区Buffer:表示向外缓冲一段距离。(参见Qgis开发10-缓冲区.docx)。由QgsGeometry::buffer()实现。

等距线offsetCurve:表示与指定线相等距离的线。由QgsGeometry::offsetCurve()实现。参考:http://www.w3school.com.cn/tags/canvas_miterlimit.asp)

(参考:http://cagd.cs.byu.edu/~557/text/ch8.pdf)

抽稀Simplify:将较多节点的实体减少节点。由QgsGeometry::simplify()实现。

加密Densify:将较少节点的实体增加节点。只能将元素点取出后逐个加密实现。

中心点Centroid:当前实体的中心点。由QgsGeometry::centroid()实现。

内部点PointOnSurface:因为多边形可能有孔,也可能有飞地,此函数保证返回一个在多边形内部的点。由QgsGeometry::pointOnSurface()实现。(参考:http://www.cnblogs.com/sillyemperor/archive/2009/12/03/1616042.html)

外部多边形ConvexHull:外接多边形。由QgsGeometry::convexHull()实现。

 

参考:http://blog.csdn.net/cdl2008sky/article/details/7275949

 

3  方法

qgis2.4.0\python\plugins\processing\algs\qgis\ftools

所有的几何操作都在QgsGeometry中进行。

QgsGeometry::intersects()进行相交测试,如果为true,则相交。

QgsGeometry::intersection()返回相交的几何图形。

4  示例

1)       添加相应的action和界面元素

2)       声明SLOT函数

//xx.h

    voidbuffer(void);

 

3)       连接action的Signal和SLOT

//xx.cpp

    connect(ui->actionBuffer,SIGNAL(triggered()),this,SLOT(buffer()));

4)       实现SLOT函数

//xx.cpp

 

/**

 *@briefMainWindow::cetroid

 *

 *centroid.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::centroid(void)

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    while(featureIt.nextFeature(f))

    {

        constQgsFields*pFields=f.fields();

        ids<<f.id();

 

        for(inti=0;i<pFields->size();++i){

            qDebug()<<"Feild["<<i<<"]="<<pFields->at(i).name()<<endl;

            qDebug()<<"Attribute["<<i<<"]="<<f.attribute(i).toString()<<endl;

        }

 

        QgsGeometry*pGeo=f.geometry();

        QgsGeometry*pResultGeo=pGeo->centroid();

        QgsRubberBand*pResultRubber=createRubberBand(m_pMapCanvas,pResultGeo,QColor(0,255,0));

        return;

    }

 

}

 

/**

 *@briefMainWindow::convexHull

 *

 *convexHull.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::convexHull(void)

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    while(featureIt.nextFeature(f))

    {

        constQgsFields*pFields=f.fields();

        ids<<f.id();

 

        for(inti=0;i<pFields->size();++i){

            qDebug()<<"Feild["<<i<<"]="<<pFields->at(i).name()<<endl;

            qDebug()<<"Attribute["<<i<<"]="<<f.attribute(i).toString()<<endl;

        }

 

        QgsGeometry*pGeo=f.geometry();

        QgsGeometry*pOuter=pGeo->convexHull();

        QgsGeometry*pResultGeo=pOuter;

        QgsRubberBand*pResultRubber=createRubberBand(m_pMapCanvas,pResultGeo,QColor(0,255,0));

 

        return;

    }

}

 

/**

 *@briefMainWindow::pointOnSurface

 *

 *pointOnSurface.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::pointOnSurface(void)

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    while(featureIt.nextFeature(f))

    {

        constQgsFields*pFields=f.fields();

        ids<<f.id();

 

        for(inti=0;i<pFields->size();++i){

            qDebug()<<"Feild["<<i<<"]="<<pFields->at(i).name()<<endl;

            qDebug()<<"Attribute["<<i<<"]="<<f.attribute(i).toString()<<endl;

        }

 

        QgsGeometry*pGeo=f.geometry();

        QgsGeometry*pOuter=pGeo->pointOnSurface();

        QgsGeometry*pResultGeo=pOuter;

        QgsRubberBand*pResultRubber=createRubberBand(m_pMapCanvas,pResultGeo,QColor(0,255,0));

 

        return;

    }

 

}

 

/**

 *@briefMainWindow::interpolate

 *

 *interpolate.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::interpolate(void)

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    QgsPolylinepolyline;

    {

        QgsPointpt1(-94,44);

        QgsPointpt2(-86,44);

        QgsPointpt3(-86,40);

        QgsPointpt4(-94,40);

        polyline<<pt1<<pt2<<pt3<<pt4;

    }

    QgsGeometry*pLine=QgsGeometry::fromPolyline(polyline);

    QgsGeometry*pOuter=pLine->interpolate(1);

    QgsGeometry*pResultGeo=pOuter;

    QgsRubberBand*pResultRubber=createRubberBand(m_pMapCanvas,pResultGeo,QColor(0,255,0));

 

    return;

 

}

 

/**

 *@briefMainWindow::offsetcurve

 *

 *offsetcurve.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::offsetCurve()

{

    QgsPolylinepolyline;

    {

        QgsPointpt1(-94,44);

        QgsPointpt2(-86,44);

        QgsPointpt3(-86,40);

        QgsPointpt4(-94,40);

        polyline<<pt1<<pt2<<pt3<<pt4;

    }

    QgsPolygonpolygon;

    polygon<<polyline;

    QgsGeometry*pIntersect=QgsGeometry::fromPolygon(polygon);

    //           GEOSBUF_JOIN_ROUND=1,

    //           GEOSBUF_JOIN_MITRE=2,

    //           GEOSBUF_JOIN_BEVEL=3

    //ifwidth<0,thenright,>0,left.

    QgsGeometry*pLine=QgsGeometry::fromPolyline(polyline);

    QgsGeometry*pOuter=pLine->offsetCurve(0.3,5,2,0.3);//onlyworkwithpolyline

    QgsGeometry*pResultGeo=pOuter;

    QgsRubberBand*pGeoRubber=createRubberBand(m_pMapCanvas,pIntersect,QColor(255,255,0));

    QgsRubberBand*pResultRubber=createRubberBand(m_pMapCanvas,pResultGeo,QColor(0,255,0));

 

    return;

}

 

/**

 *@briefMainWindow::intersect

 *

 *intersect.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::intersect()

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    while(featureIt.nextFeature(f))

    {

        constQgsFields*pFields=f.fields();

        ids<<f.id();

 

        for(inti=0;i<pFields->size();++i){

            qDebug()<<"Feild["<<i<<"]="<<pFields->at(i).name()<<endl;

            qDebug()<<"Attribute["<<i<<"]="<<f.attribute(i).toString()<<endl;

        }

 

        QgsGeometry*pGeo=f.geometry();

        QgsPolylinepolyline;

        {

            QgsPointpt1(-94,44);

            QgsPointpt2(-86,44);

            QgsPointpt3(-86,40);

            QgsPointpt4(-94,40);

            polyline<<pt1<<pt2<<pt3<<pt4;

        }

        QgsPolygonpolygon;

        polygon<<polyline;

        QgsGeometry*pIntersect=QgsGeometry::fromPolygon(polygon);

        if(pGeo->intersects(pIntersect)){

            QgsGeometry*pResultGeo=pGeo->intersection(pIntersect);

            QgsRubberBand*pRubberResultGeo= newQgsRubberBand(m_pMapCanvas,pResultGeo->type());

            pRubberResultGeo->addGeometry(pResultGeo,NULL);

            pRubberResultGeo->setColor(QColor(0,255,0));

            pRubberResultGeo->setWidth(2);

            pRubberResultGeo->setActive(true);

 

            qDebug()<<"WKT="<<pResultGeo->exportToWkt()<<endl;

            qDebug()<<"WKB="<<pResultGeo->asWkb()<<endl;

 

        }

 

        return;

    }

 

}

 

/**

 *@briefMainWindow::dissolve

 *

 *dissolve.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::dissolve()

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    while(featureIt.nextFeature(f))

    {

        constQgsFields*pFields=f.fields();

        ids<<f.id();

 

        for(inti=0;i<pFields->size();++i){

            qDebug()<<"Feild["<<i<<"]="<<pFields->at(i).name()<<endl;

            qDebug()<<"Attribute["<<i<<"]="<<f.attribute(i).toString()<<endl;

        }

 

        QgsGeometry*pGeo=f.geometry();

        QgsPolylinepolyline;

        {

            QgsPointpt1(-94,44);

            QgsPointpt2(-86,44);

            QgsPointpt3(-86,40);

            QgsPointpt4(-94,40);

            polyline<<pt1<<pt2<<pt3<<pt4;

        }

        QgsPolygonpolygon;

        polygon<<polyline;

        QgsGeometry*pIntersect=QgsGeometry::fromPolygon(polygon);

        if(pGeo->intersects(pIntersect)){

            QgsGeometry*pResultGeo=pGeo->combine(pIntersect);

            QgsRubberBand*pRubberResultGeo= newQgsRubberBand(m_pMapCanvas,pResultGeo->type());

            pRubberResultGeo->addGeometry(pResultGeo,NULL);

            pRubberResultGeo->setColor(QColor(0,255,0));

            pRubberResultGeo->setWidth(2);

            pRubberResultGeo->setActive(true);

 

            qDebug()<<"WKT="<<pResultGeo->exportToWkt()<<endl;

            qDebug()<<"WKB="<<pResultGeo->asWkb()<<endl;

 

        }

 

        return;

    }

}

 

/**

 *@briefMainWindow::simplify

 *

 *simplify.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::simplify()

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    while(featureIt.nextFeature(f))

    {

        constQgsFields*pFields=f.fields();

        ids<<f.id();

 

        for(inti=0;i<pFields->size();++i){

            qDebug()<<"Feild["<<i<<"]="<<pFields->at(i).name()<<endl;

            qDebug()<<"Attribute["<<i<<"]="<<f.attribute(i).toString()<<endl;

        }

 

        QgsGeometry*pGeo=f.geometry();

        QgsPolylinepolyline;

        {

            QgsPointpt1(-94,44);

            QgsPointpt2(-86,44);

            QgsPointpt3(-86,40);

            QgsPointpt4(-94,40);

            polyline<<pt1<<pt2<<pt3<<pt4;

        }

        QgsPolygonpolygon;

        polygon<<polyline;

        QgsGeometry*pIntersect=QgsGeometry::fromPolygon(polygon);

        if(pGeo->intersects(pIntersect)){

            QgsGeometry*pOrigin=pGeo->combine(pIntersect);

            QgsGeometry*pSimplify=pOrigin->simplify(0.1);

            QgsGeometry*pResultGeo=pSimplify;

            QgsRubberBand*pRubberResultGeo= newQgsRubberBand(m_pMapCanvas,pResultGeo->type());

            pRubberResultGeo->addGeometry(pResultGeo,NULL);

            pRubberResultGeo->setColor(QColor(0,255,0));

            pRubberResultGeo->setWidth(2);

            pRubberResultGeo->setActive(true);

 

            qDebug()<<"OriginWKT="<<pOrigin->exportToWkt()<<endl;

            qDebug()<<"OriginWKB="<<pOrigin->asWkb()<<endl;

            qDebug()<<"SimplifyWKT="<<pResultGeo->exportToWkt()<<endl;

            qDebug()<<"SimplifyWKB="<<pResultGeo->asWkb()<<endl;

        }

 

        return;

    }

}

 

/**

 *@briefMainWindow::unionGeo

 *

 *union.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::unionGeo()

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    while(featureIt.nextFeature(f))

    {

        constQgsFields*pFields=f.fields();

        ids<<f.id();

 

        for(inti=0;i<pFields->size();++i){

            qDebug()<<"Feild["<<i<<"]="<<pFields->at(i).name()<<endl;

            qDebug()<<"Attribute["<<i<<"]="<<f.attribute(i).toString()<<endl;

        }

 

        QgsGeometry*pGeo=f.geometry();

        QgsPolylinepolyline;

        {

            QgsPointpt1(-94,44);

            QgsPointpt2(-86,44);

            QgsPointpt3(-86,40);

            QgsPointpt4(-94,40);

            polyline<<pt1<<pt2<<pt3<<pt4;

        }

        QgsPolygonpolygon;

        polygon<<polyline;

        QgsGeometry*pIntersect=QgsGeometry::fromPolygon(polygon);

        if(pGeo->intersects(pIntersect)){

            QgsGeometry*pOuter=pIntersect->difference(pGeo);

            QgsGeometry*pIntersection=pIntersect->intersection(pGeo);

            QgsGeometry*pInner=pGeo->difference(pIntersect);

            QgsGeometry*pResultGeo=pOuter;

            QgsRubberBand*pOuterRubber=createRubberBand(m_pMapCanvas,pOuter,QColor(0,255,0));

            QgsRubberBand*pIntersectionRubber=createRubberBand(m_pMapCanvas,pIntersection,QColor(255,0,0));

            QgsRubberBand*pInnerRubber=createRubberBand(m_pMapCanvas,pInner,QColor(0,0,255));

        }

 

        return;

    }

}

 

/**

 *@briefMainWindow::symDifference

 *

 *symDifference.

 *@authorsf2gis@163.com

 *@date2014-08-2718:44:17

 */

voidMainWindow::symDifference()

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    while(featureIt.nextFeature(f))

    {

        constQgsFields*pFields=f.fields();

        ids<<f.id();

 

        for(inti=0;i<pFields->size();++i){

            qDebug()<<"Feild["<<i<<"]="<<pFields->at(i).name()<<endl;

            qDebug()<<"Attribute["<<i<<"]="<<f.attribute(i).toString()<<endl;

        }

 

        QgsGeometry*pGeo=f.geometry();

        QgsPolylinepolyline;

        {

            QgsPointpt1(-94,44);

            QgsPointpt2(-86,44);

            QgsPointpt3(-86,40);

            QgsPointpt4(-94,40);

            polyline<<pt1<<pt2<<pt3<<pt4;

        }

        QgsPolygonpolygon;

        polygon<<polyline;

        QgsGeometry*pIntersect=QgsGeometry::fromPolygon(polygon);

        if(pGeo->intersects(pIntersect)){

            QgsGeometry*pOuter=pIntersect->symDifference(pGeo);

            QgsGeometry*pResultGeo=pOuter;

            QgsRubberBand*pResultRubber=createRubberBand(m_pMapCanvas,pResultGeo,QColor(0,255,0));

        }

 

        return;

    }

}

 

 

/**

 *@briefMainWindow::buffer

 *

 *buffer.

 *@authorsf2gis@163.com

 *@date2014-08-2617:19:37

 */

voidMainWindow::buffer()

{

    QgsVectorLayer*pVectorLayer=(QgsVectorLayer*)m_pMapCanvas->currentLayer();

    QgsFeatureIteratorfeatureIt=pVectorLayer->getFeatures();

    featureIt.rewind();

 

    QgsFeatureIdsids;

    QgsFeaturef;

    while(featureIt.nextFeature(f))

    {

        constQgsFields*pFields=f.fields();

        ids<<f.id();

 

        for(inti=0;i<pFields->size();++i){

            qDebug()<<"Feild["<<i<<"]="<<pFields->at(i).name()<<endl;

            qDebug()<<"Attribute["<<i<<"]="<<f.attribute(i).toString()<<endl;

        }

 

        QgsGeometry*pGeo=f.geometry();

        QgsGeometry*pBuffer=pGeo->buffer(0.1,5);

        QgsRubberBand*pRubberBuffer= newQgsRubberBand(m_pMapCanvas,pBuffer->type());

        pRubberBuffer->addGeometry(pBuffer,NULL);

        pRubberBuffer->setColor(QColor(0,255,0));

        pRubberBuffer->setWidth(2);

        pRubberBuffer->setActive(true);

        return;

    }

}

 

 

0 0