OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to calculate the area of polygons and sort from the largest area to the smallest

  • Thread starter Thread starter kregtopoli
  • Start date Start date
K

kregtopoli

Guest
I'm trying to export contours from contourf to geojson, the problem is that after generating the polygons, there are actually two lines on top of each other on each border.

The question is how to calculate the area of polygons and sort from the largest area to the smallest to create single polygons without double border lines.

Code:
t = ax.contourf(xi, yi, z1, cmap="rainbow_r", levels=20, antialiased=False)

poly_features = []
for col in t.collections:
    for contour_path in col.get_paths(): 
        for ncp,cp in enumerate(contour_path.to_polygons()):
            x = cp[:,0]
            y = cp[:,1]
            new_shape = geometry.Polygon([(i[0], i[1]) for i in zip(x,y)])
            if ncp == 0:
                poly = new_shape
            else:
                poly = poly.difference(new_shape)
        poly_features.append(Feature(geometry=poly))

feature_collection = FeatureCollection(poly_features)

enter image description here

enter image description here

enter image description here
<p>I'm trying to export contours from contourf to geojson, the problem is that after generating the polygons, there are actually two lines on top of each other on each border.</p>
<p>The question is how to calculate the area of polygons and sort from the largest area to the smallest to create single polygons without double border lines.</p>
<pre><code>t = ax.contourf(xi, yi, z1, cmap="rainbow_r", levels=20, antialiased=False)

poly_features = []
for col in t.collections:
for contour_path in col.get_paths():
for ncp,cp in enumerate(contour_path.to_polygons()):
x = cp[:,0]
y = cp[:,1]
new_shape = geometry.Polygon([(i[0], i[1]) for i in zip(x,y)])
if ncp == 0:
poly = new_shape
else:
poly = poly.difference(new_shape)
poly_features.append(Feature(geometry=poly))

feature_collection = FeatureCollection(poly_features)
</code></pre>
<p><a href="https://i.sstatic.net/rgPAuakZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/rgPAuakZ.png" alt="enter image description here" /></a></p>
<p><a href="https://i.sstatic.net/8cr3JFTK.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8cr3JFTK.png" alt="enter image description here" /></a></p>
<p><a href="https://i.sstatic.net/A0rT9O8J.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/A0rT9O8J.png" alt="enter image description here" /></a></p>
 
Top