Search code examples
openlayers-5

How do I Convert an OpenLayers Polygon to a Circle?


I have a drawing feature where, as in one case, a person can draw a circle using the methodology in OL docs example. When that's saved, the server needed to be have it converted to a polygon, and I was able to do that using fromCircle.

Now, I'm needing to make the circle modifiable after it's been converted and saved. But I don't see a clear cut way to get a Circle geometry out of the Polygon tools provided in the library. There is a Polygon.circular, but that doesn't sound like what I want.

I'm guessing the only way to do this is to grab the center, and one of the segment points, and figure out the radius manually?


Solution

  • As long as fromCircle used sides set to a multiple of 4 and rotation zero (which are the default) center and radius can be easily obtained to convert back to a circle:

     center = ol.extent.getCenter(polygon.getExtent());
     radius = ol.extent.getWidth(polygon.getExtent())/2;