0

Is it possible to number shapefiles? Before merging the single point layer I want to give them numbers. After filtering the merged files I need to split them (the number should be a marker for the splitting process).

  • Add a field and calculate it to a unique number for each file? – jbchurchill Feb 2 '15 at 19:07
  • number the name of the files, or number attributes within each file? – DPSSpatial Feb 2 '15 at 19:09
  • The attributes. – Pimpel Feb 2 '15 at 19:09
  • Does this work with batch processing? – Pimpel Feb 2 '15 at 19:10
1

give this a run. it should give an attribute called "number" and start from 0.

import  arcpy
from arcpy import env
import os
import time

arcpy.env.overwriteOutput = True

y =  #Your folder that contains all of the subfolders

def main(x):
    try:
        import arcpy, sys, traceback, os, glob, shutil
        arcpy.env.overwriteOutput = True
        masterFolder = x

        #collect a list of subfolders in master folder
        arcpy.env.workspace = masterFolder
        arcpy.ListWorkspaces('','Folder')
        subfolderLst = arcpy.ListWorkspaces('','Folder')
        print subfolderLst
        for subfolder in subfolderLst:
            arcpy.env.workspace = subfolder
            fcLst = arcpy.ListFeatureClasses()
            x = 0

            for fc in fcLst:
                arcpy.env.overwriteOutput = True

                file = fc in fcLst
                filename = "'" + arcpy.env.workspace + os.sep + fc + "'"
                print filename
                arcpy.AddField_management(fc, 'number','text')

                arcpy.CalculateField_management(fc, 'number', x, "PYTHON" )
                x += 1 




    except:
        print arcpy.GetMessages(2)



main(y)
1

I wrote this a while back and is should get you close. what this will do is step through all of the folders in a root folder, find the shape files, add an attribute called "shpname" and popuplate it with the path to that shape. you can modify it to just toss in the fc name instead of the path. then when you go to merge all of your shapefiles will have an attribute called shpname with each feature having the name of the original shapefile as a column

import  arcpy
from arcpy import env
import os
import time

arcpy.env.overwriteOutput = True

y = r"PATH TO ROOT FOLDER" #Your folder that contains all of the subfolders

def main(x):
    try:
        import arcpy, sys, traceback, os, glob, shutil
        arcpy.env.overwriteOutput = True
        masterFolder = x

        #collect a list of subfolders in master folder
        arcpy.env.workspace = masterFolder
        arcpy.ListWorkspaces('','Folder')
        subfolderLst = arcpy.ListWorkspaces('','Folder')
        print subfolderLst
        for subfolder in subfolderLst:
            arcpy.env.workspace = subfolder
            fcLst = arcpy.ListFeatureClasses()

            for fc in fcLst:
                arcpy.env.overwriteOutput = True
                file = fc in fcLst
                filename = "'" + arcpy.env.workspace + os.sep + fc + "'"
                print filename
                arcpy.AddField_management(fc, 'shpname','text')

                arcpy.CalculateField_management(fc, 'shpname', filename, "PYTHON" )




    except:
        print arcpy.GetMessages(2)



main(y)
  • Is it also possible with the processing tool via batch processing? I can do it with the field calculator tool but only for one file. The is to create a field in the table wich should be filled with numbers (shape1 should have '1' in the table, shape 2 = '2' and so on). – Pimpel Feb 3 '15 at 8:29

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.