# Sort points on line in PolyData

**URL:** <https://discourse.vtk.org/t/sort-points-on-line-in-polydata/3851>\
**Category:** Support\
**Created:** [July 24, 2020, 9:14pm UTC](https://discourse.vtk.org/t/sort-points-on-line-in-polydata/3851 "2020-07-24T21:14:20Z")\
**Posts on this page:** 1\
**Showing post:** 29

<div class="post-metadata">

**Author:** ![PBrockmann](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pbrockmann/32/524_2.png) [@PBrockmann](https://discourse.vtk.org/u/PBrockmann)\
**Post date:** [April 21, 2023, 3:21pm UTC](https://discourse.vtk.org/t/sort-points-on-line-in-polydata/3851/29 "2023-04-21T15:21:55Z")

</div>

The previous example works with another input files where  
all self intersected polygons have been separated.

wget -nc [https://thredds-su.ipsl.fr/thredds/fileServer/ipsl\_thredds/brocksce/tmp/polys\_03.pickle](https://thredds-su.ipsl.fr/thredds/fileServer/ipsl_thredds/brocksce/tmp/polys_03.pickle)

See details from answer given in  
[https://discourse.vtk.org/t/create-separated-regions-from-polydata](https://discourse.vtk.org/t/create-separated-regions-from-polydata)

```auto
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import shapely
from shapely import geometry
import cartopy.crs as ccrs
import random
import pickle

#-----------------------------------------
! wget -nc https://thredds-su.ipsl.fr/thredds/fileServer/ipsl_thredds/brocksce/tmp/polys_03.pickle

with open('./polys_03.pickle', "rb") as poly_file:
    polygons = pickle.load(poly_file)

#-----------------------------------------
def polygon2patch(poly, **kwargs):
    path = Path.make_compound_path(
           Path(poly.exterior.coords, closed=True),
           *[Path(ring.coords) for ring in poly.interiors])
    patch = PathPatch(path, **kwargs)
    return patch

#-----------------------------------------
fig = plt.figure(figsize=(10,5))

map_proj = ccrs.Orthographic(-10, 80)
#map_proj = ccrs.Orthographic(-10, 60)
ax = fig.add_subplot(1, 1, 1, projection=map_proj)

transform = ccrs.Geodetic()

holesNumber = []
for n,polygonA in enumerate(polygons):
    if n != 16: continue
    holes = []
    for m,polygonB in enumerate(polygons):
        if (m == n): continue     
        if polygonA.contains(polygonB):
            holes.append(polygonB.exterior.coords)
            holesNumber.append(m)
    if n in holesNumber: continue # n is a hole
    polygonNew = geometry.Polygon(polygonA.exterior.coords, holes=holes)
    polygonNew = shapely.geometry.polygon.orient(polygonNew) # Orient to be oriented counter-clockwise
    random_color = "#"+''.join([random.choice('0123456789ABCDEF') for i in range(6)])
    patch = polygon2patch(polygonNew, transform=transform, facecolor=random_color, lw=0.5, edgecolor="black")
    ax.add_patch(patch)
    
ax.set_global()
ax.gridlines()
plt.show()

```

![image](https://discourse.vtk.org/uploads/default/original/2X/c/c0f884a60e1ad2890da37e851c811ec84093b661.png)

---

_[View the full topic](https://discourse.vtk.org/t/sort-points-on-line-in-polydata/3851)._
