2

test2 is point data and test is polygon data I will write test2's attribute to test table i want to make it

 i want 

 date|Num|add|결과|x|y|BUFF_DIST|contain  
 20170103|640272|강원도 인제군...|정좌표|318102|501346|30000.0000|86,91,235,2485...|

I want to use pyqgis and I wrote code but I don't know how to write point's attirbute to polygon table

my code is

#-*- coding: utf-8 -*-
from qgis.utils import iface
from PyQt4.QtCore import QVariant
NF = "FRMHS_NO"
f = iface.addVectorLayer("D:/2018/nonmoon/test/test.shp","test","ogr")
g = iface.addVectorLayer("D:/2018/nonmoon/test/test2.shp","test2","ogr")
f.startEditing()
res = f.dataProvider().addAttributes([QgsField("contain",QVariant.String)])
f.updateFields()

I have a image. first img is map second img is test(polygon data)'s table last img is test2(point data)'s table

mapping img polygon's table point's table

1 Answer 1

1

You could use the QgsFeature::setAttributes() method to set your values for your single feature:

from qgis.utils import iface
from PyQt4.QtCore import QVariant
NF = "FRMHS_NO"
f = iface.addVectorLayer("D:/2018/nonmoon/test/test.shp","test","ogr")
g = iface.addVectorLayer("D:/2018/nonmoon/test/test2.shp","test2","ogr")
f.startEditing()
res = f.dataProvider().addAttributes([QgsField("contain",QVariant.String)])
f.updateFields()

# Set attributes according to ordering of fields
for features in f.getFeatures():
    features.setAttributes([123,62,'강원도 인제군...','정좌표',318102,501346,30000.0000,'86,91,235,2485...'])
    f.updateFeature(features)

f.commitChanges()
6
  • Thank you for answer. But i want to write file G 's id field to file F's table that point contain to polygon. No write on one's own
    – 박진호
    Feb 20, 2018 at 8:30
  • @박진호 - Have you considered calling the Join attributes by location tool from the Processing Framework?
    – Joseph
    Feb 20, 2018 at 10:14
  • @박진호 - If you want point features to contain the polygon columns such as id, use something like: processing.runalg('qgis:joinattributesbylocation', point_layer, polygon_layer, ['intersects'], 0.00, 1, '', 1, output)
    – Joseph
    Feb 21, 2018 at 11:23
  • @박진호 - Yes, first use import processing and then you could use the following by adjusting to your paths: processing.runalg('qgis:joinattributesbylocation', "path/to/point.shp", "path/to/polygon.shp", ['intersects'], 0.00, 1, '', 1, "path/to/output.shp")
    – Joseph
    Feb 21, 2018 at 11:30
  • Thank you for your answer. But I want to upgrade this 'Join attribute by location'. I want to have file g's all id number to contain polygon and I add polygon field ('contain') about file g's all id number. "ex) contain field = 89,91,235,2845...last id number"
    – 박진호
    Feb 21, 2018 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.