Scripting (Python) Feature Class content into Shapefiles based on an UTM data structure

4157
21
Jump to solution
04-26-2016 02:49 AM
EduardoAbreu-Freire
New Contributor III

Consider a Feature Class (part of an Enterprise GDB content) and two of its fields are "UTM_grid" and the "OID".

Scripting with python 2.7 for ArcGIS 10.3.

"UTM_grid" field was broken down into "UTM_block" + "UTM_sheet" 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.

---

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_grid_value = row[0]
            oid_value = row[1]

            try:
                b_s_split = UTM_grid_value.split('_')     # split into block & sheet

            except Exception, e:
                pass
                print(UTM_grid_value)
                print(e)

            else:
                # create folders & subfolders (blocks & sheets) in a directory
                dic.setdefault( b_s_split[0], {} ).setdefault( b_s_split[1], [] ).append( oid_value )
                dir_path = os.path.dirname("E:\\")     # full path to directory
                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))
0 Kudos
21 Replies
BlakeTerhune
MVP Regular Contributor

My suggestion would be to do something like this where you can specify the beginning of the value. Assuming the structure of the FOL_250K field values will stay the same, you can specify just the block or the block and sheet.

# Get list of distinct UTM values
utm_field = "FOL_250K"
utm_prefix_filter = arcpy.GetParameterAsText(0)
if utm_prefix_filter:
    where_clause = "FOL_250K LIKE '{}%'".format(utm_prefix_filter)
else:
    where_clause = ""
sql_prefix = "DISTINCT {}".format(utm_field)
sql_suffix = None
distinct_utm = [
    i[0] for i in arcpy.da.SearchCursor(
        feature_class, utm_field, where_clause, sql_clause=(sql_prefix, sql_suffix)
    )
]
BlakeTerhune
MVP Regular Contributor

Sorry, I just realized that where_clause doesn't use the utm_field variable I had set up. Line 5 from the code above should be:

where_clause = "{} LIKE '{}%'".format(utm_field, utm_prefix_filter)