I have a shapefile made up of polylines. Polylines have a contour attribute. I want create new shapefiles based on these contour attributes. Something like this:

depth = [-10, -20, -30, -40]
map = myfile.shp
for d in depth:
    for row in map:
        if row.contour == d
            add to newshape_d.shp

Sorry if this code makes no sense, but that is the gist of what I am trying to do.

  • Use Split By Attributes
    – BERA
    Sep 2, 2020 at 9:18
  • @BERA unfortunately this will split by every type of contour depth and I only want to select a few.
    – jusef
    Sep 2, 2020 at 9:54
  • Field calculate first and divide them into depth groups, or select by attribute
    – BERA
    Sep 2, 2020 at 10:07
  • 2
    If you stay with ArcPy rather than using Split By Attributes then you should look into using a SearchCursor to read features from the first shapefile and then an InsertCursor to write them to the second shapefile.
    – PolyGeo
    Sep 2, 2020 at 10:25
  • @PolyGeo thanks, I will try this.
    – jusef
    Sep 2, 2020 at 10:44

2 Answers 2


A very straightforward approach would be to select the features that match the values in depth and write them to a the new shapefile

import arcpy

depth = [-10, -20, -30, -40]

arcpy.MakeFeatureLayer_management(map, 'subset', 'contour IN {}'.format(depth)) #the SQL syntax may need tweaking
arcpy.CopyFeatures_management('subset', 'newshape_d.shp')

I agree with PolyGeo, SearchCursor and InsertCursor are the fastest ways to go here.

I usually first create a search cursor to iterate through the entire source feature to populate a dictionary or list of what I want to add to the new shapefiles.

Then I will iterate through that dict or list and use an insert cursor to add the records to each file I want respectively. Usually iterating though dictionaries and lists are much much faster than dealing with shapefiles and tools. There is a bit of overhead in creating the dict but your still money ahead and this way you only have to create 1 Search Cursor and as many InsertCursors as you have contour intervals.

Some relevant docs for you

Here is an example script.

import arcpy
import os

workspace_path = r"C:\pathtoworkspace"
PolylineShapefile = r"C:\PathToFile"

tf = r"C:\PathToFile"  # path to a template shapefile if you want, makes it easier to set up fields you want.
sr = arcpy.SpatialReference(1000) # spatial reference

ContourList = [10, 20, 30, 40]
contour_dict = {}

# iterate through current file and grab contours you are interested in
for contour in ContourList:
    # Note you can specify the fields the SearchCursor will access, for performance they used to recommend
    # Spelling out which fields you wanted but in the docs now.
    with arcpy.da.SearchCursor(PolylineShapefile) as cursor:  #Search cursor
        for row in cursor:  # IterateThrough rows
            if row[1] == contour:  # if which ever field contour is in matches
                # beautiful function that will create a new list or append to existing in dict
                contour_dict.setdefault(contour, []).append(row)

# iterate through the dict to add to new shapefile.
# keys should be your contor interval and the value is a list of records from original table.
for contour, rows in contour_dict.items():
    # Process Filename and create new shp.
    target_filename = "{contour}ContourExport.shp".format(contour)
    arcpy.CreateFeatureclass_management(workspace_path, target_filename, "POLYLINE", template=tf, spatial_reference=sr)
    # Create a search Cursor to add in new selected records
    with arcpy.da.InsertCursor(os.path.join(workspace_path,target_filename)) as cursor:
        # iterate through the records for the specified contour and add to newly created shapefile
        for row in rows():

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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