I have a polygon shapefile which displays villages [NAMING]. On its attribute table I have, in one Field, their names and for each feature and I have its municipality too [POLYGON_NM].

Now what I want to do is to find if I have features repeated into the same municipality.

I wanted to work with ModelBuilder, and I've tried with some scripting code into Field Calculator, but it's a disaster more than success.

Here is a screenshot:



If I understand correctly, you want to determine if you have duplicate NAME values within the same municipality. Here is a quick way that may do what you want.

  1. Create a new field in your table called Merged.
  2. Use the field calculator to concatenate the two string columns, [POLYGON_NM] + [NAMING]
  3. Right click on the Merged column and choose Summarize from the popup menu and click the OK button on the dialog. The output table will will have a column called Count, if there is a number greater than one in this column then you will have identified duplicate records in the original table.
  • Well, it fits perfectly as I wanted. Now I'll join the summarized table with the original one, and just "Select by attributes" where Count > 1 . Many thanks @dblanchett ! – juasmilla Aug 6 '14 at 8:51

I am providing a scripted alternative either in the interactive window or as a stand alone script.

layer = "YOURLAYERNAME" #interactive window in quote, standalone script needs path
arcpy.AddField_management(layer, "Same", "TEXT")
rows = arcpy.UpdateCursor(layer)
for row in rows:
    if str(row.getValue("POLYGON_NM)) == str(row.getValue("NAMING"):
        row.Same = "Yes"

Then you can select by attributes or do a definition query to look for duplicated municipalities.

PS The cstr() isn't necessary, but I am used to dealing with integer fields when comparing so it is habit.

After I thought about this a while

I thought there may be a more fun way of doing this. One where you didn't need another column to house the selected features but just keep appending each feature to the current selection.

arcpy.SelectLayerByAttribute_management(layer, "CLEAR_SELECTION") #clear selection if present
rows = arcpy.SearchCursor(layer) #no need for update cursor so we can use search
for row in rows:
    if row.getValue("POLYGON_NM) == row.getValue("NAMING"): #find those in common
        arcpy.SelectLayerByAttribute_management(layer, "ADD_TO_SELECTION", ' "FID" = \'' + row.getValue("FID") + '\'') #take this feature's FID (assuming it is there or use some other unique identifier and add it to selected features.  If none are selected, it creates a new selection.

This should create a new selection for you. I am assuming the shapefile has a FID or some other unique identifier. I haven't tested this, and I pieced some of it together from some ArcMap documentation. I probably messed up the quotations around the WHERE clause part, but that is where the fun of debugging comes in on your part.

Read through the arcpy parts of these to verify me and/or improve it: Select Layer By Attribute (Data Management) SearchCursor

  • it seems to fit as well as the other, and simpler.Thanks for all @Branco . I'll try and comment. – juasmilla Aug 8 '14 at 6:55
  • What is cstr()? Also, nesting SelectLayerByAttribute() inside a tight for loop like that is going to be quite bad on performance. It would be loads faster to create a list and then use IN for your sql_clause only once. – Paul Aug 8 '14 at 16:38
  • Whoops! I am mixing languages with cstr(). Meant to be str(). I thought about a list and then setting up a semi-complex query out of it (which I wanted to avoid), but I didn't think about using the IN operator in SQL. I should brush up more on my SQL skills. Thanks for the correction and addition! – Branco Aug 8 '14 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.