A Total Ellipse on the Map

156 阅读2分钟

A Total Ellipse on the Map

Or, how to choose the best way to walk to the beach after work

It was a cool autumn evening when Hila Kloper and I were thinking of going to the beach after work. The beach is about 2.5Km away from the office.

We were even considering strolling down the streets of Tel Aviv, willing to stretch our path to 3Km, and thinking to ourselves “mmm we wonder how far this stretch can take us?”

Well, long story short, we didn’t go to the beach. Instead, we wrote a script that draws an ellipse around the office and the beach. The ellipse covers the city area we may go through if we ever decide to go to the beach after work.

When developers want to do something fun outside and they end up writing a script about it instead.

It’s the Ellipse of Life

Or - why should we care about ellipses?

A circle is in some way the “natural” area around one point. An ellipse is the “natural” area around two points or a line. To name a few examples, bodies of mass move in elliptic orbits, ellipses represent the distortion caused by projecting a 3D map on 2D, and ellipses are also an accurate way to plot confidence of noisy GPS data (and confidence areas in 2D data in general).

In our case, we wanted to draw an area around the line starting at our office and ending at the beach. The easiest solution we found for drawing ellipses involved shapely and pyplot. It still required some modifications due to our GPS and map constraints.

So, if you are here because you are looking for an easy copy-pastable code that draws an ellipse on a map — you can go to this repository we made. If you are also interested to learn how we found the complete solution to our problem, you are welcome to join us for the ride. We rediscover elementary geometry, learn about coordinates systems, and play around with some math code.

Girls Just Wanna Have Ellipses

Us: “Oh there must be a package somewhere that can draw an ellipse on a map!”
The Internet: “No there isn’t.”
Us: “But there must be a simple copy-pastable code somewhere!”
Stackoverflow: “I have incomprehensible ones if you want.”
Us: “Well, OK, we’ll take an hour and make one ourselves!”
Reality: “…”

Figuring out what an ellipse actually is was the first challenge.
Wolfram Alpha told us that an ellipse is the set of points that have the same sum-of-distances from two mutual centers. Or something like that. Wolfram Alpha can be rather cryptic sometimes. But they have a gif so that’s nice.

Ah… now that’s clear.

So all we have to do is make sure we have these inputs:

  • p1, p2 - GPS coordinates.
  • r - the radius which is actually the sum of distances from a point on the ellipse to the two centers.

Then follow this plan:

  1. Find a and b, the axes of the ellipse.
  2. Draw an ellipse around the origin (0,0) measured in meters.
  3. Move the ellipse to the center between the input GPS locations.
  4. Rotate according to the angle between the input GPS locations.