Select to view content in your preferred language

Arcpy - search duplicate attributes, then update values

7988
24
Jump to solution
01-29-2015 08:10 PM
timdunlevie
Regular Contributor

Hi all,

Could someone point me in the right direction.

I have limited python scripting experience but know a enough (to be dangerous).

This is what I want to do.

I have a shapefile which has duplicate objects based on an "ID".

Object1: ID1 + Owner1

Object2: ID1 + Owner2

Object3: ID1 + Owner3

in this example there are 3 objects with identical ID but with 3 different owners for the same object.

What I want to do is search through the shapefiles for all identical "ID", then update a new shapefile with :

Object1: ID1 + Owner1 + Owner2 + Owner3

The new shapefile would have 3 extra columns added for the Owner field to be updated to.

so I want 1 object only but keep the owner information in the row.

thanks,

Tim

Tags (1)
0 Kudos
24 Replies
XanderBakker
Esri Esteemed Contributor

Good question... I don't think so.

The Alter Field tool that was released not so lng ago only allows you to change the name and alias of an existing field. However, you could delete the field, after the empty output featureclass is created and add the field with the same name, but with a larger length. before you start inserting the information. The downside is that it will change the order of the fields and the way to construct the output row would be a little more complex.

0 Kudos
XanderBakker
Esri Esteemed Contributor

If you don't mind the changed order of the fields, you could:

  • rename the existing OWNER field using Alter Field (ArcGIS Help (10.2, 10.2.1, and 10.2.2) ). eg "OWNER_old"
  • next add a new field with the name OWNER with the larger length
  • fill the new OWNER field with the values the "OWNER_old" field (Field Calculator)
  • Delete the "OWNER_old" field
  • then run the script
timdunlevie
Regular Contributor

Thanks Xander.

That’s exactly what I did !

Thanks also to the other replies again and the different approaches.

0 Kudos
XanderBakker
Esri Esteemed Contributor

You're welcome...

0 Kudos
AnthonyGiles
Honored Contributor

Tim,

You could use the feature class to feature class tool:

ArcGIS Help 10.1

In the field map parameters right click on your owner field and extend the size. Once created delete your original feature class and rename your new one to the same as your old one

Regards

Anthony

BlakeTerhune
MVP Regular Contributor

What is the purpose of removing (and returning) the item from the dictionary just before you write it to the shapefile? I don't see the "tmp" variable used anywhere else.

tmp = dct.pop(p_id)

0 Kudos
XanderBakker
Esri Esteemed Contributor

The reason is, when I ommit this, it wil write the equal amount of features to the output. Removing it from the dictionary will cause the condition on line 36 to return False (and not add an ID that has already been added).

If you need any additional explanation on the code, just let me know.

Kind regards, Xander

BlakeTerhune
MVP Regular Contributor

Thanks Xander, I see it now. I'm just trying to understand the code and your process.

So you are iterating through the fc_in rows and then checking if the current ID is in the dictionary before writing the data to fc_out. I was thinking it might be easier to iterate through the dictionary instead of fc_in so you wouldn't have to remove the dictionary item but then I don't know how you would find the correct row in fc_in to copy the data in the rest of the fields. Is there a way to do that? Seems like it could be a faster because you wouldn't have to touch every row of in_fc; you're done after checking every item in the dictionary.

0 Kudos
XanderBakker
Esri Esteemed Contributor

I see your point. However, I'm using the da cursors which is rather fast. Using the dictionary as basis, you would have to extract the corresponding feature from fc_in. This would require creating a cursor with probably a where clause on oid to extract the feature you're interested in. I think this would create a script that perfoms slower than what I am doing...

0 Kudos
BlakeTerhune
MVP Regular Contributor

Ah, yes, the where clause when you create the cursor! But then you would have to use a dynamic where clause and create a new cursor each time. I agree with you then, it would be silly to do that compared to just popping the item from the dictionary.

Thanks for the clarification Xander!

0 Kudos