2

I am currently struggling to find the number of sides that intersect between two or more polygons in a layer, the layer contains around 2700 polygons. All polygons have a rectangular form, as it can be seen it in the following picture. The picture is a small part but everything looks more o less the same enter image description here

I have tried using intersection function, but I don’t seem to have proper results, or at least the ones I expected.

In the picture there are numbers on some polygons (I put them there just to explain better), what I want to do is:

  • For the polygon 1, I would have just one side that intersect with other polygons
  • For the polygon 2, I would have 2 sides that intersects with other polygons
  • For the polygon 3, applies the same as polygon 2
  • For the polygon 4, I would have just one sides that intersects with other polygon
  • For the polygon 5, applies the same as polygon 4

In other words:

Polygon Sides
1   1
2   2
3   2
4   1
5   1

Does anyone have an idea how to achieve this using GeoPandas or similar moduls?

3

Your question is similar to:

Find all neighbors using geopandas

but you want count, instead of the neighbour names. Based on the answer to it you can try:

import geopandas as gpd

file = r'C:\folder\file.shp'

df = gpd.read_file(file) # open file

for index,row in df.iterrows():
    df.at[index, "Count"] = df[~df.geometry.disjoint(row.geometry)].shape[0] #Calculate and add number of neighbours 

You might get a higher count than you want since it will count all neighbours including those sharing "corners", for example polygon 2 and the polygon bottom right of it.

  • Thx for the useful link to the other question and the code example. I run some simulations and in the results i got the number 1.0 for those polygons that don´t have any neighbor. So my question would be: is the number 1.0 the polygon itself? – Yiyi Aug 9 '18 at 13:42

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.