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

7433
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
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

weird...

... and what if you try this:

def main():
    import arcpy
    import os
    fc_in = r"C:\Forum\DistLinePol\test.gdb\Points" # this one exists
    fld_count = "Count"
    fc_out = r"C:\Forum\DistLinePol\test.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()

View solution in original post

29 Replies
XanderBakker
Esri Esteemed Contributor

You could use some python to do this:

def main():
    import arcpy
    import os

    fc_in = r"C:\Forum\DistLinePol\test.gdb\Points" # this one exists
    fld_count = "Count"
    fc_out = r"C:\Forum\DistLinePol\test.gdb\Points_out" # this one will be created

    # 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", fc_in)

    # 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()

This code can be transformed in a tool that you can use in a model...

BruceHarold
Esri Regular Contributor

Hi

The Data Interoperability extension has a Cloner transformer for this task, if you want a zero-coding approach.

It is an interesting problem, you're going to end up with coincident points, what is the geoprocessing task you're tackling?

Regards

0 Kudos
SethPaine1
New Contributor III

HI,

I want to get the average distance of a set of points from the start of a route.  However, my point feature class has points that represent multiple records.  In this case, the each point may represent many salmon redds (spawning beds).  I want to know average location of spawning beds per year along a river route, but since each point represents multiple beds, calculating the mean distance of the points along the route under-represents points with multiple beds.

Therefore, if I can generate a feature class where each point represents an individual spawning bed, I can then calculate mean distance along the route.

Cheers,

SP

0 Kudos
SethPaine1
New Contributor III

Hi, I was trying that script and returned this error:

Traceback (most recent call last):

  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript

    exec codeObject in __main__.__dict__

  File "D:\ArcGIS Tools\CreateDuplicateRecordsOnCountField.py", line 28, in <module>

    main()

  File "D:\ArcGIS Tools\CreateDuplicateRecordsOnCountField.py", line 14, in main

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

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 1800, in CreateFeatureclass

    raise e

ExecuteError: ERROR 000622: Failed to execute (Create Feature Class). Parameters are not valid.

ERROR 000628: Cannot set input into parameter spatial_reference.

Any ideas on how to solve this?

Thanks!

0 Kudos
XanderBakker
Esri Esteemed Contributor

When it creates the output featureclass it is taking the input featureclass as the spatial reference. The error indicates that the featureclass is not valid to extract the spatial reference from. What did you specify as input featureclass? Does it have a spatial reference (=coordinate system)?

0 Kudos
SethPaine1
New Contributor III

Yes, the input is projected in NAD_1983_UTM_Zone_10N

0 Kudos
XanderBakker
Esri Esteemed Contributor

weird...

... and what if you try this:

def main():
    import arcpy
    import os
    fc_in = r"C:\Forum\DistLinePol\test.gdb\Points" # this one exists
    fld_count = "Count"
    fc_out = r"C:\Forum\DistLinePol\test.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()
SethPaine1
New Contributor III

That did the trick.  Thank you!  Much appreciated.

0 Kudos
XanderBakker
Esri Esteemed Contributor

You're welcome. Glad it worked!

0 Kudos