Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

    } 
share|improve this question
2  
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
1  
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

1 Answer 1

up vote 6 down vote accepted

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!).

share|improve this answer
    
Thanks. This is what I wanted but im going to ask another question, on how to convert the decimals to Km,s –  Blake Loizides Feb 29 '12 at 8:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.