基于其他矢量图层属性创建新字段(pyqgis)

时间:2017-05-16 11:07:36

标签: python attributes qgis

我想在矢量图层“Join”中创建一个新字段,基于矢量图层“Buildings”和“Intersection”中的属性,但PyQgis会返回一系列的数字,特别是该字段的第一个特征值“交集”(我猜错误在for循环的脚本/序列中)。你能帮帮我吗?

featuresB=layerBuildings.getFeatures() 
featuresJ=layerJoin.getFeatures() 
featuresI=layerIntersection.getFeatures()    
for fJ in featuresJ:
    for fI in featuresI:
         for fB in featuresB:
             if fI.geometry().area()*2>=fB.geometry().area():
                print fI[field_x]
             else:
                print fJ[field_y]

2 个答案:

答案 0 :(得分:0)

我按照你想要的想法附上一个小例子。 但如果你在没有任何联合字段的情况下这样做,你将覆盖值。

我希望能够提供帮助或作为解决问题的指南

from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *

#TOC Layers
layerB = QgsMapLayerRegistry.instance().mapLayersByName('farmacias')[0]
layerJ = QgsMapLayerRegistry.instance().mapLayersByName('call2016')[0]
layerI = QgsMapLayerRegistry.instance().mapLayersByName('albergues')[0]

#Provider
provider = layerJ.dataProvider()
provider.addAttributes( [ QgsField('new_field', QVariant.String)])
layerJ.updateFields()

#Loop           
for j in layerJ.getFeatures():
    #print "Layer Join :" + str(j)
    for b in layerB.getFeatures():
        #print "Layer Buildings :" + str(b)
        for i in layerI.getFeatures():
            #print "Layer Intersection :" + str(i)
            #if "your condition"
            provider.changeAttributeValues({j.id() : {provider.fieldNameMap()['new_field'] : i[0]}})
            layerJ.updateFeature(j)

答案 1 :(得分:0)

第二,你问了整个代码:这里有一些小改动:

 $('li a').mouseover(function () {
    var rhomboidImg = $(this).data('rhomboid-img');
    $('#img-nav-rhomboid').css('background', 'url('+rhomboidImg+') no-repeat');
});