[OpenLayers-Users] Function to create a geographically correct
circle
Michael
mdm at yachtpc.com
Mon Jun 6 01:20:25 EDT 2011
Regular Polygons are fine for many applications. I needed range rings
for ocean races, which did not correctly reflect the projection of the
map. Although it projected like a nice round circle, the correct shape
should have been oval.
Did not find a geographic circle function in OL, so I adapted one from
one found online.
I present it here should anyone want it, or have a better idea, or want
to point out that this was in OL all along.
*function GeoCircle(latin, lonin, radius)// thanks to VE site for basic
algo. Returns an OL Polygon of diameter in nautical miles
{
var locs = new Array();
var lat1 = latin * Math.PI/180.0;
var lon1 = lonin * Math.PI/180.0;
var d = radius/3440.07;// change this value to the radius of the
Earth in your units
var x;
for (x = 0; x <= 360; x+=6) //gives 60 points. More than enough
for a smooth circle most likely
{
var tc = (x / 90)* Math.PI / 2;
var lat =
Math.asin(Math.sin(lat1)*Math.cos(d)+Math.cos(lat1)*Math.sin(d)*Math.cos(tc));
lat = 180.0 * lat / Math.PI;
var lon;
if (Math.cos(lat1)==0)
{
lon=lonin; // endpoint a pole
}
else
{
lon = ((lon1 - Math.asin(Math.sin(tc) *
Math.sin(d)/Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI;
}
lon = 180.0 * lon / Math.PI;
var loc = new pt(lon,lat);
locs.push(loc);
}
var poly = new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.Polygon(new OpenLayers.Geometry.LinearRing(locs)));
return poly; //this can be added to a map
}
*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110605/b55daaf5/attachment.html
More information about the Users
mailing list