OpenLayers Algorithm – How to Get Radius of a Circle in OpenLayers

algorithmgeometryjavascriptopenlayers-2polygon

How do you get the radius of a circle in openlayers? I have searched quite a bit and cannot find any resource for this. please help.

Here is my code but I want the radius or circumference of the circle and not the area.

         // The Drag function is now made and called
         // Add Drag
     drag = new OpenLayers.Control.DragFeature(polygonLayer, {
     autoActivate: true,
     onComplete: displayArea 
     });


    function displayArea(feature){
    var area = feature.geometry.getArea();

    alert(area);

    } 

Best Answer

According to clarification in the comment thread,

... a user [draws] a circle. In Openlayers a circle contains 40 points. I am able to getBounds, getVertices, and getArea in an alert. But I am wanting the Radius or Circumference of the circle.

Many GISes do approximate circles (and circular buffers) with regular polygons. Their designers had choices; the GISes I have used place the vertices at the correct radii (rather than, say, placing the edges tangent to the correct circle). The resulting polygons therefore fall entirely within the intended circles and consequently have areas that are slightly too small and circumferences that are slightly too short. If you care about sub-percent accuracy you need to compensate. Otherwise, don't bother.

Here, then, are several options depending on accuracy needs:

  1. Assume the polygon really is a circle. That is tantamount to supposing that its area A bears the familiar relationship with the radius r found for a circle; namely, that A = Pi * r^2. Solving for r gives r = Sqrt(A / Pi) = 0.56419 * Sqrt(A).

    Take some care about units of measurement: areas are in squared units. The corresponding radius will be in those units. Please do not compute areas in squared degrees! Use projected coordinates instead (preferable an equal-area projection). Typically areas will be in squared meters (or squared kilometers), so the radii will be in meters or kilometers, respectively. (If areas are in acres, convert to squared meters/kilometers/feet or whatever and then apply this formula.)

  2. Use the formula for the area of a regular polygon. With a radius of r and k vertices (where in practice k typically varies from 4 through 360), the area now is A = r^2 * k Sin(360 / k) / 2. Solving for r yields r = Sqrt(2 A / (k Sin(360/k))). For k = 40 that's r = 0.565352 * Sqrt(A).

    The adjustment compared to option 1 equals 0.565352 / 0.56419 = 1.00206. That is, you can compute the area as if the polygon were a true circle and then increase the answer by 0.206% (about one part in 485).

    With this approach your code might look like the following (untested):

    function displayRadius(circle) {
      var area = circle.geometry.getArea();
      var radius = 0.565352 * Math.sqrt(area);
      alert(radius);
    }
    
  3. You can use a similar approach based on a computed perimeter, P (aka the circumference). Assuming the polygon is a circle gives r = P / (2 Pi). The more precise formula for a regular k - gon is r = P / (2 k sin(180 / k)). In the first case the factor 1 / (2 Pi) = 0.159155 and in the second case (with k = 40) it is 0.159319: a 0.103% upwards adjustment.

    Because less adjustment is needed with the perimeter-based approach, one might be tempted to use the circle-based perimeter formula and be done with it. However, it's possible to compute areas extremely accurately (due to the existence of equal-area projections) whereas computing perimeters with similar accuracy is more difficult and fussy.

Note that for the more accurate options you only have to compute the adjustment factor once (in terms of k); it does not depend on the size of the circle.

One could work out a similar formula based on computing the bounding box (extent) of the near-circle, but this could be unreliable, because it would depend on how exactly the polygon is oriented.

Finally, for really large circles (more than hundreds of kilometers), you may need to use formulas of spherical geometry and you first need to decide whether you want the radius or circumference (because it is no longer the case that the circumference equals 2 Pi times the radius!).