Skip to content

Instantly share code, notes, and snippets.

@mapmeld
Created August 9, 2012 00:40
Show Gist options
  • Save mapmeld/3299980 to your computer and use it in GitHub Desktop.
Save mapmeld/3299980 to your computer and use it in GitHub Desktop.
Multi-Column Populator for QGIS
# Multi-Column Populator for QGIS
# Go through a huge String of bus stops and the buses which stop there
# Set Route1 = 1 wherever that route stops
# Set Route2 = 1 wherever that route stops
# and so on
from PyQt4 import QtCore
# populate dictionary of points based on the OBJECTID attribute used to identify the bus stop
pts_by_id = { }
activeLayer = qgis.utils.iface.activeLayer()
layerData = activeLayer.dataProvider()
columns = layerData.fields()
#for key, value in columns.items():
# print str(key) + " = " + str(value.name())
layerData.select(layerData.attributeIndexes())
pt = QgsFeature()
while layerData.nextFeature(pt):
feature_id = pt.id()
attributes = pt.attributeMap()
# retrieve stop ID - from QVariant integer to string
stop_id = str( attributes[0].toInt()[0] )
pts_by_id[ stop_id ] = feature_id
# clear current stops
layerData.changeAttributeValues({ feature_id : { 9: QtCore.QVariant(None), 10: QtCore.QVariant(None), 11: QtCore.QVariant(None), 12: QtCore.QVariant(None), 13: QtCore.QVariant(None), 14: QtCore.QVariant(None), 15: QtCore.QVariant(None), 16: QtCore.QVariant(None), 17: QtCore.QVariant(None), 18: QtCore.QVariant(None) } })
# load a huge list of coded stops
stopcode = ''' MaconStop.new("445","Vineville Avenue and Craft Street",["1"],[-83.6468612269,32.8411410784],[],[,,,,,,,,,,,]),
MaconStop.new("447","Washington Street and College Street",["1"],[-83.6387002311,32.8391812902],[],[,,,,,,,,,,,]),
MaconStop.new("448","Vineville Avenue and Holt Avenue",["1"],[-83.6499982737,32.8420212388],[,,,,,,,,,,,],[,,,,,,,,,,,]),
MaconStop.new("449","Vineville Avenue and Ward Street",["1"],[-83.6506756945,32.842432113],[,,,,,,,,,,,],[,,,,,,,,,,,]),
...'''
# I shortened the huge list for this Gist
stopcode = stopcode.split('MaconStop')
firstline = 1
for line in stopcode:
print line
# the first line is blank - skip it
if(firstline == 1):
firstline = 0
continue
# determine stop ID and which route is confirmed by this line
stopid = line.split('"')[1]
routenum = line.split(']')[0]
routenum = routenum.split('[')[1]
routenum = routenum.replace('"','')
if(pts_by_id.has_key(stopid) == False):
# this stop was deleted
continue
# is on Route 1, so I set column [9] for this feature to be 1
if(routenum == "1"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 9: QtCore.QVariant(1) } })
# Route 2, column [10]
elif(routenum == "2"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 10: QtCore.QVariant(1) } })
# Route 3, column [11]
elif(routenum == "3"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 11: QtCore.QVariant(1) } })
# Route 4, column [12]
elif(routenum == "4"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 12: QtCore.QVariant(1) } })
# Route 5, column [13]
elif(routenum == "5"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 13: QtCore.QVariant(1) } })
# Route 6, column [14]
elif(routenum == "6"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 14: QtCore.QVariant(1) } })
# Route 9, column[15]
elif(routenum == "9"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 15: QtCore.QVariant(1) } })
# Route 11, column[16]
elif(routenum == "11"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 16: QtCore.QVariant(1) } })
# Route 12, column[18]
elif(routenum == "12"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 18: QtCore.QVariant(1) } })
# Route 13, column [17]
elif(routenum == "13"):
layerData.changeAttributeValues({ pts_by_id[stopid] : { 17: QtCore.QVariant(1) } })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment