1

I have a large grid file, and I am looking to extract each individual polygon that makes up the grid into a new layer. Essentially, I would like to replicate selecting a layer by mouse click, then "Create Layer from Selected Features".

I am familiar with coding in ArcPy and the Python environment as a whole, I suspect the best approach is to script this process there. My grid table does have each cell attached to a unique number, should this prove useful.

I've written the following code to attempt this:

import arcpy
import sys 
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = "C:/Users/Eric/Documents/"

#Set Local Variables
in_grid = "fishnet_clipped.shp"
out_feature = "polygrid2.shp"
where_clause = ' "FID" = \'2\' '

# Executing Select
arcpy.Select_analysis(in_grid, out_feature, where_clause)
  • So you want one layer per feature, each containing a single feature.. is that right? Do you have any code to build on? You say you are familiar with arcpy but I would like to see some of your code to understand just how familiar you are to propose an answer you will understand without covering topics you already know. – Michael Stimson Jul 16 '17 at 21:13
  • Don't forget to take the Tour. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. – PolyGeo Jul 16 '17 at 21:18
  • @MichaelStimson That's exactly correct! I've attached code to start building off of, I'm curious if I am in the right direction, once I establish the code I hope to save each layer as individual shapefiles as well. In addition, I would like to loop the script based on the varying FID code (1,2,3,4,5, etc.) – E. Weglarz Jul 16 '17 at 22:52
  • 1
    I would start with an arcpy.da.SearchCursor resources.arcgis.com/en/help/main/10.2/index.html#//… and establish the where clause 'FID = {}'.format(row[0]) for the selection then make feature layer with the output, unless there's some reason that you need to reference the original feature class, and then just for finesse use Apply Symbology from Layer resources.arcgis.com/en/help/main/10.2/index.html#//… to make each layer have the same symbology. – Michael Stimson Jul 16 '17 at 22:57
2

As I said in my comment I would start a search cursor to get each FID then use that value to export each feature.. As the input is a shapefile you could use the count of features and make a range but it's better not to count on that as next time the input could be a geodatabase feature class where OIDs are not contiguous.

import arcpy
import sys 
from arcpy import env
# from arcpy.sa import * # not needed

# Set environment settings
env.workspace = "C:/Users/Eric/Documents/"

# assuming we're starting in ArcMap
mxd = arcpy.mapping.MapDocument("Current") # reference the current map document
df  = arcpy.mapping.ListDataFrames(mxd)[0] # get the first data frame

#Set Local Variables
in_grid = "fishnet_clipped.shp"
sym_lyr = "make your layer file or map layer and symbolize" # template layer

with arcpy.da.SearchCursor(in_grid,'FID') as sCur:
    for row in sCur:
        out_feature  = "polygrid{}.shp".format(row[0]) # put the FID into the name
        where_clause = "FID = {}".format(row[0])       # put the FID into the where clause
        # Executing Select
        arcpy.Select_analysis(in_grid, out_feature, where_clause)
        arcpy.MakeFeatureLayer_management(out_feature,"FID_{}".format(row[0]))    # make a new layer
        arcpy.mapping.AddLayer(df,"FID_{}".format(row[0]))                        # add it to the map
        arcpy.ApplySymbologyFromLayer_management("FID_{}".format(row[0]),sym_lyr) # symbolize from template layer

arcpy.RefreshTOC()       # prompt to update the TOC (table of contents)
arcpy.RefreshActiveView()# and active view (map)

This code assumes you're running it from ArcMap and will add each exported feature class as a layer as it is created. If you do not wish to symbolize the layer the same as another layer then comment out or remove the apply symbology line.

|improve this answer|||||

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.