I would like to assign a unique ID to polygons in a dataset. The catch to my issue is that I would like polygons that fall within a polygon to have the same unique ID. My example is related to buildings. I have many buildings in a dataset. Each building should have a unique ID, however, buildings that are comprised of multiple polygons (see graphic) should have the same unique ID. I know of a way to do this through a series of dissolves, and spatial joins, but I would like to do this in a calculation. My preference is to do this in ArcMap, but QGIS would also be acceptable.

Any thoughts or help would be appreciated.


enter image description here


Here is a graphic of what I am hoping to resolve. All touching polygons have the same ID. This is unique in the entire dataset, not with the building that is shown.

enter image description here

  • You're probably not going to get away with doing this without some kind of a spatial join. Have you thought about incorporating this script? – Fezter Oct 10 '13 at 23:10
  • In your diagram have your three smaller polygons been "clipped into" (coplanar) with the larger one or are they overlapping? – PolyGeo Oct 10 '13 at 23:55
  • The polygons do not overlap. They are coplannar. – Ryan Garnett Oct 15 '13 at 15:28
  • Would you be able to revise your diagram to include some numbers as an illustration of what you are after. For example in your diagram I am thinking that you want one polygon to be given the first "unique ID" and four others to be given the second, but including them in the diagram would make this clear. – PolyGeo May 5 '14 at 7:06
  • This topic provides a solution of similar task: gis.stackexchange.com/questions/151204/… – Comrade Che Mar 4 '16 at 6:08

I would assume you would need to write a python script that will parse through all features within that shapefile.

Each time it comes to a new feature, it would need to check for a unique id. If it doesn't exist, it would create one.

Next, it would run a query to check for features that... either share a boundary, common line segment, etc... There are options in the select by location tool in ArcMap. They should be accessible in python, too. With those features, it copies over the unique id to those features and moves on.

Eventually, it should get to the point where all features that meet the spatial query you are using share a common unique id.

I am assuming that the topology isn't incorrect, and that there aren't small spaces in between polygons too.

Hopefully that points you in the right direction or gives you an idea.


The way I would try going about this is using the Select by Attributes tool/script to select a feature based on the FID (just a way to work yourself through the features.. start low and move up) and if it lacks a unique id.

From there, I would use the Select By Location tool/script to add to current selected features within the same layer that either touch a boundary or intersect. Here I would check what version of ArcMap you have and what options are available. The ESRI documentation has some decent information for each version.

Now to actually work with the selected features seems a bit tricky. Check out this post (http://forums.arcgis.com/threads/43214-select-by-location-and-calculate-field-from-update-cursor-row-help) since it has details on ways to access the selected features and other suggestions. There is also another suggestion from (use a selection of features in ArcMap in Python script). Each of these does it in a different way, but they should provide you an idea of how to go about doing it.

From there, you use the Update Cursor to update the unique id field to whatever you'd like and loop until there are no more features that need updating.

I am sure there are some things I am missing and some features that may not be selected in this process. I would suggest trying it out and seeing what the results are then adjusting the selection process in the select by location part. A couple of tweaks and maybe an hour or so of coding/reading should get you on the right track.

It might be easier to try building this visually in Model Builder since you can just drag and drop tools and use the 'magic wand' to connect them. It might make flow easier to visually see.

For the Select By Attribute, Select By Location, and Update Cursor information, just Google arcpy and the name to find the ESRI documentation. I can't post more than 2 links. Sorry for no snippet, but the work flow should get you started.

  • When you do the Select By Location, you'll need to check if any of the resulting selection already have a unique ID, and if it does, use that to update the other features. An example might be if you select a small internal polygon, only the large encircling polygon will be selected by location and have the unique ID updated. The remaining polygons inside the large polygon will only be updated either when the large polygon is selected or when they are each selected separately, but they will need to receive the same unique ID. – Adam Jul 19 '15 at 23:21

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.