# How to improve the speed of "inpolygon"?

**URL:** https://discourse.julialang.org/t/how-to-improve-the-speed-of-inpolygon/70969
**Category:** Performance
**Tags:** question
**Created:** [November 4, 2021, 5:29pm UTC](https://discourse.julialang.org/t/how-to-improve-the-speed-of-inpolygon/70969 "2021-11-04T17:29:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 4, 2021, 5:29pm UTC](https://discourse.julialang.org/t/how-to-improve-the-speed-of-inpolygon/70969/1 "2021-11-04T17:29:43Z")

</div>

I have about 30,000 pairs of longitude and latitude data (lon1 and lon2). My goal is to check if they will fall into a polygon defined by (lonW and latW).

In Matlab, I can do this as below. It is blazing fast. It literally takes no time to finish.  
`in = inpolygon(lon1, lat1, lonW, latW);`

Below is how I’m doing it in Julia.

```julia
using PolygonOps
using StaticArrays
polygon = SVector.(lonW, latW);
in = [inpolygon((x, y), polygon; in=true, on=false, out=false) for x in lon1, y in lat1];

```

It will take forever to finish. In fact I’m never able to wait long enough for it to complete.It will make my computer to run very hot and noisy. As you know, this is not normal. Julia is supposed to be faster than Matlab. Anyone knows a faster way to process this in Julia? Many thanks.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 4, 2021, 5:37pm UTC](https://discourse.julialang.org/t/how-to-improve-the-speed-of-inpolygon/70969/2 "2021-11-04T17:37:34Z")

</div>

You want `for (x,y) in zip(lon1, lat1)`. `for x in lon1, y in lat1` constructs the Cartesian product, so it’s doing way more work.

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [November 4, 2021, 5:38pm UTC](https://discourse.julialang.org/t/how-to-improve-the-speed-of-inpolygon/70969/3 "2021-11-04T17:38:08Z")

</div>

Are you sure you want 90.000.000 pairs of checking?

---

<div class="post-metadata">

### Author: ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)
#### Post date: [November 4, 2021, 5:45pm UTC](https://discourse.julialang.org/t/how-to-improve-the-speed-of-inpolygon/70969/4 "2021-11-04T17:45:15Z")

</div>

Do you know how the algorithm of Matlab works?

I mean, by analyzing the points separately, you can maybe reduce the problem?  
Also, if the polygon is convex you can exclude many, many points (convex hull, etc.)

[Point in polygons](https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm) is actually a pretty old and famous problem so I wouldn’t wonder if Matlab has sophisticated algorithms for that.

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 4, 2021, 6:03pm UTC](https://discourse.julialang.org/t/how-to-improve-the-speed-of-inpolygon/70969/5 "2021-11-04T18:03:13Z")

</div>

Many thanks, Oscar! That solved my issue beautifully. Now it is running blazingly fast. I’m surprised by the big difference!

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 4, 2021, 6:03pm UTC](https://discourse.julialang.org/t/how-to-improve-the-speed-of-inpolygon/70969/6 "2021-11-04T18:03:55Z")

</div>

Unfortunately, I don’t.

Thanks All for your replies!
