Consider a Feature Class and two of its fields are "UTM_grid" and the "OID".
Scripting with python 2.7 for ArcGIS 10.3.
"UTM_grid" field contains the geographic info (e.g. B1SD40B) and was broken down into "UTM_block" + "UTM_sheet" (B1 + SD40B) and then using a dictionary and list structure UTM_block-->UTM_sheet-->OID was organized with this syntax:
{ 'UTM_block_1' : { 'UTM_sheet_A' : [ oid_1 , oid_2 ] }, { UTM_sheet_2 : [ oid_3 , oid_4, oid_5 ] } }
We need to create shapefiles with features (identified by the object value - oid) based on the follow data structure (OIDs will be organized regarding an UTM/location based structure):
- UTM_block_* will be a folder
- UTM-sheet_* its subfolder.
- List of objects (OIDs) will populate the shapefile.
Note: Features Classes to export are in a SDE geodatabase. We read/list the FCs in that GDB and for each FC we need to process the export of its features by UTM block-sheet into shapefiles. Looking at the fields "UTM_grid" and "ObjectID" we check while sheet ( utm_grid[1] ) = "foo", read the corresponding OID, and put the Feature into a Shapefile inside the (sub)folder (which has the associated sheet name). The same logic could be used to organize sheets inside the block folders.
Another point we have to deal with is that the shapefile should only have some fields of the original Feature Class:
We have used fieldmappings and arcpy.FeatureClassToFeatureClass_conversion() to create a non-UTM-organized shapefile from a Feature Class with the fields that we need.
Now the problem is how to process the feature export to get UTM-organized shapefiles! How can we manage it?
Bellow is what we have now inside a function:
with arcpy.da.SearchCursor(feature_class, ['UTM_grid' , 'OBJECTID']) as cursor:
dic = dict()
for row in cursor:
UTM_sheet_value = row[0]
oid_value = row[1]
try:
b_s_split = UTM_sheet_value.split('_') # split of block & sheet
except Exception, e:
pass
print(UTM_sheet_value)
print(e)
else:
dic.setdefault( b_s_split[0], {} ).setdefault( b_s_split[1], [] ).append( oid_value )
# create folders & subfolders (blocks & sheets) in a directory
dir_path = os.path.dirname("E:\\")
dirs = [ [b_s_split[0]] , [b_s_split[1]] ]
for item in itertools.product(*dirs):
if not os.path.isdir(os.path.join(dir_path, *item)):
os.makedirs(os.path.join(dir_path, *item))