Copy features a number of times based on a numeric field value

9706
29
Jump to solution
12-30-2014 10:05 AM
SethPaine1
New Contributor III

I have a point feature class with a Count field.  I want to copy each feature in the feature class a number of times based on the number in the Count field.  For example, if a feature has a Count value of 6, I want six new features created with all the same attributes as the original feature.

All the iteration model tools I can find want to group features based on an attribute value, but I want the number of iterations to be equal to the numeric value in my Count field.

Thanks

0 Kudos
29 Replies
EthanDuke
New Contributor III

Hey Xander Bakker‌, This works like a charm as long as I don't have the workspace feature class open in the Table of Contents. Thank you so much! Python is our friend.

(This post is edited with a strikethrough of text as I was unable to replicate the error)

XanderBakker
Esri Esteemed Contributor

Hi Ethan Duke ,

Can you explain what you mean with that it doesn't work when have the featureclass open in the TOC? I suppose you mean the output featureclass and you want to overwrite it? I just did a test with the input featureclass in the TOC and it works without problems on my computer:

When running it just works (when a new output is created):

0 Kudos
EthanDuke
New Contributor III

Xander Bakker‌,

I'm unable to recreate the error. I had run the tool unsuccessfully with the original feature class open in the Table of Contents. I ran the tool again with the layer removed and it was successful. I must have been incorrect in assuming the error was caused by lock issue. It is working well. Mine works just as yours (above screenshots). I'm surprised at how quickly it runs.

AmeliaCorit
New Contributor

Hi Xander, 

I have run the code and the geoprocessing tool and I am having the following error message

Runtime error
Traceback (most recent call last):
File "<string>", line 24, in <module>
File "<string>", line 21, in main
TypeError: value #7 - unsupported type: tuple

Any ideas on how to solve this?

Thanks!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Amelia7 ,

If you can share the code you used and (a sample of) the data used I can track where things are going wrong.

0 Kudos
AmeliaCorit
New Contributor

Hi Xander,

Thanks for your response.

The code that I am using is as follow:

>>> def main():

... import arcpy

... import os

... fc_in = r"F:\Code.gdb\Data2" # this one exists

... fld_count = "No_Adress"

... fc_out = r"F:\Code.gdb\Points_out" # this one will be created

... sr = arcpy.Describe(fc_in).spatialReference

...

... # create the empty output featureclass

... path, name = os.path.split(fc_out)

... arcpy.CreateFeatureclass_management(path, name, "POINT", fc_in, "SAME_AS_TEMPLATE", "SAME_AS_TEMPLATE", sr)

...

... # insert the features into the output fc

... with arcpy.da.SearchCursor(fc_in, '*') as curs_in:

... flds_in = curs_in.fields

... idx_cnt = flds_in.index(fld_count)

... with arcpy.da.InsertCursor(fc_out, '*') as curs_out:

... for row in curs_in:

... cnt = row[idx_cnt]

... for i in range(0, cnt):

... curs_out.insertRow(row)

...

... if __name__ == '__main__':

... main()

...

Runtime error

Traceback (most recent call last):

File "

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Amelia7 ,

Thanks for sharing your code, but can you also share (a sample of) your data?

0 Kudos
SethPaine1
New Contributor III

I did develop a terribly tedious work around (I am not skilled in python).  I first exported my points to a personal geodatabase.  Then, using Access, I followed the process I found here within the personal geodatabase:

Creating duplicate rows - microsoft.public.access.queries

I created a table [tblNums] with a single numeric field [Num] and values from 1 to the maximum quantity (1-102 in my case).

Then I created a a make-table query of my current table and tblNums (do not link the two tables). I set the criteria

under the [Num] field to:

  <=[Count]

This created a number of duplicate records for each original record equal to the number in my original Count field.

I then imported the table into ArcMap, and exported as a new table to give it an OID field.  Then I took my original point feature class and populated two coordinate fields.  Next I joined my original feature class point data table to my new table with replicas (based on a Unique ID).  I then created two coordinate fields in my new table and copied the coordinates from the origin table.  Then I removed the Join.  Lastly, I projected my new table with duplicates based on the coordinate data.

It did work, and now I have my original point data with duplicate overlaying features based on the number in my count field.  But I wouldn't want to incorporate this process into a regular work flow.

XanderBakker
Esri Esteemed Contributor

I guess the code I provided earlier represents a slightly simpler way to duplicate the points...

BrendanSoulé
New Contributor III

So glad I found this thread! Had the exact same issue I was trying to solve and this was the perfect solution.

 

Thank you!

0 Kudos