-1
  1. I have some polygons with their respective coordinates on which i used uniary_union().
  2. All the polygons are now become one.
  3. Now that big polygon has a group of polygons that are connect with each other. I want to know, how to find the max number of small polygons connected in that big polygon and also the coordinates of those polygons as well.enter image description here

I want to know the coordinates of the 2grey polygons and the 3 orange polygons.

1 Answers1

0

The following script shows how you can do this:

import geopandas as gpd
from matplotlib import pyplot as plt
import shapely

# Prepare test data
row, col = (0, 0)
nb = 4
rects = []
colors = []
row_nbs = []
size = 10
while row <= nb:
    color = "orange"
    if (row % 2) == 0:
        color = "grey"
    while col <= row and col <= nb:
        rect = (col*size, row*size, col*size + size, row*size + size)
        rects.append(rect)
        colors.append(color)
        row_nbs.append(row)
        col += 1
    col = 0
    row += 1

geoms = [shapely.box(*rect) for rect in rects]
gdf = gpd.GeoDataFrame({"color": colors, "row_nb": row_nbs, "geometry": geoms}, crs=31370)  # type: ignore
print(f"Test dataset:\n{gdf}")

# Dissolve + explode the test dataset
dissolve_gdf = gdf.dissolve(by=["color", "row_nb"]).explode().reset_index()  # type: ignore
print(f"\ndissolve_gdf:\n{dissolve_gdf}")

# sjoin both
joined_gdf = gdf.sjoin(dissolve_gdf, predicate="within")
print(f"\nresult_gdf:\n{joined_gdf}")

# count the number of rectangles per row
counts_df = joined_gdf.groupby(["row_nb_right", "color_right"]).size()
print(f"\ncounts_df:\n{counts_df}")

# plot the test data
gdf.plot(color=gdf["color"], edgecolor="black")
plt.show()

This is the plotted test data I used

test data

The counts_df dataframe contains the number of rectangles per row/color

counts_df:
row_nb_right  color_right
0             grey           1
1             orange         2
2             grey           3
3             orange         4
4             grey           5
dtype: int64

The joined_gdf dataframe contains the rectangles with coordinates per row/color

result_gdf:
   color_left  row_nb_left                                           geometry  index_right color_right  row_nb_right  level_2
0        grey            0  POLYGON ((10.000 0.000, 10.000 10.000, 0.000 1...            0        grey             0        0
1      orange            1  POLYGON ((10.000 10.000, 10.000 20.000, 0.000 ...            3      orange             1        0
2      orange            1  POLYGON ((20.000 10.000, 20.000 20.000, 10.000...            3      orange             1        0
3        grey            2  POLYGON ((10.000 20.000, 10.000 30.000, 0.000 ...            1        grey             2        0
4        grey            2  POLYGON ((20.000 20.000, 20.000 30.000, 10.000...            1        grey             2        0
5        grey            2  POLYGON ((30.000 20.000, 30.000 30.000, 20.000...            1        grey             2        0
6      orange            3  POLYGON ((10.000 30.000, 10.000 40.000, 0.000 ...            4      orange             3        0
7      orange            3  POLYGON ((20.000 30.000, 20.000 40.000, 10.000...            4      orange             3        0
8      orange            3  POLYGON ((30.000 30.000, 30.000 40.000, 20.000...            4      orange             3        0
9      orange            3  POLYGON ((40.000 30.000, 40.000 40.000, 30.000...            4      orange             3        0
10       grey            4  POLYGON ((10.000 40.000, 10.000 50.000, 0.000 ...            2        grey             4        0
11       grey            4  POLYGON ((20.000 40.000, 20.000 50.000, 10.000...            2        grey             4        0
12       grey            4  POLYGON ((30.000 40.000, 30.000 50.000, 20.000...            2        grey             4        0
13       grey            4  POLYGON ((40.000 40.000, 40.000 50.000, 30.000...            2        grey             4        0
14       grey            4  POLYGON ((50.000 40.000, 50.000 50.000, 40.000...            2        grey             4        0
Pieter
  • 340
  • 1
  • 6