简体   繁体   中英

unable to SetField ogr OFTDate field, how do I set a OFTDate field

Im using OGR to create a feature from another feature and write it out, it all seems to work except for the ogr.OFTDate field.

out_feature_fields = [
    ogr.FieldDefn("enc", ogr.OFTString),
    ogr.FieldDefn('edition', ogr.OFTInteger),
    ogr.FieldDefn('updno', ogr.OFTInteger),
    ogr.FieldDefn('usage', ogr.OFTInteger),
    ogr.FieldDefn('status', ogr.OFTInteger),
    ogr.FieldDefn('issue_date', ogr.OFTDate)
]

layer_def = ogr.FeatureDefn()
for field_defn in out_feature_fields:
    layer_def.AddFieldDefn(field_defn)

#logging here gives a date 20200601
logging.info(f"in feature {out_feature.GetField('DSID_ISDT')}")

outfeature = ogr.Feature(layer_def)
outfeature.SetGeometry(coverage_geom)
outfeature.SetField('enc', out_feature.GetField('DSID_DSNM')[:-4])
outfeature.SetField('edition', out_feature.GetField('DSID_EDTN'))
outfeature.SetField('updno', out_feature.GetField('DSID_UPDN'))
outfeature.SetField('usage', out_feature.GetField('DSID_DSNM')[2])
outfeature.SetFieldNull('status')
outfeature.SetField('issue_date', out_feature.GetField('DSID_ISDT'))

# logging here gives null
logging.info(f"out feature {outfeature.GetField('issue_date')}")

this logs

2020-06-29 17:40:08 INFO     in feature 20200611
2020-06-29 17:40:08 INFO     out feature None

So Im failing to set the date field and its Null in my output, Im thnking there is something Im missing about setting date fields in ogr but I dont know what and my search efforts have not helped so far.

Seems I had to set the date like this

feature.SetField('issue_date',  f"{year}-{month}-{day}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM