I need Polygon Neighbors to give me data in a specific format. Every polygon should have its own line in the output. Take this, for example(it's obviously not ArcGIS formatted, but it serves the purpose of an example):

enter image description here

This should give the following output, in text or excel:

1 2 5
2 1 3 5
3 2 4
4 3 5 6
5 1 2 4
6 4

It's important that the first column be in ascending numerical order. I have over 300,000 lines of this.

I have the input feature class. I can use ModelBuilder and Python.


The code below (which comes from one of my training courses) does the key part of what you are describing. Basically, it takes an input polygon feature class (testFC) on which you have run Polygon Neighbors to create a table (NEIGHBOURS). The feature class has an extra field called NEIGHBOURS (Text, 100) added.

What the code does is to:

  1. Make a Table View from the table which is needed to be able Select By Attribute on it later
  2. Open an Update Cursor (uCursor) on the polygon feature class to access the ID and empty NEIGHBOURS fields
  3. For each polygon it selects the corresponding rows in the NEIGHBOUR table and opens a Search Cursor (sCursor) on them
  4. Use the Search Cursor to build up a comma separated list of the IDs for each neighbouring polygon
  5. Strip any commas from either end of the comma separated list
  6. Write that list into the NEIGHBOURS field

The code:


with arcpy.da.UpdateCursor(r"C:\test.gdb\testFC",["ID","NEIGHBOURS"]) as uCursor:
    for uRow in uCursor:
        arcpy.SelectLayerByAttribute_management("NEIGHBOURS_View","NEW_SELECTION","src_ID = " + "'" + uRow[0] + "'")
        with arcpy.da.SearchCursor("NEIGHBOURS_View","nbr_ID") as sCursor:
            neighbours = ""
            for sRow in sCursor:
                neighbours = neighbours + "," + sRow[0]
        uRow[1] = neighbours.strip(",")

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.