Ali Sultan
Ali Sultan

Reputation: 290

splitting circle into two using LineString Python Shapely

I have created a circle using Shapely and would like to split it into two using LineString.

I created the circle as follows

from functools import partial

import pyproj
from shapely import geometry
from shapely.geometry import Point, Polygon, shape, MultiPoint, LineString, mapping 
from shapely.ops import transform, split

radius = 92600
lon = 54.08
lat = 17.05

local_azimuthal_projection = "+proj=aeqd +R=6371000 +units=m +lat_0={} +lon_0={}".format(
    lat, lon
)
wgs84_to_aeqd = partial(
    pyproj.transform,
    pyproj.Proj("+proj=longlat +datum=WGS84 +no_defs"),
    pyproj.Proj(local_azimuthal_projection),
)
aeqd_to_wgs84 = partial(
    pyproj.transform,
    pyproj.Proj(local_azimuthal_projection),
    pyproj.Proj("+proj=longlat +datum=WGS84 +no_defs"),
)

center = Point(float(lon), float(lat))
point_transformed = transform(wgs84_to_aeqd, center)
buffer = point_transformed.buffer(radius)
# Get the polygon with lat lon coordinates
circle_poly = transform(aeqd_to_wgs84, buffer)

For the line Splitter I have the following:

splitter = LingString([Point(54.79,16.90), Point(53.56,16.65)])

Now I want to see the two split shapes so I used split function.

result = split(circle_poly, splitter)

However, this only results the same circle and not two shapes. At the end I would like to use one section of the split to form another shape.

Upvotes: 1

Views: 2197

Answers (1)

swatchai
swatchai

Reputation: 18782

To split a circle or a polygon, you can use spatial difference operation with another polygon. Shapely does not allow the use of line to do so.

"Shapely can not represent the difference between an object and a lower dimensional object (such as the difference between a polygon and a line or point) as a single object."

See document:

In your case, you can build two polygons that have the line as the common edges. Make sure that the 2 polygons together are big enough to cover the whole polygon you are splitting. Then you use that polygons to do the job.

If you want crude results from the difference operation, you can turn the line into a slim polygon by buffer operation, and use it.

For the second approach, here is the relevant code:

the_line = LineString([(54.95105, 17.048144), (53.40473921, 17.577181)])
buff_line = the_line.buffer(0.000001)  #is polygon

# the `difference` operation between 2 polygons
multi_pgon = circle_poly.difference(buff_line)

The result is a multi-polygon geometry object.

diff-oper

Upvotes: 2

Related Questions