Script to create duplicate points based on field value

1445
3
Jump to solution
08-15-2016 02:32 PM
RichardBunten1
New Contributor III

I can't get this script to work.  I am trying to create create a script that takes a point file with a single point representing a location and a field in its table "UNIT_NUMBE" and create duplicate points for that single point based on the value in this "UNIT_NUMBE" field.

Attached is the script that I have so far: 

 It bails out at line28 or the line that reads     CountRange = range(InRow.count)

I get a runtime error: Row: Field count does not exist

Anyone help me with this.  Thanks in Advanced.


					
				
			
			
				
			
			
				
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Code in question (I believe you do need a field called 'count', although I'm not familiar with the old cursors, anymore):

import sys, arcpy

#InFeatClass = sys.argv[1]
#OutFeatClass = sys.argv[2]

InFeatClass = "S:/GIS_Public/GIS_Data/DefaultGDB/Default.gdb/tempUnitsperParcel"
OutFeatClass = "S:/GIS_Public/GIS_Data/DefaultGDB/Default.gdb/tempUnitsperParcel4"

# overwriting output is ok.
arcpy.env.overwriteOutput = True

# copy the count = 1 features to create a new feature class, saves having to do
# new feature class, copy spatial reference and fields..

arcpy.Select_analysis(InFeatClass,OutFeatClass,"UNIT_NUMBE = 1")

# setup cursor objects - search the source, isert the destination
InsCur = arcpy.InsertCursor(OutFeatClass)
SrchCur = arcpy.SearchCursor(InFeatClass,"UNIT_NUMBE > 1")

# get the list of fields to copy
FieldList = arcpy.ListFields(InFeatClass,"*")
NoCalcFields = ["FID","OBJECTID","SHAPE"]
          
for InRow in SrchCur:
    # create a range and then iterate over it
    # inserting a new row for each count
    CountRange = range(InRow.count)
    for indx in CountRange:
          newRow = InsCur.newRow() # create a new (empty) feature
          # copy values from the source to the destination
        for ThisField in FieldList:
            if ThisField.name.upper() not in NoCalcFields:
                newRow.setValue(ThisField.name,InRow.getValue(ThisField.name))
        # copy the shape
        newRow.setValue("SHAPE",InRow.getValue("SHAPE"))
        # Store it
        InsCur.insertRow(newRow)     ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

3 Replies
DanPatterson_Retired
MVP Emeritus

to save a download and unzip, can you check the code to see if it is hardcoded to a field named 'count' or post a url to the original

DarrenWiens2
MVP Honored Contributor

Code in question (I believe you do need a field called 'count', although I'm not familiar with the old cursors, anymore):

import sys, arcpy

#InFeatClass = sys.argv[1]
#OutFeatClass = sys.argv[2]

InFeatClass = "S:/GIS_Public/GIS_Data/DefaultGDB/Default.gdb/tempUnitsperParcel"
OutFeatClass = "S:/GIS_Public/GIS_Data/DefaultGDB/Default.gdb/tempUnitsperParcel4"

# overwriting output is ok.
arcpy.env.overwriteOutput = True

# copy the count = 1 features to create a new feature class, saves having to do
# new feature class, copy spatial reference and fields..

arcpy.Select_analysis(InFeatClass,OutFeatClass,"UNIT_NUMBE = 1")

# setup cursor objects - search the source, isert the destination
InsCur = arcpy.InsertCursor(OutFeatClass)
SrchCur = arcpy.SearchCursor(InFeatClass,"UNIT_NUMBE > 1")

# get the list of fields to copy
FieldList = arcpy.ListFields(InFeatClass,"*")
NoCalcFields = ["FID","OBJECTID","SHAPE"]
          
for InRow in SrchCur:
    # create a range and then iterate over it
    # inserting a new row for each count
    CountRange = range(InRow.count)
    for indx in CountRange:
          newRow = InsCur.newRow() # create a new (empty) feature
          # copy values from the source to the destination
        for ThisField in FieldList:
            if ThisField.name.upper() not in NoCalcFields:
                newRow.setValue(ThisField.name,InRow.getValue(ThisField.name))
        # copy the shape
        newRow.setValue("SHAPE",InRow.getValue("SHAPE"))
        # Store it
        InsCur.insertRow(newRow)     ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
RichardBunten1
New Contributor III

Neil and Darren hit the nail on the head.  I changed the "NUMBE" field to "Count" and the code ran perfectly.

Thanks!

0 Kudos