Qgis开发-矢量空间分析
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
*