Skip to content

Instantly share code, notes, and snippets.

@mapmeld
Created August 8, 2012 23:43
Show Gist options
  • Save mapmeld/3299769 to your computer and use it in GitHub Desktop.
Save mapmeld/3299769 to your computer and use it in GitHub Desktop.
Column Populator Script for QGIS
# Column Populator Script for QGIS
# We have a lot of RouteNames in one column, and we want that to translate into other columns
# For example, RouteName=Ocmulgee should set Route5=1 and leave all others NULL
from PyQt4 import QtCore
activeLayer = qgis.utils.iface.activeLayer()
layerData = activeLayer.dataProvider()
columns = layerData.fields()
# how to determine column keys
#for key, value in columns.items():
# print str(key) + " = " + str(value.name())
# our column keys
# key = 6: RouteName
# key = 8: RouteBIRD
# key = 9: Route1
# key = 10: Route2
# key = 11: Route3
# key = 12: Route4
# key = 13: Route5
# key = 14: Route6
# key = 15: Route9
# key = 16: Route11
# key = 17: Route13 - out of order!
# key = 18: Route12 - out of order!
# go through the attributes of each point in the layer
layerData.select(layerData.attributeIndexes())
pt = QgsFeature()
while layerData.nextFeature(pt):
feature_id = pt.id()
attributes = pt.attributeMap()
# display every point's id, keys, and attribute values
#print "Point: " + str(feature_id)
#for (key, attribute) in attributes.iteritems():
# print "%d: %s" % (key, attribute.toString())
# use values of one column ( 6 - RouteName ) to set others
routename = attributes[6].toString()
# is on Route 1, so I set column [9] for this feature to be 1
if(routename == "Vineville/ Charter Hospital"):
attributes[9] = QtCore.QVariant(1)
elif(routename == "Vineville"):
attributes[9] = QtCore.QVariant(1)
# Route 2, column [10]
elif(routename == "Bellvue/ Log Cabin"):
attributes[10] = QtCore.QVariant(1)
elif(routename == "Bellevue/Zebulon"):
attributes[10] = QtCore.QVariant(1)
elif(routename == "Bellevue/ Zebulon"):
attributes[10] = QtCore.QVariant(1)
elif(routename == "Bellevue/ Log Cabin"):
attributes[10] = QtCore.QVariant(1)
# Route 3, column [11]
elif(routename == "West Macon"):
attributes[11] = QtCore.QVariant(1)
# Route 4, column [12]
elif(routename == "North Highland"):
attributes[12] = QtCore.QVariant(1)
# Route 5, column [13]
elif(routename == "Ocmulgee"):
attributes[13] = QtCore.QVariant(1)
# Route 6, column [14]
elif(routename == "Westgate"):
attributes[14] = QtCore.QVariant(1)
# Route 9, column[15]
elif(routename == "Macon Mall"):
attributes[15] = QtCore.QVariant(1)
# Route 11, column[16]
elif(routename == "East Macon"):
attributes[16] = QtCore.QVariant(1)
# Route 12, column[18]
elif(routename == "Houston"):
attributes[18] = QtCore.QVariant(1)
# Route 13, column [17]
elif(routename == "North Macon"):
attributes[17] = QtCore.QVariant(1)
elif(routename == "Route"):
attributes[17] = QtCore.QVariant(1)
layerData.changeAttributeValues({ feature_id: attributes })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment