I'm looking for a solution to automate a process I have found myself re-doing manually quite often. I have a dataset that consists of one column named "quarter" that is formatted "2018 Q1" (or Q3, or Q4, etc, going from 2016 to 2050) and several thousand rows. What I need to do is to select only the numeric part of that field and put it in a new field called "year." Once this is done, I need to split the layer into new shapefiles; one for every year.

I've tried ModelBuilder but I just don't understand the workflow and can't get it to work.

If anyone has python script that does something similar to this I can try changing it.

  • Does this sound like your typical workflow? 1) Create a new field (string or numeric) 2) Calculate the value of that field based on the "yyyy qq" formatted field 3) select the rows pertaining to each specific year 4) write the selection to a new shapefile?
    – smiller
    Mar 5 '18 at 16:48
  • what software are you using?
    – atxgis
    Mar 5 '18 at 16:49
  • Yes. Add New field, Select by Attribute, Field Calculator. Then I split the shapefile in QGIS. I work with QGIS and ArcMap.
    – 1buzz
    Mar 5 '18 at 16:53
  • Would you be OK doing the split in arcpy rather than QGIS?
    – smiller
    Mar 5 '18 at 16:57
  • Yes, that's not an issue at all.
    – 1buzz
    Mar 5 '18 at 16:57

This answer uses arcpy / ArcGIS 10.5. Especially if you have a different version, I recommend putting the tools into Model Builder to check whether the arcpy syntax is different. (I assumed you are using ArcGIS due to the mention of model builder).

arcpy.AddField_management(in_table=datasetname, field_name='Year',
                          field_type='TEXT', field_length=4)
arcpy.CalculateField_management(datasetname, "Year",
                                                '!quarter!.split(" ")[0]', "PYTHON_9.3")
yearlist = [str(x) for x in range(2016, 2051)]
for year in yearlist:
    whereclause = '"Year" = ' + "'%s'" % year
    # print whereclause
            "selectedYears", "NEW_SELECTION", whereclause)
    selectedYearsfile = "year " + year + ".shp" # rename and provide your path. Could use os.path.join if you want to write it to a different folder
    arcpy.CopyFeatures_management("selectedYears", selectedYearsfile)

If there's a concern that some years may not have any data, add in a GetCount_management before writing out the file.

  • That was quick. Thanks. I'll try it out and let you know. Thanks a lot!
    – 1buzz
    Mar 5 '18 at 17:17
  • The first part, selecting values and generating a new "Year" column works and I´m fine with that. The second part, where it splits the layer doesn´t work for me. I get a 999998 error. But I can live with it. I can do the split easily in QGIS. Thanks again for your help!
    – 1buzz
    Mar 5 '18 at 19:24
  • Glad to hear it's at least partially working for you! The error code isn't very descriptive; per this thread you can try turning off background processing in the arcpy environment... or maybe you have to set up the environment ahead of time (not included in my script snippet -it appears as arcpy.env.workspace = pathtoworkspace )
    – smiller
    Mar 5 '18 at 20:59
  • I was first getting an error with the "%i" in line 9 (variable not defined). So I removed it, ran the code again. After that, I started getting that 999998 error. I put the "%I" back again and the syntax error was gone and just kept getting error 999998. I'll test it tomorrow again and if I get it fixed I'll post it here.
    – 1buzz
    Mar 6 '18 at 1:12
  • 1
    And of course you're right! It finally worked. Thanks a lot for your help. Another thing I changed is the last line. I put it within the For; otherwise it just created a file for the last selected year.
    – 1buzz
    Mar 7 '18 at 16:22

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