# Compute surfaces of intersection between circles in julia with LibGEOS

**URL:** <https://discourse.julialang.org/t/compute-surfaces-of-intersection-between-circles-in-julia-with-libgeos/59811>\
**Category:** Geo\
**Tags:** question, libgeos\
**Created:** [April 22, 2021, 3:06pm UTC](https://discourse.julialang.org/t/compute-surfaces-of-intersection-between-circles-in-julia-with-libgeos/59811 "2021-04-22T15:06:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meije\_Gawinowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/meije_gawinowski/32/24284_2.png) [@Meije\_Gawinowski](https://discourse.julialang.org/u/Meije_Gawinowski)\
**Post date:** [April 22, 2021, 3:06pm UTC](https://discourse.julialang.org/t/compute-surfaces-of-intersection-between-circles-in-julia-with-libgeos/59811/1 "2021-04-22T15:06:55Z")

</div>

I need to compute surfaces of intersection between different circles. I am used to do it in R with the sf library and it works fine but I need to do it in Julia with the LibGEOS library and it is not as easy…

My first question would be, how to create a circle polygon? By creating the center and extend it with a buffer? For example:  
circle\_i = buffer(point\_i, radius\_i) ?

My second question is, now that I have created some circles, can I put them in a common object, like a multipolygon?

My third and last question is, if I have an ensemble of circles, can I use the intersection function to compute all the intersections of all my circles?

I hope I am clear enough, I could really use some help regarding these questions!

---

<div class="post-metadata">

**Author:** ![visr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/visr/32/17204_2.png) [@visr](https://discourse.julialang.org/u/visr)\
**Post date:** [April 22, 2021, 9:16pm UTC](https://discourse.julialang.org/t/compute-surfaces-of-intersection-between-circles-in-julia-with-libgeos/59811/2 "2021-04-22T21:16:27Z")

</div>

Hi, welcome! Yeah this sure is possible. Not quite as well documented as sf though.

GEOS doesn’t have circles, only the geometry types listed [here](https://trac.osgeo.org/geos/#SpatialModelandFunctions). Indeed taking a buffer of a point is a good way to create a circular polygon.

Why do you want to put the circles together in a common object? It should be doable to put them together in a MultiPolygon or GeometryCollection, but in the example below a simple Vector of Polygons is probably easier.

```julia
using LibGEOS, Plots

# 3 overlapping circles in a triangle
circles = [
    buffer(Point(0.0, 0.0), 1.0),
    buffer(Point(1.0, 0.0), 1.0),
    buffer(Point(0.5, 1.0), 1.0),
]

isects = Polygon[]
for (i, g1) in enumerate(circles)
    for (j, g2) in enumerate(circles)
        # avoid counting double, and only use non empty intersections
        if i > j && LibGEOS.intersects(g1, g2)
            isect = LibGEOS.intersection(g1, g2)
            push!(isects, isect)
        end
    end
end

plot(isects)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/5/75bf9b87934546ae7ca48c7afc711c93a9459851.png)

---

<div class="post-metadata">

**Author:** ![Meije\_Gawinowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/meije_gawinowski/32/24284_2.png) [@Meije\_Gawinowski](https://discourse.julialang.org/u/Meije_Gawinowski)\
**Post date:** [April 23, 2021, 6:27am UTC](https://discourse.julialang.org/t/compute-surfaces-of-intersection-between-circles-in-julia-with-libgeos/59811/3 "2021-04-23T06:27:51Z")

</div>

Thank you so much for your answer, it is very helpful!

I guess for the intersection function I wanted an object like a multipolygon because I need to compute all the intersections between the circles, not only 2 by 2 intersections. With your example, I would also need to have a polygon for the intersection between the three circles. Ideally I would need as many polygons as overlapped surfaces, just like this figure below:

 ![ex_R](https://global.discourse-cdn.com/julialang/original/3X/3/8/38e1c54b80da2bfe24ec7b65bb8e5856b92a8e59.jpeg)

But since the intersection function only takes 2 polygons as arguments it might not be possible?

---

<div class="post-metadata">

**Author:** ![visr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/visr/32/17204_2.png) [@visr](https://discourse.julialang.org/u/visr)\
**Post date:** [April 23, 2021, 10:20am UTC](https://discourse.julialang.org/t/compute-surfaces-of-intersection-between-circles-in-julia-with-libgeos/59811/4 "2021-04-23T10:20:35Z")

</div>

Hmm, I’m actually not sure how to do this. So in your figure there are no overlapping areas anymore right? It looks like the result of a [union in QGIS](https://docs.qgis.org/3.16/en/docs/user_manual/processing_algs/qgis/vectoroverlay.html#union). However [union in GEOS](https://shapely.readthedocs.io/en/stable/manual.html#efficient-unions) also directly dissolves the result. Though probably with a combination of the other functions you can effectively do the same?

```julia
# need to go through GeoInterface, see issue #87
mp = MultiPolygon(GeoInterface.coordinates(circles))
plot(LibGEOS.unaryUnion(mp))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/d/7df5002c992fea07b02a2e05962388cb53c5a41d.png)

---

<div class="post-metadata">

**Author:** ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)\
**Post date:** [April 24, 2021, 12:55pm UTC](https://discourse.julialang.org/t/compute-surfaces-of-intersection-between-circles-in-julia-with-libgeos/59811/5 "2021-04-24T12:55:11Z")

</div>

You should be able to write each colored surface as intersections between circles or their complements.  
For example, the orange region is A \cap B \cap \neg C, where A is the top circle, B is the right circle and C is the left circle.
