I've got a similar question to this one How to programmatically populate a shapefile field with polygon areas in OGR?, but the solution doesn't seem to be working for me. I am trying to import a shapefile from PostGIS into python using OGR, add a field, then populate the field with data from a csv based on an ID and the date. When I print the fields for the feature during the loop, the field gets the correct data but it isn't writing to the layer o3_proj (last two print statements). Am I using the SetFeature function incorrectly? I'm new to PostGIS and ogr, so please speak slowly!
import os, ogr, gdal, csv driver = ogr.GetDriverByName('PostgreSQL') datasource = driver.Open("PG: host='localhost' port='54321' dbname='chicago' user='postgres'", update = 1) o3_proj = datasource.GetLayerByName('o3_monitors(geom3528)') date =  maxozone =  aqi =  aqs_site_id =  f = csv.reader(open('/ozone/ozone_data.csv','rU')) x = list(f) for iii in range(1,len(x)): date.append(x[iii]) aqs_site_id.append(x[iii]) maxozone.append(float(x[iii])) aqi.append(int(x[iii])) daily_field = ogr.FieldDefn('temp_o3',ogr.OFTReal) o3_proj.CreateField(daily_field) for feat in o3_proj: for jjj in range(0,len(date)): if aqs_site_id[jjj] == feat.GetField(0) and date[jjj] == '03/06/2005': o3_proj.SetFeature(feat) feat.SetField2(6,maxozone[jjj]) o3_proj.SetFeature(feat) print str(feat.GetField(0))+','+str(feat.GetField(1))+','+str(feat.GetField(2))+','+str(feat.GetField(3))+','+str(feat.GetField(4))+','+str(feat.GetField(5))+','+str(feat.GetField(6)) print o3_proj.GetFeature(feat.GetFID()).GetField(6)
Result of the print statements:
17-031-0064,41.79079,-87.60165,2001,2012,None,0.036 None 17-031-0076,41.7514,-87.71349,2004,2012,None,0.038 None 17-031-4007,42.06028,-87.86323,2002,2012,None,0.035 None 17-031-4201,42.14,-87.79922,2001,2012,None,0.031 None