4

I am currently using ArcMap 10.4.

My task is to find which polygons share a border given a specific feature. I would like to automate this workflow.

For instance, I have 2 polygon feature dataset (check the figure 1): one with regions (blue border) and the other one with bridges ( pink). I want to find out which regions have in common a bridge. I used the Polygon Neighbors but this gave me, as a result, all the neighbors that one region has, for instance, (figure1): the region 10 has as neighbors: 9 and 11, however, what I need is to get ONLY the neighbors with shared bridge: in this case, for the region 10, I will need ONLY as result, the region 9, because there is a bridge on the shared border between 9 and 10.

For region 9, I need to get 10 and 11, because 9 shares a bridge WITH 10 and WITH 11.

For region 6, I need to get 5, and so on.

figure 1

0
5

This can be done in ModelBuilder as you have asked to automate this.

Use a feature selection iterator to iterate over your regions. Output of that feeds into a select by location tool to select your bridges. The selection on the bridges can be fed back into another select by location tool to select all regions that are selecting the selected bridges. Remove from that selection the region itself and you have your answer.

The key to all this is to know that all geoprocessing tools honour selections first.

Whilst ModelBuilder would undoubtedly be the quickest way to automate all this I suspect a scripting approach would be easier to manage the looping aspects of this processing problem.

1
  • 1
    yeah, definitive, a scripting approach might perform better the looping, however, I will try first the model builder, this will give me an idea of the steps and the workflow before implementing any script. Thank you!
    – Monitotier
    Oct 1 '21 at 10:15
2

I know you ask for ArcMap specifically, but it has been a long time since I used ArcPy. Here is the logic I would use in PyQGIS. Hopefully it gives you a starting point to translate to ArcPy.

## get reference to the QGIS project
proj = QgsProject.instance()

## get reference to map layers
polys = proj.mapLayersByName('grid')[0]
bridges = proj.mapLayersByName('bridges')[0]

## get features of layers
b_feats = list(bridges.getFeatures())
p_feats = list(polys.getFeatures())

## iterate through every polygon and every bridge to find which intersect
for p in p_feats:
    intersecting_bridges = []
    for b in b_feats:
        if p.geometry().intersects(b.geometry()):
            ## when an intersecting geometry is found, add it to a list
            intersecting_bridges.append(b)
    
    ## iterate through the intersecting bridges and find the polygons that intersect with them    
    for i in intersecting_bridges:
        for q in p_feats:
            ## when an intersection is found that does not match the initial polygon , print the result
            if i.geometry().intersects(q.geometry()) and q.id() != p.id():
                print(str(p.id()) + ' is connected to ' + str(q.id()))

enter image description here enter image description here

1

With some data preparation it can be done using 3 or less queries. So run polygon to lines tool with default parameters and delete edges between polygons that do not intersect bridges: enter image description here

From here on you can proceed with a) select polygon of interest b) select edges that share boundaries with it and c) select polygons that share boundaries with edges. Output for polygon with FID = 47: enter image description here

Alternative: create a copy of remaining edges, swap values in 2 columns: enter image description here

and merge them into single table. By doing this you are essentially creating edges table for undirected graph. Picture below shows result of select by attributes "LEFT_FID" =47 (compare it with 2nd picture):

enter image description here

With very little efforts list of neighbors can be transferred into table of original polygons: enter image description here

Or you can play with relates. First option is modification of Honbyd solution. Second is my preference, because attribute query replaces slow queries by location and graph edges table can be used repeatedly.

1
  • Your hints were also really helpful for me to find a way to implement it with model builder. It was very creative, Thank you!
    – Monitotier
    Dec 8 '21 at 16:07
0

If I had two shapefiles (big polygons bridges let's say) I would first run the Intersect tool to find the common areas and obtain the FID from both bridges and big polygons. I would open the attribute table, select all rows, and copy and paste them into Excel. Then I would create a pivot table in Excel with FID of bridges as rows and FID of big polygons as both columns and values. This would leave me with a list of bridges by FID with adjacent pairs of polygons. No ModelBuilder or code would be needed.

2
  • It doesn't answer the question about neighbors of polygons.
    – FelixIP
    Oct 1 '21 at 20:21
  • Actually this option was the easiest to implement. I could even automatize it making an intersect in arcpy and then using a pivot table in python as suggested. It definitively does the trick
    – Monitotier
    Dec 7 '21 at 12:34

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.