First of all, the code which is below gives me the attribute names of a shapefile correctly if I use PyScripter IDE:
from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis-dev", True)
QgsApplication.initQgis()
layer = QgsVectorLayer("C:/istanbul/istanbul_geo-wgs84.shp", "sero", "ogr")
fields = layer.pendingFields()
for field in fields.itervalues():
print "Name:%s Type:%s, Length:%s" % ( field.name(), field.typeName(), field.length())
If I want to access attribute names of a shapefile, Aptana Studio gives me an error message below:
for field in fields.itervalues():
AttributeError: 'QgsFields' object has no attribute 'itervalues'
d:\src\qgis\src\core\qgsvectorlayer.cpp(188) : (QgsVectorLayer::~QgsVectorLayer) entered.
According to the console in Aptana Studio, my system can read vector layers:
d:\src\qgis\src\core\qgsvectorlayer.cpp(1628) : (QgsVectorLayer::setDataProvider) Extent of layer: 28.0019662445739925,40.7878698688807120 : 29.9327258493754194,41.5270420672795026
d:\src\qgis\src\core\qgscoordinatereferencesystem.cpp(449) : (QgsCoordinateReferenceSystem::createFromWkt) authid recognized as EPSG:4326
I used OSGeo4W installer and configured my IDE based on Nathan's expressions:
How to add QGIS Python API Intellisense support in Aptana Studio3/Pydev
and finally my "aptana.bat" file to initialize the IDE:
@ECHO OFF
set OSGEO4W_ROOT=C:\OSGeo4W
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass-6.4.3\etc\env.bat
set PATH=%OSGEO4W_ROOT%\bin;%OSGEO4W_ROOT%\apps\qgis-dev\bin;%PATH%
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis-dev\python
set QGISHOME=%OSGEO4W_ROOT%\apps\qgis-dev
call "C:\Users\Geomatics\AppData\Local\Aptana Studio 3\studio3.bat" %*
How are we gonna tackle this problem? Thanks in advance…
Best Answer
The QgsFields object does not implement the itervalues() method. You can easily iterate manually: