1

How can I load vector layer from a list? Such as

latitude = [...] 
longitude = [...]
some_data_belongs_to_the_coordinates = [...]

vector_layer = QgsVectorLayer(?)
  • Tricky question. You need the right provider according to the Class: QgsVectorLayer. Maybe a workaround could be to write down the data in a .csv inside a temp folder and then load it. – Oscar Campo Oct 23 '18 at 12:45
  • Yes this is what i am doing right now but i just want to remove unneccesary csv implementations. – MS. Oct 31 '18 at 10:59
0

Find attached a small code fraqment (qgis 2.18) which adds points from 3 lists to a memory layer. May be that helps:

from PyQt4.QtCore import QVariant

latitude = [10,20,30] 
longitude = [30,40,50]
some_data_belongs_to_the_coordinates = [100,200.1,300]

layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' ,'memory')
fields = QgsFields()
fields.append(QgsField('value', QVariant.Double))
pr = layer.dataProvider()
pr.addAttributes(fields)
layer.updateFields()

for i in range(0, len(latitude)):
    pt = QgsFeature()
    point1 = QgsPoint(longitude[i],latitude[i])
    pt.setGeometry(QgsGeometry.fromPoint(point1))
    pt.setAttributes([some_data_belongs_to_the_coordinates[i]])
    pr.addFeatures([pt])
    layer.updateExtents()

QgsMapLayerRegistry.instance().addMapLayers([layer])
  • can someone explain that : What are the dataproviders for vector layers and how to use them?. i have searched for the cookbook but there was too few data provider example about this. – MS. Nov 8 '18 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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