Use row as input to other processes in python

2562
1
Jump to solution
12-05-2014 01:19 PM
DavidCampbell
New Contributor III

I am new to python and need help. Looked for my answer but don't know if I am even using the correct terminology.

I have a model in which I iterated through rows and performed a set of processes on each. For example a feature class with 10 features and on each of the 10 features I wanted to create an individual feature class....or... for each row create a raster based on the field (e.g.) ID. I am trying to turn this to python due to a host of reasons.

Here is what I have so far, but I cannot get it to work. Any help would be appreciated.

 

# Import arcpy module
print "Starting...."
import arcpy
from arcpy import env

# Check out any necessary licenses
arcpy
.CheckOutExtension("spatial")
env
.overwriteOutput = True

selectingfeatures
= "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Processing\\TEMP\\trans1.shp"
Trans1_shp = "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Processing\\TEMP\\trans1.shp"
basecldem
= "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Data\\From_Client\\20141023_DataDumpFromBrent\\ClosureSurface\\basecldem"
TEMP
= "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Processing\\TEMP"
Zone3_shp = "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Processing\\TEMP\\Zone2.shp"
transarea
= "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Processing\\TEMP\\transarea"
RegionGRP = "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Processing\\TEMP\\regiongrp"
ZonalMean = "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Processing\\TEMP\\zonalmean"
ZoneArea = "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Processing\\TEMP\\zonearea"
Transition__n_ = "\\\\silver\\clients\\SYN\\Projects\\P696\\8_BaseMine\\Processing\\TEMP\\transition_%n%"
I_trans1
= "I_trans1_FID"

selectingLayer
= arcpy.MakeFeatureLayer_management(selectingfeatures)
rows
= arcpy.SearchCursor(selectingLayer)
#cursor = arcpy.SearchCursor(fc)
#row = cursor.next()
#while row:
# print(row.getValue(field))
for row in rows:
  arcpy
.FeatureClassToFeatureClass_conversion(row, TEMP, "Zone2.shp", "", "ID \"ID\" true true false 4 Short 0 4 ,First,#,\\\\silver\\clients\\SYNCRUDE\\Projects\\P696\\8_BaseMine\\Processing\\TEMP\\trans1.shp,ID,-1,-1", "")
  
print "Finished PFC 2 FC...."
# Process: Polygon to Raster
  arcpy
.PolygonToRaster_conversion(Zone3_shp, "ID", ZoneArea, "CELL_CENTER", "NONE", "1")
  
print "Finished Polygon to Raster...."
# Process: Extract by Mask
  arcpy
.gp.ExtractByMask_sa(basecldem, Zone3_shp, transarea)
  
print "Extract by Mask...."
# Process: Region Group
  arcpy
.gp.RegionGroup_sa(ZoneArea, RegionGRP, "FOUR", "WITHIN", "ADD_LINK", "")
  
print "Finished Region Group...."
# Process: Zonal Statistics
  arcpy
.gp.ZonalStatistics_sa(RegionGRP, "VALUE", transarea, ZonalMean, "MEAN", "DATA")
  
print "Finished Zonal Statistics...."
# Process: Raster Calculator
  arcpy
.gp.RasterCalculator_sa("If( \"%basecldem%\" > ( 0.5 + \"%ZonalMean%\" ), 0, 1 )", Transition__n_)
  
print "Finished Raster Calcualtor"

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JonesGeorge
New Contributor II

Hi David,

Here is a code that will split the features based on number of rows. i.e., if a shapefile contains 10 rows it will split the shapefile into 10 different shapefiles. I hope this will help:

Open the mxd and run the this the python window in ArcGIS. Instead of "sample" put your layer name that appears in the Table of Contents in ArcMap

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "sample", df)[0]
fields = ['FID']
with arcpy.da.SearchCursor(lyr, fields) as cursor:
    for row in cursor:
        expression =  " FID = "+str(row[0])+" "
        arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",expression)
        arcpy.RefreshActiveView()
          #copying the features selected to an output location
        arcpy.CopyFeatures_management(lyr, r"C:\out2\\"+str(row[0])+".shp")

Thanks

-Jones

View solution in original post

0 Kudos
1 Reply
JonesGeorge
New Contributor II

Hi David,

Here is a code that will split the features based on number of rows. i.e., if a shapefile contains 10 rows it will split the shapefile into 10 different shapefiles. I hope this will help:

Open the mxd and run the this the python window in ArcGIS. Instead of "sample" put your layer name that appears in the Table of Contents in ArcMap

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "sample", df)[0]
fields = ['FID']
with arcpy.da.SearchCursor(lyr, fields) as cursor:
    for row in cursor:
        expression =  " FID = "+str(row[0])+" "
        arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",expression)
        arcpy.RefreshActiveView()
          #copying the features selected to an output location
        arcpy.CopyFeatures_management(lyr, r"C:\out2\\"+str(row[0])+".shp")

Thanks

-Jones

0 Kudos