Creating a Geodesic Circle in OpenLayers

image

I recently asked a question on GIS Stack Exchange on how to create a  buffer around a point that took into account the curvature of the earth. OpenLayers has support for geodesic measurements, but not creating geodesic polygons. Drawing a standard polygon on a Mercator projected map can produce features with very different measurements from their intended “on the ground” equivalents.

Paul Ramsey pointed out that “the scale errors for Mercator are very high indeed (infinite, in fact, at the poles) increasing as you head north/south from the true scale latitude.” In fact drawing a circle (in Ireland – 53 degrees North) with a 10km geometric radius produced a circle with an on the ground radius of 6km.  A huge margin of error over a very short distance (see a previous post on the same subject).

After a useful answer from Dan Shoutis, it appeared most of the work to implement geodesic circles was already available in the OpenLayers API. The OpenLayers’ geodesic functions are based on code adapted from Chris Veness work at http://www.movable-type.co.uk/scripts/latlong-vincenty-direct.html.

The code to create a regular (non-geodesic) polygon can be seen in the OpenLayers source here. There is a function added after the class OpenLayers.Geometry.Polygon.createRegularPolygon that can be used to “create a regular polygon around a radius. Useful for creating circles and the like.”

This function only required a couple of changes – notably using Longitude and Latitude and rather than X and Ys, to produce geodesic polygons. If you are using the Mercator projection then transformations requires proj4js support.

Code

An online demo of the function is available at http://geographika.co.uk/samples/geodesic/. The adapted function can be seen at http://geographika.co.uk/samples/geodesic/GeodesicPolygon.js.

I have uploaded the full source code for the demo to http://bitbucket.org/geographika/openlayers/src.

image

Just don’t try drawing circles around the map edges..



5 views shared on this article. Join in...

  1. Sebastian says:

    Great! Thank you for this solution. I have been looking for this for a long time.

  2. Hector says:

    Hi,
    Whaou, this is great ! I was also looking for this.

    I got a question. Does the polyline draw should have a “buffer zone”. I ask this because, i don’t get one.
    I need to do a buffer around a polygon. Do you have any idea ? I should maybe think about getting the center of my polygon and calculate a circle like you did…

    Thanks in advance for any answer.
    Great job again.

    Hector

  3. captdragon says:

    Awesome! Please submit this to openlayers code and do everyone a favor!

  4. Immobilien says:

    Saved my day! Thanks!

  5. Ralph Dell says:

    The OpenLayers function was very helpful. It will be in production soon. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment

You may use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>