# How to get the radius of a “circle” in Openlayers

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
drag = new OpenLayers.Control.DragFeature(polygonLayer, {
autoActivate: true,
onComplete: displayArea
});

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

}
``````
• Can you elaborate as to what you mean by radius? Do you mean the radius of the smallest circle that will enclose each polygon? – Darren Cope Feb 28 '12 at 17:50
• Circles don't exist in OL. They are drawn as a multi-sided polygons to closely approximate a "circle". – Vadim Feb 28 '12 at 19:16
• @DarrenCope I used the getArea function, and it gave me the following number 2.3247876326140506 or 0.006341782939435769 . Do you know how many km,s this is so I can get an idea – Blake Loizides Feb 28 '12 at 19:23
• Do you just want the area of the polygon, not 'radius'? – Darren Cope Feb 28 '12 at 20:00
• I allow a user to draw 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 I.E 40km. – Blake Loizides Feb 28 '12 at 21:41

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);