Select to view content in your preferred language

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

11182
29
Jump to solution
12-30-2014 10:05 AM
SethPaine1
Occasional Contributor

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
SethPaine1
Occasional Contributor

As you suggested, I made a toolbox tool from the script and it works perfectly for points.  A funny thing happens when I try it with polygons though (after changing the feature type to POLYGON in the script).  It seems to work and produces a polygon layer, complete with a table representing the duplicated features.  But the polygons will not draw.  If I zoom to the full extent of the feature class, it zooms to the world, but if I zoom to any particular feature, the extent does not move at all.  I don't really need this tool for polygons right now, but am curious as to that particular behavior.

PS, what do you normally work on with GIS?  Any tips for learning python without taking expensive ESRI classes?

Cheers,

Seth

0 Kudos
XanderBakker
Esri Esteemed Contributor

There are so many resources on the Internet that can help you to learn Python, it is to much to mention.

One way is to do a search in GeoNet: https://community.esri.com/search.jspa?q=learn+python  or just Google it.

You will find a large number of post with lots of like to different sites like:

Dive Into Python

Learn Python

If you are willing to pay for a book, then this post references a very good book:

Re: Any Good Python Books

I have posted some Python Snippets that might help:

Some Python Snippets 

EthanDuke
Regular Contributor

Seth, I'm trying to accomplish this as well (with points only), but I'm not very skilled in scripts. I also tried the data interoperability tool mentioned and failed. I've spent more time trying to do this than I care to mention and for several years I have been manually doing this for tens of thousands of features (bird detections). This sure would be a game changer for me and our little NGO. I've tried several times with the python provided, but I couldn't overcome the errors. I know it has been a while, but could you share the modelbuilder with me or let me know how you accomplished this?

Thanks!

Ethan

0 Kudos
XanderBakker
Esri Esteemed Contributor

If it is a process that you will repeat, it might be good to create a tool from the script. Can you provide a sample of your data and a brief explanation of which field contains the number a feature should be repeated? Also please provide the version of the software you have ArcMap or ArcGIS Pro. I will see if I can create the tool for you so you can run this more easily. Anyway, for other workflows it might come in handy to learn some python.

EthanDuke
Regular Contributor

Thanks for reaching out, Xander. I've provided a link to the data, here. The field containing the number for which each feature is to be repeated by is "Count_". All attributes need to remain the same. It would be great if the count field for the copied records was change to one, but that can be done easily enough after an otherwise successful script run. I'm using the latest version of ArcMap 10.5.0.6491. I hear you on the need to learn Python. I'm completely self-taught in GIS. It's been years of banging of my head against wall until I found solutions and workflows that would have been much easier to accomplish if I knew python. I've got my hands full running our small NGO that covers a lot of ground. I'll work on learning more from the resources mentioned above.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Ethan Duke ,

A small correction,the current version of ArcMap is 10.6 (10.6.0.8321). I just changed the code to create the tool and to do a test run. The updated code is:

def main():
    import arcpy
    import os

    # parameters of tool
    fc_in = arcpy.GetParameterAsText(0)
    fld_count = arcpy.GetParameterAsText(1)
    fc_out = arcpy.GetParameterAsText(2)

    # create the empty output featureclass
    arcpy.AddMessage("Create output featureclass...")
    sr = arcpy.Describe(fc_in).spatialReference
    shp_type = (str(arcpy.Describe(fc_in).shapeType)).upper()
    path, name = os.path.split(fc_out)
    arcpy.CreateFeatureclass_management(path, name, shp_type, fc_in, "SAME_AS_TEMPLATE", "SAME_AS_TEMPLATE", sr)

    # insert the features into the output fc
    cnt_feats = 0
    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]
                lst_row = list(row)
                lst_row[idx_cnt] = 1
                row_out = tuple(lst_row)
                for i in range(0, cnt):
                    cnt_feats += 1
                    if cnt_feats % 1000 == 0:
                        arcpy.AddMessage("Writing feature: {}".format(cnt_feats))
                    curs_out.insertRow(row_out)

    arcpy.AddMessage("Finished writing {} features...".format(cnt_feats))


if __name__ == '__main__':
    main()

The tool will look like this:

When I did some test runs the feature get duplicated based on the selected count field and the count field is reset to 1.

I did notice that your data has a GlobalID field. The problem with a GlobalID field is when write the the feature the GlobalID is changed in the output:

Do you have any attachments related to the points?

I can attach the toolbox, but possibly you won't be able to open it with ArcMap 10.5. I could create a 10.3 version at home which will be compatible with 10.5 that you have.

EthanDuke
Regular Contributor

Hi, xander_bakker,

This is great. I'm working remotely and will secure more robust internet and update ArcMap (within 48 hours). The attachments and/or GUID are not necessary with this particular feature class after this stage of the workflow. I will work on this ASAP with results. Thank you!!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Ethan Duke ,

I have attached the toolbox (10.6). If you have any problems upgrading to 10.6 or with the toolbox, just let me know and I see what I can do.

Kind regards, Xander

CollinHorace
Emerging Contributor

Hi Xander,

This is an excellent tool. However, I can not get it to work with a geodatabase or sde input. Is there something that I need to change? Any help would be greatly appreciated. Screenshot below:

Python Error

XanderBakker
Esri Esteemed Contributor

Hi collinhorace ,

The error refers to a specific field called "StoreNum". Can you verify what type of field it is in both input and output featureclass and specify if the input and output featureclass are in different types of workspaces? I see the output is written to the default file geodatabase. Where is the input stored?