I have created two functions: one to add a field to a layer and one to calculate a field. I am having issues with the calculate field function when trying to calculate values for a numeric column. I do not get an error, but the column is blank after the code runs (it was blank to begin with). I've tried both OFTInteger and OFTReal as field types with the same result. Here are the functions:
def add_field(lyr, field_name, field_type): field = ogr.FieldDefn(field_name, field_type) field.SetWidth(50) lyr.CreateField(field) def calc_field(calc_lyr, field, exp): for feature in calc_lyr: geom = feature.GetGeometryRef() area = geom.GetArea() feature.SetField(field, exp) calc_lyr.SetFeature(feature)
and here are the function calls to add and calculate a numeric field:
add_field(dxf_lyr, 'color', ogr.OFTInteger) calc_field(dxf_lyr, 'color', 150)
The field gets added, but the values don't get caculated. What am I missing?