# Creating polygon from two not connected linestrings using shapely

I'd like to know if there is a way to create a polygon in shapely from two not connected linestrings. I've been struggling with this question from some hours now and I can't find a way to deal with it.

For example, I'd like to have the polygon with those linestrings as its exterior coords.

``````from shapely.geometry import Polygon

p1 = Polygon([1.32, 7.66], [1.4, 5.56], [2.98, 4.5], [5.86, 4], [12.16, 4.88])
p2 = Polygon([1.62, 0], [2, 1], [3.16, 1.68], [5, 2], [8.44, 2.14], [9.92, 0.58], [12, 0], [13.62, 0.8])
``````
• By definition, a polygon is composed of the vertices which describe a closed ring. Your vertices are not closed rings. They are therefore not polygons. There are other requirements besides closed (not crossing, not touching other parts,...) but you need to work on that first. Nov 9, 2020 at 14:06

You can get the lines' coordinates and use them to make a polygon. But it depends. In many cases, you will probably have invalid polygon.

• Let's make two lines using the coordinates in your question:

``````from shapely.geometry import Polygon, LineString
l1 = LineString([[1.32, 7.66], [1.4, 5.56], [2.98, 4.5], [5.86, 4], [12.16, 4.88]])
l2 = LineString([[1.62, 0], [2, 1], [3.16, 1.68], [5, 2], [8.44, 2.14], [9.92, 0.58], [12, 0], [13.62, 0.8]])

p = Polygon([*list(l1.coords), *list(l2.coords)])
``````

As you see, there are many self-intersecting edges. So the result is an invalid polygon.

• Let's reverse the `l2`'s coordinates:

``````p = Polygon([*list(l1.coords), *list(l2.coords)[::-1]])
``````

Again, self-intersecting edges.

• Let's change `l2`'s location. (added 10 to y-coordinate):

``````l2 = LineString([[1.62, 10], [2, 11], [3.16, 11.68], [5, 12], [8.44, 12.14], [9.92, 10.58], [12, 10], [13.62, 10.8]])
p = Polygon([*list(l1.coords), *list(l2.coords)])
``````

• Let's reverse the `l2`'s coordinates:

``````p = Polygon([*list(l1.coords), *list(l2.coords)[::-1]])
``````

Now, this is a valid polygon.

Briefly, you cannot be sure if the result polygon will be valid. It depends.

• Oh nice, thank you for your answer !! To manage the situation, I've made a list comprehensive loop with all the combinations of coordinates using `itertools.product()` and I'm only keeping the valid ones with `shapely.obj.is_valid`. Nov 9, 2020 at 15:39