4

I recently started using Qgis python console for Qgis development. My first task is to load the layer and print its attributes.I firstly load a point layer using code

mylr = QgsVectorLayer("/home/ali/Python Work/work for centerline/","pnt.shp", "ogr")
iter = mylr.getFeatures()
for feature in iter:

geom = feature.geometry()
print "Feature ID %d: " % feature.id()

if geom.type() == QGis.Point:
    x = geom.asPoint()
    print "Point: " + str(x)
elif geom.type() == QGis.Line:
    x = geom.asPolyline()
    print "Line: %d points" % len(x)
elif geom.type() == QGis.Polygon:
    x = geom.asPolygon()
    numPts = 0
    for ring in x:
    numPts += len(ring)
    print "Polygon: %d rings with %d points" % (len(x), numPts)
else:
    print "Unknown"    

attrs = feature.attributes()
print attrs

it printed the attributes of the layer pnt.shp.Then i changed the layer name

mylr = QgsVectorLayer("/home/ali/Python Work/work for centerline/","pgon.shp", "ogr")

all the following code was same.When i run the script it printed the attributes of the layer i loaded before pnt.shp. Every time i run my script it always print the attributes of pnt.shp and do not load the current layer Even if i change the veriables it always print the same layer attributes Please help me to solve this problem

  • Please format the code in the post properly. – underdark Oct 6 '14 at 18:34
3

As shown in: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html#vector-layers

The second parameter of QqgVectorLayer() is the name of the layer to be shown in qgis. This should work:

QgsVectorLayer("/home/ali/Python Work/work for centerline/pnt.shp","pnt", "ogr")

QgsVectorLayer("/home/ali/Python Work/work for centerline/pgon.shp","pgon", "ogr")

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.