3

I'm new to PyQGIS so here goes: Give a new QGIS project, how can we draw some Points on the map?

I tried the following but nothing showed up. Should they be placed in a Vector layer before they can be rendered?

line_start = QgsPoint(50,50)
line_end = QgsPoint(100,150)
line = QgsGeometry.fromPolyline([line_start,line_end])

Update

from PyQt4.QtCore import QVariant  

lineLayer = QgsVectorLayer("LineString", 'test layer', "memory")  

lineLayer.startEditing()  
layerData = lineLayer.dataProvider() 
layerData.addAttributes([ QgsField("ID", QVariant.String), QgsField("latStart", QVariant.String), QgsField("lonStart", QVariant.String), QgsField("latEnd", QVariant.String), QgsField("lonEnd", QVariant.String) ])
lineLayer.commitChanges()

QgsMapLayerRegistry.instance().addMapLayer(lineLayer)  

This code created a layer but nothing appears on the map even after using the Zoom to layer function on that layer.

3

You have forgotten to add the feature:

lineLayer = QgsVectorLayer("LineString", 'test layer', "memory")  
pr = lineLayer.dataProvider()
# define the fields of the layer
pr.addAttributes([ QgsField("ID", QVariant.String), QgsField("latStart", QVariant.String), QgsField("lonStart", QVariant.String), QgsField("latEnd", QVariant.String), QgsField("lonEnd", QVariant.String) ])
# create a feature
feat = QgsFeature()
# add the geometry to the feature
line_start = QgsPoint(50,50)
line_end = QgsPoint(100,150)
feat.setGeometry(QgsGeometry.fromPolyline([line_start,line_end]))
# add the attributes (5 fields, string format) to the feature
feat.setAttributes(["a", "b", "c","d","e"])
# add the feature to the layer
pr.addFeatures([feat])
# update the extent of the layer
lineLayer.updateExtents()
# update the fields
lineLayer.updateFields()
# show the layer
QgsMapLayerRegistry.instance().addMapLayer(lineLayer)  

enter image description here

2

Hi Athena and welcome to PyQGIS programming! For having a good start you could take a look at PyQGIS Cookbook! Anyway, the snippets below is an example what you are trying to do of:

from PyQt4.QtCore import QVariant

## create an empty memory layer
vl = QgsVectorLayer("Point", "myLayer", "memory")
## define and add a field ID to memory layer "myLayer"
provider = vl.dataProvider()
provider.addAttributes([QgsField("ID", QVariant.Int)])
## create a new feature for the layer "myLayer"
ft = QgsFeature()
## set the value 1 to the new field "ID"
ft.setAttributes([1])
## set the geometry defined from the point X: 50, Y: 100
ft.setGeometry(QgsGeometry.fromPoint(QgsPoint(100,200)))
## finally insert the feature
pr.addFeatures([ft])
## add layer to the registry and over the map canvas
QgsMapLayerRegistry.instance().addMapLayer(vl)

Happy QGISsing!

1

I am no expert but I would assume when you draw any points, lines or polygons, they will have to be placed in a vector layer beforehand.

The following code is from here which describes how to add a line layer and attributes:

from qgis.core import QgsVectorLayer, QgsField, QgsMapLayerRegistry  
from PyQt4.QtCore import QVariant  

#create Layer
self.lineLayer = QgsVectorLayer("LineString", name, "memory")

Hope this helps.

  • 1
    Updated question with the code from the page you suggested. Almost there, layer is created but nothing shows on the map. – Athena Wisdom Oct 30 '14 at 16:48

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.