4

I am trying to create a new column on attribute table of my shapefile. This table must be the FID formatted with 4 (or any number) digits (for instance: 0 = 0000, 1 = 0001, 2 = 0002...)

but I just can't, I must be missing something, I already take a look on other users codes and I cannot find my bug... My code just add a new column, but doesn't write anything...

def iterable_field_creator(shp_path, number=2 or 3 or 4):
    import ogr

    # Checking if parameter is a shp, create driver, datasource and layer
    if shp_path.endswith(".shp"):
        DriverName = "ESRI Shapefile"
        driver = ogr.GetDriverByName(DriverName)
        datasource = driver.Open(shp_path, 1) # 1 para escrever
        lyr = datasource.GetLayer()
        feature = lyr.GetNextFeature()
    else:
        print("\033[31mNOT A SHAPEFILE\033[m\n"
              "The program will be closed")
        quit()


    # Add new field on Att table (ID_it) 
    idField = ogr.FieldDefn("ID_it", ogr.OFTString)
    idField.SetWidth(number)
    lyr.CreateField(idField)

    ### I did try this way before
    # for features in lyr:
    #     fid = features.GetFID()
    #     id_it = formating_number(str(fid), number)
    #     feature = lyr.GetFeature(fid)
    #     print("The feature FID nº \033[31m{}\033[m will be called \033[33m{}\033[m on ID_it field!!!".format(
    #         fid, id_it))
    #     feature.SetField(id_it, "ID_it")
    # del features
    #

    while feature:
        fid = feature.GetFID()
        id_it = formating_number(str(fid), number)
        feature.SetField("ID_it", str(id_it))
        print("A feição de FID nº \033[31m{}\033[m terá o Identificador iterável de código \033[33m{}\033[m!!!".format(
            fid, id_it))
        feature = lyr.GetNextFeature()

    datasource.Destroy()

    osgeo = None
    ogr = None
    lyr= None
    driver = None
    DriverName = None

1 Answer 1

2

Everything was almost ok, I've reformatted back to for loop (your for loop had unnecessary "feature = lyr.GetFeature(fid)" line). The most important part was adding

lyr.SetFeature(feature)

in the loop to preserve changes

def iterable_field_creator(shp_path, number=2 or 3 or 4):

    import ogr

    # Checking if parameter is a shp, create driver, datasource and layer
    if shp_path.endswith(".shp"):
        DriverName = "ESRI Shapefile"
        driver = ogr.GetDriverByName(DriverName)
        datasource = driver.Open(shp_path, 1)  # 1 para escrever
        lyr = datasource.GetLayer()

    else:
        print("\033[31mNOT A SHAPEFILE\033[m\n"
              "The program will be closed")
        quit()

    # Add new field on Att table (ID_it)
    idField = ogr.FieldDefn("ID_it", ogr.OFTString)
    idField.SetWidth(number)
    lyr.CreateField(idField)

    for feature in lyr:  #reformatted
        fid = feature.GetFID()
        id_it = formatting_number(fid, number)
        feature.SetField("ID_it", str(id_it))
        lyr.SetFeature(feature)  #added in order to save field value

    datasource.Destroy()

    osgeo = None
    ogr = None
    lyr = None
    driver = None
    DriverName = None
3
  • Thank you very much! My code is working right now! Nov 29, 2020 at 18:53
  • @ConsalterR-1988 great, consider accepting this answer
    – MrXsquared
    Nov 29, 2020 at 21:45
  • For sure! it is done. Sorry, i am a newby here. Thank you very much for warn me about it! Dec 8, 2020 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.